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.

238 lines
6.1 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package leveldb
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/md5"
  6. "fmt"
  7. "io"
  8. "os"
  9. "github.com/syndtr/goleveldb/leveldb"
  10. "github.com/syndtr/goleveldb/leveldb/opt"
  11. leveldb_util "github.com/syndtr/goleveldb/leveldb/util"
  12. "github.com/chrislusf/seaweedfs/weed/filer2"
  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. func init() {
  18. filer2.Stores = append(filer2.Stores, &LevelDB2Store{})
  19. }
  20. type LevelDB2Store struct {
  21. dbs []*leveldb.DB
  22. dbCount int
  23. }
  24. func (store *LevelDB2Store) GetName() string {
  25. return "leveldb2"
  26. }
  27. func (store *LevelDB2Store) Initialize(configuration weed_util.Configuration, prefix string) (err error) {
  28. dir := configuration.GetString(prefix + "dir")
  29. return store.initialize(dir, 8)
  30. }
  31. func (store *LevelDB2Store) initialize(dir string, dbCount int) (err error) {
  32. glog.Infof("filer store leveldb2 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: 4,
  40. }
  41. for d := 0; d < dbCount; d++ {
  42. dbFolder := fmt.Sprintf("%s/%02d", dir, d)
  43. os.MkdirAll(dbFolder, 0755)
  44. db, dbErr := leveldb.OpenFile(dbFolder, opts)
  45. if dbErr != nil {
  46. glog.Errorf("filer store open dir %s: %v", dbFolder, dbErr)
  47. return
  48. }
  49. store.dbs = append(store.dbs, db)
  50. }
  51. store.dbCount = dbCount
  52. return
  53. }
  54. func (store *LevelDB2Store) BeginTransaction(ctx context.Context) (context.Context, error) {
  55. return ctx, nil
  56. }
  57. func (store *LevelDB2Store) CommitTransaction(ctx context.Context) error {
  58. return nil
  59. }
  60. func (store *LevelDB2Store) RollbackTransaction(ctx context.Context) error {
  61. return nil
  62. }
  63. func (store *LevelDB2Store) InsertEntry(ctx context.Context, entry *filer2.Entry) (err error) {
  64. dir, name := entry.DirAndName()
  65. key, partitionId := genKey(dir, name, store.dbCount)
  66. value, err := entry.EncodeAttributesAndChunks()
  67. if err != nil {
  68. return fmt.Errorf("encoding %s %+v: %v", entry.FullPath, entry.Attr, err)
  69. }
  70. err = store.dbs[partitionId].Put(key, value, nil)
  71. if err != nil {
  72. return fmt.Errorf("persisting %s : %v", entry.FullPath, err)
  73. }
  74. // println("saved", entry.FullPath, "chunks", len(entry.Chunks))
  75. return nil
  76. }
  77. func (store *LevelDB2Store) UpdateEntry(ctx context.Context, entry *filer2.Entry) (err error) {
  78. return store.InsertEntry(ctx, entry)
  79. }
  80. func (store *LevelDB2Store) FindEntry(ctx context.Context, fullpath filer2.FullPath) (entry *filer2.Entry, err error) {
  81. dir, name := fullpath.DirAndName()
  82. key, partitionId := genKey(dir, name, store.dbCount)
  83. data, err := store.dbs[partitionId].Get(key, nil)
  84. if err == leveldb.ErrNotFound {
  85. return nil, filer_pb.ErrNotFound
  86. }
  87. if err != nil {
  88. return nil, fmt.Errorf("get %s : %v", entry.FullPath, err)
  89. }
  90. entry = &filer2.Entry{
  91. FullPath: fullpath,
  92. }
  93. err = entry.DecodeAttributesAndChunks(data)
  94. if err != nil {
  95. return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
  96. }
  97. // println("read", entry.FullPath, "chunks", len(entry.Chunks), "data", len(data), string(data))
  98. return entry, nil
  99. }
  100. func (store *LevelDB2Store) DeleteEntry(ctx context.Context, fullpath filer2.FullPath) (err error) {
  101. dir, name := fullpath.DirAndName()
  102. key, partitionId := genKey(dir, name, store.dbCount)
  103. err = store.dbs[partitionId].Delete(key, nil)
  104. if err != nil {
  105. return fmt.Errorf("delete %s : %v", fullpath, err)
  106. }
  107. return nil
  108. }
  109. func (store *LevelDB2Store) DeleteFolderChildren(ctx context.Context, fullpath filer2.FullPath) (err error) {
  110. directoryPrefix, partitionId := genDirectoryKeyPrefix(fullpath, "", store.dbCount)
  111. batch := new(leveldb.Batch)
  112. iter := store.dbs[partitionId].NewIterator(&leveldb_util.Range{Start: directoryPrefix}, nil)
  113. for iter.Next() {
  114. key := iter.Key()
  115. if !bytes.HasPrefix(key, directoryPrefix) {
  116. break
  117. }
  118. fileName := getNameFromKey(key)
  119. if fileName == "" {
  120. continue
  121. }
  122. batch.Delete(append(directoryPrefix, []byte(fileName)...))
  123. }
  124. iter.Release()
  125. err = store.dbs[partitionId].Write(batch, nil)
  126. if err != nil {
  127. return fmt.Errorf("delete %s : %v", fullpath, err)
  128. }
  129. return nil
  130. }
  131. func (store *LevelDB2Store) ListDirectoryEntries(ctx context.Context, fullpath filer2.FullPath, startFileName string, inclusive bool,
  132. limit int) (entries []*filer2.Entry, err error) {
  133. directoryPrefix, partitionId := genDirectoryKeyPrefix(fullpath, "", store.dbCount)
  134. lastFileStart, _ := genDirectoryKeyPrefix(fullpath, startFileName, store.dbCount)
  135. iter := store.dbs[partitionId].NewIterator(&leveldb_util.Range{Start: lastFileStart}, nil)
  136. for iter.Next() {
  137. key := iter.Key()
  138. if !bytes.HasPrefix(key, directoryPrefix) {
  139. break
  140. }
  141. fileName := getNameFromKey(key)
  142. if fileName == "" {
  143. continue
  144. }
  145. if fileName == startFileName && !inclusive {
  146. continue
  147. }
  148. limit--
  149. if limit < 0 {
  150. break
  151. }
  152. entry := &filer2.Entry{
  153. FullPath: filer2.NewFullPath(string(fullpath), fileName),
  154. }
  155. // println("list", entry.FullPath, "chunks", len(entry.Chunks))
  156. if decodeErr := entry.DecodeAttributesAndChunks(iter.Value()); decodeErr != nil {
  157. err = decodeErr
  158. glog.V(0).Infof("list %s : %v", entry.FullPath, err)
  159. break
  160. }
  161. entries = append(entries, entry)
  162. }
  163. iter.Release()
  164. return entries, err
  165. }
  166. func genKey(dirPath, fileName string, dbCount int) (key []byte, partitionId int) {
  167. key, partitionId = hashToBytes(dirPath, dbCount)
  168. key = append(key, []byte(fileName)...)
  169. return key, partitionId
  170. }
  171. func genDirectoryKeyPrefix(fullpath filer2.FullPath, startFileName string, dbCount int) (keyPrefix []byte, partitionId int) {
  172. keyPrefix, partitionId = hashToBytes(string(fullpath), dbCount)
  173. if len(startFileName) > 0 {
  174. keyPrefix = append(keyPrefix, []byte(startFileName)...)
  175. }
  176. return keyPrefix, partitionId
  177. }
  178. func getNameFromKey(key []byte) string {
  179. return string(key[md5.Size:])
  180. }
  181. // hash directory, and use last byte for partitioning
  182. func hashToBytes(dir string, dbCount int) ([]byte, int) {
  183. h := md5.New()
  184. io.WriteString(h, dir)
  185. b := h.Sum(nil)
  186. x := b[len(b)-1]
  187. return b, int(x) % dbCount
  188. }