seaweedfs/weed/filer2/memdb/memdb_store.go

121 lines
2.9 KiB
Go
Raw Normal View History

2018-05-13 04:45:29 +08:00
package memdb
import (
"github.com/chrislusf/seaweedfs/weed/filer2"
"github.com/google/btree"
"strings"
"fmt"
2018-05-13 15:11:26 +08:00
"time"
"github.com/spf13/viper"
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
}
type Entry struct {
*filer2.Entry
}
func (a Entry) Less(b btree.Item) bool {
return strings.Compare(string(a.FullPath), string(b.(Entry).FullPath)) < 0
}
func (filer *MemDbStore) GetName() string {
return "memory"
}
func (filer *MemDbStore) Initialize(viper *viper.Viper) (err error) {
2018-05-13 04:45:29 +08:00
filer.tree = btree.New(8)
return nil
2018-05-13 04:45:29 +08:00
}
func (filer *MemDbStore) InsertEntry(entry *filer2.Entry) (err error) {
// println("inserting", entry.FullPath)
entry.Crtime = time.Now()
2018-05-13 04:45:29 +08:00
filer.tree.ReplaceOrInsert(Entry{entry})
return nil
}
func (filer *MemDbStore) UpdateEntry(entry *filer2.Entry) (err error) {
if _, err = filer.FindEntry(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-13 15:11:26 +08:00
entry.Mtime = time.Now()
filer.tree.ReplaceOrInsert(Entry{entry})
2018-05-13 04:45:29 +08:00
return nil
}
func (filer *MemDbStore) FindEntry(fullpath filer2.FullPath) (entry *filer2.Entry, err error) {
2018-05-13 04:45:29 +08:00
item := filer.tree.Get(Entry{&filer2.Entry{FullPath: fullpath}})
if item == nil {
return nil, nil
2018-05-13 04:45:29 +08:00
}
entry = item.(Entry).Entry
return entry, nil
2018-05-13 04:45:29 +08:00
}
func (filer *MemDbStore) DeleteEntry(fullpath filer2.FullPath) (entry *filer2.Entry, err error) {
item := filer.tree.Delete(Entry{&filer2.Entry{FullPath: fullpath}})
if item == nil {
return nil, nil
}
entry = item.(Entry).Entry
return entry, nil
}
2018-05-14 05:02:29 +08:00
func (filer *MemDbStore) ListDirectoryEntries(fullpath filer2.FullPath, startFileName string, inclusive bool, limit int) (entries []*filer2.Entry, err error) {
startFrom := string(fullpath)
if startFileName != "" {
startFrom = startFrom + "/" + startFileName
}
filer.tree.AscendGreaterOrEqual(Entry{&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-13 04:45:29 +08:00
entry := item.(Entry).Entry
// 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
}