dbapi.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2018 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 "context"
  18. // wrapper of db-s to provide mockable custom local chunk store access to syncer
  19. type DBAPI struct {
  20. db *LDBStore
  21. loc *LocalStore
  22. }
  23. func NewDBAPI(loc *LocalStore) *DBAPI {
  24. return &DBAPI{loc.DbStore, loc}
  25. }
  26. // to obtain the chunks from address or request db entry only
  27. func (d *DBAPI) Get(ctx context.Context, addr Address) (*Chunk, error) {
  28. return d.loc.Get(ctx, addr)
  29. }
  30. // current storage counter of chunk db
  31. func (d *DBAPI) CurrentBucketStorageIndex(po uint8) uint64 {
  32. return d.db.CurrentBucketStorageIndex(po)
  33. }
  34. // iteration storage counter and proximity order
  35. func (d *DBAPI) Iterator(from uint64, to uint64, po uint8, f func(Address, uint64) bool) error {
  36. return d.db.SyncIterator(from, to, po, f)
  37. }
  38. // to obtain the chunks from address or request db entry only
  39. func (d *DBAPI) GetOrCreateRequest(ctx context.Context, addr Address) (*Chunk, bool) {
  40. return d.loc.GetOrCreateRequest(ctx, addr)
  41. }
  42. // to obtain the chunks from key or request db entry only
  43. func (d *DBAPI) Put(ctx context.Context, chunk *Chunk) {
  44. d.loc.Put(ctx, chunk)
  45. }