leveldb_store.go 6.3 KB

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