Commit db65b87d authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 6c37f3b3
...@@ -18,6 +18,7 @@ import ( ...@@ -18,6 +18,7 @@ import (
"context" "context"
"sort" "sort"
"lab.nexedi.com/kirr/go123/xerr"
//"lab.nexedi.com/kirr/neo/go/zodb" //"lab.nexedi.com/kirr/neo/go/zodb"
pickle "github.com/kisielk/og-rek" pickle "github.com/kisielk/og-rek"
) )
...@@ -73,9 +74,12 @@ type ZBTree struct { ...@@ -73,9 +74,12 @@ type ZBTree struct {
// Get searches BTree by key. // Get searches BTree by key.
// //
// It loads intermediate BTree nodes from database on demand as needed. // It loads intermediate BTree nodes from database on demand as needed.
// XXX ok -> ErrKeyNotFound? func (t *ZBTree) Get(ctx context.Context, key KEY) (_ interface{}, _ bool, err error) {
func (t *ZBTree) Get(ctx context.Context, key KEY) (interface{}, bool, error) { defer xerr.Contextf(&err, "btree(%s): get %s") // XXX + url?
t.PActivate(ctx) // XXX err err = t.PActivate(ctx)
if err != nil {
return nil, false, err
}
if len(t.data) == 0 { if len(t.data) == 0 {
// empty btree // empty btree
...@@ -97,24 +101,35 @@ func (t *ZBTree) Get(ctx context.Context, key KEY) (interface{}, bool, error) { ...@@ -97,24 +101,35 @@ func (t *ZBTree) Get(ctx context.Context, key KEY) (interface{}, bool, error) {
case *ZBTree: case *ZBTree:
t.PDeactivate() t.PDeactivate()
t = child t = child
t.PActivate(ctx) // XXX err err = t.PActivate(ctx)
if err != nil {
return nil, false, err
}
case *ZBucket: case *ZBucket:
t.PDeactivate() t.PDeactivate()
child.PActivate(ctx) // XXX err return child.Get(ctx, key)
v, ok := child.get(key)
child.PDeactivate()
return v, ok, nil
} }
} }
} }
// Get searches Bucket by key.
func (b *ZBucket) Get(ctx context.Context, key KEY) (_ interface{}, _ bool, err error) {
defer xerr.Contextf(&err, "bucket(%s): get %s") // XXX + url?
err = b.PActivate(ctx)
if err != nil {
return nil, false, err
}
v, ok := b.get(key)
b.PDeactivate()
return v, ok, nil
}
// get searches Bucket by key. // get searches Bucket by key.
// //
// no loading from database is done. The bucket must be already activated. // no loading from database is done. The bucket must be already activated.
func (b *ZBucket) get(key KEY) (interface{}, bool) { func (b *ZBucket) get(key KEY) (interface{}, bool) {
// XXX b.PActivate ? XXX better caller? (or inside if get -> Get)
// search i: K(i-1) < k ≤ K(i) ; K(-1) = -∞, K(len) = +∞ // search i: K(i-1) < k ≤ K(i) ; K(-1) = -∞, K(len) = +∞
i := sort.Search(len(b.keys), func(i int) bool { i := sort.Search(len(b.keys), func(i int) bool {
return key <= b.keys[i] return key <= b.keys[i]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment