github.com/uber/kraken@v0.1.4/lib/store/cache_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 store
    15  
    16  import (
    17  	"fmt"
    18  	"os"
    19  
    20  	"github.com/uber/kraken/lib/store/base"
    21  	"github.com/uber/kraken/lib/store/metadata"
    22  )
    23  
    24  // cacheStore provides basic cache file operations. Intended to be embedded in
    25  // higher level structs.
    26  type cacheStore struct {
    27  	state   base.FileState
    28  	backend base.FileStore
    29  }
    30  
    31  func newCacheStore(dir string, backend base.FileStore) (*cacheStore, error) {
    32  	if err := os.MkdirAll(dir, 0775); err != nil {
    33  		return nil, fmt.Errorf("mkdir: %s", err)
    34  	}
    35  	state := base.NewFileState(dir)
    36  	return &cacheStore{state, backend}, nil
    37  }
    38  
    39  func (s *cacheStore) GetCacheFileReader(name string) (FileReader, error) {
    40  	return s.newFileOp().GetFileReader(name)
    41  }
    42  
    43  func (s *cacheStore) GetCacheFileStat(name string) (os.FileInfo, error) {
    44  	return s.newFileOp().GetFileStat(name)
    45  }
    46  
    47  func (s *cacheStore) DeleteCacheFile(name string) error {
    48  	return s.newFileOp().DeleteFile(name)
    49  }
    50  
    51  func (s *cacheStore) GetCacheFileMetadata(name string, md metadata.Metadata) error {
    52  	return s.newFileOp().GetFileMetadata(name, md)
    53  }
    54  
    55  func (s *cacheStore) SetCacheFileMetadata(name string, md metadata.Metadata) (bool, error) {
    56  	return s.newFileOp().SetFileMetadata(name, md)
    57  }
    58  
    59  func (s *cacheStore) GetOrSetCacheFileMetadata(name string, md metadata.Metadata) error {
    60  	return s.newFileOp().GetOrSetFileMetadata(name, md)
    61  }
    62  
    63  func (s *cacheStore) DeleteCacheFileMetadata(name string, md metadata.Metadata) error {
    64  	return s.newFileOp().DeleteFileMetadata(name, md)
    65  }
    66  
    67  func (s *cacheStore) ListCacheFiles() ([]string, error) {
    68  	return s.newFileOp().ListNames()
    69  }
    70  
    71  func (s *cacheStore) newFileOp() base.FileOp {
    72  	return s.backend.NewFileOp().AcceptState(s.state)
    73  }