Commit 50f570c8 authored by Kirill Smelkov's avatar Kirill Smelkov

go/zodb/btree: Verify that BTree/Bucket keys come in sorted order

This is one of BTree invariants - check it on load.
parent c1ba9a90
......@@ -215,6 +215,7 @@ func (b *bucketState) PySetState(pystate interface{}) (err error) {
b.keys = make([]KEY, 0, n)
b.values = make([]interface{}, 0, n)
var kprev int64
for i := 0; i < n; i++ {
xk := t[2*i]
v := t[2*i+1]
......@@ -229,7 +230,11 @@ func (b *bucketState) PySetState(pystate interface{}) (err error) {
return fmt.Errorf("data: [%d]: key overflows %T", i, kk)
}
// XXX check keys are sorted?
if i > 0 && !(k > kprev) {
return fmt.Errorf("data: [%d]: key not ↑", i)
}
kprev = k
b.keys = append(b.keys, kk)
b.values = append(b.values, v)
}
......@@ -325,6 +330,7 @@ func (bt *btreeState) PySetState(pystate interface{}) (err error) {
n := (len(t) + 1) / 2
bt.data = make([]_BTreeItem, 0, n)
var kprev int64
for i, idx := 0, 0; i < n; i++ {
key := int64(math.Min<Key>) // KEY(-) (qualifies for )
if i > 0 {
......@@ -343,6 +349,11 @@ func (bt *btreeState) PySetState(pystate interface{}) (err error) {
return fmt.Errorf("data: [%d]: key overflows %T", i, kkey)
}
if i > 1 && !(key > kprev) {
fmt.Errorf("data: [%d]: key not ↑", i)
}
kprev = key
switch child.(type) {
default:
return fmt.Errorf("data: [%d]: child must be BTree|Bucket; got %T", i, child)
......
......@@ -217,6 +217,7 @@ func (b *iobucketState) PySetState(pystate interface{}) (err error) {
b.keys = make([]int32, 0, n)
b.values = make([]interface{}, 0, n)
var kprev int64
for i := 0; i < n; i++ {
xk := t[2*i]
v := t[2*i+1]
......@@ -231,7 +232,11 @@ func (b *iobucketState) PySetState(pystate interface{}) (err error) {
return fmt.Errorf("data: [%d]: key overflows %T", i, kk)
}
// XXX check keys are sorted?
if i > 0 && !(k > kprev) {
return fmt.Errorf("data: [%d]: key not ↑", i)
}
kprev = k
b.keys = append(b.keys, kk)
b.values = append(b.values, v)
}
......@@ -327,6 +332,7 @@ func (bt *iobtreeState) PySetState(pystate interface{}) (err error) {
n := (len(t) + 1) / 2
bt.data = make([]_IOBTreeItem, 0, n)
var kprev int64
for i, idx := 0, 0; i < n; i++ {
key := int64(math.MinInt32) // int32(-∞) (qualifies for ≤)
if i > 0 {
......@@ -345,6 +351,11 @@ func (bt *iobtreeState) PySetState(pystate interface{}) (err error) {
return fmt.Errorf("data: [%d]: key overflows %T", i, kkey)
}
if i > 1 && !(key > kprev) {
fmt.Errorf("data: [%d]: key not ↑", i)
}
kprev = key
switch child.(type) {
default:
return fmt.Errorf("data: [%d]: child must be IOBTree|IOBucket; got %T", i, child)
......
......@@ -217,6 +217,7 @@ func (b *lobucketState) PySetState(pystate interface{}) (err error) {
b.keys = make([]int64, 0, n)
b.values = make([]interface{}, 0, n)
var kprev int64
for i := 0; i < n; i++ {
xk := t[2*i]
v := t[2*i+1]
......@@ -231,7 +232,11 @@ func (b *lobucketState) PySetState(pystate interface{}) (err error) {
return fmt.Errorf("data: [%d]: key overflows %T", i, kk)
}
// XXX check keys are sorted?
if i > 0 && !(k > kprev) {
return fmt.Errorf("data: [%d]: key not ↑", i)
}
kprev = k
b.keys = append(b.keys, kk)
b.values = append(b.values, v)
}
......@@ -327,6 +332,7 @@ func (bt *lobtreeState) PySetState(pystate interface{}) (err error) {
n := (len(t) + 1) / 2
bt.data = make([]_LOBTreeItem, 0, n)
var kprev int64
for i, idx := 0, 0; i < n; i++ {
key := int64(math.MinInt64) // int64(-∞) (qualifies for ≤)
if i > 0 {
......@@ -345,6 +351,11 @@ func (bt *lobtreeState) PySetState(pystate interface{}) (err error) {
return fmt.Errorf("data: [%d]: key overflows %T", i, kkey)
}
if i > 1 && !(key > kprev) {
fmt.Errorf("data: [%d]: key not ↑", i)
}
kprev = key
switch child.(type) {
default:
return fmt.Errorf("data: [%d]: child must be LOBTree|LOBucket; got %T", i, child)
......
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