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.

188 lines
4.6 KiB

7 years ago
7 years ago
7 years ago
6 years ago
6 years ago
6 years ago
7 years ago
  1. package leveldb
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "github.com/chrislusf/seaweedfs/weed/filer2"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. weed_util "github.com/chrislusf/seaweedfs/weed/util"
  9. "github.com/syndtr/goleveldb/leveldb"
  10. "github.com/syndtr/goleveldb/leveldb/opt"
  11. leveldb_util "github.com/syndtr/goleveldb/leveldb/util"
  12. )
  13. const (
  14. DIR_FILE_SEPARATOR = byte(0x00)
  15. )
  16. func init() {
  17. filer2.Stores = append(filer2.Stores, &LevelDBStore{})
  18. }
  19. type LevelDBStore struct {
  20. db *leveldb.DB
  21. }
  22. func (store *LevelDBStore) GetName() string {
  23. return "leveldb"
  24. }
  25. func (store *LevelDBStore) Initialize(configuration weed_util.Configuration) (err error) {
  26. dir := configuration.GetString("dir")
  27. return store.initialize(dir)
  28. }
  29. func (store *LevelDBStore) initialize(dir string) (err error) {
  30. glog.Infof("filer store dir: %s", dir)
  31. if err := weed_util.TestFolderWritable(dir); err != nil {
  32. return fmt.Errorf("Check Level Folder %s Writable: %s", dir, err)
  33. }
  34. opts := &opt.Options{
  35. BlockCacheCapacity: 32 * 1024 * 1024, // default value is 8MiB
  36. WriteBuffer: 16 * 1024 * 1024, // default value is 4MiB
  37. CompactionTableSizeMultiplier: 10,
  38. }
  39. if store.db, err = leveldb.OpenFile(dir, opts); err != nil {
  40. glog.Infof("filer store open dir %s: %v", dir, err)
  41. return
  42. }
  43. return
  44. }
  45. func (store *LevelDBStore) BeginTransaction(ctx context.Context) (context.Context, error) {
  46. return ctx, nil
  47. }
  48. func (store *LevelDBStore) CommitTransaction(ctx context.Context) error {
  49. return nil
  50. }
  51. func (store *LevelDBStore) RollbackTransaction(ctx context.Context) error {
  52. return nil
  53. }
  54. func (store *LevelDBStore) InsertEntry(ctx context.Context, entry *filer2.Entry) (err error) {
  55. key := genKey(entry.DirAndName())
  56. value, err := entry.EncodeAttributesAndChunks()
  57. if err != nil {
  58. return fmt.Errorf("encoding %s %+v: %v", entry.FullPath, entry.Attr, err)
  59. }
  60. err = store.db.Put(key, value, nil)
  61. if err != nil {
  62. return fmt.Errorf("persisting %s : %v", entry.FullPath, err)
  63. }
  64. // println("saved", entry.FullPath, "chunks", len(entry.Chunks))
  65. return nil
  66. }
  67. func (store *LevelDBStore) UpdateEntry(ctx context.Context, entry *filer2.Entry) (err error) {
  68. return store.InsertEntry(ctx, entry)
  69. }
  70. func (store *LevelDBStore) FindEntry(ctx context.Context, fullpath filer2.FullPath) (entry *filer2.Entry, err error) {
  71. key := genKey(fullpath.DirAndName())
  72. data, err := store.db.Get(key, nil)
  73. if err == leveldb.ErrNotFound {
  74. return nil, filer2.ErrNotFound
  75. }
  76. if err != nil {
  77. return nil, fmt.Errorf("get %s : %v", entry.FullPath, err)
  78. }
  79. entry = &filer2.Entry{
  80. FullPath: fullpath,
  81. }
  82. err = entry.DecodeAttributesAndChunks(data)
  83. if err != nil {
  84. return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
  85. }
  86. // println("read", entry.FullPath, "chunks", len(entry.Chunks), "data", len(data), string(data))
  87. return entry, nil
  88. }
  89. func (store *LevelDBStore) DeleteEntry(ctx context.Context, fullpath filer2.FullPath) (err error) {
  90. key := genKey(fullpath.DirAndName())
  91. err = store.db.Delete(key, nil)
  92. if err != nil {
  93. return fmt.Errorf("delete %s : %v", fullpath, err)
  94. }
  95. return nil
  96. }
  97. func (store *LevelDBStore) ListDirectoryEntries(ctx context.Context, fullpath filer2.FullPath, startFileName string, inclusive bool,
  98. limit int) (entries []*filer2.Entry, err error) {
  99. directoryPrefix := genDirectoryKeyPrefix(fullpath, "")
  100. iter := store.db.NewIterator(&leveldb_util.Range{Start: genDirectoryKeyPrefix(fullpath, startFileName)}, nil)
  101. for iter.Next() {
  102. key := iter.Key()
  103. if !bytes.HasPrefix(key, directoryPrefix) {
  104. break
  105. }
  106. fileName := getNameFromKey(key)
  107. if fileName == "" {
  108. continue
  109. }
  110. if fileName == startFileName && !inclusive {
  111. continue
  112. }
  113. limit--
  114. if limit < 0 {
  115. break
  116. }
  117. entry := &filer2.Entry{
  118. FullPath: filer2.NewFullPath(string(fullpath), fileName),
  119. }
  120. if decodeErr := entry.DecodeAttributesAndChunks(iter.Value()); decodeErr != nil {
  121. err = decodeErr
  122. glog.V(0).Infof("list %s : %v", entry.FullPath, err)
  123. break
  124. }
  125. entries = append(entries, entry)
  126. }
  127. iter.Release()
  128. return entries, err
  129. }
  130. func genKey(dirPath, fileName string) (key []byte) {
  131. key = []byte(dirPath)
  132. key = append(key, DIR_FILE_SEPARATOR)
  133. key = append(key, []byte(fileName)...)
  134. return key
  135. }
  136. func genDirectoryKeyPrefix(fullpath filer2.FullPath, startFileName string) (keyPrefix []byte) {
  137. keyPrefix = []byte(string(fullpath))
  138. keyPrefix = append(keyPrefix, DIR_FILE_SEPARATOR)
  139. if len(startFileName) > 0 {
  140. keyPrefix = append(keyPrefix, []byte(startFileName)...)
  141. }
  142. return keyPrefix
  143. }
  144. func getNameFromKey(key []byte) string {
  145. sepIndex := len(key) - 1
  146. for sepIndex >= 0 && key[sepIndex] != DIR_FILE_SEPARATOR {
  147. sepIndex--
  148. }
  149. return string(key[sepIndex+1:])
  150. }