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.

237 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. weed_util "github.com/chrislusf/seaweedfs/weed/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, prefix string) (err error) {
  27. dir := configuration.GetString(prefix + "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) DeleteFolderChildren(ctx context.Context, fullpath filer2.FullPath) (err error) {
  109. directoryPrefix, partitionId := genDirectoryKeyPrefix(fullpath, "", store.dbCount)
  110. batch := new(leveldb.Batch)
  111. iter := store.dbs[partitionId].NewIterator(&leveldb_util.Range{Start: directoryPrefix}, nil)
  112. for iter.Next() {
  113. key := iter.Key()
  114. if !bytes.HasPrefix(key, directoryPrefix) {
  115. break
  116. }
  117. fileName := getNameFromKey(key)
  118. if fileName == "" {
  119. continue
  120. }
  121. batch.Delete(append(directoryPrefix, []byte(fileName)...))
  122. }
  123. iter.Release()
  124. err = store.dbs[partitionId].Write(batch, nil)
  125. if err != nil {
  126. return fmt.Errorf("delete %s : %v", fullpath, err)
  127. }
  128. return nil
  129. }
  130. func (store *LevelDB2Store) ListDirectoryEntries(ctx context.Context, fullpath filer2.FullPath, startFileName string, inclusive bool,
  131. limit int) (entries []*filer2.Entry, err error) {
  132. directoryPrefix, partitionId := genDirectoryKeyPrefix(fullpath, "", store.dbCount)
  133. lastFileStart, _ := genDirectoryKeyPrefix(fullpath, startFileName, store.dbCount)
  134. iter := store.dbs[partitionId].NewIterator(&leveldb_util.Range{Start: lastFileStart}, nil)
  135. for iter.Next() {
  136. key := iter.Key()
  137. if !bytes.HasPrefix(key, directoryPrefix) {
  138. break
  139. }
  140. fileName := getNameFromKey(key)
  141. if fileName == "" {
  142. continue
  143. }
  144. if fileName == startFileName && !inclusive {
  145. continue
  146. }
  147. limit--
  148. if limit < 0 {
  149. break
  150. }
  151. entry := &filer2.Entry{
  152. FullPath: filer2.NewFullPath(string(fullpath), fileName),
  153. }
  154. // println("list", entry.FullPath, "chunks", len(entry.Chunks))
  155. if decodeErr := entry.DecodeAttributesAndChunks(iter.Value()); decodeErr != nil {
  156. err = decodeErr
  157. glog.V(0).Infof("list %s : %v", entry.FullPath, err)
  158. break
  159. }
  160. entries = append(entries, entry)
  161. }
  162. iter.Release()
  163. return entries, err
  164. }
  165. func genKey(dirPath, fileName string, dbCount int) (key []byte, partitionId int) {
  166. key, partitionId = hashToBytes(dirPath, dbCount)
  167. key = append(key, []byte(fileName)...)
  168. return key, partitionId
  169. }
  170. func genDirectoryKeyPrefix(fullpath filer2.FullPath, startFileName string, dbCount int) (keyPrefix []byte, partitionId int) {
  171. keyPrefix, partitionId = hashToBytes(string(fullpath), dbCount)
  172. if len(startFileName) > 0 {
  173. keyPrefix = append(keyPrefix, []byte(startFileName)...)
  174. }
  175. return keyPrefix, partitionId
  176. }
  177. func getNameFromKey(key []byte) string {
  178. return string(key[md5.Size:])
  179. }
  180. // hash directory, and use last byte for partitioning
  181. func hashToBytes(dir string, dbCount int) ([]byte, int) {
  182. h := md5.New()
  183. io.WriteString(h, dir)
  184. b := h.Sum(nil)
  185. x := b[len(b)-1]
  186. return b, int(x) % dbCount
  187. }