github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/swarm/storage/filestore.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //
    10  //
    11  //
    12  //
    13  //
    14  //
    15  //
    16  //
    17  //
    18  //
    19  //
    20  //
    21  //
    22  //
    23  //
    24  
    25  package storage
    26  
    27  import (
    28  	"context"
    29  	"io"
    30  )
    31  
    32  /*
    33  
    34  
    35  
    36  
    37  
    38  
    39  
    40  
    41  
    42  */
    43  
    44  
    45  const (
    46  defaultLDBCapacity                = 5000000 //
    47  defaultCacheCapacity              = 10000   //
    48  defaultChunkRequestsCacheCapacity = 5000000 //
    49  )
    50  
    51  type FileStore struct {
    52  	ChunkStore
    53  	hashFunc SwarmHasher
    54  }
    55  
    56  type FileStoreParams struct {
    57  	Hash string
    58  }
    59  
    60  func NewFileStoreParams() *FileStoreParams {
    61  	return &FileStoreParams{
    62  		Hash: DefaultHash,
    63  	}
    64  }
    65  
    66  //
    67  func NewLocalFileStore(datadir string, basekey []byte) (*FileStore, error) {
    68  	params := NewDefaultLocalStoreParams()
    69  	params.Init(datadir)
    70  	localStore, err := NewLocalStore(params, nil)
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  	localStore.Validators = append(localStore.Validators, NewContentAddressValidator(MakeHashFunc(DefaultHash)))
    75  	return NewFileStore(localStore, NewFileStoreParams()), nil
    76  }
    77  
    78  func NewFileStore(store ChunkStore, params *FileStoreParams) *FileStore {
    79  	hashFunc := MakeHashFunc(params.Hash)
    80  	return &FileStore{
    81  		ChunkStore: store,
    82  		hashFunc:   hashFunc,
    83  	}
    84  }
    85  
    86  //
    87  //
    88  //
    89  //
    90  //
    91  func (f *FileStore) Retrieve(ctx context.Context, addr Address) (reader *LazyChunkReader, isEncrypted bool) {
    92  	isEncrypted = len(addr) > f.hashFunc().Size()
    93  	getter := NewHasherStore(f.ChunkStore, f.hashFunc, isEncrypted)
    94  	reader = TreeJoin(ctx, addr, getter, 0)
    95  	return
    96  }
    97  
    98  //
    99  //
   100  func (f *FileStore) Store(ctx context.Context, data io.Reader, size int64, toEncrypt bool) (addr Address, wait func(context.Context) error, err error) {
   101  	putter := NewHasherStore(f.ChunkStore, f.hashFunc, toEncrypt)
   102  	return PyramidSplit(ctx, data, putter, putter)
   103  }
   104  
   105  func (f *FileStore) HashSize() int {
   106  	return f.hashFunc().Size()
   107  }