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.

171 lines
4.1 KiB

7 years ago
7 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) InsertEntry(ctx context.Context, entry *filer2.Entry) (err error) {
  40. key := genKey(entry.DirAndName())
  41. value, err := entry.EncodeAttributesAndChunks()
  42. if err != nil {
  43. return fmt.Errorf("encoding %s %+v: %v", entry.FullPath, entry.Attr, err)
  44. }
  45. err = store.db.Put(key, value, nil)
  46. if err != nil {
  47. return fmt.Errorf("persisting %s : %v", entry.FullPath, err)
  48. }
  49. // println("saved", entry.FullPath, "chunks", len(entry.Chunks))
  50. return nil
  51. }
  52. func (store *LevelDBStore) UpdateEntry(ctx context.Context, entry *filer2.Entry) (err error) {
  53. return store.InsertEntry(ctx, entry)
  54. }
  55. func (store *LevelDBStore) FindEntry(ctx context.Context, fullpath filer2.FullPath) (entry *filer2.Entry, err error) {
  56. key := genKey(fullpath.DirAndName())
  57. data, err := store.db.Get(key, nil)
  58. if err == leveldb.ErrNotFound {
  59. return nil, filer2.ErrNotFound
  60. }
  61. if err != nil {
  62. return nil, fmt.Errorf("get %s : %v", entry.FullPath, err)
  63. }
  64. entry = &filer2.Entry{
  65. FullPath: fullpath,
  66. }
  67. err = entry.DecodeAttributesAndChunks(data)
  68. if err != nil {
  69. return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
  70. }
  71. // println("read", entry.FullPath, "chunks", len(entry.Chunks), "data", len(data), string(data))
  72. return entry, nil
  73. }
  74. func (store *LevelDBStore) DeleteEntry(ctx context.Context, fullpath filer2.FullPath) (err error) {
  75. key := genKey(fullpath.DirAndName())
  76. err = store.db.Delete(key, nil)
  77. if err != nil {
  78. return fmt.Errorf("delete %s : %v", fullpath, err)
  79. }
  80. return nil
  81. }
  82. func (store *LevelDBStore) ListDirectoryEntries(ctx context.Context, fullpath filer2.FullPath, startFileName string, inclusive bool,
  83. limit int) (entries []*filer2.Entry, err error) {
  84. directoryPrefix := genDirectoryKeyPrefix(fullpath, "")
  85. iter := store.db.NewIterator(&leveldb_util.Range{Start: genDirectoryKeyPrefix(fullpath, startFileName)}, nil)
  86. for iter.Next() {
  87. key := iter.Key()
  88. if !bytes.HasPrefix(key, directoryPrefix) {
  89. break
  90. }
  91. fileName := getNameFromKey(key)
  92. if fileName == "" {
  93. continue
  94. }
  95. if fileName == startFileName && !inclusive {
  96. continue
  97. }
  98. limit--
  99. if limit < 0 {
  100. break
  101. }
  102. entry := &filer2.Entry{
  103. FullPath: filer2.NewFullPath(string(fullpath), fileName),
  104. }
  105. if decodeErr := entry.DecodeAttributesAndChunks(iter.Value()); decodeErr != nil {
  106. err = decodeErr
  107. glog.V(0).Infof("list %s : %v", entry.FullPath, err)
  108. break
  109. }
  110. entries = append(entries, entry)
  111. }
  112. iter.Release()
  113. return entries, err
  114. }
  115. func genKey(dirPath, fileName string) (key []byte) {
  116. key = []byte(dirPath)
  117. key = append(key, DIR_FILE_SEPARATOR)
  118. key = append(key, []byte(fileName)...)
  119. return key
  120. }
  121. func genDirectoryKeyPrefix(fullpath filer2.FullPath, startFileName string) (keyPrefix []byte) {
  122. keyPrefix = []byte(string(fullpath))
  123. keyPrefix = append(keyPrefix, DIR_FILE_SEPARATOR)
  124. if len(startFileName) > 0 {
  125. keyPrefix = append(keyPrefix, []byte(startFileName)...)
  126. }
  127. return keyPrefix
  128. }
  129. func getNameFromKey(key []byte) string {
  130. sepIndex := len(key) - 1
  131. for sepIndex >= 0 && key[sepIndex] != DIR_FILE_SEPARATOR {
  132. sepIndex--
  133. }
  134. return string(key[sepIndex+1:])
  135. }