seaweedfs/weed/filer2/memdb/memdb_store.go

125 lines
3.2 KiB
Go
Raw Normal View History

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
)
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 {
return "memory"
}
func (store *MemDbStore) Initialize(configuration util.Configuration) (err error) {
2018-05-26 19:50:55 +08:00
store.tree = btree.New(8)
return nil
2018-05-13 04:45:29 +08:00
}
func (store *MemDbStore) BeginTransaction(ctx context.Context) (context.Context, error){
return ctx, nil
}
func (store *MemDbStore) CommitTransaction(ctx context.Context) error{
return nil
}
func (store *MemDbStore) RollbackTransaction(ctx context.Context) error{
return nil
}
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 {
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 {
return nil, filer2.ErrNotFound
2018-05-13 04:45:29 +08:00
}
2018-05-26 19:50:55 +08:00
entry = item.(entryItem).Entry
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) {
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-13 04:45:29 +08:00
if entry.FullPath == fullpath {
// skipping the current directory
// println("skipping the folder", entry.FullPath)
return true
}
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
}
// 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-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
}