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.

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