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.

259 lines
6.3 KiB

7 years ago
7 years ago
7 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
7 years ago
  1. package leveldb
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "strings"
  7. "github.com/syndtr/goleveldb/leveldb"
  8. "github.com/syndtr/goleveldb/leveldb/errors"
  9. "github.com/syndtr/goleveldb/leveldb/opt"
  10. leveldb_util "github.com/syndtr/goleveldb/leveldb/util"
  11. "github.com/chrislusf/seaweedfs/weed/filer2"
  12. "github.com/chrislusf/seaweedfs/weed/glog"
  13. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  14. weed_util "github.com/chrislusf/seaweedfs/weed/util"
  15. )
  16. const (
  17. DIR_FILE_SEPARATOR = byte(0x00)
  18. )
  19. func init() {
  20. filer2.Stores = append(filer2.Stores, &LevelDBStore{})
  21. }
  22. type LevelDBStore struct {
  23. db *leveldb.DB
  24. }
  25. func (store *LevelDBStore) GetName() string {
  26. return "leveldb"
  27. }
  28. func (store *LevelDBStore) Initialize(configuration weed_util.Configuration, prefix string) (err error) {
  29. dir := configuration.GetString(prefix + "dir")
  30. return store.initialize(dir)
  31. }
  32. func (store *LevelDBStore) initialize(dir string) (err error) {
  33. glog.Infof("filer store dir: %s", dir)
  34. if err := weed_util.TestFolderWritable(dir); err != nil {
  35. return fmt.Errorf("Check Level Folder %s Writable: %s", dir, err)
  36. }
  37. opts := &opt.Options{
  38. BlockCacheCapacity: 32 * 1024 * 1024, // default value is 8MiB
  39. WriteBuffer: 16 * 1024 * 1024, // default value is 4MiB
  40. CompactionTableSizeMultiplier: 10,
  41. }
  42. if store.db, err = leveldb.OpenFile(dir, opts); err != nil {
  43. if errors.IsCorrupted(err) {
  44. store.db, err = leveldb.RecoverFile(dir, opts)
  45. }
  46. if err != nil {
  47. glog.Infof("filer store open dir %s: %v", dir, err)
  48. return
  49. }
  50. }
  51. return
  52. }
  53. func (store *LevelDBStore) BeginTransaction(ctx context.Context) (context.Context, error) {
  54. return ctx, nil
  55. }
  56. func (store *LevelDBStore) CommitTransaction(ctx context.Context) error {
  57. return nil
  58. }
  59. func (store *LevelDBStore) RollbackTransaction(ctx context.Context) error {
  60. return nil
  61. }
  62. func (store *LevelDBStore) InsertEntry(ctx context.Context, entry *filer2.Entry) (err error) {
  63. key := genKey(entry.DirAndName())
  64. value, err := entry.EncodeAttributesAndChunks()
  65. if err != nil {
  66. return fmt.Errorf("encoding %s %+v: %v", entry.FullPath, entry.Attr, err)
  67. }
  68. err = store.db.Put(key, value, nil)
  69. if err != nil {
  70. return fmt.Errorf("persisting %s : %v", entry.FullPath, err)
  71. }
  72. // println("saved", entry.FullPath, "chunks", len(entry.Chunks))
  73. return nil
  74. }
  75. func (store *LevelDBStore) UpdateEntry(ctx context.Context, entry *filer2.Entry) (err error) {
  76. return store.InsertEntry(ctx, entry)
  77. }
  78. func (store *LevelDBStore) FindEntry(ctx context.Context, fullpath weed_util.FullPath) (entry *filer2.Entry, err error) {
  79. key := genKey(fullpath.DirAndName())
  80. data, err := store.db.Get(key, nil)
  81. if err == leveldb.ErrNotFound {
  82. return nil, filer_pb.ErrNotFound
  83. }
  84. if err != nil {
  85. return nil, fmt.Errorf("get %s : %v", entry.FullPath, err)
  86. }
  87. entry = &filer2.Entry{
  88. FullPath: fullpath,
  89. }
  90. err = entry.DecodeAttributesAndChunks(data)
  91. if err != nil {
  92. return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
  93. }
  94. // println("read", entry.FullPath, "chunks", len(entry.Chunks), "data", len(data), string(data))
  95. return entry, nil
  96. }
  97. func (store *LevelDBStore) DeleteEntry(ctx context.Context, fullpath weed_util.FullPath) (err error) {
  98. key := genKey(fullpath.DirAndName())
  99. err = store.db.Delete(key, nil)
  100. if err != nil {
  101. return fmt.Errorf("delete %s : %v", fullpath, err)
  102. }
  103. return nil
  104. }
  105. func (store *LevelDBStore) DeleteFolderChildren(ctx context.Context, fullpath weed_util.FullPath) (err error) {
  106. batch := new(leveldb.Batch)
  107. directoryPrefix := genDirectoryKeyPrefix(fullpath, "")
  108. iter := store.db.NewIterator(&leveldb_util.Range{Start: directoryPrefix}, nil)
  109. for iter.Next() {
  110. key := iter.Key()
  111. if !bytes.HasPrefix(key, directoryPrefix) {
  112. break
  113. }
  114. fileName := getNameFromKey(key)
  115. if fileName == "" {
  116. continue
  117. }
  118. batch.Delete([]byte(genKey(string(fullpath), fileName)))
  119. }
  120. iter.Release()
  121. err = store.db.Write(batch, nil)
  122. if err != nil {
  123. return fmt.Errorf("delete %s : %v", fullpath, err)
  124. }
  125. return nil
  126. }
  127. func (store *LevelDBStore) ListDirectoryPrefixedEntries(ctx context.Context, fullpath weed_util.FullPath, startFileName string, inclusive bool, limit int, prefix string) (entries []*filer2.Entry, err error) {
  128. count := 0
  129. notPrefixed, err := store.ListDirectoryEntries(ctx, fullpath, startFileName, inclusive, limit)
  130. if err != nil {
  131. return nil, err
  132. }
  133. if prefix == "" {
  134. return notPrefixed, nil
  135. }
  136. for count < limit {
  137. for _, entry := range notPrefixed {
  138. if strings.HasPrefix(entry.Name(), prefix) {
  139. count++
  140. entries = append(entries, entry)
  141. }
  142. }
  143. if count >= limit {
  144. break
  145. }
  146. notPrefixed, err = store.ListDirectoryEntries(ctx, fullpath, startFileName, inclusive, limit)
  147. if err != nil {
  148. return nil, err
  149. }
  150. }
  151. return entries, nil
  152. }
  153. func (store *LevelDBStore) ListDirectoryEntries(ctx context.Context, fullpath weed_util.FullPath, startFileName string, inclusive bool,
  154. limit int) (entries []*filer2.Entry, err error) {
  155. directoryPrefix := genDirectoryKeyPrefix(fullpath, "")
  156. iter := store.db.NewIterator(&leveldb_util.Range{Start: genDirectoryKeyPrefix(fullpath, startFileName)}, nil)
  157. for iter.Next() {
  158. key := iter.Key()
  159. if !bytes.HasPrefix(key, directoryPrefix) {
  160. break
  161. }
  162. fileName := getNameFromKey(key)
  163. if fileName == "" {
  164. continue
  165. }
  166. if fileName == startFileName && !inclusive {
  167. continue
  168. }
  169. limit--
  170. if limit < 0 {
  171. break
  172. }
  173. entry := &filer2.Entry{
  174. FullPath: weed_util.NewFullPath(string(fullpath), fileName),
  175. }
  176. if decodeErr := entry.DecodeAttributesAndChunks(iter.Value()); decodeErr != nil {
  177. err = decodeErr
  178. glog.V(0).Infof("list %s : %v", entry.FullPath, err)
  179. break
  180. }
  181. entries = append(entries, entry)
  182. }
  183. iter.Release()
  184. return entries, err
  185. }
  186. func genKey(dirPath, fileName string) (key []byte) {
  187. key = []byte(dirPath)
  188. key = append(key, DIR_FILE_SEPARATOR)
  189. key = append(key, []byte(fileName)...)
  190. return key
  191. }
  192. func genDirectoryKeyPrefix(fullpath weed_util.FullPath, startFileName string) (keyPrefix []byte) {
  193. keyPrefix = []byte(string(fullpath))
  194. keyPrefix = append(keyPrefix, DIR_FILE_SEPARATOR)
  195. if len(startFileName) > 0 {
  196. keyPrefix = append(keyPrefix, []byte(startFileName)...)
  197. }
  198. return keyPrefix
  199. }
  200. func getNameFromKey(key []byte) string {
  201. sepIndex := len(key) - 1
  202. for sepIndex >= 0 && key[sepIndex] != DIR_FILE_SEPARATOR {
  203. sepIndex--
  204. }
  205. return string(key[sepIndex+1:])
  206. }
  207. func (store *LevelDBStore) Shutdown() {
  208. store.db.Close()
  209. }