github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/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 19:16:45</date>
    10  //</624450119764283392>
    11  
    12  
    13  package storage
    14  
    15  import (
    16  	"context"
    17  	"io"
    18  )
    19  
    20  /*
    21  filestore提供客户端API入口点存储和检索到存储和检索
    22  它可以存储任何具有字节片表示的内容,例如文件或序列化对象等。
    23  
    24  存储:filestore调用chunker将任意大小的输入数据流分段到一个merkle散列的块树。根块的键将返回到客户端。
    25  
    26  检索:给定根块的键,文件存储将检索块块块并重建原始数据,并将其作为一个懒惰的读卡器传递回去。懒惰的读卡器是具有按需延迟处理的读卡器,即,只有在实际读取文档的特定部分时,才提取和处理重建大型文件所需的块。
    27  
    28  当chunker生成块时,filestore将它们发送到自己的块存储区。
    29  用于存储或检索的实现。
    30  **/
    31  
    32  
    33  const (
    34  defaultLDBCapacity                = 5000000 //LEVELDB的容量,默认为5*10^6*4096字节==20GB
    35  defaultCacheCapacity              = 10000   //内存块缓存的容量
    36  defaultChunkRequestsCacheCapacity = 5000000 //容纳块的传出请求的容器的容量。应设置为LEVELDB容量
    37  )
    38  
    39  type FileStore struct {
    40  	ChunkStore
    41  	hashFunc SwarmHasher
    42  }
    43  
    44  type FileStoreParams struct {
    45  	Hash string
    46  }
    47  
    48  func NewFileStoreParams() *FileStoreParams {
    49  	return &FileStoreParams{
    50  		Hash: DefaultHash,
    51  	}
    52  }
    53  
    54  //用于本地测试
    55  func NewLocalFileStore(datadir string, basekey []byte) (*FileStore, error) {
    56  	params := NewDefaultLocalStoreParams()
    57  	params.Init(datadir)
    58  	localStore, err := NewLocalStore(params, nil)
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  	localStore.Validators = append(localStore.Validators, NewContentAddressValidator(MakeHashFunc(DefaultHash)))
    63  	return NewFileStore(localStore, NewFileStoreParams()), nil
    64  }
    65  
    66  func NewFileStore(store ChunkStore, params *FileStoreParams) *FileStore {
    67  	hashFunc := MakeHashFunc(params.Hash)
    68  	return &FileStore{
    69  		ChunkStore: store,
    70  		hashFunc:   hashFunc,
    71  	}
    72  }
    73  
    74  //公共API。文档直接检索的主要入口点。使用的
    75  //支持fs的api和httpaccess
    76  //NetStore请求上的区块检索块超时,因此读卡器将
    77  //如果在请求的范围内检索块超时,则报告错误。
    78  //它返回一个读卡器,其中包含块数据以及内容是否加密。
    79  func (f *FileStore) Retrieve(ctx context.Context, addr Address) (reader *LazyChunkReader, isEncrypted bool) {
    80  	isEncrypted = len(addr) > f.hashFunc().Size()
    81  	getter := NewHasherStore(f.ChunkStore, f.hashFunc, isEncrypted)
    82  	reader = TreeJoin(ctx, addr, getter, 0)
    83  	return
    84  }
    85  
    86  //公共API。文档直接存储的主要入口点。使用的
    87  //支持fs的api和httpaccess
    88  func (f *FileStore) Store(ctx context.Context, data io.Reader, size int64, toEncrypt bool) (addr Address, wait func(context.Context) error, err error) {
    89  	putter := NewHasherStore(f.ChunkStore, f.hashFunc, toEncrypt)
    90  	return PyramidSplit(ctx, data, putter, putter)
    91  }
    92  
    93  func (f *FileStore) HashSize() int {
    94  	return f.hashFunc().Size()
    95  }
    96