2018-05-13 04:45:29 +08:00
|
|
|
package memdb
|
|
|
|
|
|
|
|
import (
|
2019-03-16 06:55:34 +08:00
|
|
|
"context"
|
2018-05-28 02:52:26 +08:00
|
|
|
"fmt"
|
2018-05-13 04:45:29 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer2"
|
2018-08-20 06:18:37 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2018-05-13 04:45:29 +08:00
|
|
|
"github.com/google/btree"
|
2018-05-28 02:52:26 +08:00
|
|
|
"strings"
|
2018-05-13 04:45:29 +08:00
|
|
|
)
|
|
|
|
|
2018-05-26 18:49:46 +08:00
|
|
|
func init() {
|
|
|
|
filer2.Stores = append(filer2.Stores, &MemDbStore{})
|
|
|
|
}
|
|
|
|
|
2018-05-13 04:45:29 +08:00
|
|
|
type MemDbStore struct {
|
|
|
|
tree *btree.BTree
|
|
|
|
}
|
|
|
|
|
2018-05-26 19:50:55 +08:00
|
|
|
type entryItem struct {
|
2018-05-13 04:45:29 +08:00
|
|
|
*filer2.Entry
|
|
|
|
}
|
|
|
|
|
2018-05-26 19:50:55 +08:00
|
|
|
func (a entryItem) Less(b btree.Item) bool {
|
|
|
|
return strings.Compare(string(a.FullPath), string(b.(entryItem).FullPath)) < 0
|
2018-05-13 04:45:29 +08:00
|
|
|
}
|
|
|
|
|
2018-05-26 19:50:55 +08:00
|
|
|
func (store *MemDbStore) GetName() string {
|
2018-05-26 18:49:46 +08:00
|
|
|
return "memory"
|
|
|
|
}
|
|
|
|
|
2018-08-20 06:17:55 +08:00
|
|
|
func (store *MemDbStore) Initialize(configuration util.Configuration) (err error) {
|
2018-05-26 19:50:55 +08:00
|
|
|
store.tree = btree.New(8)
|
2018-05-26 18:49:46 +08:00
|
|
|
return nil
|
2018-05-13 04:45:29 +08:00
|
|
|
}
|
|
|
|
|
2019-03-16 06:55:34 +08:00
|
|
|
func (store *MemDbStore) InsertEntry(ctx context.Context, entry *filer2.Entry) (err error) {
|
2018-05-13 04:45:29 +08:00
|
|
|
// println("inserting", entry.FullPath)
|
2018-05-26 19:50:55 +08:00
|
|
|
store.tree.ReplaceOrInsert(entryItem{entry})
|
2018-05-13 04:45:29 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-16 06:55:34 +08:00
|
|
|
func (store *MemDbStore) UpdateEntry(ctx context.Context, entry *filer2.Entry) (err error) {
|
|
|
|
if _, err = store.FindEntry(ctx, entry.FullPath); err != nil {
|
2018-05-26 18:49:46 +08:00
|
|
|
return fmt.Errorf("no such file %s : %v", entry.FullPath, err)
|
2018-05-13 04:45:29 +08:00
|
|
|
}
|
2018-05-26 19:50:55 +08:00
|
|
|
store.tree.ReplaceOrInsert(entryItem{entry})
|
2018-05-13 04:45:29 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-16 06:55:34 +08:00
|
|
|
func (store *MemDbStore) FindEntry(ctx context.Context, fullpath filer2.FullPath) (entry *filer2.Entry, err error) {
|
2018-05-26 19:50:55 +08:00
|
|
|
item := store.tree.Get(entryItem{&filer2.Entry{FullPath: fullpath}})
|
2018-05-13 04:45:29 +08:00
|
|
|
if item == nil {
|
2018-07-20 17:14:18 +08:00
|
|
|
return nil, filer2.ErrNotFound
|
2018-05-13 04:45:29 +08:00
|
|
|
}
|
2018-05-26 19:50:55 +08:00
|
|
|
entry = item.(entryItem).Entry
|
2018-05-26 18:49:46 +08:00
|
|
|
return entry, nil
|
2018-05-13 04:45:29 +08:00
|
|
|
}
|
|
|
|
|
2019-03-16 06:55:34 +08:00
|
|
|
func (store *MemDbStore) DeleteEntry(ctx context.Context, fullpath filer2.FullPath) (err error) {
|
2018-05-31 11:24:57 +08:00
|
|
|
store.tree.Delete(entryItem{&filer2.Entry{FullPath: fullpath}})
|
|
|
|
return nil
|
2018-05-13 04:45:29 +08:00
|
|
|
}
|
|
|
|
|
2019-03-16 06:55:34 +08:00
|
|
|
func (store *MemDbStore) ListDirectoryEntries(ctx context.Context, fullpath filer2.FullPath, startFileName string, inclusive bool, limit int) (entries []*filer2.Entry, err error) {
|
2018-05-14 05:02:29 +08:00
|
|
|
|
|
|
|
startFrom := string(fullpath)
|
|
|
|
if startFileName != "" {
|
|
|
|
startFrom = startFrom + "/" + startFileName
|
|
|
|
}
|
|
|
|
|
2018-05-26 19:50:55 +08:00
|
|
|
store.tree.AscendGreaterOrEqual(entryItem{&filer2.Entry{FullPath: filer2.FullPath(startFrom)}},
|
2018-05-13 04:45:29 +08:00
|
|
|
func(item btree.Item) bool {
|
2018-05-14 05:02:29 +08:00
|
|
|
if limit <= 0 {
|
|
|
|
return false
|
|
|
|
}
|
2018-05-26 19:50:55 +08:00
|
|
|
entry := item.(entryItem).Entry
|
2018-05-13 04:45:29 +08:00
|
|
|
// println("checking", entry.FullPath)
|
2018-05-20 03:07:15 +08:00
|
|
|
|
2018-05-13 04:45:29 +08:00
|
|
|
if entry.FullPath == fullpath {
|
|
|
|
// skipping the current directory
|
|
|
|
// println("skipping the folder", entry.FullPath)
|
|
|
|
return true
|
|
|
|
}
|
2018-05-20 03:07:15 +08:00
|
|
|
|
2018-05-14 05:02:29 +08:00
|
|
|
dir, name := entry.FullPath.DirAndName()
|
|
|
|
if name == startFileName {
|
|
|
|
if inclusive {
|
|
|
|
limit--
|
|
|
|
entries = append(entries, entry)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2018-05-20 03:07:15 +08:00
|
|
|
|
|
|
|
// only iterate the same prefix
|
|
|
|
if !strings.HasPrefix(string(entry.FullPath), string(fullpath)) {
|
2018-05-13 04:45:29 +08:00
|
|
|
// println("breaking from", entry.FullPath)
|
|
|
|
return false
|
|
|
|
}
|
2018-05-20 03:07:15 +08:00
|
|
|
|
2018-05-13 04:45:29 +08:00
|
|
|
if dir != string(fullpath) {
|
|
|
|
// this could be items in deeper directories
|
|
|
|
// println("skipping deeper folder", entry.FullPath)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// now process the directory items
|
|
|
|
// println("adding entry", entry.FullPath)
|
2018-05-14 05:02:29 +08:00
|
|
|
limit--
|
2018-05-13 04:45:29 +08:00
|
|
|
entries = append(entries, entry)
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
)
|
|
|
|
return entries, nil
|
|
|
|
}
|