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.

64 lines
1.2 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
  1. package db
  2. import . "github.com/tendermint/tmlibs/common"
  3. type DB interface {
  4. Get([]byte) []byte
  5. Set([]byte, []byte)
  6. SetSync([]byte, []byte)
  7. Delete([]byte)
  8. DeleteSync([]byte)
  9. Close()
  10. NewBatch() Batch
  11. Iterator() Iterator
  12. IteratorPrefix([]byte) Iterator
  13. // For debugging
  14. Print()
  15. Stats() map[string]string
  16. }
  17. type Batch interface {
  18. Set(key, value []byte)
  19. Delete(key []byte)
  20. Write()
  21. }
  22. type Iterator interface {
  23. Next() bool
  24. Key() []byte
  25. Value() []byte
  26. Release()
  27. Error() error
  28. }
  29. //-----------------------------------------------------------------------------
  30. const (
  31. LevelDBBackendStr = "leveldb" // legacy, defaults to goleveldb.
  32. CLevelDBBackendStr = "cleveldb"
  33. GoLevelDBBackendStr = "goleveldb"
  34. MemDBBackendStr = "memdb"
  35. )
  36. type dbCreator func(name string, dir string) (DB, error)
  37. var backends = map[string]dbCreator{}
  38. func registerDBCreator(backend string, creator dbCreator, force bool) {
  39. _, ok := backends[backend]
  40. if !force && ok {
  41. return
  42. }
  43. backends[backend] = creator
  44. }
  45. func NewDB(name string, backend string, dir string) DB {
  46. db, err := backends[backend](name, dir)
  47. if err != nil {
  48. PanicSanity(Fmt("Error initializing DB: %v", err))
  49. }
  50. return db
  51. }