github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/swarm/storage/chunkstore.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  	"sync"
    30  )
    31  
    32  /*
    33  
    34  
    35  
    36  
    37  
    38  
    39  
    40  */
    41  
    42  type ChunkStore interface {
    43  Put(context.Context, *Chunk) //
    44  	Get(context.Context, Address) (*Chunk, error)
    45  	Close()
    46  }
    47  
    48  //
    49  type MapChunkStore struct {
    50  	chunks map[string]*Chunk
    51  	mu     sync.RWMutex
    52  }
    53  
    54  func NewMapChunkStore() *MapChunkStore {
    55  	return &MapChunkStore{
    56  		chunks: make(map[string]*Chunk),
    57  	}
    58  }
    59  
    60  func (m *MapChunkStore) Put(ctx context.Context, chunk *Chunk) {
    61  	m.mu.Lock()
    62  	defer m.mu.Unlock()
    63  	m.chunks[chunk.Addr.Hex()] = chunk
    64  	chunk.markAsStored()
    65  }
    66  
    67  func (m *MapChunkStore) Get(ctx context.Context, addr Address) (*Chunk, error) {
    68  	m.mu.RLock()
    69  	defer m.mu.RUnlock()
    70  	chunk := m.chunks[addr.Hex()]
    71  	if chunk == nil {
    72  		return nil, ErrChunkNotFound
    73  	}
    74  	return chunk, nil
    75  }
    76  
    77  func (m *MapChunkStore) Close() {
    78  }