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.

80 lines
1.5 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
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. )
  9. type LevelDB struct {
  10. db *leveldb.DB
  11. }
  12. func NewLevelDB(name string) (*LevelDB, error) {
  13. dbPath := path.Join(name)
  14. db, err := leveldb.OpenFile(dbPath, nil)
  15. if err != nil {
  16. return nil, err
  17. }
  18. database := &LevelDB{db: db}
  19. return database, nil
  20. }
  21. func (db *LevelDB) Get(key []byte) []byte {
  22. res, err := db.db.Get(key, nil)
  23. if err != nil {
  24. if err == errors.ErrNotFound {
  25. return nil
  26. } else {
  27. panic(err)
  28. }
  29. }
  30. return res
  31. }
  32. func (db *LevelDB) Set(key []byte, value []byte) {
  33. err := db.db.Put(key, value, nil)
  34. if err != nil {
  35. panic(err)
  36. }
  37. }
  38. func (db *LevelDB) SetSync(key []byte, value []byte) {
  39. err := db.db.Put(key, value, &opt.WriteOptions{Sync: true})
  40. if err != nil {
  41. panic(err)
  42. }
  43. }
  44. func (db *LevelDB) Delete(key []byte) {
  45. err := db.db.Delete(key, nil)
  46. if err != nil {
  47. panic(err)
  48. }
  49. }
  50. func (db *LevelDB) DeleteSync(key []byte) {
  51. err := db.db.Delete(key, &opt.WriteOptions{Sync: true})
  52. if err != nil {
  53. panic(err)
  54. }
  55. }
  56. func (db *LevelDB) DB() *leveldb.DB {
  57. return db.db
  58. }
  59. func (db *LevelDB) Close() {
  60. db.db.Close()
  61. }
  62. func (db *LevelDB) Print() {
  63. iter := db.db.NewIterator(nil, nil)
  64. for iter.Next() {
  65. key := iter.Key()
  66. value := iter.Value()
  67. fmt.Printf("[%X]:\t[%X]\n", key, value)
  68. }
  69. }