chunkstore.go 2.0 KB

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