localstore.go 2.7 KB

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