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.

217 lines
5.3 KiB

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