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.

290 lines
5.9 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
9 years ago
9 years ago
7 years ago
7 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. "bytes"
  4. "fmt"
  5. "path/filepath"
  6. "github.com/syndtr/goleveldb/leveldb"
  7. "github.com/syndtr/goleveldb/leveldb/errors"
  8. "github.com/syndtr/goleveldb/leveldb/iterator"
  9. "github.com/syndtr/goleveldb/leveldb/opt"
  10. . "github.com/tendermint/tmlibs/common"
  11. )
  12. func init() {
  13. dbCreator := func(name string, dir string) (DB, error) {
  14. return NewGoLevelDB(name, dir)
  15. }
  16. registerDBCreator(LevelDBBackendStr, dbCreator, false)
  17. registerDBCreator(GoLevelDBBackendStr, dbCreator, false)
  18. }
  19. var _ DB = (*GoLevelDB)(nil)
  20. type GoLevelDB struct {
  21. db *leveldb.DB
  22. }
  23. func NewGoLevelDB(name string, dir string) (*GoLevelDB, error) {
  24. dbPath := filepath.Join(dir, name+".db")
  25. db, err := leveldb.OpenFile(dbPath, nil)
  26. if err != nil {
  27. return nil, err
  28. }
  29. database := &GoLevelDB{
  30. db: db,
  31. }
  32. return database, nil
  33. }
  34. // Implements DB.
  35. func (db *GoLevelDB) Get(key []byte) []byte {
  36. key = nonNilBytes(key)
  37. res, err := db.db.Get(key, nil)
  38. if err != nil {
  39. if err == errors.ErrNotFound {
  40. return nil
  41. } else {
  42. panic(err)
  43. }
  44. }
  45. return res
  46. }
  47. // Implements DB.
  48. func (db *GoLevelDB) Has(key []byte) bool {
  49. return db.Get(key) != nil
  50. }
  51. // Implements DB.
  52. func (db *GoLevelDB) Set(key []byte, value []byte) {
  53. key = nonNilBytes(key)
  54. value = nonNilBytes(value)
  55. err := db.db.Put(key, value, nil)
  56. if err != nil {
  57. PanicCrisis(err)
  58. }
  59. }
  60. // Implements DB.
  61. func (db *GoLevelDB) SetSync(key []byte, value []byte) {
  62. key = nonNilBytes(key)
  63. value = nonNilBytes(value)
  64. err := db.db.Put(key, value, &opt.WriteOptions{Sync: true})
  65. if err != nil {
  66. PanicCrisis(err)
  67. }
  68. }
  69. // Implements DB.
  70. func (db *GoLevelDB) Delete(key []byte) {
  71. key = nonNilBytes(key)
  72. err := db.db.Delete(key, nil)
  73. if err != nil {
  74. PanicCrisis(err)
  75. }
  76. }
  77. // Implements DB.
  78. func (db *GoLevelDB) DeleteSync(key []byte) {
  79. key = nonNilBytes(key)
  80. err := db.db.Delete(key, &opt.WriteOptions{Sync: true})
  81. if err != nil {
  82. PanicCrisis(err)
  83. }
  84. }
  85. func (db *GoLevelDB) DB() *leveldb.DB {
  86. return db.db
  87. }
  88. // Implements DB.
  89. func (db *GoLevelDB) Close() {
  90. db.db.Close()
  91. }
  92. // Implements DB.
  93. func (db *GoLevelDB) Print() {
  94. str, _ := db.db.GetProperty("leveldb.stats")
  95. fmt.Printf("%v\n", str)
  96. iter := db.db.NewIterator(nil, nil)
  97. for iter.Next() {
  98. key := iter.Key()
  99. value := iter.Value()
  100. fmt.Printf("[%X]:\t[%X]\n", key, value)
  101. }
  102. }
  103. // Implements DB.
  104. func (db *GoLevelDB) Stats() map[string]string {
  105. keys := []string{
  106. "leveldb.num-files-at-level{n}",
  107. "leveldb.stats",
  108. "leveldb.sstables",
  109. "leveldb.blockpool",
  110. "leveldb.cachedblock",
  111. "leveldb.openedtables",
  112. "leveldb.alivesnaps",
  113. "leveldb.aliveiters",
  114. }
  115. stats := make(map[string]string)
  116. for _, key := range keys {
  117. str, err := db.db.GetProperty(key)
  118. if err == nil {
  119. stats[key] = str
  120. }
  121. }
  122. return stats
  123. }
  124. //----------------------------------------
  125. // Batch
  126. // Implements DB.
  127. func (db *GoLevelDB) NewBatch() Batch {
  128. batch := new(leveldb.Batch)
  129. return &goLevelDBBatch{db, batch}
  130. }
  131. type goLevelDBBatch struct {
  132. db *GoLevelDB
  133. batch *leveldb.Batch
  134. }
  135. // Implements Batch.
  136. func (mBatch *goLevelDBBatch) Set(key, value []byte) {
  137. mBatch.batch.Put(key, value)
  138. }
  139. // Implements Batch.
  140. func (mBatch *goLevelDBBatch) Delete(key []byte) {
  141. mBatch.batch.Delete(key)
  142. }
  143. // Implements Batch.
  144. func (mBatch *goLevelDBBatch) Write() {
  145. err := mBatch.db.db.Write(mBatch.batch, nil)
  146. if err != nil {
  147. panic(err)
  148. }
  149. }
  150. //----------------------------------------
  151. // Iterator
  152. // NOTE This is almost identical to db/c_level_db.Iterator
  153. // Before creating a third version, refactor.
  154. // Implements DB.
  155. func (db *GoLevelDB) Iterator(start, end []byte) Iterator {
  156. itr := db.db.NewIterator(nil, nil)
  157. return newGoLevelDBIterator(itr, start, end, false)
  158. }
  159. // Implements DB.
  160. func (db *GoLevelDB) ReverseIterator(start, end []byte) Iterator {
  161. panic("not implemented yet") // XXX
  162. }
  163. type goLevelDBIterator struct {
  164. source iterator.Iterator
  165. start []byte
  166. end []byte
  167. isReverse bool
  168. isInvalid bool
  169. }
  170. var _ Iterator = (*goLevelDBIterator)(nil)
  171. func newGoLevelDBIterator(source iterator.Iterator, start, end []byte, isReverse bool) *goLevelDBIterator {
  172. if isReverse {
  173. panic("not implemented yet") // XXX
  174. }
  175. source.Seek(start)
  176. return &goLevelDBIterator{
  177. source: source,
  178. start: start,
  179. end: end,
  180. isReverse: isReverse,
  181. isInvalid: false,
  182. }
  183. }
  184. // Implements Iterator.
  185. func (itr *goLevelDBIterator) Domain() ([]byte, []byte) {
  186. return itr.start, itr.end
  187. }
  188. // Implements Iterator.
  189. func (itr *goLevelDBIterator) Valid() bool {
  190. // Once invalid, forever invalid.
  191. if itr.isInvalid {
  192. return false
  193. }
  194. // Panic on DB error. No way to recover.
  195. itr.assertNoError()
  196. // If source is invalid, invalid.
  197. if !itr.source.Valid() {
  198. itr.isInvalid = true
  199. return false
  200. }
  201. // If key is end or past it, invalid.
  202. var end = itr.end
  203. var key = itr.source.Key()
  204. if end != nil && bytes.Compare(end, key) <= 0 {
  205. itr.isInvalid = true
  206. return false
  207. }
  208. // Valid
  209. return true
  210. }
  211. // Implements Iterator.
  212. func (itr *goLevelDBIterator) Key() []byte {
  213. // Key returns a copy of the current key.
  214. // See https://github.com/syndtr/goleveldb/blob/52c212e6c196a1404ea59592d3f1c227c9f034b2/leveldb/iterator/iter.go#L88
  215. itr.assertNoError()
  216. itr.assertIsValid()
  217. return cp(itr.source.Key())
  218. }
  219. // Implements Iterator.
  220. func (itr *goLevelDBIterator) Value() []byte {
  221. // Value returns a copy of the current value.
  222. // See https://github.com/syndtr/goleveldb/blob/52c212e6c196a1404ea59592d3f1c227c9f034b2/leveldb/iterator/iter.go#L88
  223. itr.assertNoError()
  224. itr.assertIsValid()
  225. return cp(itr.source.Value())
  226. }
  227. // Implements Iterator.
  228. func (itr *goLevelDBIterator) Next() {
  229. itr.assertNoError()
  230. itr.assertIsValid()
  231. itr.source.Next()
  232. }
  233. // Implements Iterator.
  234. func (itr *goLevelDBIterator) Close() {
  235. itr.source.Release()
  236. }
  237. func (itr *goLevelDBIterator) assertNoError() {
  238. if err := itr.source.Error(); err != nil {
  239. panic(err)
  240. }
  241. }
  242. func (itr goLevelDBIterator) assertIsValid() {
  243. if !itr.Valid() {
  244. panic("goLevelDBIterator is invalid")
  245. }
  246. }