zeroshade commented on code in PR #2729:
URL: https://github.com/apache/arrow-adbc/pull/2729#discussion_r2073925136


##########
go/adbc/driver/internal/driverbase/rotating_file_writer.go:
##########
@@ -0,0 +1,328 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package driverbase
+
+import (
+       "errors"
+       "io/fs"
+       "os"
+       "path/filepath"
+       "strings"
+       "time"
+)
+
+const (
+       defaultLogNamePrefix = "apache.adbc.go"
+       defaultFileSizeMaxKb = int64(1024)
+       defaultFileCountMax  = 100
+       defaultTraceFileExt  = ".jsonl"
+)
+
+// Holds the configuration from the options
+type config struct {
+       TracingFolderPath string
+       LogNamePrefix     string
+       FileSizeMaxKb     int64
+       FileCountMax      int
+}
+
+// An option for the RotatingFileWriter
+type rotatingFileWriterOption func(*config)
+
+// Adds the TracingFolderPath option
+func WithTracingFolderPath(tracingFolderPath string) rotatingFileWriterOption {
+       return func(cfg *config) {
+               cfg.TracingFolderPath = tracingFolderPath
+       }
+}
+
+// Adds the LogNamePrefix option
+func WithLogNamePrefix(logNamePrefix string) rotatingFileWriterOption {
+       return func(cfg *config) {
+               cfg.LogNamePrefix = logNamePrefix
+       }
+}
+
+// Adds the FileSizeMaxKb option
+func WithFileSizeMaxKb(fileSizeMaxKb int64) rotatingFileWriterOption {
+       return func(cfg *config) {
+               cfg.FileSizeMaxKb = fileSizeMaxKb
+       }
+}
+
+// Adds the FileCountMax option
+func WithFileCountMax(fileCountMax int) rotatingFileWriterOption {
+       return func(cfg *config) {
+               cfg.FileCountMax = fileCountMax
+       }
+}
+
+func newConfig(options ...rotatingFileWriterOption) (cfg config, err error) {
+       cfg = config{
+               TracingFolderPath: "",
+               LogNamePrefix:     defaultLogNamePrefix,
+               FileSizeMaxKb:     defaultFileSizeMaxKb,
+               FileCountMax:      defaultFileCountMax,
+       }
+       for _, opt := range options {
+               opt(&cfg)
+       }
+       // Ensure default for tracingFolderPath
+       if strings.TrimSpace(cfg.TracingFolderPath) == "" {
+               var fullPath string
+               fullPath, err = getDefaultTracingFolderPath()
+               if err != nil {
+                       return
+               }
+               cfg.TracingFolderPath = fullPath
+       }
+
+       // Ensure default for logNamePrefix
+       if strings.TrimSpace(cfg.LogNamePrefix) == "" {
+               cfg.LogNamePrefix = defaultLogNamePrefix
+       }
+
+       // Ensure tracingFolderPath exists
+       const folderPermissions = 0755
+       err = os.MkdirAll(cfg.TracingFolderPath, folderPermissions)
+       if err != nil {
+               return
+       }
+       // Test if we can create/write a file in the traces folder
+       tempFile, err := os.CreateTemp(cfg.TracingFolderPath, cfg.LogNamePrefix)
+       if err != nil {
+               return
+       }
+       defer func() {
+               _ = tempFile.Close()
+               _ = os.Remove(tempFile.Name())
+       }()
+       _, err = tempFile.WriteString("file started")
+       if err != nil {
+               return
+       }
+
+       // Ensure default for fileSizeMaxKb
+       cfg.FileSizeMaxKb = max(defaultFileSizeMaxKb, cfg.FileSizeMaxKb)
+
+       // Ensure default for fileCountMax
+       cfg.FileCountMax = max(defaultFileCountMax, cfg.FileCountMax)
+
+       return
+}
+
+// A rotating file writer that writes bytes to new trace files into a given 
TracingFolderPath until the trace file exceeds FileSizeMaxKb in size.
+// Then a new trace file is created. If the number archived trace files 
exceeds FileCountMax, then the oldest file(s) are removed.
+// The files are named with the following pattern "<LogNamePrefix>-<current 
date/time UTC>.jsonl"
+type rotatingFileWriterImpl struct {
+       TracingFolderPath string
+       LogNamePrefix     string
+       FileSizeMaxKb     int64
+       FileCountMax      int
+       CurrentWriter     *os.File
+}
+
+// Creates a new RotatingFileWriter from the given options
+func NewRotatingFileWriter(options ...rotatingFileWriterOption) 
(*rotatingFileWriterImpl, error) {
+       cfg, err := newConfig(options...)
+       if err != nil {
+               return nil, err
+       }
+
+       return &rotatingFileWriterImpl{
+               TracingFolderPath: cfg.TracingFolderPath,
+               LogNamePrefix:     cfg.LogNamePrefix,
+               FileSizeMaxKb:     cfg.FileSizeMaxKb,
+               FileCountMax:      cfg.FileCountMax,
+               CurrentWriter:     nil,
+       }, nil
+}
+
+func getDefaultTracingFolderPath() (string, error) {
+       userHome, err := os.UserHomeDir()
+       if err != nil {

Review Comment:
   should we use this or should we use `os.UserConfigDir` instead?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to