github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/swarm/storage/localstore.go (about) 1 // Copyright 2016 The Spectrum Authors 2 // This file is part of the Spectrum library. 3 // 4 // The Spectrum 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 Spectrum 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 Spectrum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package storage 18 19 import ( 20 "encoding/binary" 21 ) 22 23 // LocalStore is a combination of inmemory db over a disk persisted db 24 // implements a Get/Put with fallback (caching) logic using any 2 ChunkStores 25 type LocalStore struct { 26 memStore ChunkStore 27 DbStore ChunkStore 28 } 29 30 // This constructor uses MemStore and DbStore as components 31 func NewLocalStore(hash SwarmHasher, params *StoreParams) (*LocalStore, error) { 32 dbStore, err := NewDbStore(params.ChunkDbPath, hash, params.DbCapacity, params.Radius) 33 if err != nil { 34 return nil, err 35 } 36 return &LocalStore{ 37 memStore: NewMemStore(dbStore, params.CacheCapacity), 38 DbStore: dbStore, 39 }, nil 40 } 41 42 // LocalStore is itself a chunk store 43 // unsafe, in that the data is not integrity checked 44 func (self *LocalStore) Put(chunk *Chunk) { 45 chunk.dbStored = make(chan bool) 46 self.memStore.Put(chunk) 47 if chunk.wg != nil { 48 chunk.wg.Add(1) 49 } 50 go func() { 51 self.DbStore.Put(chunk) 52 if chunk.wg != nil { 53 chunk.wg.Done() 54 } 55 }() 56 } 57 58 // Get(chunk *Chunk) looks up a chunk in the local stores 59 // This method is blocking until the chunk is retrieved 60 // so additional timeout may be needed to wrap this call if 61 // ChunkStores are remote and can have long latency 62 func (self *LocalStore) Get(key Key) (chunk *Chunk, err error) { 63 chunk, err = self.memStore.Get(key) 64 if err == nil { 65 return 66 } 67 chunk, err = self.DbStore.Get(key) 68 if err != nil { 69 return 70 } 71 chunk.Size = int64(binary.LittleEndian.Uint64(chunk.SData[0:8])) 72 self.memStore.Put(chunk) 73 return 74 } 75 76 // Close local store 77 func (self *LocalStore) Close() {}