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.

217 lines
3.9 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. // +build gcc
  2. package db
  3. import (
  4. "fmt"
  5. "path"
  6. "github.com/jmhodges/levigo"
  7. )
  8. func init() {
  9. dbCreator := func(name string, dir string) (DB, error) {
  10. return NewCLevelDB(name, dir)
  11. }
  12. registerDBCreator(LevelDBBackendStr, dbCreator, true)
  13. registerDBCreator(CLevelDBBackendStr, dbCreator, false)
  14. }
  15. type CLevelDB struct {
  16. db *levigo.DB
  17. ro *levigo.ReadOptions
  18. wo *levigo.WriteOptions
  19. woSync *levigo.WriteOptions
  20. }
  21. func NewCLevelDB(name string, dir string) (*CLevelDB, error) {
  22. dbPath := path.Join(dir, name+".db")
  23. opts := levigo.NewOptions()
  24. opts.SetCache(levigo.NewLRUCache(1 << 30))
  25. opts.SetCreateIfMissing(true)
  26. db, err := levigo.Open(dbPath, opts)
  27. if err != nil {
  28. return nil, err
  29. }
  30. ro := levigo.NewReadOptions()
  31. wo := levigo.NewWriteOptions()
  32. woSync := levigo.NewWriteOptions()
  33. woSync.SetSync(true)
  34. database := &CLevelDB{
  35. db: db,
  36. ro: ro,
  37. wo: wo,
  38. woSync: woSync,
  39. }
  40. return database, nil
  41. }
  42. func (db *CLevelDB) Get(key []byte) []byte {
  43. panicNilKey(key)
  44. res, err := db.db.Get(db.ro, key)
  45. if err != nil {
  46. panic(err)
  47. }
  48. return res
  49. }
  50. func (db *CLevelDB) Has(key []byte) bool {
  51. panicNilKey(key)
  52. panic("not implemented yet")
  53. }
  54. func (db *CLevelDB) Set(key []byte, value []byte) {
  55. panicNilKey(key)
  56. err := db.db.Put(db.wo, key, value)
  57. if err != nil {
  58. panic(err)
  59. }
  60. }
  61. func (db *CLevelDB) SetSync(key []byte, value []byte) {
  62. panicNilKey(key)
  63. err := db.db.Put(db.woSync, key, value)
  64. if err != nil {
  65. panic(err)
  66. }
  67. }
  68. func (db *CLevelDB) Delete(key []byte) {
  69. panicNilKey(key)
  70. err := db.db.Delete(db.wo, key)
  71. if err != nil {
  72. panic(err)
  73. }
  74. }
  75. func (db *CLevelDB) DeleteSync(key []byte) {
  76. panicNilKey(key)
  77. err := db.db.Delete(db.woSync, key)
  78. if err != nil {
  79. panic(err)
  80. }
  81. }
  82. func (db *CLevelDB) DB() *levigo.DB {
  83. return db.db
  84. }
  85. func (db *CLevelDB) Close() {
  86. db.db.Close()
  87. db.ro.Close()
  88. db.wo.Close()
  89. db.woSync.Close()
  90. }
  91. func (db *CLevelDB) Print() {
  92. itr := db.Iterator(BeginningKey(), EndingKey())
  93. defer itr.Release()
  94. for ; itr.Valid(); itr.Next() {
  95. key := itr.Key()
  96. value := itr.Value()
  97. fmt.Printf("[%X]:\t[%X]\n", key, value)
  98. }
  99. }
  100. func (db *CLevelDB) Stats() map[string]string {
  101. // TODO: Find the available properties for the C LevelDB implementation
  102. keys := []string{}
  103. stats := make(map[string]string)
  104. for _, key := range keys {
  105. str := db.db.PropertyValue(key)
  106. stats[key] = str
  107. }
  108. return stats
  109. }
  110. //----------------------------------------
  111. // Batch
  112. func (db *CLevelDB) NewBatch() Batch {
  113. batch := levigo.NewWriteBatch()
  114. return &cLevelDBBatch{db, batch}
  115. }
  116. type cLevelDBBatch struct {
  117. db *CLevelDB
  118. batch *levigo.WriteBatch
  119. }
  120. func (mBatch *cLevelDBBatch) Set(key, value []byte) {
  121. mBatch.batch.Put(key, value)
  122. }
  123. func (mBatch *cLevelDBBatch) Delete(key []byte) {
  124. mBatch.batch.Delete(key)
  125. }
  126. func (mBatch *cLevelDBBatch) Write() {
  127. err := mBatch.db.db.Write(mBatch.db.wo, mBatch.batch)
  128. if err != nil {
  129. panic(err)
  130. }
  131. }
  132. //----------------------------------------
  133. // Iterator
  134. func (db *CLevelDB) Iterator(start, end []byte) Iterator {
  135. /*
  136. XXX
  137. itr := db.db.NewIterator(db.ro)
  138. itr.Seek([]byte{0x00})
  139. return cLevelDBIterator{itr}
  140. */
  141. return nil
  142. }
  143. func (db *CLevelDB) ReverseIterator(start, end []byte) Iterator {
  144. // XXX
  145. return nil
  146. }
  147. type cLevelDBIterator struct {
  148. itr *levigo.Iterator
  149. }
  150. func (c cLevelDBIterator) Seek(key []byte) {
  151. if key == nil {
  152. key = []byte{0x00}
  153. }
  154. c.itr.Seek(key)
  155. }
  156. func (c cLevelDBIterator) Valid() bool {
  157. return c.itr.Valid()
  158. }
  159. func (c cLevelDBIterator) Key() []byte {
  160. if !c.itr.Valid() {
  161. panic("cLevelDBIterator Key() called when invalid")
  162. }
  163. return c.itr.Key()
  164. }
  165. func (c cLevelDBIterator) Value() []byte {
  166. if !c.itr.Valid() {
  167. panic("cLevelDBIterator Value() called when invalid")
  168. }
  169. return c.itr.Value()
  170. }
  171. func (c cLevelDBIterator) Next() {
  172. if !c.itr.Valid() {
  173. panic("cLevelDBIterator Next() called when invalid")
  174. }
  175. c.itr.Next()
  176. }
  177. func (c cLevelDBIterator) Release() {
  178. c.itr.Close()
  179. }
  180. func (c cLevelDBIterator) GetError() error {
  181. return c.itr.GetError()
  182. }