Commit 8b85f11d authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 767d6bf7
...@@ -103,7 +103,7 @@ type ( ...@@ -103,7 +103,7 @@ type (
// information about last data page which Set/Put/Delete modified // information about last data page which Set/Put/Delete modified
hitD *d // data page & pos of last write access hitD *d // data page & pos of last write access
hitDi int hitDi int
hitP *x // parent & pos for data page (= -1 if no parent) hitP *x // parent & pos for data page (= nil/-1 if no parent)
hitPi int hitPi int
hitKmin xkey // hitD allowed key range is [hitKmin, hitKmax) hitKmin xkey // hitD allowed key range is [hitKmin, hitKmax)
hitKmax xkey hitKmax xkey
...@@ -233,7 +233,7 @@ func (t *Tree) Clear() { ...@@ -233,7 +233,7 @@ func (t *Tree) Clear() {
clr(t.r) clr(t.r)
t.c, t.first, t.last, t.r = 0, nil, nil, nil t.c, t.first, t.last, t.r = 0, nil, nil, nil
t.hitD, t.hitDi, t.hitP, t.hitPi = nil, -1, nil, -1 t.hitD, t.hitDi, t.hitP, t.hitPi = nil, -1, nil, -1
t.hitKmin, t.hitKmax, t.hitPKmax = xkey{}, xkey{}, xkey{} t.hitKmin, t.hitKmax, t.hitPKmin, t.hitPKmax = xkey{}, xkey{}, xkey{}, xkey{}
t.ver++ t.ver++
} }
...@@ -308,7 +308,7 @@ func (t *Tree) catX(p, q, r *x, pi int) { ...@@ -308,7 +308,7 @@ func (t *Tree) catX(p, q, r *x, pi int) {
// Delete removes the k's KV pair, if it exists, in which case Delete returns // Delete removes the k's KV pair, if it exists, in which case Delete returns
// true. // true.
func (t *Tree) Delete(k interface{} /*K*/) (ok bool) { func (t *Tree) Delete(k interface{} /*K*/) (ok bool) {
//dbg("--- PRE Delete(%v)\t; %v @%d, [%v, %v) PKmax: %v\n%s", k, t.hitD, t.hitDi, t.hitKmin, t.hitKmax, t.hitPKmax, t.dump()) //dbg("--- PRE Delete(%v)\t; %v @%d, [%v, %v) pk: [%v, %v)\n%s", k, t.hitD, t.hitDi, t.hitKmin, t.hitKmax, t.hitPKmin, t.hitPKmax, t.dump())
defer t.checkHit(k, opDel) defer t.checkHit(k, opDel)
//defer func() { //defer func() {
// dbg("--- POST\n%s\n====\n", t.dump()) // dbg("--- POST\n%s\n====\n", t.dump())
...@@ -317,36 +317,33 @@ func (t *Tree) Delete(k interface{} /*K*/) (ok bool) { ...@@ -317,36 +317,33 @@ func (t *Tree) Delete(k interface{} /*K*/) (ok bool) {
// check if we can do the delete nearby previous change // check if we can do the delete nearby previous change
i, ok := t.hitFind(k) i, ok := t.hitFind(k)
if i >= 0 { if i >= 0 {
//dbg("hit found\t-> %d, %v", i, ok)
dd := t.hitD dd := t.hitD
switch { switch {
case !ok: case !ok:
//dbg("ok'") // tried to delete last or element past max k in hitD
if i >= dd.c { if i >= dd.c {
// tried to delete element past max k in hitD
i-- i--
} }
t.hitDi = i t.hitDi = i
return false return false
case dd.c > kd: case dd.c > kd:
//dbg("extract'")
t.extract(dd, i) t.extract(dd, i)
if t.hitDi >= dd.c { if t.hitDi >= dd.c {
t.hitDi-- t.hitDi--
} }
return true return true
// here: need to extract / underflow but check to not underflow too much XXX // here: need to extract / underflow but we have to check: if underflowing
// (no underflowX must be called ?) // would cause upper level underflow (underflowX) -> we cannot extract /
// underflow here - need to do the usual scan from root to underflow index pages.
default: default:
p, pi := t.hitP, t.hitPi p, pi := t.hitP, t.hitPi
if p != nil && p.c < kx && p != t.r { if p != nil && p.c < kx && p != t.r {
break break
} }
//dbg("underflow'")
t.extract(dd, i) t.extract(dd, i)
// XXX recheck // XXX recheck
...@@ -369,18 +366,11 @@ func (t *Tree) Delete(k interface{} /*K*/) (ok bool) { ...@@ -369,18 +366,11 @@ func (t *Tree) Delete(k interface{} /*K*/) (ok bool) {
var p *x var p *x
q := t.r q := t.r
if q == nil { if q == nil {
// XXX update hit ?
return false return false
} }
// XXX recheck what is better - directly update t.hitK* or copy t.hitKmin, t.hitKmax = xkey{}, xkey{} // initially [-∞, +∞)
// NOTE having t.hitK* up to data is handy for underflowX t.hitPKmin, t.hitPKmax = xkey{}, xkey{}
//var hitKmin, hitKmax xkey // initially [-∞, +∞)
//var hitPKmax xkey // Kmax for whole hitP
t.hitKmin = xkey{} // initially [-∞, +∞)
t.hitKmax = xkey{}
t.hitPKmin = xkey{}
t.hitPKmax = xkey{}
for { for {
i, ok := t.find(q, k) i, ok := t.find(q, k)
...@@ -391,7 +381,6 @@ func (t *Tree) Delete(k interface{} /*K*/) (ok bool) { ...@@ -391,7 +381,6 @@ func (t *Tree) Delete(k interface{} /*K*/) (ok bool) {
} }
if x.c < kx && q != t.r { if x.c < kx && q != t.r {
//dbg("underflowX")
x, i = t.underflowX(p, x, pi, i) x, i = t.underflowX(p, x, pi, i)
} }
...@@ -403,46 +392,35 @@ func (t *Tree) Delete(k interface{} /*K*/) (ok bool) { ...@@ -403,46 +392,35 @@ func (t *Tree) Delete(k interface{} /*K*/) (ok bool) {
q = x.x[pi].ch q = x.x[pi].ch
if pi > 0 { if pi > 0 {
//hitKmin.set(p.x[pi-1].k)
t.hitKmin.set(p.x[pi-1].k) t.hitKmin.set(p.x[pi-1].k)
//dbg("hitKmin: %v", t.hitKmin)
} }
if pi < p.c { // == p.c means ∞ if pi < p.c { // k=∞ @ pi=p.c
//hitKmax.set(p.x[pi].k)
t.hitKmax.set(p.x[pi].k) t.hitKmax.set(p.x[pi].k)
//dbg("hitKmax: %v", t.hitKmax)
} }
case *d: case *d:
// data page found - perform the delete // data page found - perform the delete
t.hitP = p t.hitP = p
t.hitPi = pi t.hitPi = pi
//t.hitKmin = hitKmin
//t.hitKmax = hitKmax
//t.hitPKmax = hitPKmax
if !ok { if !ok {
//dbg("!ok")
t.hitD = x t.hitD = x
if i >= x.c { if i >= x.c {
// tried to delete element past max k in hitD // tried to delete last or element past max k in hitD
i-- i--
} }
t.hitDi = i t.hitDi = i
return false return false
} }
//dbg("extract")
t.extract(x, i) t.extract(x, i)
if x.c < kd { if x.c < kd {
if q != t.r { if q != t.r {
// NOTE overflow will correct hit Kmin, Kmax, P and Pi as needed // NOTE overflow will correct hit Kmin, Kmax, P and Pi as needed
//dbg("underflow")
t.underflow(p, x, pi) t.underflow(p, x, pi)
} else if t.c == 0 { } else if t.c == 0 {
//dbg("clear")
t.Clear() t.Clear()
} }
} }
...@@ -464,11 +442,13 @@ func (t *Tree) extract(q *d, i int) { // (r interface{} /*V*/) { ...@@ -464,11 +442,13 @@ func (t *Tree) extract(q *d, i int) { // (r interface{} /*V*/) {
} }
q.d[q.c] = zde // GC q.d[q.c] = zde // GC
t.c-- t.c--
t.hitD = q t.hitD = q
//if i >= q.c { // NOTE extract users - in the end - must decrement t.hitDi if t.hitDi == t.hitD.c
// i-- // we are not doing it right here because unaltered position is
//} // required in case merging a right sibling data page will be needed.
t.hitDi = i t.hitDi = i
return return
} }
...@@ -725,7 +705,7 @@ func (t *Tree) SeekLast() (e *Enumerator, err error) { ...@@ -725,7 +705,7 @@ func (t *Tree) SeekLast() (e *Enumerator, err error) {
// Set sets the value associated with k. // Set sets the value associated with k.
func (t *Tree) Set(k interface{} /*K*/, v interface{} /*V*/) { func (t *Tree) Set(k interface{} /*K*/, v interface{} /*V*/) {
//dbg("--- PRE Set(%v, %v)\t; %v @%d, [%v, %v) PKmax: %v\n%s", k, v, t.hitD, t.hitDi, t.hitKmin, t.hitKmax, t.hitPKmax, t.dump()) //dbg("--- PRE Set(%v, %v)\t; %v @%d, [%v, %v) pk: [%v, %v)\n%s", k, v, t.hitD, t.hitDi, t.hitKmin, t.hitKmax, t.hitPKmin, t.hitPKmax, t.dump())
defer t.checkHit(k, opSet) defer t.checkHit(k, opSet)
//defer func() { //defer func() {
// dbg("--- POST\n%s\n====\n", t.dump()) // dbg("--- POST\n%s\n====\n", t.dump())
...@@ -735,18 +715,15 @@ func (t *Tree) Set(k interface{} /*K*/, v interface{} /*V*/) { ...@@ -735,18 +715,15 @@ func (t *Tree) Set(k interface{} /*K*/, v interface{} /*V*/) {
// check if we can do the update nearby previous change // check if we can do the update nearby previous change
i, ok := t.hitFind(k) i, ok := t.hitFind(k)
if i >= 0 { if i >= 0 {
//dbg("hit found\t-> %d, %v", i, ok)
dd := t.hitD dd := t.hitD
switch { switch {
case ok: case ok:
//dbg("ok'")
dd.d[i].v = v dd.d[i].v = v
t.hitDi = i t.hitDi = i
return return
case dd.c < 2*kd: case dd.c < 2*kd:
//dbg("insert'")
t.insert(dd, i, k, v) t.insert(dd, i, k, v)
return return
...@@ -756,7 +733,6 @@ func (t *Tree) Set(k interface{} /*K*/, v interface{} /*V*/) { ...@@ -756,7 +733,6 @@ func (t *Tree) Set(k interface{} /*K*/, v interface{} /*V*/) {
default: default:
p, pi := t.hitP, t.hitPi p, pi := t.hitP, t.hitPi
if p == nil || p.c <= 2*kx { if p == nil || p.c <= 2*kx {
//dbg("overflow'")
t.overflow(p, dd, pi, i, k, v) t.overflow(p, dd, pi, i, k, v)
return return
} }
...@@ -774,14 +750,8 @@ func (t *Tree) Set(k interface{} /*K*/, v interface{} /*V*/) { ...@@ -774,14 +750,8 @@ func (t *Tree) Set(k interface{} /*K*/, v interface{} /*V*/) {
return return
} }
// XXX recheck what is better - to update hits in t. directly or here and copy at last t.hitKmin, t.hitKmax = xkey{}, xkey{} // initially [-∞, +∞)
// (performance) NOTE for splitX it is handy to have them in t. t.hitPKmin, t.hitPKmax = xkey{}, xkey{}
//var hitKmin, hitKmax xkey // initially [-∞, +∞)
//var hitPKmax xkey // Kmax for whole hitP
t.hitKmin = xkey{} // initially [-∞, +∞)
t.hitKmax = xkey{}
t.hitPKmin = xkey{}
t.hitPKmax = xkey{}
for { for {
i, ok := t.find(q, k) i, ok := t.find(q, k)
......
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