github.com/FUSIONFoundation/efsn@v3.6.2-0.20200916075423-dbb5dd5d2cc7+incompatible/swarm/storage/filestore.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package storage
    18  
    19  import (
    20  	"context"
    21  	"io"
    22  )
    23  
    24  /*
    25  FileStore provides the client API entrypoints Store and Retrieve to store and retrieve
    26  It can store anything that has a byte slice representation, so files or serialised objects etc.
    27  
    28  Storage: FileStore calls the Chunker to segment the input datastream of any size to a merkle hashed tree of chunks. The key of the root block is returned to the client.
    29  
    30  Retrieval: given the key of the root block, the FileStore retrieves the block chunks and reconstructs the original data and passes it back as a lazy reader. A lazy reader is a reader with on-demand delayed processing, i.e. the chunks needed to reconstruct a large file are only fetched and processed if that particular part of the document is actually read.
    31  
    32  As the chunker produces chunks, FileStore dispatches them to its own chunk store
    33  implementation for storage or retrieval.
    34  */
    35  
    36  const (
    37  	defaultLDBCapacity                = 5000000 // capacity for LevelDB, by default 5*10^6*4096 bytes == 20GB
    38  	defaultCacheCapacity              = 10000   // capacity for in-memory chunks' cache
    39  	defaultChunkRequestsCacheCapacity = 5000000 // capacity for container holding outgoing requests for chunks. should be set to LevelDB capacity
    40  )
    41  
    42  type FileStore struct {
    43  	ChunkStore
    44  	hashFunc SwarmHasher
    45  }
    46  
    47  type FileStoreParams struct {
    48  	Hash string
    49  }
    50  
    51  func NewFileStoreParams() *FileStoreParams {
    52  	return &FileStoreParams{
    53  		Hash: DefaultHash,
    54  	}
    55  }
    56  
    57  // for testing locally
    58  func NewLocalFileStore(datadir string, basekey []byte) (*FileStore, error) {
    59  	params := NewDefaultLocalStoreParams()
    60  	params.Init(datadir)
    61  	localStore, err := NewLocalStore(params, nil)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  	localStore.Validators = append(localStore.Validators, NewContentAddressValidator(MakeHashFunc(DefaultHash)))
    66  	return NewFileStore(localStore, NewFileStoreParams()), nil
    67  }
    68  
    69  func NewFileStore(store ChunkStore, params *FileStoreParams) *FileStore {
    70  	hashFunc := MakeHashFunc(params.Hash)
    71  	return &FileStore{
    72  		ChunkStore: store,
    73  		hashFunc:   hashFunc,
    74  	}
    75  }
    76  
    77  // Public API. Main entry point for document retrieval directly. Used by the
    78  // FS-aware API and httpaccess
    79  // Chunk retrieval blocks on netStore requests with a timeout so reader will
    80  // report error if retrieval of chunks within requested range time out.
    81  // It returns a reader with the chunk data and whether the content was encrypted
    82  func (f *FileStore) Retrieve(ctx context.Context, addr Address) (reader *LazyChunkReader, isEncrypted bool) {
    83  	isEncrypted = len(addr) > f.hashFunc().Size()
    84  	getter := NewHasherStore(f.ChunkStore, f.hashFunc, isEncrypted)
    85  	reader = TreeJoin(ctx, addr, getter, 0)
    86  	return
    87  }
    88  
    89  // Public API. Main entry point for document storage directly. Used by the
    90  // FS-aware API and httpaccess
    91  func (f *FileStore) Store(ctx context.Context, data io.Reader, size int64, toEncrypt bool) (addr Address, wait func(context.Context) error, err error) {
    92  	putter := NewHasherStore(f.ChunkStore, f.hashFunc, toEncrypt)
    93  	return PyramidSplit(ctx, data, putter, putter)
    94  }
    95  
    96  func (f *FileStore) HashSize() int {
    97  	return f.hashFunc().Size()
    98  }