本站源代码
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.

188 lines
3.1KB

  1. package goleveldb
  2. import (
  3. "github.com/syndtr/goleveldb/leveldb"
  4. "github.com/syndtr/goleveldb/leveldb/cache"
  5. "github.com/syndtr/goleveldb/leveldb/filter"
  6. "github.com/syndtr/goleveldb/leveldb/opt"
  7. "github.com/syndtr/goleveldb/leveldb/storage"
  8. "github.com/lunny/nodb/config"
  9. "github.com/lunny/nodb/store/driver"
  10. "os"
  11. )
  12. const defaultFilterBits int = 10
  13. type Store struct {
  14. }
  15. func (s Store) String() string {
  16. return DBName
  17. }
  18. type MemStore struct {
  19. }
  20. func (s MemStore) String() string {
  21. return MemDBName
  22. }
  23. type DB struct {
  24. path string
  25. cfg *config.LevelDBConfig
  26. db *leveldb.DB
  27. opts *opt.Options
  28. iteratorOpts *opt.ReadOptions
  29. cache cache.Cache
  30. filter filter.Filter
  31. }
  32. func (s Store) Open(path string, cfg *config.Config) (driver.IDB, error) {
  33. if err := os.MkdirAll(path, os.ModePerm); err != nil {
  34. return nil, err
  35. }
  36. db := new(DB)
  37. db.path = path
  38. db.cfg = &cfg.LevelDB
  39. db.initOpts()
  40. var err error
  41. db.db, err = leveldb.OpenFile(db.path, db.opts)
  42. if err != nil {
  43. return nil, err
  44. }
  45. return db, nil
  46. }
  47. func (s Store) Repair(path string, cfg *config.Config) error {
  48. db, err := leveldb.RecoverFile(path, newOptions(&cfg.LevelDB))
  49. if err != nil {
  50. return err
  51. }
  52. db.Close()
  53. return nil
  54. }
  55. func (s MemStore) Open(path string, cfg *config.Config) (driver.IDB, error) {
  56. db := new(DB)
  57. db.path = path
  58. db.cfg = &cfg.LevelDB
  59. db.initOpts()
  60. var err error
  61. db.db, err = leveldb.Open(storage.NewMemStorage(), db.opts)
  62. if err != nil {
  63. return nil, err
  64. }
  65. return db, nil
  66. }
  67. func (s MemStore) Repair(path string, cfg *config.Config) error {
  68. return nil
  69. }
  70. func (db *DB) initOpts() {
  71. db.opts = newOptions(db.cfg)
  72. db.iteratorOpts = &opt.ReadOptions{}
  73. db.iteratorOpts.DontFillCache = true
  74. }
  75. func newOptions(cfg *config.LevelDBConfig) *opt.Options {
  76. opts := &opt.Options{}
  77. opts.ErrorIfMissing = false
  78. cfg.Adjust()
  79. //opts.BlockCacher = cache.NewLRU(cfg.CacheSize)
  80. opts.BlockCacheCapacity = cfg.CacheSize
  81. //we must use bloomfilter
  82. opts.Filter = filter.NewBloomFilter(defaultFilterBits)
  83. if !cfg.Compression {
  84. opts.Compression = opt.NoCompression
  85. } else {
  86. opts.Compression = opt.SnappyCompression
  87. }
  88. opts.BlockSize = cfg.BlockSize
  89. opts.WriteBuffer = cfg.WriteBufferSize
  90. return opts
  91. }
  92. func (db *DB) Close() error {
  93. return db.db.Close()
  94. }
  95. func (db *DB) Put(key, value []byte) error {
  96. return db.db.Put(key, value, nil)
  97. }
  98. func (db *DB) Get(key []byte) ([]byte, error) {
  99. v, err := db.db.Get(key, nil)
  100. if err == leveldb.ErrNotFound {
  101. return nil, nil
  102. }
  103. return v, nil
  104. }
  105. func (db *DB) Delete(key []byte) error {
  106. return db.db.Delete(key, nil)
  107. }
  108. func (db *DB) NewWriteBatch() driver.IWriteBatch {
  109. wb := &WriteBatch{
  110. db: db,
  111. wbatch: new(leveldb.Batch),
  112. }
  113. return wb
  114. }
  115. func (db *DB) NewIterator() driver.IIterator {
  116. it := &Iterator{
  117. db.db.NewIterator(nil, db.iteratorOpts),
  118. }
  119. return it
  120. }
  121. func (db *DB) Begin() (driver.Tx, error) {
  122. return nil, driver.ErrTxSupport
  123. }
  124. func (db *DB) NewSnapshot() (driver.ISnapshot, error) {
  125. snapshot, err := db.db.GetSnapshot()
  126. if err != nil {
  127. return nil, err
  128. }
  129. s := &Snapshot{
  130. db: db,
  131. snp: snapshot,
  132. }
  133. return s, nil
  134. }
  135. func init() {
  136. driver.Register(Store{})
  137. driver.Register(MemStore{})
  138. }
上海开阖软件有限公司 沪ICP备12045867号-1