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.

248 lines
6.4 KiB

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