github.com/runner-mei/ql@v1.1.0/btree.go (about) 1 // Copyright 2014 The ql Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package ql 6 7 import ( 8 "io" 9 ) 10 11 const ( 12 kx = 128 //DONE benchmark tune this number if using custom key/value type(s). 13 kd = 64 //DONE benchmark tune this number if using custom key/value type(s). 14 ) 15 16 type ( 17 // cmp compares a and b. Return value is: 18 // 19 // < 0 if a < b 20 // 0 if a == b 21 // > 0 if a > b 22 // 23 cmp func(a, b []interface{}) int 24 25 d struct { // data page 26 c int 27 d [2*kd + 1]de 28 n *d 29 p *d 30 } 31 32 de struct { // d element 33 k []interface{} 34 v []interface{} 35 } 36 37 enumerator struct { 38 err error 39 hit bool 40 i int 41 k []interface{} 42 q *d 43 t *tree 44 ver int64 45 } 46 47 // tree is a B+tree. 48 tree struct { 49 c int 50 cmp cmp 51 first *d 52 last *d 53 r interface{} 54 ver int64 55 } 56 57 xe struct { // x element 58 ch interface{} 59 sep *d 60 } 61 62 x struct { // index page 63 c int 64 x [2*kx + 2]xe 65 } 66 ) 67 68 var ( // R/O zero values 69 zd d 70 zde de 71 zx x 72 zxe xe 73 ) 74 75 func clr(q interface{}) { 76 switch z := q.(type) { 77 case *x: 78 for i := 0; i <= z.c; i++ { // Ch0 Sep0 ... Chn-1 Sepn-1 Chn 79 clr(z.x[i].ch) 80 } 81 *z = zx // GC 82 case *d: 83 *z = zd // GC 84 } 85 } 86 87 // -------------------------------------------------------------------------- x 88 89 func newX(ch0 interface{}) *x { 90 r := &x{} 91 r.x[0].ch = ch0 92 return r 93 } 94 95 func (q *x) extract(i int) { 96 q.c-- 97 if i < q.c { 98 copy(q.x[i:], q.x[i+1:q.c+1]) 99 q.x[q.c].ch = q.x[q.c+1].ch 100 q.x[q.c].sep = nil // GC 101 q.x[q.c+1] = zxe // GC 102 } 103 } 104 105 func (q *x) insert(i int, d *d, ch interface{}) *x { 106 c := q.c 107 if i < c { 108 q.x[c+1].ch = q.x[c].ch 109 copy(q.x[i+2:], q.x[i+1:c]) 110 q.x[i+1].sep = q.x[i].sep 111 } 112 c++ 113 q.c = c 114 q.x[i].sep = d 115 q.x[i+1].ch = ch 116 return q 117 } 118 119 func (q *x) siblings(i int) (l, r *d) { 120 if i >= 0 { 121 if i > 0 { 122 l = q.x[i-1].ch.(*d) 123 } 124 if i < q.c { 125 r = q.x[i+1].ch.(*d) 126 } 127 } 128 return 129 } 130 131 // -------------------------------------------------------------------------- d 132 133 func (l *d) mvL(r *d, c int) { 134 copy(l.d[l.c:], r.d[:c]) 135 copy(r.d[:], r.d[c:r.c]) 136 l.c += c 137 r.c -= c 138 } 139 140 func (l *d) mvR(r *d, c int) { 141 copy(r.d[c:], r.d[:r.c]) 142 copy(r.d[:c], l.d[l.c-c:]) 143 r.c += c 144 l.c -= c 145 } 146 147 // ----------------------------------------------------------------------- tree 148 149 // treeNew returns a newly created, empty tree. The compare function is used 150 // for key collation. 151 func treeNew(cmp cmp) *tree { 152 return &tree{cmp: cmp} 153 } 154 155 // Clear removes all K/V pairs from the tree. 156 func (t *tree) Clear() { 157 if t.r == nil { 158 return 159 } 160 161 clr(t.r) 162 t.c, t.first, t.last, t.r = 0, nil, nil, nil 163 t.ver++ 164 } 165 166 func (t *tree) cat(p *x, q, r *d, pi int) { 167 t.ver++ 168 q.mvL(r, r.c) 169 if r.n != nil { 170 r.n.p = q 171 } else { 172 t.last = q 173 } 174 q.n = r.n 175 if p.c > 1 { 176 p.extract(pi) 177 p.x[pi].ch = q 178 } else { 179 t.r = q 180 } 181 } 182 183 func (t *tree) catX(p, q, r *x, pi int) { 184 t.ver++ 185 q.x[q.c].sep = p.x[pi].sep 186 copy(q.x[q.c+1:], r.x[:r.c]) 187 q.c += r.c + 1 188 q.x[q.c].ch = r.x[r.c].ch 189 if p.c > 1 { 190 p.c-- 191 pc := p.c 192 if pi < pc { 193 p.x[pi].sep = p.x[pi+1].sep 194 copy(p.x[pi+1:], p.x[pi+2:pc+1]) 195 p.x[pc].ch = p.x[pc+1].ch 196 p.x[pc].sep = nil // GC 197 p.x[pc+1].ch = nil // GC 198 } 199 return 200 } 201 202 t.r = q 203 } 204 205 //Delete removes the k's KV pair, if it exists, in which case Delete returns 206 //true. 207 func (t *tree) Delete(k []interface{}) (ok bool) { 208 pi := -1 209 var p *x 210 q := t.r 211 if q == nil { 212 return 213 } 214 215 for { 216 var i int 217 i, ok = t.find(q, k) 218 if ok { 219 switch z := q.(type) { 220 case *x: 221 dp := z.x[i].sep 222 switch { 223 case dp.c > kd: 224 t.extract(dp, 0) 225 default: 226 if z.c < kx && q != t.r { 227 t.underflowX(p, &z, pi, &i) 228 } 229 pi = i + 1 230 p = z 231 q = z.x[pi].ch 232 ok = false 233 continue 234 } 235 case *d: 236 t.extract(z, i) 237 if z.c >= kd { 238 return 239 } 240 241 if q != t.r { 242 t.underflow(p, z, pi) 243 } else if t.c == 0 { 244 t.Clear() 245 } 246 } 247 return 248 } 249 250 switch z := q.(type) { 251 case *x: 252 if z.c < kx && q != t.r { 253 t.underflowX(p, &z, pi, &i) 254 } 255 pi = i 256 p = z 257 q = z.x[i].ch 258 case *d: 259 return 260 } 261 } 262 } 263 264 func (t *tree) extract(q *d, i int) { // (r []interface{}) { 265 t.ver++ 266 //r = q.d[i].v // prepared for Extract 267 q.c-- 268 if i < q.c { 269 copy(q.d[i:], q.d[i+1:q.c+1]) 270 } 271 q.d[q.c] = zde // GC 272 t.c-- 273 return 274 } 275 276 func (t *tree) find(q interface{}, k []interface{}) (i int, ok bool) { 277 var mk []interface{} 278 l := 0 279 switch z := q.(type) { 280 case *x: 281 h := z.c - 1 282 for l <= h { 283 m := (l + h) >> 1 284 mk = z.x[m].sep.d[0].k 285 switch cmp := t.cmp(k, mk); { 286 case cmp > 0: 287 l = m + 1 288 case cmp == 0: 289 return m, true 290 default: 291 h = m - 1 292 } 293 } 294 case *d: 295 h := z.c - 1 296 for l <= h { 297 m := (l + h) >> 1 298 mk = z.d[m].k 299 switch cmp := t.cmp(k, mk); { 300 case cmp > 0: 301 l = m + 1 302 case cmp == 0: 303 return m, true 304 default: 305 h = m - 1 306 } 307 } 308 } 309 return l, false 310 } 311 312 // First returns the first item of the tree in the key collating order, or 313 // (nil, nil) if the tree is empty. 314 func (t *tree) First() (k []interface{}, v []interface{}) { 315 if q := t.first; q != nil { 316 q := &q.d[0] 317 k, v = q.k, q.v 318 } 319 return 320 } 321 322 // Get returns the value associated with k and true if it exists. Otherwise Get 323 // returns (nil, false). 324 func (t *tree) Get(k []interface{}) (v []interface{}, ok bool) { 325 q := t.r 326 if q == nil { 327 return 328 } 329 330 for { 331 var i int 332 if i, ok = t.find(q, k); ok { 333 switch z := q.(type) { 334 case *x: 335 return z.x[i].sep.d[0].v, true 336 case *d: 337 return z.d[i].v, true 338 } 339 } 340 switch z := q.(type) { 341 case *x: 342 q = z.x[i].ch 343 default: 344 return 345 } 346 } 347 } 348 349 func (t *tree) insert(q *d, i int, k []interface{}, v []interface{}) *d { 350 t.ver++ 351 c := q.c 352 if i < c { 353 copy(q.d[i+1:], q.d[i:c]) 354 } 355 c++ 356 q.c = c 357 q.d[i].k, q.d[i].v = k, v 358 t.c++ 359 return q 360 } 361 362 // Last returns the last item of the tree in the key collating order, or (nil, 363 // nil) if the tree is empty. 364 func (t *tree) Last() (k []interface{}, v []interface{}) { 365 if q := t.last; q != nil { 366 q := &q.d[q.c-1] 367 k, v = q.k, q.v 368 } 369 return 370 } 371 372 // Len returns the number of items in the tree. 373 func (t *tree) Len() int { 374 return t.c 375 } 376 377 func (t *tree) overflow(p *x, q *d, pi, i int, k []interface{}, v []interface{}) { 378 t.ver++ 379 l, r := p.siblings(pi) 380 381 if l != nil && l.c < 2*kd && i > 0 { 382 l.mvL(q, 1) 383 t.insert(q, i-1, k, v) 384 return 385 } 386 387 if r != nil && r.c < 2*kd { 388 if i < 2*kd { 389 q.mvR(r, 1) 390 t.insert(q, i, k, v) 391 } else { 392 t.insert(r, 0, k, v) 393 } 394 return 395 } 396 397 t.split(p, q, pi, i, k, v) 398 } 399 400 // Seek returns an enumerator positioned on a an item such that k >= item's 401 // key. ok reports if k == item.key The enumerator's position is possibly 402 // after the last item in the tree. 403 func (t *tree) Seek(k []interface{}) (e *enumerator, ok bool) { 404 q := t.r 405 if q == nil { 406 e = &enumerator{nil, false, 0, k, nil, t, t.ver} 407 return 408 } 409 410 for { 411 var i int 412 if i, ok = t.find(q, k); ok { 413 switch z := q.(type) { 414 case *x: 415 e = &enumerator{nil, ok, 0, k, z.x[i].sep, t, t.ver} 416 return 417 case *d: 418 e = &enumerator{nil, ok, i, k, z, t, t.ver} 419 return 420 } 421 } 422 switch z := q.(type) { 423 case *x: 424 q = z.x[i].ch 425 case *d: 426 e = &enumerator{nil, ok, i, k, z, t, t.ver} 427 return 428 } 429 } 430 } 431 432 // SeekFirst returns an enumerator positioned on the first KV pair in the tree, 433 // if any. For an empty tree, err == io.EOF is returned and e will be nil. 434 func (t *tree) SeekFirst() (e *enumerator, err error) { 435 q := t.first 436 if q == nil { 437 return nil, io.EOF 438 } 439 440 return &enumerator{nil, true, 0, q.d[0].k, q, t, t.ver}, nil 441 } 442 443 // SeekLast returns an enumerator positioned on the last KV pair in the tree, 444 // if any. For an empty tree, err == io.EOF is returned and e will be nil. 445 func (t *tree) SeekLast() (e *enumerator, err error) { 446 q := t.last 447 if q == nil { 448 return nil, io.EOF 449 } 450 451 return &enumerator{nil, true, q.c - 1, q.d[q.c-1].k, q, t, t.ver}, nil 452 } 453 454 // Set sets the value associated with k. 455 func (t *tree) Set(k []interface{}, v []interface{}) { 456 pi := -1 457 var p *x 458 q := t.r 459 if q != nil { 460 for { 461 i, ok := t.find(q, k) 462 if ok { 463 switch z := q.(type) { 464 case *x: 465 z.x[i].sep.d[0].v = v 466 case *d: 467 z.d[i].v = v 468 } 469 return 470 } 471 472 switch z := q.(type) { 473 case *x: 474 if z.c > 2*kx { 475 t.splitX(p, &z, pi, &i) 476 } 477 pi = i 478 p = z 479 q = z.x[i].ch 480 case *d: 481 switch { 482 case z.c < 2*kd: 483 t.insert(z, i, k, v) 484 default: 485 t.overflow(p, z, pi, i, k, v) 486 } 487 return 488 } 489 } 490 } 491 492 z := t.insert(&d{}, 0, k, v) 493 t.r, t.first, t.last = z, z, z 494 return 495 } 496 497 func (t *tree) split(p *x, q *d, pi, i int, k []interface{}, v []interface{}) { 498 t.ver++ 499 r := &d{} 500 if q.n != nil { 501 r.n = q.n 502 r.n.p = r 503 } else { 504 t.last = r 505 } 506 q.n = r 507 r.p = q 508 509 copy(r.d[:], q.d[kd:2*kd]) 510 for i := range q.d[kd:] { 511 q.d[kd+i] = zde 512 } 513 q.c = kd 514 r.c = kd 515 if pi >= 0 { 516 p.insert(pi, r, r) 517 } else { 518 t.r = newX(q).insert(0, r, r) 519 } 520 if i > kd { 521 t.insert(r, i-kd, k, v) 522 return 523 } 524 525 t.insert(q, i, k, v) 526 } 527 528 func (t *tree) splitX(p *x, pp **x, pi int, i *int) { 529 t.ver++ 530 q := *pp 531 r := &x{} 532 copy(r.x[:], q.x[kx+1:]) 533 q.c = kx 534 r.c = kx 535 if pi >= 0 { 536 p.insert(pi, q.x[kx].sep, r) 537 } else { 538 t.r = newX(q).insert(0, q.x[kx].sep, r) 539 } 540 q.x[kx].sep = nil 541 for i := range q.x[kx+1:] { 542 q.x[kx+i+1] = zxe 543 } 544 if *i > kx { 545 *pp = r 546 *i -= kx + 1 547 } 548 } 549 550 func (t *tree) underflow(p *x, q *d, pi int) { 551 t.ver++ 552 l, r := p.siblings(pi) 553 554 if l != nil && l.c+q.c >= 2*kd { 555 l.mvR(q, 1) 556 } else if r != nil && q.c+r.c >= 2*kd { 557 q.mvL(r, 1) 558 r.d[r.c] = zde // GC 559 } else if l != nil { 560 t.cat(p, l, q, pi-1) 561 } else { 562 t.cat(p, q, r, pi) 563 } 564 } 565 566 func (t *tree) underflowX(p *x, pp **x, pi int, i *int) { 567 t.ver++ 568 var l, r *x 569 q := *pp 570 571 if pi >= 0 { 572 if pi > 0 { 573 l = p.x[pi-1].ch.(*x) 574 } 575 if pi < p.c { 576 r = p.x[pi+1].ch.(*x) 577 } 578 } 579 580 if l != nil && l.c > kx { 581 q.x[q.c+1].ch = q.x[q.c].ch 582 copy(q.x[1:], q.x[:q.c]) 583 q.x[0].ch = l.x[l.c].ch 584 q.x[0].sep = p.x[pi-1].sep 585 q.c++ 586 *i++ 587 l.c-- 588 p.x[pi-1].sep = l.x[l.c].sep 589 return 590 } 591 592 if r != nil && r.c > kx { 593 q.x[q.c].sep = p.x[pi].sep 594 q.c++ 595 q.x[q.c].ch = r.x[0].ch 596 p.x[pi].sep = r.x[0].sep 597 copy(r.x[:], r.x[1:r.c]) 598 r.c-- 599 rc := r.c 600 r.x[rc].ch = r.x[rc+1].ch 601 r.x[rc].sep = nil 602 r.x[rc+1].ch = nil 603 return 604 } 605 606 if l != nil { 607 *i += l.c + 1 608 t.catX(p, l, q, pi-1) 609 *pp = l 610 return 611 } 612 613 t.catX(p, q, r, pi) 614 } 615 616 // ----------------------------------------------------------------- enumerator 617 618 // Next returns the currently enumerated item, if it exists and moves to the 619 // next item in the key collation order. If there is no item to return, err == 620 // io.EOF is returned. 621 func (e *enumerator) Next() (k []interface{}, v []interface{}, err error) { 622 if err = e.err; err != nil { 623 return 624 } 625 626 if e.ver != e.t.ver { 627 f, hit := e.t.Seek(e.k) 628 if !e.hit && hit { 629 if err = f.next(); err != nil { 630 return 631 } 632 } 633 634 *e = *f 635 } 636 if e.q == nil { 637 e.err, err = io.EOF, io.EOF 638 return 639 } 640 641 if e.i >= e.q.c { 642 if err = e.next(); err != nil { 643 return 644 } 645 } 646 647 i := e.q.d[e.i] 648 k, v = i.k, i.v 649 e.k, e.hit = k, false 650 e.next() 651 return 652 } 653 654 func (e *enumerator) next() error { 655 if e.q == nil { 656 e.err = io.EOF 657 return io.EOF 658 } 659 660 switch { 661 case e.i < e.q.c-1: 662 e.i++ 663 default: 664 if e.q, e.i = e.q.n, 0; e.q == nil { 665 e.err = io.EOF 666 } 667 } 668 return e.err 669 } 670 671 // Prev returns the currently enumerated item, if it exists and moves to the 672 // previous item in the key collation order. If there is no item to return, err 673 // == io.EOF is returned. 674 func (e *enumerator) Prev() (k []interface{}, v []interface{}, err error) { 675 if err = e.err; err != nil { 676 return 677 } 678 679 if e.ver != e.t.ver { 680 f, hit := e.t.Seek(e.k) 681 if !e.hit && hit { 682 if err = f.prev(); err != nil { 683 return 684 } 685 } 686 687 *e = *f 688 } 689 if e.q == nil { 690 e.err, err = io.EOF, io.EOF 691 return 692 } 693 694 if e.i >= e.q.c { 695 if err = e.next(); err != nil { 696 return 697 } 698 } 699 700 i := e.q.d[e.i] 701 k, v = i.k, i.v 702 e.k, e.hit = k, false 703 e.prev() 704 return 705 } 706 707 func (e *enumerator) prev() error { 708 if e.q == nil { 709 e.err = io.EOF 710 return io.EOF 711 } 712 713 switch { 714 case e.i > 0: 715 e.i-- 716 default: 717 if e.q = e.q.p; e.q == nil { 718 e.err = io.EOF 719 break 720 } 721 722 e.i = e.q.c - 1 723 } 724 return e.err 725 }