github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/swarm/storage/filestore.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 12:09:49</date>
    10  //</624342681010573312>
    11  
    12  //
    13  //
    14  //
    15  //
    16  //
    17  //
    18  //
    19  //
    20  //
    21  //
    22  //
    23  //
    24  //
    25  //
    26  //
    27  
    28  package storage
    29  
    30  import (
    31  	"context"
    32  	"io"
    33  )
    34  
    35  /*
    36  
    37  
    38  
    39  
    40  
    41  
    42  
    43  
    44  
    45  */
    46  
    47  
    48  const (
    49  defaultLDBCapacity                = 5000000 //
    50  defaultCacheCapacity              = 10000   //
    51  defaultChunkRequestsCacheCapacity = 5000000 //
    52  )
    53  
    54  type FileStore struct {
    55  	ChunkStore
    56  	hashFunc SwarmHasher
    57  }
    58  
    59  type FileStoreParams struct {
    60  	Hash string
    61  }
    62  
    63  func NewFileStoreParams() *FileStoreParams {
    64  	return &FileStoreParams{
    65  		Hash: DefaultHash,
    66  	}
    67  }
    68  
    69  //
    70  func NewLocalFileStore(datadir string, basekey []byte) (*FileStore, error) {
    71  	params := NewDefaultLocalStoreParams()
    72  	params.Init(datadir)
    73  	localStore, err := NewLocalStore(params, nil)
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  	localStore.Validators = append(localStore.Validators, NewContentAddressValidator(MakeHashFunc(DefaultHash)))
    78  	return NewFileStore(localStore, NewFileStoreParams()), nil
    79  }
    80  
    81  func NewFileStore(store ChunkStore, params *FileStoreParams) *FileStore {
    82  	hashFunc := MakeHashFunc(params.Hash)
    83  	return &FileStore{
    84  		ChunkStore: store,
    85  		hashFunc:   hashFunc,
    86  	}
    87  }
    88  
    89  //
    90  //
    91  //
    92  //
    93  //
    94  func (f *FileStore) Retrieve(ctx context.Context, addr Address) (reader *LazyChunkReader, isEncrypted bool) {
    95  	isEncrypted = len(addr) > f.hashFunc().Size()
    96  	getter := NewHasherStore(f.ChunkStore, f.hashFunc, isEncrypted)
    97  	reader = TreeJoin(ctx, addr, getter, 0)
    98  	return
    99  }
   100  
   101  //
   102  //
   103  func (f *FileStore) Store(ctx context.Context, data io.Reader, size int64, toEncrypt bool) (addr Address, wait func(context.Context) error, err error) {
   104  	putter := NewHasherStore(f.ChunkStore, f.hashFunc, toEncrypt)
   105  	return PyramidSplit(ctx, data, putter, putter)
   106  }
   107  
   108  func (f *FileStore) HashSize() int {
   109  	return f.hashFunc().Size()
   110  }
   111