github.com/divan/go-ethereum@v1.8.14-0.20180820134928-1de9ada4016d/swarm/storage/chunkstore.go (about) 1 // Copyright 2016 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package storage 18 19 import ( 20 "context" 21 "sync" 22 ) 23 24 /* 25 ChunkStore interface is implemented by : 26 27 - MemStore: a memory cache 28 - DbStore: local disk/db store 29 - LocalStore: a combination (sequence of) memStore and dbStore 30 - NetStore: cloud storage abstraction layer 31 - FakeChunkStore: dummy store which doesn't store anything just implements the interface 32 */ 33 type ChunkStore interface { 34 Put(context.Context, *Chunk) // effectively there is no error even if there is an error 35 Get(context.Context, Address) (*Chunk, error) 36 Close() 37 } 38 39 // MapChunkStore is a very simple ChunkStore implementation to store chunks in a map in memory. 40 type MapChunkStore struct { 41 chunks map[string]*Chunk 42 mu sync.RWMutex 43 } 44 45 func NewMapChunkStore() *MapChunkStore { 46 return &MapChunkStore{ 47 chunks: make(map[string]*Chunk), 48 } 49 } 50 51 func (m *MapChunkStore) Put(ctx context.Context, chunk *Chunk) { 52 m.mu.Lock() 53 defer m.mu.Unlock() 54 m.chunks[chunk.Addr.Hex()] = chunk 55 chunk.markAsStored() 56 } 57 58 func (m *MapChunkStore) Get(ctx context.Context, addr Address) (*Chunk, error) { 59 m.mu.RLock() 60 defer m.mu.RUnlock() 61 chunk := m.chunks[addr.Hex()] 62 if chunk == nil { 63 return nil, ErrChunkNotFound 64 } 65 return chunk, nil 66 } 67 68 func (m *MapChunkStore) Close() { 69 }