github.com/lzl124631x/go-ethereum@v1.8.12-0.20180615081455-574378edb50c/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 "github.com/ethereum/go-ethereum/metrics" 23 ) 24 25 //metrics variables 26 var ( 27 dbStorePutCounter = metrics.NewRegisteredCounter("storage.db.dbstore.put.count", nil) 28 ) 29 30 // LocalStore is a combination of inmemory db over a disk persisted db 31 // implements a Get/Put with fallback (caching) logic using any 2 ChunkStores 32 type LocalStore struct { 33 memStore ChunkStore 34 DbStore ChunkStore 35 } 36 37 // This constructor uses MemStore and DbStore as components 38 func NewLocalStore(hash SwarmHasher, params *StoreParams) (*LocalStore, error) { 39 dbStore, err := NewDbStore(params.ChunkDbPath, hash, params.DbCapacity, params.Radius) 40 if err != nil { 41 return nil, err 42 } 43 return &LocalStore{ 44 memStore: NewMemStore(dbStore, params.CacheCapacity), 45 DbStore: dbStore, 46 }, nil 47 } 48 49 func (self *LocalStore) CacheCounter() uint64 { 50 return uint64(self.memStore.(*MemStore).Counter()) 51 } 52 53 func (self *LocalStore) DbCounter() uint64 { 54 return self.DbStore.(*DbStore).Counter() 55 } 56 57 // LocalStore is itself a chunk store 58 // unsafe, in that the data is not integrity checked 59 func (self *LocalStore) Put(chunk *Chunk) { 60 chunk.dbStored = make(chan bool) 61 self.memStore.Put(chunk) 62 if chunk.wg != nil { 63 chunk.wg.Add(1) 64 } 65 go func() { 66 dbStorePutCounter.Inc(1) 67 self.DbStore.Put(chunk) 68 if chunk.wg != nil { 69 chunk.wg.Done() 70 } 71 }() 72 } 73 74 // Get(chunk *Chunk) looks up a chunk in the local stores 75 // This method is blocking until the chunk is retrieved 76 // so additional timeout may be needed to wrap this call if 77 // ChunkStores are remote and can have long latency 78 func (self *LocalStore) Get(key Key) (chunk *Chunk, err error) { 79 chunk, err = self.memStore.Get(key) 80 if err == nil { 81 return 82 } 83 chunk, err = self.DbStore.Get(key) 84 if err != nil { 85 return 86 } 87 chunk.Size = int64(binary.LittleEndian.Uint64(chunk.SData[0:8])) 88 self.memStore.Put(chunk) 89 return 90 } 91 92 // Close local store 93 func (self *LocalStore) Close() {}