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.

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