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
904 B

10 years ago
  1. package db
  2. import (
  3. "path"
  4. . "github.com/tendermint/tendermint/common"
  5. )
  6. type DB interface {
  7. Get([]byte) []byte
  8. Set([]byte, []byte)
  9. SetSync([]byte, []byte)
  10. Delete([]byte)
  11. DeleteSync([]byte)
  12. Close()
  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.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.GetString("db_dir"), name+".db"))
  33. if err != nil {
  34. PanicCrisis(err)
  35. }
  36. dbs.Set(name, db)
  37. return db
  38. default:
  39. PanicSanity(Fmt("Unknown DB backend: %v", config.GetString("db_backend")))
  40. }
  41. return nil
  42. }