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.

190 lines
4.7 KiB

  1. package leveldb
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/md5"
  6. "fmt"
  7. "io"
  8. "github.com/chrislusf/seaweedfs/weed/filer2"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. weed_util "github.com/chrislusf/seaweedfs/weed/util"
  11. "github.com/syndtr/goleveldb/leveldb"
  12. "github.com/syndtr/goleveldb/leveldb/opt"
  13. leveldb_util "github.com/syndtr/goleveldb/leveldb/util"
  14. )
  15. func init() {
  16. filer2.Stores = append(filer2.Stores, &LevelDB2Store{})
  17. }
  18. // known theoretically 128 bit MD5 collision of 2 directories.
  19. // (but really? please show some real examples)
  20. type LevelDB2Store struct {
  21. db *leveldb.DB
  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)
  29. }
  30. func (store *LevelDB2Store) initialize(dir string) (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: 10,
  39. }
  40. if store.db, err = leveldb.OpenFile(dir, opts); err != nil {
  41. glog.Infof("filer store open dir %s: %v", dir, err)
  42. return
  43. }
  44. return
  45. }
  46. func (store *LevelDB2Store) BeginTransaction(ctx context.Context) (context.Context, error) {
  47. return ctx, nil
  48. }
  49. func (store *LevelDB2Store) CommitTransaction(ctx context.Context) error {
  50. return nil
  51. }
  52. func (store *LevelDB2Store) RollbackTransaction(ctx context.Context) error {
  53. return nil
  54. }
  55. func (store *LevelDB2Store) InsertEntry(ctx context.Context, entry *filer2.Entry) (err error) {
  56. key := genKey(entry.DirAndName())
  57. value, err := entry.EncodeAttributesAndChunks()
  58. if err != nil {
  59. return fmt.Errorf("encoding %s %+v: %v", entry.FullPath, entry.Attr, err)
  60. }
  61. err = store.db.Put(key, value, nil)
  62. if err != nil {
  63. return fmt.Errorf("persisting %s : %v", entry.FullPath, err)
  64. }
  65. // println("saved", entry.FullPath, "chunks", len(entry.Chunks))
  66. return nil
  67. }
  68. func (store *LevelDB2Store) UpdateEntry(ctx context.Context, entry *filer2.Entry) (err error) {
  69. return store.InsertEntry(ctx, entry)
  70. }
  71. func (store *LevelDB2Store) FindEntry(ctx context.Context, fullpath filer2.FullPath) (entry *filer2.Entry, err error) {
  72. key := genKey(fullpath.DirAndName())
  73. data, err := store.db.Get(key, nil)
  74. if err == leveldb.ErrNotFound {
  75. return nil, filer2.ErrNotFound
  76. }
  77. if err != nil {
  78. return nil, fmt.Errorf("get %s : %v", entry.FullPath, err)
  79. }
  80. entry = &filer2.Entry{
  81. FullPath: fullpath,
  82. }
  83. err = entry.DecodeAttributesAndChunks(data)
  84. if err != nil {
  85. return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
  86. }
  87. // println("read", entry.FullPath, "chunks", len(entry.Chunks), "data", len(data), string(data))
  88. return entry, nil
  89. }
  90. func (store *LevelDB2Store) DeleteEntry(ctx context.Context, fullpath filer2.FullPath) (err error) {
  91. key := genKey(fullpath.DirAndName())
  92. err = store.db.Delete(key, nil)
  93. if err != nil {
  94. return fmt.Errorf("delete %s : %v", fullpath, err)
  95. }
  96. return nil
  97. }
  98. func (store *LevelDB2Store) ListDirectoryEntries(ctx context.Context, fullpath filer2.FullPath, startFileName string, inclusive bool,
  99. limit int) (entries []*filer2.Entry, err error) {
  100. directoryPrefix := genDirectoryKeyPrefix(fullpath, "")
  101. iter := store.db.NewIterator(&leveldb_util.Range{Start: genDirectoryKeyPrefix(fullpath, startFileName)}, nil)
  102. for iter.Next() {
  103. key := iter.Key()
  104. if !bytes.HasPrefix(key, directoryPrefix) {
  105. break
  106. }
  107. fileName := getNameFromKey(key)
  108. if fileName == "" {
  109. continue
  110. }
  111. if fileName == startFileName && !inclusive {
  112. continue
  113. }
  114. limit--
  115. if limit < 0 {
  116. break
  117. }
  118. entry := &filer2.Entry{
  119. FullPath: filer2.NewFullPath(string(fullpath), fileName),
  120. }
  121. if decodeErr := entry.DecodeAttributesAndChunks(iter.Value()); decodeErr != nil {
  122. err = decodeErr
  123. glog.V(0).Infof("list %s : %v", entry.FullPath, err)
  124. break
  125. }
  126. entries = append(entries, entry)
  127. }
  128. iter.Release()
  129. return entries, err
  130. }
  131. func genKey(dirPath, fileName string) (key []byte) {
  132. key = hashToBytes(dirPath)
  133. key = append(key, []byte(fileName)...)
  134. return key
  135. }
  136. func genDirectoryKeyPrefix(fullpath filer2.FullPath, startFileName string) (keyPrefix []byte) {
  137. keyPrefix = hashToBytes(string(fullpath))
  138. if len(startFileName) > 0 {
  139. keyPrefix = append(keyPrefix, []byte(startFileName)...)
  140. }
  141. return keyPrefix
  142. }
  143. func getNameFromKey(key []byte) string {
  144. return string(key[8:])
  145. }
  146. func hashToBytes(dir string) []byte {
  147. h := md5.New()
  148. io.WriteString(h, dir)
  149. b := h.Sum(nil)
  150. return b
  151. }