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.

231 lines
5.9 KiB

7 years ago
4 years ago
7 years ago
4 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
4 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. leveldb_errors "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 leveldb_errors.IsCorrupted(err) {
  43. store.db, err = leveldb.RecoverFile(dir, opts)
  44. }
  45. if err != nil {
  46. glog.Infof("filer store open dir %s: %v", dir, err)
  47. return
  48. }
  49. }
  50. return
  51. }
  52. func (store *LevelDBStore) BeginTransaction(ctx context.Context) (context.Context, error) {
  53. return ctx, nil
  54. }
  55. func (store *LevelDBStore) CommitTransaction(ctx context.Context) error {
  56. return nil
  57. }
  58. func (store *LevelDBStore) RollbackTransaction(ctx context.Context) error {
  59. return nil
  60. }
  61. func (store *LevelDBStore) InsertEntry(ctx context.Context, entry *filer2.Entry) (err error) {
  62. key := genKey(entry.DirAndName())
  63. value, err := entry.EncodeAttributesAndChunks()
  64. if err != nil {
  65. return fmt.Errorf("encoding %s %+v: %v", entry.FullPath, entry.Attr, err)
  66. }
  67. err = store.db.Put(key, value, nil)
  68. if err != nil {
  69. return fmt.Errorf("persisting %s : %v", entry.FullPath, err)
  70. }
  71. // println("saved", entry.FullPath, "chunks", len(entry.Chunks))
  72. return nil
  73. }
  74. func (store *LevelDBStore) UpdateEntry(ctx context.Context, entry *filer2.Entry) (err error) {
  75. return store.InsertEntry(ctx, entry)
  76. }
  77. func (store *LevelDBStore) FindEntry(ctx context.Context, fullpath weed_util.FullPath) (entry *filer2.Entry, err error) {
  78. key := genKey(fullpath.DirAndName())
  79. data, err := store.db.Get(key, nil)
  80. if err == leveldb.ErrNotFound {
  81. return nil, filer_pb.ErrNotFound
  82. }
  83. if err != nil {
  84. return nil, fmt.Errorf("get %s : %v", entry.FullPath, err)
  85. }
  86. entry = &filer2.Entry{
  87. FullPath: fullpath,
  88. }
  89. err = entry.DecodeAttributesAndChunks(data)
  90. if err != nil {
  91. return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
  92. }
  93. // println("read", entry.FullPath, "chunks", len(entry.Chunks), "data", len(data), string(data))
  94. return entry, nil
  95. }
  96. func (store *LevelDBStore) DeleteEntry(ctx context.Context, fullpath weed_util.FullPath) (err error) {
  97. key := genKey(fullpath.DirAndName())
  98. err = store.db.Delete(key, nil)
  99. if err != nil {
  100. return fmt.Errorf("delete %s : %v", fullpath, err)
  101. }
  102. return nil
  103. }
  104. func (store *LevelDBStore) DeleteFolderChildren(ctx context.Context, fullpath weed_util.FullPath) (err error) {
  105. batch := new(leveldb.Batch)
  106. directoryPrefix := genDirectoryKeyPrefix(fullpath, "")
  107. iter := store.db.NewIterator(&leveldb_util.Range{Start: directoryPrefix}, nil)
  108. for iter.Next() {
  109. key := iter.Key()
  110. if !bytes.HasPrefix(key, directoryPrefix) {
  111. break
  112. }
  113. fileName := getNameFromKey(key)
  114. if fileName == "" {
  115. continue
  116. }
  117. batch.Delete([]byte(genKey(string(fullpath), fileName)))
  118. }
  119. iter.Release()
  120. err = store.db.Write(batch, nil)
  121. if err != nil {
  122. return fmt.Errorf("delete %s : %v", fullpath, err)
  123. }
  124. return nil
  125. }
  126. func (store *LevelDBStore) ListDirectoryPrefixedEntries(ctx context.Context, fullpath weed_util.FullPath, startFileName string, inclusive bool, limit int, prefix string) (entries []*filer2.Entry, err error) {
  127. return nil, filer2.ErrUnsupportedListDirectoryPrefixed
  128. }
  129. func (store *LevelDBStore) ListDirectoryEntries(ctx context.Context, fullpath weed_util.FullPath, startFileName string, inclusive bool,
  130. limit int) (entries []*filer2.Entry, err error) {
  131. directoryPrefix := genDirectoryKeyPrefix(fullpath, "")
  132. iter := store.db.NewIterator(&leveldb_util.Range{Start: genDirectoryKeyPrefix(fullpath, startFileName)}, nil)
  133. for iter.Next() {
  134. key := iter.Key()
  135. if !bytes.HasPrefix(key, directoryPrefix) {
  136. break
  137. }
  138. fileName := getNameFromKey(key)
  139. if fileName == "" {
  140. continue
  141. }
  142. if fileName == startFileName && !inclusive {
  143. continue
  144. }
  145. limit--
  146. if limit < 0 {
  147. break
  148. }
  149. entry := &filer2.Entry{
  150. FullPath: weed_util.NewFullPath(string(fullpath), fileName),
  151. }
  152. if decodeErr := entry.DecodeAttributesAndChunks(iter.Value()); decodeErr != nil {
  153. err = decodeErr
  154. glog.V(0).Infof("list %s : %v", entry.FullPath, err)
  155. break
  156. }
  157. entries = append(entries, entry)
  158. }
  159. iter.Release()
  160. return entries, err
  161. }
  162. func genKey(dirPath, fileName string) (key []byte) {
  163. key = []byte(dirPath)
  164. key = append(key, DIR_FILE_SEPARATOR)
  165. key = append(key, []byte(fileName)...)
  166. return key
  167. }
  168. func genDirectoryKeyPrefix(fullpath weed_util.FullPath, startFileName string) (keyPrefix []byte) {
  169. keyPrefix = []byte(string(fullpath))
  170. keyPrefix = append(keyPrefix, DIR_FILE_SEPARATOR)
  171. if len(startFileName) > 0 {
  172. keyPrefix = append(keyPrefix, []byte(startFileName)...)
  173. }
  174. return keyPrefix
  175. }
  176. func getNameFromKey(key []byte) string {
  177. sepIndex := len(key) - 1
  178. for sepIndex >= 0 && key[sepIndex] != DIR_FILE_SEPARATOR {
  179. sepIndex--
  180. }
  181. return string(key[sepIndex+1:])
  182. }
  183. func (store *LevelDBStore) Shutdown() {
  184. store.db.Close()
  185. }