database.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. // this is a clone of an earlier state of the ethereum ethdb/database
  18. // no need for queueing/caching
  19. import (
  20. "fmt"
  21. "github.com/ethereum/go-ethereum/metrics"
  22. "github.com/syndtr/goleveldb/leveldb"
  23. "github.com/syndtr/goleveldb/leveldb/iterator"
  24. "github.com/syndtr/goleveldb/leveldb/opt"
  25. )
  26. const openFileLimit = 128
  27. type LDBDatabase struct {
  28. db *leveldb.DB
  29. }
  30. func NewLDBDatabase(file string) (*LDBDatabase, error) {
  31. // Open the db
  32. db, err := leveldb.OpenFile(file, &opt.Options{OpenFilesCacheCapacity: openFileLimit})
  33. if err != nil {
  34. return nil, err
  35. }
  36. database := &LDBDatabase{db: db}
  37. return database, nil
  38. }
  39. func (db *LDBDatabase) Put(key []byte, value []byte) {
  40. metrics.GetOrRegisterCounter("ldbdatabase.put", nil).Inc(1)
  41. err := db.db.Put(key, value, nil)
  42. if err != nil {
  43. fmt.Println("Error put", err)
  44. }
  45. }
  46. func (db *LDBDatabase) Get(key []byte) ([]byte, error) {
  47. metrics.GetOrRegisterCounter("ldbdatabase.get", nil).Inc(1)
  48. dat, err := db.db.Get(key, nil)
  49. if err != nil {
  50. return nil, err
  51. }
  52. return dat, nil
  53. }
  54. func (db *LDBDatabase) Delete(key []byte) error {
  55. return db.db.Delete(key, nil)
  56. }
  57. func (db *LDBDatabase) LastKnownTD() []byte {
  58. data, _ := db.Get([]byte("LTD"))
  59. if len(data) == 0 {
  60. data = []byte{0x0}
  61. }
  62. return data
  63. }
  64. func (db *LDBDatabase) NewIterator() iterator.Iterator {
  65. metrics.GetOrRegisterCounter("ldbdatabase.newiterator", nil).Inc(1)
  66. return db.db.NewIterator(nil, nil)
  67. }
  68. func (db *LDBDatabase) Write(batch *leveldb.Batch) error {
  69. metrics.GetOrRegisterCounter("ldbdatabase.write", nil).Inc(1)
  70. return db.db.Write(batch, nil)
  71. }
  72. func (db *LDBDatabase) Close() {
  73. // Close the leveldb database
  74. db.db.Close()
  75. }