seaweedfs/weed/filer2/memdb/memdb_store.go

133 lines
3.4 KiB
Go
Raw Normal View History

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"
"sync"
2018-05-12 20:45:29 +00:00
)
func init() {
filer2.Stores = append(filer2.Stores, &MemDbStore{})
}
2018-05-12 20:45:29 +00:00
type MemDbStore struct {
tree *btree.BTree
treeLock sync.Mutex
2018-05-12 20:45:29 +00:00
}
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 {
return "memory"
}
func (store *MemDbStore) Initialize(configuration util.Configuration) (err error) {
2018-05-26 11:50:55 +00:00
store.tree = btree.New(8)
return nil
2018-05-12 20:45:29 +00:00
}
2019-03-31 06:09:16 +00:00
func (store *MemDbStore) BeginTransaction(ctx context.Context) (context.Context, error) {
return ctx, nil
}
2019-03-31 06:09:16 +00:00
func (store *MemDbStore) CommitTransaction(ctx context.Context) error {
return nil
}
2019-03-31 06:09:16 +00:00
func (store *MemDbStore) RollbackTransaction(ctx context.Context) error {
return nil
}
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)
store.treeLock.Lock()
2018-05-26 11:50:55 +00:00
store.tree.ReplaceOrInsert(entryItem{entry})
store.treeLock.Unlock()
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 {
return fmt.Errorf("no such file %s : %v", entry.FullPath, err)
2018-05-12 20:45:29 +00:00
}
store.treeLock.Lock()
2018-05-26 11:50:55 +00:00
store.tree.ReplaceOrInsert(entryItem{entry})
store.treeLock.Unlock()
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 {
return nil, filer2.ErrNotFound
2018-05-12 20:45:29 +00:00
}
2018-05-26 11:50:55 +00:00
entry = item.(entryItem).Entry
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) {
store.treeLock.Lock()
store.tree.Delete(entryItem{&filer2.Entry{FullPath: fullpath}})
store.treeLock.Unlock()
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-12 20:45:29 +00:00
if entry.FullPath == fullpath {
// skipping the current directory
// println("skipping the folder", entry.FullPath)
return true
}
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
}
// 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-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
}