github.com/kaydxh/golang@v0.0.131/pkg/binlog/datastore/filestore/file.store.go (about)

     1  /*
     2   *Copyright (c) 2023, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package filestore
    23  
    24  import (
    25  	"context"
    26  	"fmt"
    27  	"path/filepath"
    28  	"sync"
    29  
    30  	ds_ "github.com/kaydxh/golang/pkg/binlog/datastore"
    31  	rotate_ "github.com/kaydxh/golang/pkg/file-rotate"
    32  )
    33  
    34  var _ ds_.DataStore = (*FileDataStore)(nil)
    35  
    36  type FileDataStore struct {
    37  	rotateFiler    *rotate_.RotateFiler
    38  	rotateFilers   map[string]*rotate_.RotateFiler //message key -> rotateFilter
    39  	rotateFilersMu sync.Mutex
    40  	rootDir        string
    41  	opts           []rotate_.RotateFilerOption
    42  }
    43  
    44  func NewFileDataStore(filedir string, options ...rotate_.RotateFilerOption) (*FileDataStore, error) {
    45  	rotate, err := rotate_.NewRotateFiler(filedir, options...)
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  	s := &FileDataStore{
    50  		rootDir: filedir,
    51  		opts:    options,
    52  	}
    53  	s.rotateFiler = rotate
    54  	return s, nil
    55  }
    56  
    57  func (s *FileDataStore) WriteData(ctx context.Context, arg interface{}, key ds_.MessageKey) (n int64, err error) {
    58  	rotateFiler := s.getOrCreate(ctx, key.Key)
    59  	p, ok := arg.([][]byte)
    60  	if !ok {
    61  		return 0, fmt.Errorf("invalid data type")
    62  	}
    63  	_, tn, err := rotateFiler.WriteBytesLine(p)
    64  	return int64(tn), err
    65  }
    66  
    67  func (s *FileDataStore) getOrCreate(ctx context.Context, key string) *rotate_.RotateFiler {
    68  	if key == "" {
    69  		return s.rotateFiler
    70  	}
    71  
    72  	s.rotateFilersMu.Lock()
    73  	defer s.rotateFilersMu.Unlock()
    74  	rotateFiler, ok := s.rotateFilers[key]
    75  	if !ok {
    76  		rotateFiler, _ = rotate_.NewRotateFiler(
    77  			filepath.Join(s.rootDir, key),
    78  			s.opts...,
    79  		)
    80  		s.rotateFilers[key] = rotateFiler
    81  	}
    82  
    83  	return rotateFiler
    84  }