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.

181 lines
4.4 KiB

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