You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
2.8 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package db
  2. import . "github.com/tendermint/tmlibs/common"
  3. type DB interface {
  4. Get([]byte) []byte // NOTE: returns nil iff never set or deleted.
  5. Set([]byte, []byte)
  6. SetSync([]byte, []byte)
  7. Delete([]byte)
  8. DeleteSync([]byte)
  9. Close()
  10. NewBatch() Batch
  11. Iterator() Iterator
  12. // For debugging
  13. Print()
  14. // Stats returns a map of property values for all keys and the size of the cache.
  15. Stats() map[string]string
  16. // CacheDB wraps the DB w/ a cache.
  17. CacheDB() CacheDB
  18. }
  19. type CacheDB interface {
  20. DB
  21. Write() // Write to the underlying DB
  22. }
  23. type SetDeleter interface {
  24. Set(key, value []byte)
  25. Delete(key []byte)
  26. }
  27. type Batch interface {
  28. SetDeleter
  29. Write()
  30. }
  31. /*
  32. Usage:
  33. for itr.Seek(mykey); itr.Valid(); itr.Next() {
  34. k, v := itr.Key(); itr.Value()
  35. ....
  36. }
  37. */
  38. type Iterator interface {
  39. // Seek moves the iterator the position of the key given or, if the key
  40. // doesn't exist, the next key that does exist in the database. If the key
  41. // doesn't exist, and there is no next key, the Iterator becomes invalid.
  42. Seek(key []byte)
  43. // Valid returns false only when an Iterator has iterated past either the
  44. // first or the last key in the database.
  45. Valid() bool
  46. // Next moves the iterator to the next sequential key in the database, as
  47. // defined by the Comparator in the ReadOptions used to create this Iterator.
  48. //
  49. // If Valid returns false, this method will panic.
  50. Next()
  51. // Prev moves the iterator to the previous sequential key in the database, as
  52. // defined by the Comparator in the ReadOptions used to create this Iterator.
  53. //
  54. // If Valid returns false, this method will panic.
  55. Prev()
  56. // Key returns the key of the cursor.
  57. //
  58. // If Valid returns false, this method will panic.
  59. Key() []byte
  60. // Value returns the key of the cursor.
  61. //
  62. // If Valid returns false, this method will panic.
  63. Value() []byte
  64. // GetError returns an IteratorError from LevelDB if it had one during
  65. // iteration.
  66. //
  67. // This method is safe to call when Valid returns false.
  68. GetError() error
  69. // Close deallocates the given Iterator.
  70. Close()
  71. }
  72. //-----------------------------------------------------------------------------
  73. // Main entry
  74. const (
  75. LevelDBBackendStr = "leveldb" // legacy, defaults to goleveldb.
  76. CLevelDBBackendStr = "cleveldb"
  77. GoLevelDBBackendStr = "goleveldb"
  78. MemDBBackendStr = "memdb"
  79. FSDBBackendStr = "fsdb" // using the filesystem naively
  80. )
  81. type dbCreator func(name string, dir string) (DB, error)
  82. var backends = map[string]dbCreator{}
  83. func registerDBCreator(backend string, creator dbCreator, force bool) {
  84. _, ok := backends[backend]
  85. if !force && ok {
  86. return
  87. }
  88. backends[backend] = creator
  89. }
  90. func NewDB(name string, backend string, dir string) DB {
  91. db, err := backends[backend](name, dir)
  92. if err != nil {
  93. PanicSanity(Fmt("Error initializing DB: %v", err))
  94. }
  95. return db
  96. }