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.

50 lines
941 B

10 years ago
10 years ago
10 years ago
10 years ago
  1. package db
  2. import (
  3. "path"
  4. . "github.com/tendermint/tendermint/common"
  5. "github.com/tendermint/tendermint/config"
  6. )
  7. type DB interface {
  8. Get([]byte) []byte
  9. Set([]byte, []byte)
  10. SetSync([]byte, []byte)
  11. Delete([]byte)
  12. DeleteSync([]byte)
  13. Close()
  14. // For debugging
  15. Print()
  16. }
  17. //-----------------------------------------------------------------------------
  18. // Database types
  19. const DBBackendMemDB = "memdb"
  20. const DBBackendLevelDB = "leveldb"
  21. var dbs = NewCMap()
  22. func GetDB(name string) DB {
  23. db := dbs.Get(name)
  24. if db != nil {
  25. return db.(DB)
  26. }
  27. switch config.App().GetString("DB.Backend") {
  28. case DBBackendMemDB:
  29. db := NewMemDB()
  30. dbs.Set(name, db)
  31. return db
  32. case DBBackendLevelDB:
  33. db, err := NewLevelDB(path.Join(config.App().GetString("DB.Dir"), name+".db"))
  34. if err != nil {
  35. panic(err)
  36. }
  37. dbs.Set(name, db)
  38. return db
  39. default:
  40. panic(Fmt("Unknown DB backend: %v", config.App().GetString("DB.Backend")))
  41. }
  42. }