github.com/gobitfly/go-ethereum@v1.8.12/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 "sync" 20 21 /* 22 ChunkStore interface is implemented by : 23 24 - MemStore: a memory cache 25 - DbStore: local disk/db store 26 - LocalStore: a combination (sequence of) memStore and dbStore 27 - NetStore: cloud storage abstraction layer 28 - FakeChunkStore: dummy store which doesn't store anything just implements the interface 29 */ 30 type ChunkStore interface { 31 Put(*Chunk) // effectively there is no error even if there is an error 32 Get(Address) (*Chunk, error) 33 Close() 34 } 35 36 // MapChunkStore is a very simple ChunkStore implementation to store chunks in a map in memory. 37 type MapChunkStore struct { 38 chunks map[string]*Chunk 39 mu sync.RWMutex 40 } 41 42 func NewMapChunkStore() *MapChunkStore { 43 return &MapChunkStore{ 44 chunks: make(map[string]*Chunk), 45 } 46 } 47 48 func (m *MapChunkStore) Put(chunk *Chunk) { 49 m.mu.Lock() 50 defer m.mu.Unlock() 51 m.chunks[chunk.Addr.Hex()] = chunk 52 chunk.markAsStored() 53 } 54 55 func (m *MapChunkStore) Get(addr Address) (*Chunk, error) { 56 m.mu.RLock() 57 defer m.mu.RUnlock() 58 chunk := m.chunks[addr.Hex()] 59 if chunk == nil { 60 return nil, ErrChunkNotFound 61 } 62 return chunk, nil 63 } 64 65 func (m *MapChunkStore) Close() { 66 }