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.

298 lines
6.0 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(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. } 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. itr := db.db.NewIterator(nil, nil)
  97. for itr.Next() {
  98. key := itr.Key()
  99. value := itr.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, &opt.WriteOptions{Sync: false})
  146. if err != nil {
  147. panic(err)
  148. }
  149. }
  150. // Implements Batch.
  151. func (mBatch *goLevelDBBatch) WriteSync() {
  152. err := mBatch.db.db.Write(mBatch.batch, &opt.WriteOptions{Sync: true})
  153. if err != nil {
  154. panic(err)
  155. }
  156. }
  157. //----------------------------------------
  158. // Iterator
  159. // NOTE This is almost identical to db/c_level_db.Iterator
  160. // Before creating a third version, refactor.
  161. // Implements DB.
  162. func (db *GoLevelDB) Iterator(start, end []byte) Iterator {
  163. itr := db.db.NewIterator(nil, nil)
  164. return newGoLevelDBIterator(itr, start, end, false)
  165. }
  166. // Implements DB.
  167. func (db *GoLevelDB) ReverseIterator(start, end []byte) Iterator {
  168. panic("not implemented yet") // XXX
  169. }
  170. type goLevelDBIterator struct {
  171. source iterator.Iterator
  172. start []byte
  173. end []byte
  174. isReverse bool
  175. isInvalid bool
  176. }
  177. var _ Iterator = (*goLevelDBIterator)(nil)
  178. func newGoLevelDBIterator(source iterator.Iterator, start, end []byte, isReverse bool) *goLevelDBIterator {
  179. if isReverse {
  180. panic("not implemented yet") // XXX
  181. }
  182. source.Seek(start)
  183. return &goLevelDBIterator{
  184. source: source,
  185. start: start,
  186. end: end,
  187. isReverse: isReverse,
  188. isInvalid: false,
  189. }
  190. }
  191. // Implements Iterator.
  192. func (itr *goLevelDBIterator) Domain() ([]byte, []byte) {
  193. return itr.start, itr.end
  194. }
  195. // Implements Iterator.
  196. func (itr *goLevelDBIterator) Valid() bool {
  197. // Once invalid, forever invalid.
  198. if itr.isInvalid {
  199. return false
  200. }
  201. // Panic on DB error. No way to recover.
  202. itr.assertNoError()
  203. // If source is invalid, invalid.
  204. if !itr.source.Valid() {
  205. itr.isInvalid = true
  206. return false
  207. }
  208. // If key is end or past it, invalid.
  209. var end = itr.end
  210. var key = itr.source.Key()
  211. if end != nil && bytes.Compare(end, key) <= 0 {
  212. itr.isInvalid = true
  213. return false
  214. }
  215. // Valid
  216. return true
  217. }
  218. // Implements Iterator.
  219. func (itr *goLevelDBIterator) Key() []byte {
  220. // Key returns a copy of the current key.
  221. // See https://github.com/syndtr/goleveldb/blob/52c212e6c196a1404ea59592d3f1c227c9f034b2/leveldb/iterator/iter.go#L88
  222. itr.assertNoError()
  223. itr.assertIsValid()
  224. return cp(itr.source.Key())
  225. }
  226. // Implements Iterator.
  227. func (itr *goLevelDBIterator) Value() []byte {
  228. // Value returns a copy of the current value.
  229. // See https://github.com/syndtr/goleveldb/blob/52c212e6c196a1404ea59592d3f1c227c9f034b2/leveldb/iterator/iter.go#L88
  230. itr.assertNoError()
  231. itr.assertIsValid()
  232. return cp(itr.source.Value())
  233. }
  234. // Implements Iterator.
  235. func (itr *goLevelDBIterator) Next() {
  236. itr.assertNoError()
  237. itr.assertIsValid()
  238. itr.source.Next()
  239. }
  240. // Implements Iterator.
  241. func (itr *goLevelDBIterator) Close() {
  242. itr.source.Release()
  243. }
  244. func (itr *goLevelDBIterator) assertNoError() {
  245. if err := itr.source.Error(); err != nil {
  246. panic(err)
  247. }
  248. }
  249. func (itr goLevelDBIterator) assertIsValid() {
  250. if !itr.Valid() {
  251. panic("goLevelDBIterator is invalid")
  252. }
  253. }