2018-05-26 10:49:46 +00:00
|
|
|
package leveldb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-03-15 22:55:34 +00:00
|
|
|
"context"
|
2018-05-27 18:52:26 +00:00
|
|
|
"fmt"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
2020-08-31 16:35:16 +00:00
|
|
|
leveldb_errors "github.com/syndtr/goleveldb/leveldb/errors"
|
2019-05-18 00:33:49 +00:00
|
|
|
"github.com/syndtr/goleveldb/leveldb/opt"
|
2018-05-27 18:52:26 +00:00
|
|
|
leveldb_util "github.com/syndtr/goleveldb/leveldb/util"
|
2021-03-14 20:20:14 +00:00
|
|
|
"os"
|
2019-12-13 08:23:05 +00:00
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer"
|
2019-12-13 08:23:05 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2020-03-08 01:01:39 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
2019-12-13 08:23:05 +00:00
|
|
|
weed_util "github.com/chrislusf/seaweedfs/weed/util"
|
2018-05-26 10:49:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
DIR_FILE_SEPARATOR = byte(0x00)
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2020-09-01 07:21:19 +00:00
|
|
|
filer.Stores = append(filer.Stores, &LevelDBStore{})
|
2018-05-26 10:49:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type LevelDBStore struct {
|
|
|
|
db *leveldb.DB
|
|
|
|
}
|
|
|
|
|
2018-05-26 11:50:55 +00:00
|
|
|
func (store *LevelDBStore) GetName() string {
|
2018-05-26 10:49:46 +00:00
|
|
|
return "leveldb"
|
|
|
|
}
|
|
|
|
|
2020-01-29 17:09:55 +00:00
|
|
|
func (store *LevelDBStore) Initialize(configuration weed_util.Configuration, prefix string) (err error) {
|
|
|
|
dir := configuration.GetString(prefix + "dir")
|
2018-05-26 11:50:55 +00:00
|
|
|
return store.initialize(dir)
|
2018-05-26 10:49:46 +00:00
|
|
|
}
|
|
|
|
|
2018-05-26 11:50:55 +00:00
|
|
|
func (store *LevelDBStore) initialize(dir string) (err error) {
|
2018-12-06 07:24:25 +00:00
|
|
|
glog.Infof("filer store dir: %s", dir)
|
2021-03-14 20:20:14 +00:00
|
|
|
os.MkdirAll(dir, 0755)
|
2018-05-26 10:49:46 +00:00
|
|
|
if err := weed_util.TestFolderWritable(dir); err != nil {
|
|
|
|
return fmt.Errorf("Check Level Folder %s Writable: %s", dir, err)
|
|
|
|
}
|
|
|
|
|
2019-05-18 00:33:49 +00:00
|
|
|
opts := &opt.Options{
|
|
|
|
BlockCacheCapacity: 32 * 1024 * 1024, // default value is 8MiB
|
|
|
|
WriteBuffer: 16 * 1024 * 1024, // default value is 4MiB
|
2019-05-18 01:14:25 +00:00
|
|
|
CompactionTableSizeMultiplier: 10,
|
2019-05-18 00:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if store.db, err = leveldb.OpenFile(dir, opts); err != nil {
|
2020-08-31 16:35:16 +00:00
|
|
|
if leveldb_errors.IsCorrupted(err) {
|
2020-05-22 17:54:42 +00:00
|
|
|
store.db, err = leveldb.RecoverFile(dir, opts)
|
|
|
|
}
|
2020-05-26 07:03:44 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.Infof("filer store open dir %s: %v", dir, err)
|
|
|
|
return
|
|
|
|
}
|
2018-05-26 10:49:46 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-31 06:09:16 +00:00
|
|
|
func (store *LevelDBStore) BeginTransaction(ctx context.Context) (context.Context, error) {
|
2019-03-31 06:08:29 +00:00
|
|
|
return ctx, nil
|
|
|
|
}
|
2019-03-31 06:09:16 +00:00
|
|
|
func (store *LevelDBStore) CommitTransaction(ctx context.Context) error {
|
2019-03-31 06:08:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-03-31 06:09:16 +00:00
|
|
|
func (store *LevelDBStore) RollbackTransaction(ctx context.Context) error {
|
2019-03-31 06:08:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
func (store *LevelDBStore) InsertEntry(ctx context.Context, entry *filer.Entry) (err error) {
|
2018-05-26 10:49:46 +00:00
|
|
|
key := genKey(entry.DirAndName())
|
|
|
|
|
|
|
|
value, err := entry.EncodeAttributesAndChunks()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("encoding %s %+v: %v", entry.FullPath, entry.Attr, err)
|
|
|
|
}
|
|
|
|
|
2020-09-03 18:00:20 +00:00
|
|
|
if len(entry.Chunks) > 50 {
|
|
|
|
value = weed_util.MaybeGzipData(value)
|
|
|
|
}
|
|
|
|
|
2018-05-26 10:49:46 +00:00
|
|
|
err = store.db.Put(key, value, nil)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("persisting %s : %v", entry.FullPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// println("saved", entry.FullPath, "chunks", len(entry.Chunks))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
func (store *LevelDBStore) UpdateEntry(ctx context.Context, entry *filer.Entry) (err error) {
|
2018-05-26 10:49:46 +00:00
|
|
|
|
2019-03-15 22:55:34 +00:00
|
|
|
return store.InsertEntry(ctx, entry)
|
2018-05-26 10:49:46 +00:00
|
|
|
}
|
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
func (store *LevelDBStore) FindEntry(ctx context.Context, fullpath weed_util.FullPath) (entry *filer.Entry, err error) {
|
2018-05-26 10:49:46 +00:00
|
|
|
key := genKey(fullpath.DirAndName())
|
|
|
|
|
|
|
|
data, err := store.db.Get(key, nil)
|
|
|
|
|
|
|
|
if err == leveldb.ErrNotFound {
|
2020-03-08 01:01:39 +00:00
|
|
|
return nil, filer_pb.ErrNotFound
|
2018-05-26 10:49:46 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2021-01-12 10:28:57 +00:00
|
|
|
return nil, fmt.Errorf("get %s : %v", fullpath, err)
|
2018-05-26 10:49:46 +00:00
|
|
|
}
|
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
entry = &filer.Entry{
|
2018-05-26 10:49:46 +00:00
|
|
|
FullPath: fullpath,
|
|
|
|
}
|
2020-09-03 18:00:20 +00:00
|
|
|
err = entry.DecodeAttributesAndChunks(weed_util.MaybeDecompressData((data)))
|
2018-05-26 10:49:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// println("read", entry.FullPath, "chunks", len(entry.Chunks), "data", len(data), string(data))
|
|
|
|
|
|
|
|
return entry, nil
|
|
|
|
}
|
|
|
|
|
2020-03-23 07:01:34 +00:00
|
|
|
func (store *LevelDBStore) DeleteEntry(ctx context.Context, fullpath weed_util.FullPath) (err error) {
|
2018-05-26 10:49:46 +00:00
|
|
|
key := genKey(fullpath.DirAndName())
|
|
|
|
|
|
|
|
err = store.db.Delete(key, nil)
|
|
|
|
if err != nil {
|
2018-05-31 03:24:57 +00:00
|
|
|
return fmt.Errorf("delete %s : %v", fullpath, err)
|
2018-05-26 10:49:46 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 03:24:57 +00:00
|
|
|
return nil
|
2018-05-26 10:49:46 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 15:23:20 +00:00
|
|
|
func (store *LevelDBStore) DeleteFolderChildren(ctx context.Context, fullpath weed_util.FullPath) (err error) {
|
2019-12-13 08:23:05 +00:00
|
|
|
|
|
|
|
batch := new(leveldb.Batch)
|
|
|
|
|
|
|
|
directoryPrefix := genDirectoryKeyPrefix(fullpath, "")
|
|
|
|
iter := store.db.NewIterator(&leveldb_util.Range{Start: directoryPrefix}, nil)
|
|
|
|
for iter.Next() {
|
|
|
|
key := iter.Key()
|
|
|
|
if !bytes.HasPrefix(key, directoryPrefix) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
fileName := getNameFromKey(key)
|
|
|
|
if fileName == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
batch.Delete([]byte(genKey(string(fullpath), fileName)))
|
|
|
|
}
|
|
|
|
iter.Release()
|
|
|
|
|
|
|
|
err = store.db.Write(batch, nil)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("delete %s : %v", fullpath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-16 07:56:24 +00:00
|
|
|
func (store *LevelDBStore) ListDirectoryEntries(ctx context.Context, dirPath weed_util.FullPath, startFileName string, includeStartFile bool, limit int64, eachEntryFunc filer.ListEachEntryFunc) (lastFileName string, err error) {
|
|
|
|
return store.ListDirectoryPrefixedEntries(ctx, dirPath, startFileName, includeStartFile, limit, "", eachEntryFunc)
|
2020-11-23 05:10:41 +00:00
|
|
|
}
|
2018-05-26 10:49:46 +00:00
|
|
|
|
2021-01-16 07:56:24 +00:00
|
|
|
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) {
|
2020-11-23 05:10:41 +00:00
|
|
|
|
2021-01-15 07:10:37 +00:00
|
|
|
directoryPrefix := genDirectoryKeyPrefix(dirPath, prefix)
|
2021-01-02 04:23:23 +00:00
|
|
|
lastFileStart := directoryPrefix
|
|
|
|
if startFileName != "" {
|
2021-01-15 07:10:37 +00:00
|
|
|
lastFileStart = genDirectoryKeyPrefix(dirPath, startFileName)
|
2021-01-02 04:23:23 +00:00
|
|
|
}
|
2018-05-26 10:49:46 +00:00
|
|
|
|
2021-01-02 04:23:23 +00:00
|
|
|
iter := store.db.NewIterator(&leveldb_util.Range{Start: lastFileStart}, nil)
|
2018-05-26 10:49:46 +00:00
|
|
|
for iter.Next() {
|
|
|
|
key := iter.Key()
|
|
|
|
if !bytes.HasPrefix(key, directoryPrefix) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
fileName := getNameFromKey(key)
|
|
|
|
if fileName == "" {
|
|
|
|
continue
|
|
|
|
}
|
2021-01-15 07:10:37 +00:00
|
|
|
if fileName == startFileName && !includeStartFile {
|
2018-05-26 10:49:46 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
limit--
|
|
|
|
if limit < 0 {
|
|
|
|
break
|
|
|
|
}
|
2021-02-01 04:11:44 +00:00
|
|
|
lastFileName = fileName
|
2020-09-01 07:21:19 +00:00
|
|
|
entry := &filer.Entry{
|
2021-01-15 07:10:37 +00:00
|
|
|
FullPath: weed_util.NewFullPath(string(dirPath), fileName),
|
2018-05-26 10:49:46 +00:00
|
|
|
}
|
2020-09-03 18:00:20 +00:00
|
|
|
if decodeErr := entry.DecodeAttributesAndChunks(weed_util.MaybeDecompressData(iter.Value())); decodeErr != nil {
|
2018-05-26 10:49:46 +00:00
|
|
|
err = decodeErr
|
|
|
|
glog.V(0).Infof("list %s : %v", entry.FullPath, err)
|
|
|
|
break
|
|
|
|
}
|
2021-01-16 07:56:24 +00:00
|
|
|
if !eachEntryFunc(entry) {
|
|
|
|
break
|
|
|
|
}
|
2018-05-26 10:49:46 +00:00
|
|
|
}
|
|
|
|
iter.Release()
|
|
|
|
|
2021-01-16 07:56:24 +00:00
|
|
|
return lastFileName, err
|
2018-05-26 10:49:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func genKey(dirPath, fileName string) (key []byte) {
|
|
|
|
key = []byte(dirPath)
|
|
|
|
key = append(key, DIR_FILE_SEPARATOR)
|
|
|
|
key = append(key, []byte(fileName)...)
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
2020-03-23 07:01:34 +00:00
|
|
|
func genDirectoryKeyPrefix(fullpath weed_util.FullPath, startFileName string) (keyPrefix []byte) {
|
2018-05-26 10:49:46 +00:00
|
|
|
keyPrefix = []byte(string(fullpath))
|
|
|
|
keyPrefix = append(keyPrefix, DIR_FILE_SEPARATOR)
|
|
|
|
if len(startFileName) > 0 {
|
|
|
|
keyPrefix = append(keyPrefix, []byte(startFileName)...)
|
|
|
|
}
|
|
|
|
return keyPrefix
|
|
|
|
}
|
|
|
|
|
2018-05-27 18:52:26 +00:00
|
|
|
func getNameFromKey(key []byte) string {
|
2018-05-26 10:49:46 +00:00
|
|
|
|
|
|
|
sepIndex := len(key) - 1
|
|
|
|
for sepIndex >= 0 && key[sepIndex] != DIR_FILE_SEPARATOR {
|
|
|
|
sepIndex--
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(key[sepIndex+1:])
|
|
|
|
|
|
|
|
}
|
2020-03-15 03:30:26 +00:00
|
|
|
|
|
|
|
func (store *LevelDBStore) Shutdown() {
|
|
|
|
store.db.Close()
|
|
|
|
}
|