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.

49 lines
926 B

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. // For debugging
  14. Print()
  15. }
  16. //-----------------------------------------------------------------------------
  17. // Database types
  18. const DBBackendMemDB = "memdb"
  19. const DBBackendLevelDB = "leveldb"
  20. var dbs = NewCMap()
  21. func GetDB(name string) DB {
  22. db := dbs.Get(name)
  23. if db != nil {
  24. return db.(DB)
  25. }
  26. switch config.App.GetString("DB.Backend") {
  27. case DBBackendMemDB:
  28. db := NewMemDB()
  29. dbs.Set(name, db)
  30. return db
  31. case DBBackendLevelDB:
  32. db, err := NewLevelDB(path.Join(config.App.GetString("DB.Dir"), name+".db"))
  33. if err != nil {
  34. panic(err)
  35. }
  36. dbs.Set(name, db)
  37. return db
  38. default:
  39. panic(Fmt("Unknown DB backend: %v", config.App.GetString("DB.Backend")))
  40. }
  41. }