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.

147 lines
2.8 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package db
  2. import (
  3. "fmt"
  4. "path"
  5. "github.com/syndtr/goleveldb/leveldb"
  6. "github.com/syndtr/goleveldb/leveldb/errors"
  7. "github.com/syndtr/goleveldb/leveldb/opt"
  8. . "github.com/tendermint/go-common"
  9. )
  10. func init() {
  11. dbCreator := func(name string, dir string) (DB, error) {
  12. return NewGoLevelDB(name, dir)
  13. }
  14. registerDBCreator(LevelDBBackendStr, dbCreator, false)
  15. registerDBCreator(GoLevelDBBackendStr, dbCreator, false)
  16. }
  17. type GoLevelDB struct {
  18. db *leveldb.DB
  19. }
  20. func NewGoLevelDB(name string, dir string) (*GoLevelDB, error) {
  21. dbPath := path.Join(dir, name+".db")
  22. db, err := leveldb.OpenFile(dbPath, nil)
  23. if err != nil {
  24. return nil, err
  25. }
  26. database := &GoLevelDB{db: db}
  27. return database, nil
  28. }
  29. func (db *GoLevelDB) Get(key []byte) []byte {
  30. res, err := db.db.Get(key, nil)
  31. if err != nil {
  32. if err == errors.ErrNotFound {
  33. return nil
  34. } else {
  35. PanicCrisis(err)
  36. }
  37. }
  38. return res
  39. }
  40. func (db *GoLevelDB) Set(key []byte, value []byte) {
  41. err := db.db.Put(key, value, nil)
  42. if err != nil {
  43. PanicCrisis(err)
  44. }
  45. }
  46. func (db *GoLevelDB) SetSync(key []byte, value []byte) {
  47. err := db.db.Put(key, value, &opt.WriteOptions{Sync: true})
  48. if err != nil {
  49. PanicCrisis(err)
  50. }
  51. }
  52. func (db *GoLevelDB) Delete(key []byte) {
  53. err := db.db.Delete(key, nil)
  54. if err != nil {
  55. PanicCrisis(err)
  56. }
  57. }
  58. func (db *GoLevelDB) DeleteSync(key []byte) {
  59. err := db.db.Delete(key, &opt.WriteOptions{Sync: true})
  60. if err != nil {
  61. PanicCrisis(err)
  62. }
  63. }
  64. func (db *GoLevelDB) DB() *leveldb.DB {
  65. return db.db
  66. }
  67. func (db *GoLevelDB) Close() {
  68. db.db.Close()
  69. }
  70. func (db *GoLevelDB) Print() {
  71. str, _ := db.db.GetProperty("leveldb.stats")
  72. fmt.Printf("%v\n", str)
  73. iter := db.db.NewIterator(nil, nil)
  74. for iter.Next() {
  75. key := iter.Key()
  76. value := iter.Value()
  77. fmt.Printf("[%X]:\t[%X]\n", key, value)
  78. }
  79. }
  80. func (db *GoLevelDB) Stats() map[string]string {
  81. keys := []string{
  82. "leveldb.num-files-at-level{n}",
  83. "leveldb.stats",
  84. "leveldb.sstables",
  85. "leveldb.blockpool",
  86. "leveldb.cachedblock",
  87. "leveldb.openedtables",
  88. "leveldb.alivesnaps",
  89. "leveldb.aliveiters",
  90. }
  91. stats := make(map[string]string)
  92. for _, key := range keys {
  93. str, err := db.db.GetProperty(key)
  94. if err == nil {
  95. stats[key] = str
  96. }
  97. }
  98. return stats
  99. }
  100. func (db *GoLevelDB) Iterator() Iterator {
  101. return db.db.NewIterator(nil, nil)
  102. }
  103. func (db *GoLevelDB) NewBatch() Batch {
  104. batch := new(leveldb.Batch)
  105. return &goLevelDBBatch{db, batch}
  106. }
  107. //--------------------------------------------------------------------------------
  108. type goLevelDBBatch struct {
  109. db *GoLevelDB
  110. batch *leveldb.Batch
  111. }
  112. func (mBatch *goLevelDBBatch) Set(key, value []byte) {
  113. mBatch.batch.Put(key, value)
  114. }
  115. func (mBatch *goLevelDBBatch) Delete(key []byte) {
  116. mBatch.batch.Delete(key)
  117. }
  118. func (mBatch *goLevelDBBatch) Write() {
  119. err := mBatch.db.db.Write(mBatch.batch, nil)
  120. if err != nil {
  121. PanicCrisis(err)
  122. }
  123. }