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