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.

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