github.com/cznic/ql@v1.2.1-0.20181122101857-b60735abf8a0/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  }
   274  
   275  func (t *tree) find(q interface{}, k []interface{}) (i int, ok bool) {
   276  	var mk []interface{}
   277  	l := 0
   278  	switch z := q.(type) {
   279  	case *x:
   280  		h := z.c - 1
   281  		for l <= h {
   282  			m := (l + h) >> 1
   283  			mk = z.x[m].sep.d[0].k
   284  			switch cmp := t.cmp(k, mk); {
   285  			case cmp > 0:
   286  				l = m + 1
   287  			case cmp == 0:
   288  				return m, true
   289  			default:
   290  				h = m - 1
   291  			}
   292  		}
   293  	case *d:
   294  		h := z.c - 1
   295  		for l <= h {
   296  			m := (l + h) >> 1
   297  			mk = z.d[m].k
   298  			switch cmp := t.cmp(k, mk); {
   299  			case cmp > 0:
   300  				l = m + 1
   301  			case cmp == 0:
   302  				return m, true
   303  			default:
   304  				h = m - 1
   305  			}
   306  		}
   307  	}
   308  	return l, false
   309  }
   310  
   311  // First returns the first item of the tree in the key collating order, or
   312  // (nil, nil) if the tree is empty.
   313  func (t *tree) First() (k []interface{}, v []interface{}) {
   314  	if q := t.first; q != nil {
   315  		q := &q.d[0]
   316  		k, v = q.k, q.v
   317  	}
   318  	return
   319  }
   320  
   321  // Get returns the value associated with k and true if it exists. Otherwise Get
   322  // returns (nil, false).
   323  func (t *tree) Get(k []interface{}) (v []interface{}, ok bool) {
   324  	q := t.r
   325  	if q == nil {
   326  		return
   327  	}
   328  
   329  	for {
   330  		var i int
   331  		if i, ok = t.find(q, k); ok {
   332  			switch z := q.(type) {
   333  			case *x:
   334  				return z.x[i].sep.d[0].v, true
   335  			case *d:
   336  				return z.d[i].v, true
   337  			}
   338  		}
   339  		switch z := q.(type) {
   340  		case *x:
   341  			q = z.x[i].ch
   342  		default:
   343  			return
   344  		}
   345  	}
   346  }
   347  
   348  func (t *tree) insert(q *d, i int, k []interface{}, v []interface{}) *d {
   349  	t.ver++
   350  	c := q.c
   351  	if i < c {
   352  		copy(q.d[i+1:], q.d[i:c])
   353  	}
   354  	c++
   355  	q.c = c
   356  	q.d[i].k, q.d[i].v = k, v
   357  	t.c++
   358  	return q
   359  }
   360  
   361  // Last returns the last item of the tree in the key collating order, or (nil,
   362  // nil) if the tree is empty.
   363  func (t *tree) Last() (k []interface{}, v []interface{}) {
   364  	if q := t.last; q != nil {
   365  		q := &q.d[q.c-1]
   366  		k, v = q.k, q.v
   367  	}
   368  	return
   369  }
   370  
   371  // Len returns the number of items in the tree.
   372  func (t *tree) Len() int {
   373  	return t.c
   374  }
   375  
   376  func (t *tree) overflow(p *x, q *d, pi, i int, k []interface{}, v []interface{}) {
   377  	t.ver++
   378  	l, r := p.siblings(pi)
   379  
   380  	if l != nil && l.c < 2*kd && i > 0 {
   381  		l.mvL(q, 1)
   382  		t.insert(q, i-1, k, v)
   383  		return
   384  	}
   385  
   386  	if r != nil && r.c < 2*kd {
   387  		if i < 2*kd {
   388  			q.mvR(r, 1)
   389  			t.insert(q, i, k, v)
   390  		} else {
   391  			t.insert(r, 0, k, v)
   392  		}
   393  		return
   394  	}
   395  
   396  	t.split(p, q, pi, i, k, v)
   397  }
   398  
   399  // Seek returns an enumerator positioned on a an item such that k >= item's
   400  // key. ok reports if k == item.key The enumerator's position is possibly
   401  // after the last item in the tree.
   402  func (t *tree) Seek(k []interface{}) (e *enumerator, ok bool) {
   403  	q := t.r
   404  	if q == nil {
   405  		e = &enumerator{nil, false, 0, k, nil, t, t.ver}
   406  		return
   407  	}
   408  
   409  	for {
   410  		var i int
   411  		if i, ok = t.find(q, k); ok {
   412  			switch z := q.(type) {
   413  			case *x:
   414  				e = &enumerator{nil, ok, 0, k, z.x[i].sep, t, t.ver}
   415  				return
   416  			case *d:
   417  				e = &enumerator{nil, ok, i, k, z, t, t.ver}
   418  				return
   419  			}
   420  		}
   421  		switch z := q.(type) {
   422  		case *x:
   423  			q = z.x[i].ch
   424  		case *d:
   425  			e = &enumerator{nil, ok, i, k, z, t, t.ver}
   426  			return
   427  		}
   428  	}
   429  }
   430  
   431  // SeekFirst returns an enumerator positioned on the first KV pair in the tree,
   432  // if any. For an empty tree, err == io.EOF is returned and e will be nil.
   433  func (t *tree) SeekFirst() (e *enumerator, err error) {
   434  	q := t.first
   435  	if q == nil {
   436  		return nil, io.EOF
   437  	}
   438  
   439  	return &enumerator{nil, true, 0, q.d[0].k, q, t, t.ver}, nil
   440  }
   441  
   442  // SeekLast returns an enumerator positioned on the last KV pair in the tree,
   443  // if any. For an empty tree, err == io.EOF is returned and e will be nil.
   444  func (t *tree) SeekLast() (e *enumerator, err error) {
   445  	q := t.last
   446  	if q == nil {
   447  		return nil, io.EOF
   448  	}
   449  
   450  	return &enumerator{nil, true, q.c - 1, q.d[q.c-1].k, q, t, t.ver}, nil
   451  }
   452  
   453  // Set sets the value associated with k.
   454  func (t *tree) Set(k []interface{}, v []interface{}) {
   455  	pi := -1
   456  	var p *x
   457  	q := t.r
   458  	if q != nil {
   459  		for {
   460  			i, ok := t.find(q, k)
   461  			if ok {
   462  				switch z := q.(type) {
   463  				case *x:
   464  					z.x[i].sep.d[0].v = v
   465  				case *d:
   466  					z.d[i].v = v
   467  				}
   468  				return
   469  			}
   470  
   471  			switch z := q.(type) {
   472  			case *x:
   473  				if z.c > 2*kx {
   474  					t.splitX(p, &z, pi, &i)
   475  				}
   476  				pi = i
   477  				p = z
   478  				q = z.x[i].ch
   479  			case *d:
   480  				switch {
   481  				case z.c < 2*kd:
   482  					t.insert(z, i, k, v)
   483  				default:
   484  					t.overflow(p, z, pi, i, k, v)
   485  				}
   486  				return
   487  			}
   488  		}
   489  	}
   490  
   491  	z := t.insert(&d{}, 0, k, v)
   492  	t.r, t.first, t.last = z, z, z
   493  }
   494  
   495  func (t *tree) split(p *x, q *d, pi, i int, k []interface{}, v []interface{}) {
   496  	t.ver++
   497  	r := &d{}
   498  	if q.n != nil {
   499  		r.n = q.n
   500  		r.n.p = r
   501  	} else {
   502  		t.last = r
   503  	}
   504  	q.n = r
   505  	r.p = q
   506  
   507  	copy(r.d[:], q.d[kd:2*kd])
   508  	for i := range q.d[kd:] {
   509  		q.d[kd+i] = zde
   510  	}
   511  	q.c = kd
   512  	r.c = kd
   513  	if pi >= 0 {
   514  		p.insert(pi, r, r)
   515  	} else {
   516  		t.r = newX(q).insert(0, r, r)
   517  	}
   518  	if i > kd {
   519  		t.insert(r, i-kd, k, v)
   520  		return
   521  	}
   522  
   523  	t.insert(q, i, k, v)
   524  }
   525  
   526  func (t *tree) splitX(p *x, pp **x, pi int, i *int) {
   527  	t.ver++
   528  	q := *pp
   529  	r := &x{}
   530  	copy(r.x[:], q.x[kx+1:])
   531  	q.c = kx
   532  	r.c = kx
   533  	if pi >= 0 {
   534  		p.insert(pi, q.x[kx].sep, r)
   535  	} else {
   536  		t.r = newX(q).insert(0, q.x[kx].sep, r)
   537  	}
   538  	q.x[kx].sep = nil
   539  	for i := range q.x[kx+1:] {
   540  		q.x[kx+i+1] = zxe
   541  	}
   542  	if *i > kx {
   543  		*pp = r
   544  		*i -= kx + 1
   545  	}
   546  }
   547  
   548  func (t *tree) underflow(p *x, q *d, pi int) {
   549  	t.ver++
   550  	l, r := p.siblings(pi)
   551  
   552  	if l != nil && l.c+q.c >= 2*kd {
   553  		l.mvR(q, 1)
   554  	} else if r != nil && q.c+r.c >= 2*kd {
   555  		q.mvL(r, 1)
   556  		r.d[r.c] = zde // GC
   557  	} else if l != nil {
   558  		t.cat(p, l, q, pi-1)
   559  	} else {
   560  		t.cat(p, q, r, pi)
   561  	}
   562  }
   563  
   564  func (t *tree) underflowX(p *x, pp **x, pi int, i *int) {
   565  	t.ver++
   566  	var l, r *x
   567  	q := *pp
   568  
   569  	if pi >= 0 {
   570  		if pi > 0 {
   571  			l = p.x[pi-1].ch.(*x)
   572  		}
   573  		if pi < p.c {
   574  			r = p.x[pi+1].ch.(*x)
   575  		}
   576  	}
   577  
   578  	if l != nil && l.c > kx {
   579  		q.x[q.c+1].ch = q.x[q.c].ch
   580  		copy(q.x[1:], q.x[:q.c])
   581  		q.x[0].ch = l.x[l.c].ch
   582  		q.x[0].sep = p.x[pi-1].sep
   583  		q.c++
   584  		*i++
   585  		l.c--
   586  		p.x[pi-1].sep = l.x[l.c].sep
   587  		return
   588  	}
   589  
   590  	if r != nil && r.c > kx {
   591  		q.x[q.c].sep = p.x[pi].sep
   592  		q.c++
   593  		q.x[q.c].ch = r.x[0].ch
   594  		p.x[pi].sep = r.x[0].sep
   595  		copy(r.x[:], r.x[1:r.c])
   596  		r.c--
   597  		rc := r.c
   598  		r.x[rc].ch = r.x[rc+1].ch
   599  		r.x[rc].sep = nil
   600  		r.x[rc+1].ch = nil
   601  		return
   602  	}
   603  
   604  	if l != nil {
   605  		*i += l.c + 1
   606  		t.catX(p, l, q, pi-1)
   607  		*pp = l
   608  		return
   609  	}
   610  
   611  	t.catX(p, q, r, pi)
   612  }
   613  
   614  // ----------------------------------------------------------------- enumerator
   615  
   616  // Next returns the currently enumerated item, if it exists and moves to the
   617  // next item in the key collation order. If there is no item to return, err ==
   618  // io.EOF is returned.
   619  func (e *enumerator) Next() (k []interface{}, v []interface{}, err error) {
   620  	if err = e.err; err != nil {
   621  		return
   622  	}
   623  
   624  	if e.ver != e.t.ver {
   625  		f, hit := e.t.Seek(e.k)
   626  		if !e.hit && hit {
   627  			if err = f.next(); err != nil {
   628  				return
   629  			}
   630  		}
   631  
   632  		*e = *f
   633  	}
   634  	if e.q == nil {
   635  		e.err, err = io.EOF, io.EOF
   636  		return
   637  	}
   638  
   639  	if e.i >= e.q.c {
   640  		if err = e.next(); err != nil {
   641  			return
   642  		}
   643  	}
   644  
   645  	i := e.q.d[e.i]
   646  	k, v = i.k, i.v
   647  	e.k, e.hit = k, false
   648  	e.next()
   649  	return
   650  }
   651  
   652  func (e *enumerator) next() error {
   653  	if e.q == nil {
   654  		e.err = io.EOF
   655  		return io.EOF
   656  	}
   657  
   658  	switch {
   659  	case e.i < e.q.c-1:
   660  		e.i++
   661  	default:
   662  		if e.q, e.i = e.q.n, 0; e.q == nil {
   663  			e.err = io.EOF
   664  		}
   665  	}
   666  	return e.err
   667  }
   668  
   669  // Prev returns the currently enumerated item, if it exists and moves to the
   670  // previous item in the key collation order. If there is no item to return, err
   671  // == io.EOF is returned.
   672  func (e *enumerator) Prev() (k []interface{}, v []interface{}, err error) {
   673  	if err = e.err; err != nil {
   674  		return
   675  	}
   676  
   677  	if e.ver != e.t.ver {
   678  		f, hit := e.t.Seek(e.k)
   679  		if !e.hit && hit {
   680  			if err = f.prev(); err != nil {
   681  				return
   682  			}
   683  		}
   684  
   685  		*e = *f
   686  	}
   687  	if e.q == nil {
   688  		e.err, err = io.EOF, io.EOF
   689  		return
   690  	}
   691  
   692  	if e.i >= e.q.c {
   693  		if err = e.next(); err != nil {
   694  			return
   695  		}
   696  	}
   697  
   698  	i := e.q.d[e.i]
   699  	k, v = i.k, i.v
   700  	e.k, e.hit = k, false
   701  	e.prev()
   702  	return
   703  }
   704  
   705  func (e *enumerator) prev() error {
   706  	if e.q == nil {
   707  		e.err = io.EOF
   708  		return io.EOF
   709  	}
   710  
   711  	switch {
   712  	case e.i > 0:
   713  		e.i--
   714  	default:
   715  		if e.q = e.q.p; e.q == nil {
   716  			e.err = io.EOF
   717  			break
   718  		}
   719  
   720  		e.i = e.q.c - 1
   721  	}
   722  	return e.err
   723  }