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.

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