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.

211 lines
3.8 KiB

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. res, err := db.db.Get(db.ro, key)
  44. if err != nil {
  45. panic(err)
  46. }
  47. return res
  48. }
  49. func (db *CLevelDB) Has(key []byte) bool {
  50. panic("not implemented yet")
  51. }
  52. func (db *CLevelDB) Set(key []byte, value []byte) {
  53. err := db.db.Put(db.wo, key, value)
  54. if err != nil {
  55. panic(err)
  56. }
  57. }
  58. func (db *CLevelDB) SetSync(key []byte, value []byte) {
  59. err := db.db.Put(db.woSync, key, value)
  60. if err != nil {
  61. panic(err)
  62. }
  63. }
  64. func (db *CLevelDB) Delete(key []byte) {
  65. err := db.db.Delete(db.wo, key)
  66. if err != nil {
  67. panic(err)
  68. }
  69. }
  70. func (db *CLevelDB) DeleteSync(key []byte) {
  71. err := db.db.Delete(db.woSync, key)
  72. if err != nil {
  73. panic(err)
  74. }
  75. }
  76. func (db *CLevelDB) DB() *levigo.DB {
  77. return db.db
  78. }
  79. func (db *CLevelDB) Close() {
  80. db.db.Close()
  81. db.ro.Close()
  82. db.wo.Close()
  83. db.woSync.Close()
  84. }
  85. func (db *CLevelDB) Print() {
  86. itr := db.Iterator(BeginningKey(), EndingKey())
  87. defer itr.Release()
  88. for ; itr.Valid(); itr.Next() {
  89. key := itr.Key()
  90. value := itr.Value()
  91. fmt.Printf("[%X]:\t[%X]\n", key, value)
  92. }
  93. }
  94. func (db *CLevelDB) Stats() map[string]string {
  95. // TODO: Find the available properties for the C LevelDB implementation
  96. keys := []string{}
  97. stats := make(map[string]string)
  98. for _, key := range keys {
  99. str := db.db.PropertyValue(key)
  100. stats[key] = str
  101. }
  102. return stats
  103. }
  104. //----------------------------------------
  105. // Batch
  106. func (db *CLevelDB) NewBatch() Batch {
  107. batch := levigo.NewWriteBatch()
  108. return &cLevelDBBatch{db, batch}
  109. }
  110. type cLevelDBBatch struct {
  111. db *CLevelDB
  112. batch *levigo.WriteBatch
  113. }
  114. func (mBatch *cLevelDBBatch) Set(key, value []byte) {
  115. mBatch.batch.Put(key, value)
  116. }
  117. func (mBatch *cLevelDBBatch) Delete(key []byte) {
  118. mBatch.batch.Delete(key)
  119. }
  120. func (mBatch *cLevelDBBatch) Write() {
  121. err := mBatch.db.db.Write(mBatch.db.wo, mBatch.batch)
  122. if err != nil {
  123. panic(err)
  124. }
  125. }
  126. //----------------------------------------
  127. // Iterator
  128. func (db *CLevelDB) Iterator(start, end []byte) Iterator {
  129. /*
  130. XXX
  131. itr := db.db.NewIterator(db.ro)
  132. itr.Seek([]byte{0x00})
  133. return cLevelDBIterator{itr}
  134. */
  135. return nil
  136. }
  137. func (db *CLevelDB) ReverseIterator(start, end []byte) Iterator {
  138. // XXX
  139. return nil
  140. }
  141. type cLevelDBIterator struct {
  142. itr *levigo.Iterator
  143. }
  144. func (c cLevelDBIterator) Seek(key []byte) {
  145. if key == nil {
  146. key = []byte{0x00}
  147. }
  148. c.itr.Seek(key)
  149. }
  150. func (c cLevelDBIterator) Valid() bool {
  151. return c.itr.Valid()
  152. }
  153. func (c cLevelDBIterator) Key() []byte {
  154. if !c.itr.Valid() {
  155. panic("cLevelDBIterator Key() called when invalid")
  156. }
  157. return c.itr.Key()
  158. }
  159. func (c cLevelDBIterator) Value() []byte {
  160. if !c.itr.Valid() {
  161. panic("cLevelDBIterator Value() called when invalid")
  162. }
  163. return c.itr.Value()
  164. }
  165. func (c cLevelDBIterator) Next() {
  166. if !c.itr.Valid() {
  167. panic("cLevelDBIterator Next() called when invalid")
  168. }
  169. c.itr.Next()
  170. }
  171. func (c cLevelDBIterator) Release() {
  172. c.itr.Close()
  173. }
  174. func (c cLevelDBIterator) GetError() error {
  175. return c.itr.GetError()
  176. }