github.com/uber/kraken@v0.1.4/lib/store/base/file_store.go (about)

     1  // Copyright (c) 2016-2019 Uber Technologies, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package base
    15  
    16  import (
    17  	"github.com/andres-erbsen/clock"
    18  )
    19  
    20  // FileStore manages files and their metadata. Actual operations are done through FileOp.
    21  type FileStore interface {
    22  	NewFileOp() FileOp
    23  }
    24  
    25  // localFileStore manages all agent files on local disk.
    26  type localFileStore struct {
    27  	fileEntryFactory FileEntryFactory
    28  	fileMap          FileMap
    29  }
    30  
    31  // NewLocalFileStore initializes and returns a new FileStore.
    32  func NewLocalFileStore(clk clock.Clock) FileStore {
    33  	m := NewLATFileMap(clk)
    34  	return &localFileStore{
    35  		fileEntryFactory: NewLocalFileEntryFactory(),
    36  		fileMap:          m,
    37  	}
    38  }
    39  
    40  // NewCASFileStore initializes and returns a new Content-Addressable FileStore.
    41  // It uses the first few bytes of file digest (which is also used as file name)
    42  // as shard ID.
    43  // For every byte, one more level of directories will be created.
    44  func NewCASFileStore(clk clock.Clock) FileStore {
    45  	m := NewLATFileMap(clk)
    46  	return &localFileStore{
    47  		fileEntryFactory: NewCASFileEntryFactory(),
    48  		fileMap:          m,
    49  	}
    50  }
    51  
    52  // NewLRUFileStore initializes and returns a new LRU FileStore.
    53  // When size exceeds limit, the least recently accessed entry will be removed.
    54  func NewLRUFileStore(size int, clk clock.Clock) FileStore {
    55  	m := NewLRUFileMap(size, clk)
    56  	return &localFileStore{
    57  		fileEntryFactory: NewLocalFileEntryFactory(),
    58  		fileMap:          m,
    59  	}
    60  }
    61  
    62  // NewCASFileStoreWithLRUMap initializes and returns a new Content-Addressable
    63  // FileStore. It uses the first few bytes of file digest (which is also used as
    64  // file name) as shard ID.
    65  // For every byte, one more level of directories will be created. It also stores
    66  // objects in a LRU FileStore.
    67  // When size exceeds limit, the least recently accessed entry will be removed.
    68  func NewCASFileStoreWithLRUMap(size int, clk clock.Clock) FileStore {
    69  	m := NewLRUFileMap(size, clk)
    70  	return &localFileStore{
    71  		fileEntryFactory: NewCASFileEntryFactory(),
    72  		fileMap:          m,
    73  	}
    74  }
    75  
    76  // NewFileOp contructs a new FileOp object.
    77  func (s *localFileStore) NewFileOp() FileOp {
    78  	return NewLocalFileOp(s)
    79  }