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.

82 lines
1.6 KiB

11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
  1. package db
  2. import (
  3. "fmt"
  4. "github.com/tendermint/tendermint/Godeps/_workspace/src/github.com/syndtr/goleveldb/leveldb"
  5. "github.com/tendermint/tendermint/Godeps/_workspace/src/github.com/syndtr/goleveldb/leveldb/errors"
  6. "github.com/tendermint/tendermint/Godeps/_workspace/src/github.com/syndtr/goleveldb/leveldb/opt"
  7. "path"
  8. . "github.com/tendermint/tendermint/common"
  9. )
  10. type LevelDB struct {
  11. db *leveldb.DB
  12. }
  13. func NewLevelDB(name string) (*LevelDB, error) {
  14. dbPath := path.Join(name)
  15. db, err := leveldb.OpenFile(dbPath, nil)
  16. if err != nil {
  17. return nil, err
  18. }
  19. database := &LevelDB{db: db}
  20. return database, nil
  21. }
  22. func (db *LevelDB) Get(key []byte) []byte {
  23. res, err := db.db.Get(key, nil)
  24. if err != nil {
  25. if err == errors.ErrNotFound {
  26. return nil
  27. } else {
  28. PanicCrisis(err)
  29. }
  30. }
  31. return res
  32. }
  33. func (db *LevelDB) Set(key []byte, value []byte) {
  34. err := db.db.Put(key, value, nil)
  35. if err != nil {
  36. PanicCrisis(err)
  37. }
  38. }
  39. func (db *LevelDB) SetSync(key []byte, value []byte) {
  40. err := db.db.Put(key, value, &opt.WriteOptions{Sync: true})
  41. if err != nil {
  42. PanicCrisis(err)
  43. }
  44. }
  45. func (db *LevelDB) Delete(key []byte) {
  46. err := db.db.Delete(key, nil)
  47. if err != nil {
  48. PanicCrisis(err)
  49. }
  50. }
  51. func (db *LevelDB) DeleteSync(key []byte) {
  52. err := db.db.Delete(key, &opt.WriteOptions{Sync: true})
  53. if err != nil {
  54. PanicCrisis(err)
  55. }
  56. }
  57. func (db *LevelDB) DB() *leveldb.DB {
  58. return db.db
  59. }
  60. func (db *LevelDB) Close() {
  61. db.db.Close()
  62. }
  63. func (db *LevelDB) Print() {
  64. iter := db.db.NewIterator(nil, nil)
  65. for iter.Next() {
  66. key := iter.Key()
  67. value := iter.Value()
  68. fmt.Printf("[%X]:\t[%X]\n", key, value)
  69. }
  70. }