2018-05-12 20:45:29 +00:00
|
|
|
package memdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer2"
|
|
|
|
"github.com/google/btree"
|
|
|
|
"strings"
|
|
|
|
"fmt"
|
2018-05-26 10:49:46 +00:00
|
|
|
"github.com/spf13/viper"
|
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-05-26 11:50:55 +00:00
|
|
|
func (store *MemDbStore) Initialize(viper *viper.Viper) (err error) {
|
|
|
|
store.tree = btree.New(8)
|
2018-05-26 10:49:46 +00:00
|
|
|
return nil
|
2018-05-12 20:45:29 +00:00
|
|
|
}
|
|
|
|
|
2018-05-26 11:50:55 +00:00
|
|
|
func (store *MemDbStore) InsertEntry(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
|
|
|
|
}
|
|
|
|
|
2018-05-26 11:50:55 +00:00
|
|
|
func (store *MemDbStore) UpdateEntry(entry *filer2.Entry) (err error) {
|
|
|
|
if _, err = store.FindEntry(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
|
|
|
|
}
|
|
|
|
|
2018-05-26 11:50:55 +00:00
|
|
|
func (store *MemDbStore) FindEntry(fullpath filer2.FullPath) (entry *filer2.Entry, err error) {
|
|
|
|
item := store.tree.Get(entryItem{&filer2.Entry{FullPath: fullpath}})
|
2018-05-12 20:45:29 +00:00
|
|
|
if item == nil {
|
2018-05-26 10:49:46 +00:00
|
|
|
return nil, nil
|
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
|
|
|
}
|
|
|
|
|
2018-05-26 11:50:55 +00:00
|
|
|
func (store *MemDbStore) DeleteEntry(fullpath filer2.FullPath) (entry *filer2.Entry, err error) {
|
|
|
|
item := store.tree.Delete(entryItem{&filer2.Entry{FullPath: fullpath}})
|
2018-05-12 20:45:29 +00:00
|
|
|
if item == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2018-05-26 11:50:55 +00:00
|
|
|
entry = item.(entryItem).Entry
|
2018-05-12 20:45:29 +00:00
|
|
|
return entry, nil
|
|
|
|
}
|
|
|
|
|
2018-05-26 11:50:55 +00:00
|
|
|
func (store *MemDbStore) ListDirectoryEntries(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
|
|
|
|
}
|