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.

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