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