2019-05-18 08:33:49 +08:00
|
|
|
package leveldb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"crypto/md5"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2019-07-03 12:25:53 +08:00
|
|
|
"os"
|
2019-05-18 08:33:49 +08:00
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer2"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
weed_util "github.com/chrislusf/seaweedfs/weed/util"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/opt"
|
|
|
|
leveldb_util "github.com/syndtr/goleveldb/leveldb/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
filer2.Stores = append(filer2.Stores, &LevelDB2Store{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type LevelDB2Store struct {
|
2019-07-03 12:25:53 +08:00
|
|
|
dbs []*leveldb.DB
|
|
|
|
dbCount int
|
2019-05-18 08:33:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (store *LevelDB2Store) GetName() string {
|
|
|
|
return "leveldb2"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *LevelDB2Store) Initialize(configuration weed_util.Configuration) (err error) {
|
|
|
|
dir := configuration.GetString("dir")
|
2019-07-03 12:25:53 +08:00
|
|
|
return store.initialize(dir, 8)
|
2019-05-18 08:33:49 +08:00
|
|
|
}
|
|
|
|
|
2019-07-03 12:25:53 +08:00
|
|
|
func (store *LevelDB2Store) initialize(dir string, dbCount int) (err error) {
|
2019-05-18 08:33:49 +08:00
|
|
|
glog.Infof("filer store leveldb2 dir: %s", dir)
|
|
|
|
if err := weed_util.TestFolderWritable(dir); err != nil {
|
|
|
|
return fmt.Errorf("Check Level Folder %s Writable: %s", dir, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := &opt.Options{
|
|
|
|
BlockCacheCapacity: 32 * 1024 * 1024, // default value is 8MiB
|
|
|
|
WriteBuffer: 16 * 1024 * 1024, // default value is 4MiB
|
2019-07-03 12:25:53 +08:00
|
|
|
CompactionTableSizeMultiplier: 4,
|
2019-05-18 08:33:49 +08:00
|
|
|
}
|
|
|
|
|
2019-07-03 12:25:53 +08:00
|
|
|
for d := 0 ; d < dbCount; d++ {
|
|
|
|
dbFolder := fmt.Sprintf("%s/%02d", dir, d)
|
|
|
|
os.MkdirAll(dbFolder, 0755)
|
2019-07-03 12:28:51 +08:00
|
|
|
db, dbErr := leveldb.OpenFile(dbFolder, opts)
|
|
|
|
if dbErr != nil {
|
|
|
|
glog.Errorf("filer store open dir %s: %v", dbFolder, dbErr)
|
2019-07-03 12:25:53 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
store.dbs = append(store.dbs, db)
|
2019-05-18 08:33:49 +08:00
|
|
|
}
|
2019-07-03 12:25:53 +08:00
|
|
|
store.dbCount = dbCount
|
|
|
|
|
2019-05-18 08:33:49 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *LevelDB2Store) BeginTransaction(ctx context.Context) (context.Context, error) {
|
|
|
|
return ctx, nil
|
|
|
|
}
|
|
|
|
func (store *LevelDB2Store) CommitTransaction(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (store *LevelDB2Store) RollbackTransaction(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *LevelDB2Store) InsertEntry(ctx context.Context, entry *filer2.Entry) (err error) {
|
2019-07-03 12:25:53 +08:00
|
|
|
dir, name := entry.DirAndName()
|
|
|
|
key, partitionId := genKey(dir, name, store.dbCount)
|
2019-05-18 08:33:49 +08:00
|
|
|
|
|
|
|
value, err := entry.EncodeAttributesAndChunks()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("encoding %s %+v: %v", entry.FullPath, entry.Attr, err)
|
|
|
|
}
|
|
|
|
|
2019-07-03 12:25:53 +08:00
|
|
|
err = store.dbs[partitionId].Put(key, value, nil)
|
2019-05-18 08:33:49 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("persisting %s : %v", entry.FullPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// println("saved", entry.FullPath, "chunks", len(entry.Chunks))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *LevelDB2Store) UpdateEntry(ctx context.Context, entry *filer2.Entry) (err error) {
|
|
|
|
|
|
|
|
return store.InsertEntry(ctx, entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *LevelDB2Store) FindEntry(ctx context.Context, fullpath filer2.FullPath) (entry *filer2.Entry, err error) {
|
2019-07-03 12:25:53 +08:00
|
|
|
dir, name := fullpath.DirAndName()
|
|
|
|
key, partitionId := genKey(dir, name, store.dbCount)
|
2019-05-18 08:33:49 +08:00
|
|
|
|
2019-07-03 12:25:53 +08:00
|
|
|
data, err := store.dbs[partitionId].Get(key, nil)
|
2019-05-18 08:33:49 +08:00
|
|
|
|
|
|
|
if err == leveldb.ErrNotFound {
|
|
|
|
return nil, filer2.ErrNotFound
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("get %s : %v", entry.FullPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
entry = &filer2.Entry{
|
|
|
|
FullPath: fullpath,
|
|
|
|
}
|
|
|
|
err = entry.DecodeAttributesAndChunks(data)
|
|
|
|
if err != nil {
|
|
|
|
return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
|
|
|
|
}
|
|
|
|
|
2019-07-03 12:33:23 +08:00
|
|
|
// println("read", entry.FullPath, "chunks", len(entry.Chunks), "data", len(data), string(data))
|
2019-05-18 08:33:49 +08:00
|
|
|
|
|
|
|
return entry, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *LevelDB2Store) DeleteEntry(ctx context.Context, fullpath filer2.FullPath) (err error) {
|
2019-07-03 12:25:53 +08:00
|
|
|
dir, name := fullpath.DirAndName()
|
|
|
|
key, partitionId := genKey(dir, name, store.dbCount)
|
2019-05-18 08:33:49 +08:00
|
|
|
|
2019-07-03 12:25:53 +08:00
|
|
|
err = store.dbs[partitionId].Delete(key, nil)
|
2019-05-18 08:33:49 +08:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("delete %s : %v", fullpath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *LevelDB2Store) ListDirectoryEntries(ctx context.Context, fullpath filer2.FullPath, startFileName string, inclusive bool,
|
|
|
|
limit int) (entries []*filer2.Entry, err error) {
|
|
|
|
|
2019-07-03 12:25:53 +08:00
|
|
|
directoryPrefix, partitionId := genDirectoryKeyPrefix(fullpath, "", store.dbCount)
|
2019-05-18 08:33:49 +08:00
|
|
|
|
2019-07-03 12:25:53 +08:00
|
|
|
iter := store.dbs[partitionId].NewIterator(&leveldb_util.Range{Start: directoryPrefix}, nil)
|
2019-05-18 08:33:49 +08:00
|
|
|
for iter.Next() {
|
|
|
|
key := iter.Key()
|
|
|
|
if !bytes.HasPrefix(key, directoryPrefix) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
fileName := getNameFromKey(key)
|
|
|
|
if fileName == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if fileName == startFileName && !inclusive {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
limit--
|
|
|
|
if limit < 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
entry := &filer2.Entry{
|
|
|
|
FullPath: filer2.NewFullPath(string(fullpath), fileName),
|
|
|
|
}
|
2019-07-03 12:25:53 +08:00
|
|
|
|
2019-07-03 12:33:23 +08:00
|
|
|
// println("list", entry.FullPath, "chunks", len(entry.Chunks))
|
2019-07-03 12:25:53 +08:00
|
|
|
|
2019-05-18 08:33:49 +08:00
|
|
|
if decodeErr := entry.DecodeAttributesAndChunks(iter.Value()); decodeErr != nil {
|
|
|
|
err = decodeErr
|
|
|
|
glog.V(0).Infof("list %s : %v", entry.FullPath, err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
entries = append(entries, entry)
|
|
|
|
}
|
|
|
|
iter.Release()
|
|
|
|
|
|
|
|
return entries, err
|
|
|
|
}
|
|
|
|
|
2019-07-03 12:25:53 +08:00
|
|
|
func genKey(dirPath, fileName string, dbCount int) (key []byte, partitionId int) {
|
|
|
|
key, partitionId = hashToBytes(dirPath, dbCount)
|
2019-05-18 08:33:49 +08:00
|
|
|
key = append(key, []byte(fileName)...)
|
2019-07-03 12:25:53 +08:00
|
|
|
return key, partitionId
|
2019-05-18 08:33:49 +08:00
|
|
|
}
|
|
|
|
|
2019-07-03 12:25:53 +08:00
|
|
|
func genDirectoryKeyPrefix(fullpath filer2.FullPath, startFileName string, dbCount int) (keyPrefix []byte, partitionId int) {
|
|
|
|
keyPrefix, partitionId = hashToBytes(string(fullpath), dbCount)
|
2019-05-18 08:33:49 +08:00
|
|
|
if len(startFileName) > 0 {
|
|
|
|
keyPrefix = append(keyPrefix, []byte(startFileName)...)
|
|
|
|
}
|
2019-07-03 12:25:53 +08:00
|
|
|
return keyPrefix, partitionId
|
2019-05-18 08:33:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func getNameFromKey(key []byte) string {
|
|
|
|
|
2019-07-03 12:25:53 +08:00
|
|
|
return string(key[md5.Size:])
|
2019-05-18 08:33:49 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-07-03 12:25:53 +08:00
|
|
|
// hash directory, and use last byte for partitioning
|
|
|
|
func hashToBytes(dir string, dbCount int) ([]byte, int) {
|
2019-05-18 08:33:49 +08:00
|
|
|
h := md5.New()
|
|
|
|
io.WriteString(h, dir)
|
|
|
|
|
|
|
|
b := h.Sum(nil)
|
|
|
|
|
2019-07-03 12:25:53 +08:00
|
|
|
x := b[len(b)-1]
|
|
|
|
|
|
|
|
return b, int(x)%dbCount
|
2019-05-18 08:33:49 +08:00
|
|
|
}
|