Commit 590010d8 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent b9429db4
...@@ -20,6 +20,7 @@ package btree ...@@ -20,6 +20,7 @@ package btree
import ( import (
"context" "context"
"fmt"
"reflect" "reflect"
"sort" "sort"
...@@ -175,24 +176,34 @@ func (b *bucketState) DropState() { ...@@ -175,24 +176,34 @@ func (b *bucketState) DropState() {
} }
// PySetState implements PyStateful to set bucket data from pystate. // PySetState implements PyStateful to set bucket data from pystate.
func (b *bucketState) PySetState(pystate interface{}) error { func (b *bucketState) PySetState(pystate interface{}) (err error) {
defer xerr.Contextf(&err, "bucket(%s): setstate", b.POid())
t, ok := pystate.(pickle.Tuple) t, ok := pystate.(pickle.Tuple)
if !ok || !(1 <= len(t) && len(t) <= 2) { if !ok {
// XXX complain return fmt.Errorf("top: expect (...); got %T", pystate)
}
if !(1 <= len(t) && len(t) <= 2) {
return fmt.Errorf("top: expect [1..2](); got [%d]()", len(t))
} }
// .next present // .next present
if len(t) == 2 { if len(t) == 2 {
next, ok := t[1].(*Bucket) next, ok := t[1].(*Bucket)
if !ok { if !ok {
// XXX return fmt.Errorf(".next must be Bucket; got %T", t[1])
} }
b.next = next b.next = next
} }
// main part // main part
t, ok = t[0].(pickle.Tuple) t, ok = t[0].(pickle.Tuple)
// XXX if !ok || (len(t) % 2 != 0) if !ok {
return fmt.Errorf("data: expect (...); got %T", t[0])
}
if len(t) % 2 != 0 {
return fmt.Errorf("data: expect [%%2](); got [%d]()", len(t))
}
// reset arrays just in case // reset arrays just in case
n := len(t) / 2 n := len(t) / 2
...@@ -205,7 +216,7 @@ func (b *bucketState) PySetState(pystate interface{}) error { ...@@ -205,7 +216,7 @@ func (b *bucketState) PySetState(pystate interface{}) error {
k, ok := xk.(int64) // XXX use KEY XXX -> Xint64 k, ok := xk.(int64) // XXX use KEY XXX -> Xint64
if !ok { if !ok {
// XXX return fmt.Errorf("data: [%d]: key must be integer; got %T", i, xk)
} }
// XXX check keys are sorted? // XXX check keys are sorted?
...@@ -252,7 +263,9 @@ func (t *btreeState) DropState() { ...@@ -252,7 +263,9 @@ func (t *btreeState) DropState() {
} }
// PySetState implements zodb.PyStateful to set btree data from pystate. // PySetState implements zodb.PyStateful to set btree data from pystate.
func (bt *btreeState) PySetState(pystate interface{}) error { func (bt *btreeState) PySetState(pystate interface{}) (err error) {
defer xerr.Contextf(&err, "btree(%s): setstate", bt.POid())
// empty btree // empty btree
if _, ok := pystate.(pickle.None); ok { if _, ok := pystate.(pickle.None); ok {
bt.firstbucket = nil bt.firstbucket = nil
...@@ -261,8 +274,11 @@ func (bt *btreeState) PySetState(pystate interface{}) error { ...@@ -261,8 +274,11 @@ func (bt *btreeState) PySetState(pystate interface{}) error {
} }
t, ok := pystate.(pickle.Tuple) t, ok := pystate.(pickle.Tuple)
if !ok || !(1 <= len(t) && len(t) <= 2) { if !ok {
// XXX return fmt.Errorf("top: expect (...); got %T", pystate)
}
if !(1 <= len(t) && len(t) <= 2) {
return fmt.Errorf("top: expect [1..2](); got [%d]()", len(t))
} }
// btree with 1 child bucket without oid // btree with 1 child bucket without oid
...@@ -270,7 +286,7 @@ func (bt *btreeState) PySetState(pystate interface{}) error { ...@@ -270,7 +286,7 @@ func (bt *btreeState) PySetState(pystate interface{}) error {
bucket := zodb.NewPersistent(reflect.TypeOf(Bucket{}), bt.PJar()).(*Bucket) bucket := zodb.NewPersistent(reflect.TypeOf(Bucket{}), bt.PJar()).(*Bucket)
err := (*bucketState)(bucket).PySetState(t[0]) err := (*bucketState)(bucket).PySetState(t[0])
if err != nil { if err != nil {
// XXX return fmt.Errorf("bucket1: %s", err)
} }
bt.firstbucket = bucket bt.firstbucket = bucket
...@@ -280,13 +296,16 @@ func (bt *btreeState) PySetState(pystate interface{}) error { ...@@ -280,13 +296,16 @@ func (bt *btreeState) PySetState(pystate interface{}) error {
// regular btree // regular btree
t, ok = t[0].(pickle.Tuple) t, ok = t[0].(pickle.Tuple)
if !(ok && len(t) % 2 == 0) { if !ok {
// XXX return fmt.Errorf("data: expect (...); got %T", t[0])
}
if len(t) % 2 != 0 {
return fmt.Errorf("data: expect [%%2](); got [%d]()", len(t))
} }
bt.firstbucket, ok = t[1].(*Bucket) bt.firstbucket, ok = t[1].(*Bucket)
if !ok { if !ok {
// XXX return fmt.Errorf("first bucket: must be Bucket; got %T", t[1])
} }
n := len(t) / 2 n := len(t) / 2
...@@ -297,7 +316,7 @@ func (bt *btreeState) PySetState(pystate interface{}) error { ...@@ -297,7 +316,7 @@ func (bt *btreeState) PySetState(pystate interface{}) error {
// key[0] is unused and not saved // key[0] is unused and not saved
key, ok = t[idx].(int64) // XXX Xint key, ok = t[idx].(int64) // XXX Xint
if ! ok { if ! ok {
// XXX return fmt.Errorf("data: [%d]: key must be integer; got %T", i, t[idx])
} }
idx++ idx++
} }
...@@ -306,7 +325,7 @@ func (bt *btreeState) PySetState(pystate interface{}) error { ...@@ -306,7 +325,7 @@ func (bt *btreeState) PySetState(pystate interface{}) error {
switch child.(type) { switch child.(type) {
default: default:
// XXX return fmt.Errorf("data: [%d]: child must be BTree|Bucket; got %T", i, child)
case *BTree: // ok case *BTree: // ok
case *Bucket: // ok case *Bucket: // ok
......
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