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.

226 lines
5.6 KiB

7 years ago
7 years ago
7 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
7 years ago
  1. package leveldb
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "github.com/syndtr/goleveldb/leveldb"
  7. "github.com/syndtr/goleveldb/leveldb/errors"
  8. "github.com/syndtr/goleveldb/leveldb/opt"
  9. leveldb_util "github.com/syndtr/goleveldb/leveldb/util"
  10. "github.com/chrislusf/seaweedfs/weed/filer2"
  11. "github.com/chrislusf/seaweedfs/weed/glog"
  12. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  13. weed_util "github.com/chrislusf/seaweedfs/weed/util"
  14. )
  15. const (
  16. DIR_FILE_SEPARATOR = byte(0x00)
  17. )
  18. func init() {
  19. filer2.Stores = append(filer2.Stores, &LevelDBStore{})
  20. }
  21. type LevelDBStore struct {
  22. db *leveldb.DB
  23. }
  24. func (store *LevelDBStore) GetName() string {
  25. return "leveldb"
  26. }
  27. func (store *LevelDBStore) Initialize(configuration weed_util.Configuration, prefix string) (err error) {
  28. dir := configuration.GetString(prefix + "dir")
  29. return store.initialize(dir)
  30. }
  31. func (store *LevelDBStore) initialize(dir string) (err error) {
  32. glog.Infof("filer store dir: %s", dir)
  33. if err := weed_util.TestFolderWritable(dir); err != nil {
  34. return fmt.Errorf("Check Level Folder %s Writable: %s", dir, err)
  35. }
  36. opts := &opt.Options{
  37. BlockCacheCapacity: 32 * 1024 * 1024, // default value is 8MiB
  38. WriteBuffer: 16 * 1024 * 1024, // default value is 4MiB
  39. CompactionTableSizeMultiplier: 10,
  40. }
  41. if store.db, err = leveldb.OpenFile(dir, opts); err != nil {
  42. if errors.IsCorrupted(err) {
  43. store.db, err = leveldb.RecoverFile(dir, opts)
  44. }
  45. glog.Infof("filer store open dir %s: %v", dir, err)
  46. return
  47. }
  48. return
  49. }
  50. func (store *LevelDBStore) BeginTransaction(ctx context.Context) (context.Context, error) {
  51. return ctx, nil
  52. }
  53. func (store *LevelDBStore) CommitTransaction(ctx context.Context) error {
  54. return nil
  55. }
  56. func (store *LevelDBStore) RollbackTransaction(ctx context.Context) error {
  57. return nil
  58. }
  59. func (store *LevelDBStore) InsertEntry(ctx context.Context, entry *filer2.Entry) (err error) {
  60. key := genKey(entry.DirAndName())
  61. value, err := entry.EncodeAttributesAndChunks()
  62. if err != nil {
  63. return fmt.Errorf("encoding %s %+v: %v", entry.FullPath, entry.Attr, err)
  64. }
  65. err = store.db.Put(key, value, nil)
  66. if err != nil {
  67. return fmt.Errorf("persisting %s : %v", entry.FullPath, err)
  68. }
  69. // println("saved", entry.FullPath, "chunks", len(entry.Chunks))
  70. return nil
  71. }
  72. func (store *LevelDBStore) UpdateEntry(ctx context.Context, entry *filer2.Entry) (err error) {
  73. return store.InsertEntry(ctx, entry)
  74. }
  75. func (store *LevelDBStore) FindEntry(ctx context.Context, fullpath weed_util.FullPath) (entry *filer2.Entry, err error) {
  76. key := genKey(fullpath.DirAndName())
  77. data, err := store.db.Get(key, nil)
  78. if err == leveldb.ErrNotFound {
  79. return nil, filer_pb.ErrNotFound
  80. }
  81. if err != nil {
  82. return nil, fmt.Errorf("get %s : %v", entry.FullPath, err)
  83. }
  84. entry = &filer2.Entry{
  85. FullPath: fullpath,
  86. }
  87. err = entry.DecodeAttributesAndChunks(data)
  88. if err != nil {
  89. return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
  90. }
  91. // println("read", entry.FullPath, "chunks", len(entry.Chunks), "data", len(data), string(data))
  92. return entry, nil
  93. }
  94. func (store *LevelDBStore) DeleteEntry(ctx context.Context, fullpath weed_util.FullPath) (err error) {
  95. key := genKey(fullpath.DirAndName())
  96. err = store.db.Delete(key, nil)
  97. if err != nil {
  98. return fmt.Errorf("delete %s : %v", fullpath, err)
  99. }
  100. return nil
  101. }
  102. func (store *LevelDBStore) DeleteFolderChildren(ctx context.Context, fullpath weed_util.FullPath) (err error) {
  103. batch := new(leveldb.Batch)
  104. directoryPrefix := genDirectoryKeyPrefix(fullpath, "")
  105. iter := store.db.NewIterator(&leveldb_util.Range{Start: directoryPrefix}, nil)
  106. for iter.Next() {
  107. key := iter.Key()
  108. if !bytes.HasPrefix(key, directoryPrefix) {
  109. break
  110. }
  111. fileName := getNameFromKey(key)
  112. if fileName == "" {
  113. continue
  114. }
  115. batch.Delete([]byte(genKey(string(fullpath), fileName)))
  116. }
  117. iter.Release()
  118. err = store.db.Write(batch, nil)
  119. if err != nil {
  120. return fmt.Errorf("delete %s : %v", fullpath, err)
  121. }
  122. return nil
  123. }
  124. func (store *LevelDBStore) ListDirectoryEntries(ctx context.Context, fullpath weed_util.FullPath, startFileName string, inclusive bool,
  125. limit int) (entries []*filer2.Entry, err error) {
  126. directoryPrefix := genDirectoryKeyPrefix(fullpath, "")
  127. iter := store.db.NewIterator(&leveldb_util.Range{Start: genDirectoryKeyPrefix(fullpath, startFileName)}, nil)
  128. for iter.Next() {
  129. key := iter.Key()
  130. if !bytes.HasPrefix(key, directoryPrefix) {
  131. break
  132. }
  133. fileName := getNameFromKey(key)
  134. if fileName == "" {
  135. continue
  136. }
  137. if fileName == startFileName && !inclusive {
  138. continue
  139. }
  140. limit--
  141. if limit < 0 {
  142. break
  143. }
  144. entry := &filer2.Entry{
  145. FullPath: weed_util.NewFullPath(string(fullpath), fileName),
  146. }
  147. if decodeErr := entry.DecodeAttributesAndChunks(iter.Value()); decodeErr != nil {
  148. err = decodeErr
  149. glog.V(0).Infof("list %s : %v", entry.FullPath, err)
  150. break
  151. }
  152. entries = append(entries, entry)
  153. }
  154. iter.Release()
  155. return entries, err
  156. }
  157. func genKey(dirPath, fileName string) (key []byte) {
  158. key = []byte(dirPath)
  159. key = append(key, DIR_FILE_SEPARATOR)
  160. key = append(key, []byte(fileName)...)
  161. return key
  162. }
  163. func genDirectoryKeyPrefix(fullpath weed_util.FullPath, startFileName string) (keyPrefix []byte) {
  164. keyPrefix = []byte(string(fullpath))
  165. keyPrefix = append(keyPrefix, DIR_FILE_SEPARATOR)
  166. if len(startFileName) > 0 {
  167. keyPrefix = append(keyPrefix, []byte(startFileName)...)
  168. }
  169. return keyPrefix
  170. }
  171. func getNameFromKey(key []byte) string {
  172. sepIndex := len(key) - 1
  173. for sepIndex >= 0 && key[sepIndex] != DIR_FILE_SEPARATOR {
  174. sepIndex--
  175. }
  176. return string(key[sepIndex+1:])
  177. }
  178. func (store *LevelDBStore) Shutdown() {
  179. store.db.Close()
  180. }