Commit 984f83a6 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 232e9671
...@@ -11,10 +11,10 @@ import ( ...@@ -11,10 +11,10 @@ import (
) )
const ( const (
kx = 32 //TODO benchmark tune this number if using custom key/value type(s). //kx = 32 //TODO benchmark tune this number if using custom key/value type(s).
kd = 32 //TODO benchmark tune this number if using custom key/value type(s). //kd = 32 //TODO benchmark tune this number if using custom key/value type(s).
//kx = 2 //TODO benchmark tune this number if using custom key/value type(s). kx = 2
//kd = 2 //TODO benchmark tune this number if using custom key/value type(s). kd = 2
) )
func init() { func init() {
...@@ -39,6 +39,8 @@ type btTpool struct{ sync.Pool } ...@@ -39,6 +39,8 @@ type btTpool struct{ sync.Pool }
func (p *btTpool) get(cmp Cmp) *Tree { func (p *btTpool) get(cmp Cmp) *Tree {
x := p.Get().(*Tree) x := p.Get().(*Tree)
x.cmp = cmp x.cmp = cmp
x.hitIdx = -1
x.hitPi = -1
return x return x
} }
...@@ -510,7 +512,7 @@ func (t *Tree) insert(q *d, i int, k interface{} /*K*/, v interface{} /*V*/) *d ...@@ -510,7 +512,7 @@ func (t *Tree) insert(q *d, i int, k interface{} /*K*/, v interface{} /*V*/) *d
q.c = c q.c = c
q.d[i].k, q.d[i].v = k, v q.d[i].k, q.d[i].v = k, v
t.c++ t.c++
t.hit = d t.hit = q
t.hitIdx = i t.hitIdx = i
return q return q
} }
...@@ -532,18 +534,19 @@ func (t *Tree) Len() int { ...@@ -532,18 +534,19 @@ func (t *Tree) Len() int {
func (t *Tree) overflow(p *x, q *d, pi, i int, k interface{} /*K*/, v interface{} /*V*/) { func (t *Tree) overflow(p *x, q *d, pi, i int, k interface{} /*K*/, v interface{} /*V*/) {
t.ver++ t.ver++
t.hitP = p
l, r := p.siblings(pi) l, r := p.siblings(pi)
if l != nil && l.c < 2*kd && i != 0 { if l != nil && l.c < 2*kd && i != 0 {
l.mvL(q, 1) l.mvL(q, 1)
t.insert(q, i-1, k, v) t.insert(q, i-1, k, v)
p.x[pi-1].k = q.d[0].k p.x[pi-1].k = q.d[0].k
t.hitP = p
t.hitPi = pi t.hitPi = pi
return return
} }
if r != nil && r.c < 2*kd { if r != nil && r.c < 2*kd {
t.hitP = p
if i < 2*kd { if i < 2*kd {
q.mvR(r, 1) q.mvR(r, 1)
t.insert(q, i, k, v) t.insert(q, i, k, v)
...@@ -616,10 +619,13 @@ func (t *Tree) SeekLast() (e *Enumerator, err error) { ...@@ -616,10 +619,13 @@ 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 @%d)\n%s", k, v, t.hit, t.hitIdx, t.hitP, t.hitPi, t.dump()) dbg("--- PRE Set(%v, %v)\t(%v @%d, %v @%d)\n%s", k, v, t.hit, t.hitIdx, t.hitP, t.hitPi, t.dump())
//defer func() { defer func() {
// dbg("--- POST\n%s\n====\n", t.dump()) //if r := recover(); r != nil {
//}() // panic(r)
//}
dbg("--- POST\n%s\n====\n", t.dump())
}()
pi := -1 pi := -1
var p *x var p *x
...@@ -634,6 +640,7 @@ func (t *Tree) Set(k interface{} /*K*/, v interface{} /*V*/) { ...@@ -634,6 +640,7 @@ func (t *Tree) Set(k interface{} /*K*/, v interface{} /*V*/) {
} else { } else {
q := t.r q := t.r
if q == nil { if q == nil {
dbg("empty")
z := t.insert(btDPool.Get().(*d), 0, k, v) // XXX update hit z := t.insert(btDPool.Get().(*d), 0, k, v) // XXX update hit
t.r, t.first, t.last = z, z, z t.r, t.first, t.last = z, z, z
return return
...@@ -666,15 +673,18 @@ func (t *Tree) Set(k interface{} /*K*/, v interface{} /*V*/) { ...@@ -666,15 +673,18 @@ func (t *Tree) Set(k interface{} /*K*/, v interface{} /*V*/) {
// data page found - perform the update // data page found - perform the update
switch { switch {
case ok: case ok:
dbg("ok")
dd.d[i].v = v dd.d[i].v = v
t.hit, t.hitIdx = dd, i t.hit, t.hitIdx = dd, i
t.hitP, t.hitPi = p, pi t.hitP, t.hitPi = p, pi
case dd.c < 2*kd: case dd.c < 2*kd:
dbg("insert")
t.insert(dd, i, k, v) t.insert(dd, i, k, v)
t.hitP, t.hitPi = p, pi t.hitP, t.hitPi = p, pi
default: default:
dbg("overflow")
t.overflow(p, dd, pi, i, k, v) t.overflow(p, dd, pi, i, k, v)
} }
} }
...@@ -781,23 +791,22 @@ func (t *Tree) split(p *x, q *d, pi, i int, k interface{} /*K*/, v interface{} / ...@@ -781,23 +791,22 @@ func (t *Tree) split(p *x, q *d, pi, i int, k interface{} /*K*/, v interface{} /
} }
q.c = kd q.c = kd
r.c = kd r.c = kd
var done bool
if i > kd {
done = true
t.insert(r, i-kd, k, v)
t.hitPi = pi + 1
}
if pi >= 0 { if pi >= 0 {
p.insert(pi, r.d[0].k, r) p.insert(pi, r.d[0].k, r)
} else { } else {
t.r = newX(q).insert(0, r.d[0].k, r) p = newX(q).insert(0, r.d[0].k, r)
} t.r = p
if done {
return
} }
t.hitP = p
t.insert(q, i, k, v) if i > kd {
t.hitPi = pi t.insert(r, i-kd, k, v)
t.hitPi = pi + 1
} else {
t.insert(q, i, k, v)
t.hitPi = pi
}
} }
func (t *Tree) splitX(p *x, q *x, pi int, i int) (*x, int) { func (t *Tree) splitX(p *x, q *x, pi int, i int) (*x, int) {
......
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