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.

132 lines
2.4 KiB

  1. // +build gcc
  2. package db
  3. import (
  4. "fmt"
  5. "path"
  6. "github.com/jmhodges/levigo"
  7. . "github.com/tendermint/go-common"
  8. )
  9. func init() {
  10. registerDBCreator(CLevelDBBackendStr, func(name string, dir string) (DB, error) {
  11. return NewCLevelDB(name, dir)
  12. }, false)
  13. }
  14. type CLevelDB struct {
  15. db *levigo.DB
  16. ro *levigo.ReadOptions
  17. wo *levigo.WriteOptions
  18. woSync *levigo.WriteOptions
  19. }
  20. func NewCLevelDB(name string, dir string) (*CLevelDB, error) {
  21. dbPath := path.Join(dir, name+".db")
  22. opts := levigo.NewOptions()
  23. opts.SetCache(levigo.NewLRUCache(1 << 30))
  24. opts.SetCreateIfMissing(true)
  25. db, err := levigo.Open(dbPath, opts)
  26. if err != nil {
  27. return nil, err
  28. }
  29. ro := levigo.NewReadOptions()
  30. wo := levigo.NewWriteOptions()
  31. woSync := levigo.NewWriteOptions()
  32. woSync.SetSync(true)
  33. database := &CLevelDB{
  34. db: db,
  35. ro: ro,
  36. wo: wo,
  37. woSync: woSync,
  38. }
  39. return database, nil
  40. }
  41. func (db *CLevelDB) Get(key []byte) []byte {
  42. res, err := db.db.Get(db.ro, key)
  43. if err != nil {
  44. PanicCrisis(err)
  45. }
  46. return res
  47. }
  48. func (db *CLevelDB) Set(key []byte, value []byte) {
  49. err := db.db.Put(db.wo, key, value)
  50. if err != nil {
  51. PanicCrisis(err)
  52. }
  53. }
  54. func (db *CLevelDB) SetSync(key []byte, value []byte) {
  55. err := db.db.Put(db.woSync, key, value)
  56. if err != nil {
  57. PanicCrisis(err)
  58. }
  59. }
  60. func (db *CLevelDB) Delete(key []byte) {
  61. err := db.db.Delete(db.wo, key)
  62. if err != nil {
  63. PanicCrisis(err)
  64. }
  65. }
  66. func (db *CLevelDB) DeleteSync(key []byte) {
  67. err := db.db.Delete(db.woSync, key)
  68. if err != nil {
  69. PanicCrisis(err)
  70. }
  71. }
  72. func (db *CLevelDB) DB() *levigo.DB {
  73. return db.db
  74. }
  75. func (db *CLevelDB) Close() {
  76. db.db.Close()
  77. db.ro.Close()
  78. db.wo.Close()
  79. db.woSync.Close()
  80. }
  81. func (db *CLevelDB) Print() {
  82. iter := db.db.NewIterator(db.ro)
  83. defer iter.Close()
  84. for iter.Seek(nil); iter.Valid(); iter.Next() {
  85. key := iter.Key()
  86. value := iter.Value()
  87. fmt.Printf("[%X]:\t[%X]\n", key, value)
  88. }
  89. }
  90. func (db *CLevelDB) NewBatch() Batch {
  91. batch := levigo.NewWriteBatch()
  92. return &cLevelDBBatch{db, batch}
  93. }
  94. //--------------------------------------------------------------------------------
  95. type cLevelDBBatch struct {
  96. db *CLevelDB
  97. batch *levigo.WriteBatch
  98. }
  99. func (mBatch *cLevelDBBatch) Set(key, value []byte) {
  100. mBatch.batch.Put(key, value)
  101. }
  102. func (mBatch *cLevelDBBatch) Delete(key []byte) {
  103. mBatch.batch.Delete(key)
  104. }
  105. func (mBatch *cLevelDBBatch) Write() {
  106. err := mBatch.db.db.Write(mBatch.db.wo, mBatch.batch)
  107. if err != nil {
  108. PanicCrisis(err)
  109. }
  110. }