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.

169 lines
3.9 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) (err error) {
  73. key := genKey(fullpath.DirAndName())
  74. err = store.db.Delete(key, nil)
  75. if err != nil {
  76. return fmt.Errorf("delete %s : %v", fullpath, err)
  77. }
  78. return nil
  79. }
  80. func (store *LevelDBStore) ListDirectoryEntries(fullpath filer2.FullPath, startFileName string, inclusive bool,
  81. limit int) (entries []*filer2.Entry, err error) {
  82. directoryPrefix := genDirectoryKeyPrefix(fullpath, "")
  83. iter := store.db.NewIterator(&leveldb_util.Range{Start: genDirectoryKeyPrefix(fullpath, startFileName)}, nil)
  84. for iter.Next() {
  85. key := iter.Key()
  86. if !bytes.HasPrefix(key, directoryPrefix) {
  87. break
  88. }
  89. fileName := getNameFromKey(key)
  90. if fileName == "" {
  91. continue
  92. }
  93. if fileName == startFileName && !inclusive {
  94. continue
  95. }
  96. limit--
  97. if limit < 0 {
  98. break
  99. }
  100. entry := &filer2.Entry{
  101. FullPath: filer2.NewFullPath(string(fullpath), fileName),
  102. }
  103. if decodeErr := entry.DecodeAttributesAndChunks(iter.Value()); decodeErr != nil {
  104. err = decodeErr
  105. glog.V(0).Infof("list %s : %v", entry.FullPath, err)
  106. break
  107. }
  108. entries = append(entries, entry)
  109. }
  110. iter.Release()
  111. return entries, err
  112. }
  113. func genKey(dirPath, fileName string) (key []byte) {
  114. key = []byte(dirPath)
  115. key = append(key, DIR_FILE_SEPARATOR)
  116. key = append(key, []byte(fileName)...)
  117. return key
  118. }
  119. func genDirectoryKeyPrefix(fullpath filer2.FullPath, startFileName string) (keyPrefix []byte) {
  120. keyPrefix = []byte(string(fullpath))
  121. keyPrefix = append(keyPrefix, DIR_FILE_SEPARATOR)
  122. if len(startFileName) > 0 {
  123. keyPrefix = append(keyPrefix, []byte(startFileName)...)
  124. }
  125. return keyPrefix
  126. }
  127. func getNameFromKey(key []byte) string {
  128. sepIndex := len(key) - 1
  129. for sepIndex >= 0 && key[sepIndex] != DIR_FILE_SEPARATOR {
  130. sepIndex--
  131. }
  132. return string(key[sepIndex+1:])
  133. }