database.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 rawdb
  17. import (
  18. "github.com/ethereum/go-ethereum/ethdb"
  19. "github.com/ethereum/go-ethereum/ethdb/leveldb"
  20. "github.com/ethereum/go-ethereum/ethdb/memorydb"
  21. )
  22. // freezerdb is a databse wrapper that enabled freezer data retrievals.
  23. type freezerdb struct {
  24. ethdb.KeyValueStore
  25. ethdb.Ancienter
  26. }
  27. // nofreezedb is a database wrapper that disables freezer data retrievals.
  28. type nofreezedb struct {
  29. ethdb.KeyValueStore
  30. }
  31. // Frozen returns nil as we don't have a backing chain freezer.
  32. func (db *nofreezedb) Ancient(kind string, number uint64) ([]byte, error) {
  33. return nil, errOutOfBounds
  34. }
  35. // NewDatabase creates a high level database on top of a given key-value data
  36. // store without a freezer moving immutable chain segments into cold storage.
  37. func NewDatabase(db ethdb.KeyValueStore) ethdb.Database {
  38. return &nofreezedb{
  39. KeyValueStore: db,
  40. }
  41. }
  42. // NewDatabaseWithFreezer creates a high level database on top of a given key-
  43. // value data store with a freezer moving immutable chain segments into cold
  44. // storage.
  45. func NewDatabaseWithFreezer(db ethdb.KeyValueStore, freezer string, namespace string) (ethdb.Database, error) {
  46. frdb, err := newFreezer(freezer, namespace)
  47. if err != nil {
  48. return nil, err
  49. }
  50. go frdb.freeze(db)
  51. return &freezerdb{
  52. KeyValueStore: db,
  53. Ancienter: frdb,
  54. }, nil
  55. }
  56. // NewMemoryDatabase creates an ephemeral in-memory key-value database without a
  57. // freezer moving immutable chain segments into cold storage.
  58. func NewMemoryDatabase() ethdb.Database {
  59. return NewDatabase(memorydb.New())
  60. }
  61. // NewMemoryDatabaseWithCap creates an ephemeral in-memory key-value database
  62. // with an initial starting capacity, but without a freezer moving immutable
  63. // chain segments into cold storage.
  64. func NewMemoryDatabaseWithCap(size int) ethdb.Database {
  65. return NewDatabase(memorydb.NewWithCap(size))
  66. }
  67. // NewLevelDBDatabase creates a persistent key-value database without a freezer
  68. // moving immutable chain segments into cold storage.
  69. func NewLevelDBDatabase(file string, cache int, handles int, namespace string) (ethdb.Database, error) {
  70. db, err := leveldb.New(file, cache, handles, namespace)
  71. if err != nil {
  72. return nil, err
  73. }
  74. return NewDatabase(db), nil
  75. }
  76. // NewLevelDBDatabaseWithFreezer creates a persistent key-value database with a
  77. // freezer moving immutable chain segments into cold storage.
  78. func NewLevelDBDatabaseWithFreezer(file string, cache int, handles int, freezer string, namespace string) (ethdb.Database, error) {
  79. kvdb, err := leveldb.New(file, cache, handles, namespace)
  80. if err != nil {
  81. return nil, err
  82. }
  83. frdb, err := NewDatabaseWithFreezer(kvdb, freezer, namespace)
  84. if err != nil {
  85. kvdb.Close()
  86. return nil, err
  87. }
  88. return frdb, nil
  89. }