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.

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