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.

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