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.

174 lines
4.0 KiB

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