dbstore.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 state
  17. import (
  18. "encoding"
  19. "encoding/json"
  20. "errors"
  21. "github.com/syndtr/goleveldb/leveldb"
  22. )
  23. // ErrNotFound is returned when no results are returned from the database
  24. var ErrNotFound = errors.New("ErrorNotFound")
  25. // ErrInvalidArgument is returned when the argument type does not match the expected type
  26. var ErrInvalidArgument = errors.New("ErrorInvalidArgument")
  27. // DBStore uses LevelDB to store values.
  28. type DBStore struct {
  29. db *leveldb.DB
  30. }
  31. // NewDBStore creates a new instance of DBStore.
  32. func NewDBStore(path string) (s *DBStore, err error) {
  33. db, err := leveldb.OpenFile(path, nil)
  34. if err != nil {
  35. return nil, err
  36. }
  37. return &DBStore{
  38. db: db,
  39. }, nil
  40. }
  41. // Get retrieves a persisted value for a specific key. If there is no results
  42. // ErrNotFound is returned. The provided parameter should be either a byte slice or
  43. // a struct that implements the encoding.BinaryUnmarshaler interface
  44. func (s *DBStore) Get(key string, i interface{}) (err error) {
  45. has, err := s.db.Has([]byte(key), nil)
  46. if err != nil || !has {
  47. return ErrNotFound
  48. }
  49. data, err := s.db.Get([]byte(key), nil)
  50. if err == leveldb.ErrNotFound {
  51. return ErrNotFound
  52. }
  53. unmarshaler, ok := i.(encoding.BinaryUnmarshaler)
  54. if !ok {
  55. return json.Unmarshal(data, i)
  56. }
  57. return unmarshaler.UnmarshalBinary(data)
  58. }
  59. // Put stores an object that implements Binary for a specific key.
  60. func (s *DBStore) Put(key string, i interface{}) (err error) {
  61. bytes := []byte{}
  62. marshaler, ok := i.(encoding.BinaryMarshaler)
  63. if !ok {
  64. if bytes, err = json.Marshal(i); err != nil {
  65. return err
  66. }
  67. } else {
  68. if bytes, err = marshaler.MarshalBinary(); err != nil {
  69. return err
  70. }
  71. }
  72. return s.db.Put([]byte(key), bytes, nil)
  73. }
  74. // Delete removes entries stored under a specific key.
  75. func (s *DBStore) Delete(key string) (err error) {
  76. return s.db.Delete([]byte(key), nil)
  77. }
  78. // Close releases the resources used by the underlying LevelDB.
  79. func (s *DBStore) Close() error {
  80. return s.db.Close()
  81. }