github.com/m3shine/gochain@v2.2.26+incompatible/swarm/storage/localstore.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 "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 chunk.Done() 53 }() 54 } 55 56 // Get(chunk *Chunk) looks up a chunk in the local stores 57 // This method is blocking until the chunk is retrieved 58 // so additional timeout may be needed to wrap this call if 59 // ChunkStores are remote and can have long latency 60 func (self *LocalStore) Get(key Key) (chunk *Chunk, err error) { 61 chunk, err = self.memStore.Get(key) 62 if err == nil { 63 return 64 } 65 chunk, err = self.DbStore.Get(key) 66 if err != nil { 67 return 68 } 69 chunk.Size = int64(binary.LittleEndian.Uint64(chunk.SData[0:8])) 70 self.memStore.Put(chunk) 71 return 72 } 73 74 // Close local store 75 func (self *LocalStore) Close() {}