diff --git a/go.mod b/go.mod index 46ffe806..7577d298 100644 --- a/go.mod +++ b/go.mod @@ -17,13 +17,12 @@ require ( github.com/peterh/liner v1.2.0 github.com/streadway/amqp v1.0.0 github.com/stretchr/testify v1.5.1 // indirect - github.com/tidwall/btree v0.0.0-20191029221954-400434d76274 - github.com/tidwall/buntdb v1.1.0 + github.com/tidwall/btree v0.2.2 + github.com/tidwall/buntdb v1.1.4 github.com/tidwall/cities v0.0.0-20190730194520-dbe1ae0b862c // indirect github.com/tidwall/geoindex v1.1.0 github.com/tidwall/geojson v1.2.3 github.com/tidwall/gjson v1.6.1 - github.com/tidwall/grect v0.0.0-20161006141115-ba9a043346eb // indirect github.com/tidwall/lotsa v1.0.2 // indirect github.com/tidwall/match v1.0.1 github.com/tidwall/pretty v1.0.2 @@ -32,10 +31,8 @@ require ( github.com/tidwall/redcon v1.3.2 github.com/tidwall/resp v0.0.0-20160908231031-b2b1a7ca20e3 github.com/tidwall/rhh v1.1.0 - github.com/tidwall/rtree v0.0.0-20180113144539-6cd427091e0e // indirect github.com/tidwall/sjson v1.1.1 - github.com/tidwall/tinybtree v0.0.0-20181217131827-de5932d649b5 - github.com/tidwall/tinyqueue v0.0.0-20180302190814-1e39f5511563 // indirect + github.com/tidwall/tinybtree v1.0.1 github.com/yuin/gopher-lua v0.0.0-20200816102855-ee81675732da golang.org/x/crypto v0.0.0-20200204104054-c9f3fb736b72 golang.org/x/net v0.0.0-20200301022130-244492dfa37a diff --git a/go.sum b/go.sum index a35c4cf6..f7f200ef 100644 --- a/go.sum +++ b/go.sum @@ -166,10 +166,10 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= -github.com/tidwall/btree v0.0.0-20191029221954-400434d76274 h1:G6Z6HvJuPjG6XfNGi/feOATzeJrfgTNJY+rGrHbA04E= -github.com/tidwall/btree v0.0.0-20191029221954-400434d76274/go.mod h1:huei1BkDWJ3/sLXmO+bsCNELL+Bp2Kks9OLyQFkzvA8= -github.com/tidwall/buntdb v1.1.0 h1:H6LzK59KiNjf1nHVPFrYj4Qnl8d8YLBsYamdL8N+Bao= -github.com/tidwall/buntdb v1.1.0/go.mod h1:Y39xhcDW10WlyYXeLgGftXVbjtM0QP+/kpz8xl9cbzE= +github.com/tidwall/btree v0.2.2 h1:VVo0JW/tdidNdQzNsDR4wMbL3heaxA1DGleyzQ3/niY= +github.com/tidwall/btree v0.2.2/go.mod h1:huei1BkDWJ3/sLXmO+bsCNELL+Bp2Kks9OLyQFkzvA8= +github.com/tidwall/buntdb v1.1.4 h1:W7y9+2dM3GOswU0t3pz6+BcwZXjj/tVOhPcO6EHufME= +github.com/tidwall/buntdb v1.1.4/go.mod h1:06+/n7EFf6uUaIG5r9xZcExYN3H0Lnc+g/Kqx0fZFkI= github.com/tidwall/cities v0.0.0-20190730194520-dbe1ae0b862c h1:K/6v4woP8qym96ZKorm6mbOhEs3xhlJoEQWiJ5WT7mw= github.com/tidwall/cities v0.0.0-20190730194520-dbe1ae0b862c/go.mod h1:lV/HDp2gCcRcHJWqgt6Di54GiDrTZwh1aG2ZUPNbqa4= github.com/tidwall/geoindex v1.1.0 h1:d/pGCgKUonfQINd1235kKqx9gWBU4N7GjDS9WvbPvLY= @@ -199,12 +199,12 @@ github.com/tidwall/resp v0.0.0-20160908231031-b2b1a7ca20e3 h1:+weN0RLHfv5fugOSyH github.com/tidwall/resp v0.0.0-20160908231031-b2b1a7ca20e3/go.mod h1:18xEj855iMY2bK6tNF2A4x+nZy5gWO1iO7OOl3jETKw= github.com/tidwall/rhh v1.1.0 h1:U+3RGzEB6VoBBkLlsAaF3ThjwZ0JGibplJhDin5Ub/Y= github.com/tidwall/rhh v1.1.0/go.mod h1:37/ybjMQQ4nDztczc//g/WBd2X51vogKHWL3xC8kY24= -github.com/tidwall/rtree v0.0.0-20180113144539-6cd427091e0e h1:+NL1GDIUOKxVfbp2KoJQD9cTQ6dyP2co9q4yzmT9FZo= -github.com/tidwall/rtree v0.0.0-20180113144539-6cd427091e0e/go.mod h1:/h+UnNGt0IhNNJLkGikcdcJqm66zGD/uJGMRxK/9+Ao= +github.com/tidwall/rtree v0.0.0-20201027154624-32188eeb08a8 h1:BsKSRhu0TDB6Snq8SutN9KQHc6vqHEXJTcAFwyGNius= +github.com/tidwall/rtree v0.0.0-20201027154624-32188eeb08a8/go.mod h1:/h+UnNGt0IhNNJLkGikcdcJqm66zGD/uJGMRxK/9+Ao= github.com/tidwall/sjson v1.1.1 h1:7h1vk049Jnd5EH9NyzNiEuwYW4b5qgreBbqRC19AS3U= github.com/tidwall/sjson v1.1.1/go.mod h1:yvVuSnpEQv5cYIrO+AT6kw4QVfd5SDZoGIS7/5+fZFs= -github.com/tidwall/tinybtree v0.0.0-20181217131827-de5932d649b5 h1:NaGfypx6656w6iRmXK0buWGuYashbcFttdPe00DKUcE= -github.com/tidwall/tinybtree v0.0.0-20181217131827-de5932d649b5/go.mod h1:0aFQG6KLQz3j57CeVgXlmKO3RSQ3myhJn2H+r84IgSY= +github.com/tidwall/tinybtree v1.0.1 h1:g1kLLw/dCJgtH14AFqUoob0MtSfThw4xQILCGMQd8J8= +github.com/tidwall/tinybtree v1.0.1/go.mod h1:0aFQG6KLQz3j57CeVgXlmKO3RSQ3myhJn2H+r84IgSY= github.com/tidwall/tinyqueue v0.0.0-20180302190814-1e39f5511563 h1:Otn9S136ELckZ3KKDyCkxapfufrqDqwmGjcHfAyXRrE= github.com/tidwall/tinyqueue v0.0.0-20180302190814-1e39f5511563/go.mod h1:mLqSmt7Dv/CNneF2wfcChfN1rvapyQr01LGKnKex0DQ= github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c/go.mod h1:lB8K/P019DLNhemzwFU4jHLhdvlE6uDZjXFejJXr49I= diff --git a/internal/collection/collection.go b/internal/collection/collection.go index 02dbfe95..fd3f7d0d 100644 --- a/internal/collection/collection.go +++ b/internal/collection/collection.go @@ -10,7 +10,6 @@ import ( "github.com/tidwall/geojson/geometry" "github.com/tidwall/rbang" "github.com/tidwall/tile38/internal/deadline" - "github.com/tidwall/tinybtree" ) // yieldStep forces the iterator to yield goroutine every 255 steps. @@ -27,9 +26,13 @@ type itemT struct { obj geojson.Object } -func (item *itemT) Less(other btree.Item, ctx interface{}) bool { - value1 := item.obj.String() - value2 := other.(*itemT).obj.String() +func byID(a, b interface{}) bool { + return a.(*itemT).id < b.(*itemT).id +} + +func byValue(a, b interface{}) bool { + value1 := a.(*itemT).obj.String() + value2 := b.(*itemT).obj.String() if value1 < value2 { return true } @@ -37,12 +40,12 @@ func (item *itemT) Less(other btree.Item, ctx interface{}) bool { return false } // the values match so we'll compare IDs, which are always unique. - return item.id < other.(*itemT).id + return byID(a, b) } // Collection represents a collection of geojson objects. type Collection struct { - items tinybtree.BTree // items sorted by keys + items *btree.BTree // items sorted by keys index *geoindex.Index // items geospatially indexed values *btree.BTree // items sorted by value+key fieldMap map[string]int @@ -59,8 +62,9 @@ var counter uint64 // New creates an empty collection func New() *Collection { col := &Collection{ + items: btree.New(byID), index: geoindex.Wrap(&rbang.RTree{}), - values: btree.New(32, nil), + values: btree.New(byValue), fieldMap: make(map[string]int), fieldArr: make([]string, 0), } @@ -159,8 +163,8 @@ func (c *Collection) Set( newItem := &itemT{id: id, obj: obj} // add the new item to main btree and remove the old one if needed - oldItem, ok := c.items.Set(id, newItem) - if ok { + oldItem := c.items.Set(newItem) + if oldItem != nil { oldItem := oldItem.(*itemT) // the old item was removed, now let's remove it from the rtree/btree. if objIsSpatial(oldItem.obj) { @@ -187,7 +191,7 @@ func (c *Collection) Set( c.indexInsert(newItem) c.objects++ } else { - c.values.ReplaceOrInsert(newItem) + c.values.Set(newItem) c.nobjects++ } @@ -220,8 +224,8 @@ func (c *Collection) Set( func (c *Collection) Delete(id string) ( obj geojson.Object, fields []float64, ok bool, ) { - oldItemV, ok := c.items.Delete(id) - if !ok { + oldItemV := c.items.Delete(&itemT{id: id}) + if oldItemV == nil { return nil, nil, false } oldItem := oldItemV.(*itemT) @@ -247,8 +251,8 @@ func (c *Collection) Delete(id string) ( func (c *Collection) Get(id string) ( obj geojson.Object, fields []float64, ok bool, ) { - itemV, ok := c.items.Get(id) - if !ok { + itemV := c.items.Get(&itemT{id: id}) + if itemV == nil { return nil, nil, false } item := itemV.(*itemT) @@ -260,8 +264,8 @@ func (c *Collection) Get(id string) ( func (c *Collection) SetField(id, field string, value float64) ( obj geojson.Object, fields []float64, updated bool, ok bool, ) { - itemV, ok := c.items.Get(id) - if !ok { + itemV := c.items.Get(&itemT{id: id}) + if itemV == nil { return nil, nil, false, false } item := itemV.(*itemT) @@ -273,8 +277,8 @@ func (c *Collection) SetField(id, field string, value float64) ( func (c *Collection) SetFields( id string, inFields []string, inValues []float64, ) (obj geojson.Object, fields []float64, updatedCount int, ok bool) { - itemV, ok := c.items.Get(id) - if !ok { + itemV := c.items.Get(&itemT{id: id}) + if itemV == nil { return nil, nil, 0, false } item := itemV.(*itemT) @@ -356,20 +360,20 @@ func (c *Collection) Scan( offset = cursor.Offset() cursor.Step(offset) } - iter := func(key string, value interface{}) bool { + iter := func(item interface{}) bool { count++ if count <= offset { return true } nextStep(count, cursor, deadline) - iitm := value.(*itemT) + iitm := item.(*itemT) keepon = iterator(iitm.id, iitm.obj, c.getFieldValues(iitm.id)) return keepon } if desc { - c.items.Reverse(iter) + c.items.Descend(nil, iter) } else { - c.items.Scan(iter) + c.items.Ascend(nil, iter) } return keepon } @@ -389,18 +393,19 @@ func (c *Collection) ScanRange( offset = cursor.Offset() cursor.Step(offset) } - iter := func(key string, value interface{}) bool { + iter := func(value interface{}) bool { + item := value.(*itemT) count++ if count <= offset { return true } nextStep(count, cursor, deadline) if !desc { - if key >= end { + if item.id >= end { return false } } else { - if key <= end { + if item.id <= end { return false } } @@ -410,9 +415,9 @@ func (c *Collection) ScanRange( } if desc { - c.items.Descend(start, iter) + c.items.Descend(&itemT{id: start}, iter) } else { - c.items.Ascend(start, iter) + c.items.Ascend(&itemT{id: start}, iter) } return keepon } @@ -431,7 +436,7 @@ func (c *Collection) SearchValues( offset = cursor.Offset() cursor.Step(offset) } - iter := func(item btree.Item) bool { + iter := func(item interface{}) bool { count++ if count <= offset { return true @@ -442,9 +447,9 @@ func (c *Collection) SearchValues( return keepon } if desc { - c.values.Descend(iter) + c.values.Descend(nil, iter) } else { - c.values.Ascend(iter) + c.values.Ascend(nil, iter) } return keepon } @@ -462,7 +467,7 @@ func (c *Collection) SearchValuesRange(start, end string, desc bool, offset = cursor.Offset() cursor.Step(offset) } - iter := func(item btree.Item) bool { + iter := func(item interface{}) bool { count++ if count <= offset { return true @@ -472,16 +477,24 @@ func (c *Collection) SearchValuesRange(start, end string, desc bool, keepon = iterator(iitm.id, iitm.obj, c.getFieldValues(iitm.id)) return keepon } + pstart := &itemT{obj: String(start)} + pend := &itemT{obj: String(end)} if desc { - c.values.DescendRange(&itemT{obj: String(start)}, - &itemT{obj: String(end)}, iter) + // descend range + c.values.Descend(pstart, func(item interface{}) bool { + return bGT(c.values, item, pend) && iter(item) + }) } else { - c.values.AscendRange(&itemT{obj: String(start)}, - &itemT{obj: String(end)}, iter) + c.values.Ascend(pstart, func(item interface{}) bool { + return bLT(c.values, item, pend) && iter(item) + }) } return keepon } +func bLT(tr *btree.BTree, a, b interface{}) bool { return tr.Less(a, b) } +func bGT(tr *btree.BTree, a, b interface{}) bool { return tr.Less(b, a) } + // ScanGreaterOrEqual iterates though the collection starting with specified id. func (c *Collection) ScanGreaterOrEqual(id string, desc bool, cursor Cursor, @@ -495,7 +508,7 @@ func (c *Collection) ScanGreaterOrEqual(id string, desc bool, offset = cursor.Offset() cursor.Step(offset) } - iter := func(key string, value interface{}) bool { + iter := func(value interface{}) bool { count++ if count <= offset { return true @@ -506,9 +519,9 @@ func (c *Collection) ScanGreaterOrEqual(id string, desc bool, return keepon } if desc { - c.items.Descend(id, iter) + c.items.Descend(&itemT{id: id}, iter) } else { - c.items.Ascend(id, iter) + c.items.Ascend(&itemT{id: id}, iter) } return keepon } diff --git a/vendor/github.com/tidwall/btree/LICENSE b/vendor/github.com/tidwall/btree/LICENSE index d6456956..5f1fbd04 100644 --- a/vendor/github.com/tidwall/btree/LICENSE +++ b/vendor/github.com/tidwall/btree/LICENSE @@ -1,202 +1,18 @@ +Copyright (c) 2020 Josh Baker - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright [yyyy] [name of copyright owner] - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/vendor/github.com/tidwall/btree/README.md b/vendor/github.com/tidwall/btree/README.md index deb1e886..10b55297 100644 --- a/vendor/github.com/tidwall/btree/README.md +++ b/vendor/github.com/tidwall/btree/README.md @@ -1,107 +1,5 @@ -BTree implementation for Go -=========================== +# B-tree for Go ![Travis CI Build Status](https://api.travis-ci.org/tidwall/btree.svg?branch=master) [![GoDoc](https://godoc.org/github.com/tidwall/btree?status.svg)](https://godoc.org/github.com/tidwall/btree) -This package provides an in-memory B-Tree implementation for Go, useful as -an ordered, mutable data structure. - -This is a fork of the wonderful [google/btree](https://github.com/google/btree) package. It's has all the same great features and adds a few more. - -- Descend* functions for iterating backwards. -- Iteration performance boost. -- User defined context. - -User defined context --------------------- -This is a great new feature that allows for entering the same item into multiple B-trees, and each B-tree have a different ordering formula. - -For example: - -```go -package main - -import ( - "fmt" - - "github.com/tidwall/btree" -) - -type Item struct { - Key, Val string -} - -func (i1 *Item) Less(item btree.Item, ctx interface{}) bool { - i2 := item.(*Item) - switch tag := ctx.(type) { - case string: - if tag == "vals" { - if i1.Val < i2.Val { - return true - } else if i1.Val > i2.Val { - return false - } - // Both vals are equal so we should fall though - // and let the key comparison take over. - } - } - return i1.Key < i2.Key -} - -func main() { - - // Create a tree for keys and a tree for values. - // The "keys" tree will be sorted on the Keys field. - // The "values" tree will be sorted on the Values field. - keys := btree.New(16, "keys") - vals := btree.New(16, "vals") - - // Create some items. - users := []*Item{ - &Item{Key: "user:1", Val: "Jane"}, - &Item{Key: "user:2", Val: "Andy"}, - &Item{Key: "user:3", Val: "Steve"}, - &Item{Key: "user:4", Val: "Andrea"}, - &Item{Key: "user:5", Val: "Janet"}, - &Item{Key: "user:6", Val: "Andy"}, - } - - // Insert each user into both trees - for _, user := range users { - keys.ReplaceOrInsert(user) - vals.ReplaceOrInsert(user) - } - - // Iterate over each user in the key tree - keys.Ascend(func(item btree.Item) bool { - kvi := item.(*Item) - fmt.Printf("%s %s\n", kvi.Key, kvi.Val) - return true - }) - - fmt.Printf("\n") - // Iterate over each user in the val tree - vals.Ascend(func(item btree.Item) bool { - kvi := item.(*Item) - fmt.Printf("%s %s\n", kvi.Key, kvi.Val) - return true - }) -} - -// Should see the results -/* -user:1 Jane -user:2 Andy -user:3 Steve -user:4 Andrea -user:5 Janet -user:6 Andy - -user:4 Andrea -user:2 Andy -user:6 Andy -user:1 Jane -user:3 Steve -*/ -``` diff --git a/vendor/github.com/tidwall/btree/btree.go b/vendor/github.com/tidwall/btree/btree.go index 3b9a27de..8e38bb06 100644 --- a/vendor/github.com/tidwall/btree/btree.go +++ b/vendor/github.com/tidwall/btree/btree.go @@ -1,978 +1,605 @@ -// Copyright 2014 Google Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +// Copyright 2020 Joshua J Baker. All rights reserved. +// Use of this source code is governed by an MIT-style +// license that can be found in the LICENSE file. -// Package btree implements in-memory B-Trees of arbitrary degree. -// -// btree implements an in-memory B-Tree for use as an ordered data structure. -// It is not meant for persistent storage solutions. -// -// It has a flatter structure than an equivalent red-black or other binary tree, -// which in some cases yields better memory usage and/or performance. -// See some discussion on the matter here: -// http://google-opensource.blogspot.com/2013/01/c-containers-that-save-memory-and-time.html -// Note, though, that this project is in no way related to the C++ B-Tree -// implementation written about there. -// -// Within this tree, each node contains a slice of items and a (possibly nil) -// slice of children. For basic numeric values or raw structs, this can cause -// efficiency differences when compared to equivalent C++ template code that -// stores values in arrays within the node: -// * Due to the overhead of storing values as interfaces (each -// value needs to be stored as the value itself, then 2 words for the -// interface pointing to that value and its type), resulting in higher -// memory use. -// * Since interfaces can point to values anywhere in memory, values are -// most likely not stored in contiguous blocks, resulting in a higher -// number of cache misses. -// These issues don't tend to matter, though, when working with strings or other -// heap-allocated structures, since C++-equivalent structures also must store -// pointers and also distribute their values across the heap. -// -// This implementation is designed to be a drop-in replacement to gollrb.LLRB -// trees, (http://github.com/petar/gollrb), an excellent and probably the most -// widely used ordered tree implementation in the Go ecosystem currently. -// Its functions, therefore, exactly mirror those of -// llrb.LLRB where possible. Unlike gollrb, though, we currently don't -// support storing multiple equivalent values. package btree -import ( - "fmt" - "io" - "strings" - "sync" -) +const maxItems = 255 +const minItems = maxItems * 40 / 100 -// Item represents a single object in the tree. -type Item interface { - // Less tests whether the current item is less than the given argument. - // - // This must provide a strict weak ordering. - // If !a.Less(b) && !b.Less(a), we treat this to mean a == b (i.e. we can only - // hold one of either a or b in the tree). - // - // There is a user-defined ctx argument that is equal to the ctx value which - // is set at time of the btree contruction. - Less(than Item, ctx interface{}) bool +type node struct { + leaf bool + numItems int16 + items [maxItems]interface{} + children *[maxItems + 1]*node } -const ( - DefaultFreeListSize = 32 -) - -var ( - nilItems = make(items, 16) - nilChildren = make(children, 16) -) - -// FreeList represents a free list of btree nodes. By default each -// BTree has its own FreeList, but multiple BTrees can share the same -// FreeList. -// Two Btrees using the same freelist are safe for concurrent write access. -type FreeList struct { - mu sync.Mutex - freelist []*node +type justaLeaf struct { + leaf bool + numItems int16 + items [maxItems]interface{} } -// NewFreeList creates a new free list. -// size is the maximum size of the returned free list. -func NewFreeList(size int) *FreeList { - return &FreeList{freelist: make([]*node, 0, size)} +// BTree is an ordered set items +type BTree struct { + root *node + length int + less func(a, b interface{}) bool + lnode *node } -func (f *FreeList) newNode() (n *node) { - f.mu.Lock() - index := len(f.freelist) - 1 - if index < 0 { - f.mu.Unlock() - return new(node) +func newNode(leaf bool) *node { + n := &node{leaf: leaf} + if !leaf { + n.children = new([maxItems + 1]*node) } - n = f.freelist[index] - f.freelist[index] = nil - f.freelist = f.freelist[:index] - f.mu.Unlock() - return -} - -func (f *FreeList) freeNode(n *node) { - f.mu.Lock() - if len(f.freelist) < cap(f.freelist) { - f.freelist = append(f.freelist, n) - } - f.mu.Unlock() -} - -// ItemIterator allows callers of Ascend* to iterate in-order over portions of -// the tree. When this function returns false, iteration will stop and the -// associated Ascend* function will immediately return. -type ItemIterator func(i Item) bool - -// New creates a new B-Tree with the given degree. -// -// New(2), for example, will create a 2-3-4 tree (each node contains 1-3 items -// and 2-4 children). -func New(degree int, ctx interface{}) *BTree { - return NewWithFreeList(degree, NewFreeList(DefaultFreeListSize), ctx) -} - -// NewWithFreeList creates a new B-Tree that uses the given node free list. -func NewWithFreeList(degree int, f *FreeList, ctx interface{}) *BTree { - if degree <= 1 { - panic("bad degree") - } - return &BTree{ - degree: degree, - cow: ©OnWriteContext{freelist: f}, - ctx: ctx, - } -} - -// items stores items in a node. -type items []Item - -// insertAt inserts a value into the given index, pushing all subsequent values -// forward. -func (s *items) insertAt(index int, item Item) { - *s = append(*s, nil) - if index < len(*s) { - copy((*s)[index+1:], (*s)[index:]) - } - (*s)[index] = item -} - -// removeAt removes a value at a given index, pulling all subsequent values -// back. -func (s *items) removeAt(index int) Item { - item := (*s)[index] - copy((*s)[index:], (*s)[index+1:]) - (*s)[len(*s)-1] = nil - *s = (*s)[:len(*s)-1] - return item -} - -// pop removes and returns the last element in the list. -func (s *items) pop() (out Item) { - index := len(*s) - 1 - out = (*s)[index] - (*s)[index] = nil - *s = (*s)[:index] - return -} - -// truncate truncates this instance at index so that it contains only the -// first index items. index must be less than or equal to length. -func (s *items) truncate(index int) { - var toClear items - *s, toClear = (*s)[:index], (*s)[index:] - for len(toClear) > 0 { - toClear = toClear[copy(toClear, nilItems):] - } -} - -// find returns the index where the given item should be inserted into this -// list. 'found' is true if the item already exists in the list at the given -// index. -func (s items) find(item Item, ctx interface{}) (index int, found bool) { - i, j := 0, len(s) - for i < j { - h := i + (j-i)/2 - if !item.Less(s[h], ctx) { - i = h + 1 - } else { - j = h - } - } - if i > 0 && !s[i-1].Less(item, ctx) { - return i - 1, true - } - return i, false -} - -// children stores child nodes in a node. -type children []*node - -// insertAt inserts a value into the given index, pushing all subsequent values -// forward. -func (s *children) insertAt(index int, n *node) { - *s = append(*s, nil) - if index < len(*s) { - copy((*s)[index+1:], (*s)[index:]) - } - (*s)[index] = n -} - -// removeAt removes a value at a given index, pulling all subsequent values -// back. -func (s *children) removeAt(index int) *node { - n := (*s)[index] - copy((*s)[index:], (*s)[index+1:]) - (*s)[len(*s)-1] = nil - *s = (*s)[:len(*s)-1] return n } -// pop removes and returns the last element in the list. -func (s *children) pop() (out *node) { - index := len(*s) - 1 - out = (*s)[index] - (*s)[index] = nil - *s = (*s)[:index] - return +// PathHint is a utility type used with the *Hint() functions. Hints provide +// faster operations for clustered keys. +type PathHint struct { + path [8]uint8 } -// truncate truncates this instance at index so that it contains only the -// first index children. index must be less than or equal to length. -func (s *children) truncate(index int) { - var toClear children - *s, toClear = (*s)[:index], (*s)[index:] - for len(toClear) > 0 { - toClear = toClear[copy(toClear, nilChildren):] +// New returns a new BTree +func New(less func(a, b interface{}) bool) *BTree { + if less == nil { + panic("nil less") } + tr := new(BTree) + tr.less = less + return tr } -// node is an internal node in a tree. -// -// It must at all times maintain the invariant that either -// * len(children) == 0, len(items) unconstrained -// * len(children) == len(items) + 1 -type node struct { - items items - children children - cow *copyOnWriteContext +// Less is a convenience function that performs a comparison of two items +// using the same "less" function provided to New. +func (tr *BTree) Less(a, b interface{}) bool { + return tr.less(a, b) } -func (n *node) mutableFor(cow *copyOnWriteContext) *node { - if n.cow == cow { - return n +func (n *node) find(key interface{}, less func(a, b interface{}) bool, + hint *PathHint, depth int, +) (index int16, found bool) { + low := int16(0) + high := n.numItems - 1 + if hint != nil && depth < 8 { + index = int16(hint.path[depth]) + if index > n.numItems-1 { + index = n.numItems - 1 + } + if less(key, n.items[index]) { + high = index - 1 + } else if less(n.items[index], key) { + low = index + 1 + } else { + found = true + goto done + } } - out := cow.newNode() - if cap(out.items) >= len(n.items) { - out.items = out.items[:len(n.items)] + for low <= high { + mid := low + ((high+1)-low)/2 + if !less(key, n.items[mid]) { + low = mid + 1 + } else { + high = mid - 1 + } + } + if low > 0 && !less(n.items[low-1], key) && + !less(key, n.items[low-1]) { + index = low - 1 + found = true } else { - out.items = make(items, len(n.items), cap(n.items)) + index = low + found = false } - copy(out.items, n.items) - // Copy children - if cap(out.children) >= len(n.children) { - out.children = out.children[:len(n.children)] +done: + if hint != nil && depth < 8 { + if n.leaf && found { + hint.path[depth] = byte(index + 1) + } else { + hint.path[depth] = byte(index) + } + } + return index, found +} + +// SetHint sets or replace a value for a key using a path hint +func (tr *BTree) SetHint(item interface{}, hint *PathHint) (prev interface{}) { + if item == nil { + panic("nil item") + } + if tr.root == nil { + tr.root = newNode(true) + tr.root.items[0] = item + tr.root.numItems = 1 + tr.length = 1 + return + } + prev = tr.root.set(item, tr.less, hint, 0) + if prev != nil { + return prev + } + tr.lnode = nil + if tr.root.numItems == maxItems { + n := tr.root + right, median := n.split() + tr.root = newNode(false) + tr.root.children[0] = n + tr.root.items[0] = median + tr.root.children[1] = right + tr.root.numItems = 1 + } + tr.length++ + return prev +} + +// Set or replace a value for a key +func (tr *BTree) Set(item interface{}) (prev interface{}) { + return tr.SetHint(item, nil) +} + +func (n *node) split() (right *node, median interface{}) { + right = newNode(n.leaf) + median = n.items[maxItems/2] + copy(right.items[:maxItems/2], n.items[maxItems/2+1:]) + if !n.leaf { + copy(right.children[:maxItems/2+1], n.children[maxItems/2+1:]) + } + right.numItems = maxItems / 2 + if !n.leaf { + for i := maxItems/2 + 1; i < maxItems+1; i++ { + n.children[i] = nil + } + } + for i := maxItems / 2; i < maxItems; i++ { + n.items[i] = nil + } + n.numItems = maxItems / 2 + return right, median +} + +func (n *node) set(item interface{}, less func(a, b interface{}) bool, + hint *PathHint, depth int, +) (prev interface{}) { + i, found := n.find(item, less, hint, depth) + if found { + prev = n.items[i] + n.items[i] = item + return prev + } + if n.leaf { + copy(n.items[i+1:n.numItems+1], n.items[i:n.numItems]) + n.items[i] = item + n.numItems++ + return nil + } + prev = n.children[i].set(item, less, hint, depth+1) + if prev != nil { + return prev + } + if n.children[i].numItems == maxItems { + right, median := n.children[i].split() + copy(n.children[i+1:], n.children[i:]) + copy(n.items[i+1:], n.items[i:]) + n.items[i] = median + n.children[i+1] = right + n.numItems++ + } + return prev +} + +func (n *node) scan(iter func(item interface{}) bool) bool { + if n.leaf { + for i := int16(0); i < n.numItems; i++ { + if !iter(n.items[i]) { + return false + } + } + return true + } + for i := int16(0); i < n.numItems; i++ { + if !n.children[i].scan(iter) { + return false + } + if !iter(n.items[i]) { + return false + } + } + return n.children[n.numItems].scan(iter) +} + +// Get a value for key +func (tr *BTree) Get(key interface{}) interface{} { + return tr.GetHint(key, nil) +} + +// GetHint gets a value for key using a path hint +func (tr *BTree) GetHint(key interface{}, hint *PathHint) interface{} { + if tr.root == nil || key == nil { + return nil + } + depth := 0 + n := tr.root + for { + i, found := n.find(key, tr.less, hint, depth) + if found { + return n.items[i] + } + if n.leaf { + return nil + } + n = n.children[i] + depth++ + } +} + +// Len returns the number of items in the tree +func (tr *BTree) Len() int { + return tr.length +} + +// Delete a value for a key +func (tr *BTree) Delete(key interface{}) interface{} { + return tr.DeleteHint(key, nil) +} + +// DeleteHint deletes a value for a key using a path hint +func (tr *BTree) DeleteHint(key interface{}, hint *PathHint) interface{} { + if tr.root == nil || key == nil { + return nil + } + prev := tr.root.delete(false, key, tr.less, hint, 0) + if prev == nil { + return nil + } + tr.lnode = nil + if tr.root.numItems == 0 && !tr.root.leaf { + tr.root = tr.root.children[0] + } + tr.length-- + if tr.length == 0 { + tr.root = nil + } + return prev +} + +func (n *node) delete(max bool, key interface{}, + less func(a, b interface{}) bool, hint *PathHint, depth int, +) interface{} { + var i int16 + var found bool + if max { + i, found = n.numItems-1, true } else { - out.children = make(children, len(n.children), cap(n.children)) + i, found = n.find(key, less, hint, depth) } - copy(out.children, n.children) - return out + if n.leaf { + if found { + prev := n.items[i] + // found the items at the leaf, remove it and return. + copy(n.items[i:], n.items[i+1:n.numItems]) + n.items[n.numItems-1] = nil + n.numItems-- + return prev + } + return nil + } + + var prev interface{} + if found { + if max { + i++ + prev = n.children[i].delete(true, "", less, nil, 0) + } else { + prev = n.items[i] + maxItem := n.children[i].delete(true, "", less, nil, 0) + n.items[i] = maxItem + } + } else { + prev = n.children[i].delete(max, key, less, hint, depth+1) + } + if prev == nil { + return nil + } + if n.children[i].numItems < minItems { + if i == n.numItems { + i-- + } + if n.children[i].numItems+n.children[i+1].numItems+1 < maxItems { + // merge left + item + right + n.children[i].items[n.children[i].numItems] = n.items[i] + copy(n.children[i].items[n.children[i].numItems+1:], + n.children[i+1].items[:n.children[i+1].numItems]) + if !n.children[0].leaf { + copy(n.children[i].children[n.children[i].numItems+1:], + n.children[i+1].children[:n.children[i+1].numItems+1]) + } + n.children[i].numItems += n.children[i+1].numItems + 1 + copy(n.items[i:], n.items[i+1:n.numItems]) + copy(n.children[i+1:], n.children[i+2:n.numItems+1]) + n.items[n.numItems] = nil + n.children[n.numItems+1] = nil + n.numItems-- + } else if n.children[i].numItems > n.children[i+1].numItems { + // move left -> right + copy(n.children[i+1].items[1:], + n.children[i+1].items[:n.children[i+1].numItems]) + if !n.children[0].leaf { + copy(n.children[i+1].children[1:], + n.children[i+1].children[:n.children[i+1].numItems+1]) + } + n.children[i+1].items[0] = n.items[i] + if !n.children[0].leaf { + n.children[i+1].children[0] = + n.children[i].children[n.children[i].numItems] + } + n.children[i+1].numItems++ + n.items[i] = n.children[i].items[n.children[i].numItems-1] + n.children[i].items[n.children[i].numItems-1] = nil + if !n.children[0].leaf { + n.children[i].children[n.children[i].numItems] = nil + } + n.children[i].numItems-- + } else { + // move right -> left + n.children[i].items[n.children[i].numItems] = n.items[i] + if !n.children[0].leaf { + n.children[i].children[n.children[i].numItems+1] = + n.children[i+1].children[0] + } + n.children[i].numItems++ + n.items[i] = n.children[i+1].items[0] + copy(n.children[i+1].items[:], + n.children[i+1].items[1:n.children[i+1].numItems]) + if !n.children[0].leaf { + copy(n.children[i+1].children[:], + n.children[i+1].children[1:n.children[i+1].numItems+1]) + } + n.children[i+1].numItems-- + } + } + return prev } -func (n *node) mutableChild(i int) *node { - c := n.children[i].mutableFor(n.cow) - n.children[i] = c - return c +// Ascend the tree within the range [pivot, last] +// Pass nil for pivot to scan all item in ascending order +// Return false to stop iterating +func (tr *BTree) Ascend(pivot interface{}, iter func(item interface{}) bool) { + if tr.root == nil { + return + } + if pivot == nil { + tr.root.scan(iter) + } else if tr.root != nil { + tr.root.ascend(pivot, tr.less, nil, 0, iter) + } } -// split splits the given node at the given index. The current node shrinks, -// and this function returns the item that existed at that index and a new node -// containing all items/children after it. -func (n *node) split(i int) (Item, *node) { - item := n.items[i] - next := n.cow.newNode() - next.items = append(next.items, n.items[i+1:]...) - n.items.truncate(i) - if len(n.children) > 0 { - next.children = append(next.children, n.children[i+1:]...) - n.children.truncate(i + 1) +func (n *node) ascend(pivot interface{}, less func(a, b interface{}) bool, + hint *PathHint, depth int, iter func(item interface{}) bool, +) bool { + i, found := n.find(pivot, less, hint, depth) + if !found { + if !n.leaf { + if !n.children[i].ascend(pivot, less, hint, depth+1, iter) { + return false + } + } } - return item, next -} - -// maybeSplitChild checks if a child should be split, and if so splits it. -// Returns whether or not a split occurred. -func (n *node) maybeSplitChild(i, maxItems int) bool { - if len(n.children[i].items) < maxItems { - return false + for ; i < n.numItems; i++ { + if !iter(n.items[i]) { + return false + } + if !n.leaf { + if !n.children[i+1].scan(iter) { + return false + } + } } - first := n.mutableChild(i) - item, second := first.split(maxItems / 2) - n.items.insertAt(i, item) - n.children.insertAt(i+1, second) return true } -// insert inserts an item into the subtree rooted at this node, making sure -// no nodes in the subtree exceed maxItems items. Should an equivalent item be -// be found/replaced by insert, it will be returned. -func (n *node) insert(item Item, maxItems int, ctx interface{}) Item { - i, found := n.items.find(item, ctx) - if found { - out := n.items[i] - n.items[i] = item - return out - } - if len(n.children) == 0 { - n.items.insertAt(i, item) - return nil - } - if n.maybeSplitChild(i, maxItems) { - inTree := n.items[i] - switch { - case item.Less(inTree, ctx): - // no change, we want first split node - case inTree.Less(item, ctx): - i++ // we want second split node - default: - out := n.items[i] - n.items[i] = item - return out - } - } - return n.mutableChild(i).insert(item, maxItems, ctx) -} - -// get finds the given key in the subtree and returns it. -func (n *node) get(key Item, ctx interface{}) Item { - i, found := n.items.find(key, ctx) - if found { - return n.items[i] - } else if len(n.children) > 0 { - return n.children[i].get(key, ctx) - } - return nil -} - -// min returns the first item in the subtree. -func min(n *node) Item { - if n == nil { - return nil - } - for len(n.children) > 0 { - n = n.children[0] - } - if len(n.items) == 0 { - return nil - } - return n.items[0] -} - -// max returns the last item in the subtree. -func max(n *node) Item { - if n == nil { - return nil - } - for len(n.children) > 0 { - n = n.children[len(n.children)-1] - } - if len(n.items) == 0 { - return nil - } - return n.items[len(n.items)-1] -} - -// toRemove details what item to remove in a node.remove call. -type toRemove int - -const ( - removeItem toRemove = iota // removes the given item - removeMin // removes smallest item in the subtree - removeMax // removes largest item in the subtree -) - -// remove removes an item from the subtree rooted at this node. -func (n *node) remove(item Item, minItems int, typ toRemove, ctx interface{}) Item { - var i int - var found bool - switch typ { - case removeMax: - if len(n.children) == 0 { - return n.items.pop() - } - i = len(n.items) - case removeMin: - if len(n.children) == 0 { - return n.items.removeAt(0) - } - i = 0 - case removeItem: - i, found = n.items.find(item, ctx) - if len(n.children) == 0 { - if found { - return n.items.removeAt(i) +func (n *node) reverse(iter func(item interface{}) bool) bool { + if n.leaf { + for i := n.numItems - 1; i >= 0; i-- { + if !iter(n.items[i]) { + return false } + } + return true + } + if !n.children[n.numItems].reverse(iter) { + return false + } + for i := n.numItems - 1; i >= 0; i-- { + if !iter(n.items[i]) { + return false + } + if !n.children[i].reverse(iter) { + return false + } + } + return true +} + +// Descend the tree within the range [pivot, first] +// Pass nil for pivot to scan all item in descending order +// Return false to stop iterating +func (tr *BTree) Descend(pivot interface{}, iter func(item interface{}) bool) { + if tr.root == nil { + return + } + if pivot == nil { + tr.root.reverse(iter) + } else if tr.root != nil { + tr.root.descend(pivot, tr.less, nil, 0, iter) + } +} + +func (n *node) descend(pivot interface{}, less func(a, b interface{}) bool, + hint *PathHint, depth int, iter func(item interface{}) bool, +) bool { + i, found := n.find(pivot, less, hint, depth) + if !found { + if !n.leaf { + if !n.children[i].descend(pivot, less, hint, depth+1, iter) { + return false + } + } + i-- + } + for ; i >= 0; i-- { + if !iter(n.items[i]) { + return false + } + if !n.leaf { + if !n.children[i].reverse(iter) { + return false + } + } + } + return true +} + +// Load is for bulk loading pre-sorted items +func (tr *BTree) Load(item interface{}) interface{} { + if item == nil { + panic("nil item") + } + if tr.lnode != nil && tr.lnode.numItems < maxItems-2 { + if tr.less(tr.lnode.items[tr.lnode.numItems-1], item) { + tr.lnode.items[tr.lnode.numItems] = item + tr.lnode.numItems++ + tr.length++ return nil } - default: - panic("invalid type") } - // If we get to here, we have children. - if len(n.children[i].items) <= minItems { - return n.growChildAndRemove(i, item, minItems, typ, ctx) + prev := tr.Set(item) + if prev != nil { + return prev } - child := n.mutableChild(i) - // Either we had enough items to begin with, or we've done some - // merging/stealing, because we've got enough now and we're ready to return - // stuff. - if found { - // The item exists at index 'i', and the child we've selected can give us a - // predecessor, since if we've gotten here it's got > minItems items in it. - out := n.items[i] - // We use our special-case 'remove' call with typ=maxItem to pull the - // predecessor of item i (the rightmost leaf of our immediate left child) - // and set it into where we pulled the item from. - n.items[i] = child.remove(nil, minItems, removeMax, ctx) - return out - } - // Final recursive call. Once we're here, we know that the item isn't in this - // node and that the child is big enough to remove from. - return child.remove(item, minItems, typ, ctx) -} - -// growChildAndRemove grows child 'i' to make sure it's possible to remove an -// item from it while keeping it at minItems, then calls remove to actually -// remove it. -// -// Most documentation says we have to do two sets of special casing: -// 1) item is in this node -// 2) item is in child -// In both cases, we need to handle the two subcases: -// A) node has enough values that it can spare one -// B) node doesn't have enough values -// For the latter, we have to check: -// a) left sibling has node to spare -// b) right sibling has node to spare -// c) we must merge -// To simplify our code here, we handle cases #1 and #2 the same: -// If a node doesn't have enough items, we make sure it does (using a,b,c). -// We then simply redo our remove call, and the second time (regardless of -// whether we're in case 1 or 2), we'll have enough items and can guarantee -// that we hit case A. -func (n *node) growChildAndRemove(i int, item Item, minItems int, typ toRemove, ctx interface{}) Item { - if i > 0 && len(n.children[i-1].items) > minItems { - // Steal from left child - child := n.mutableChild(i) - stealFrom := n.mutableChild(i - 1) - stolenItem := stealFrom.items.pop() - child.items.insertAt(0, n.items[i-1]) - n.items[i-1] = stolenItem - if len(stealFrom.children) > 0 { - child.children.insertAt(0, stealFrom.children.pop()) + n := tr.root + for { + if n.leaf { + tr.lnode = n + break } - } else if i < len(n.items) && len(n.children[i+1].items) > minItems { - // steal from right child - child := n.mutableChild(i) - stealFrom := n.mutableChild(i + 1) - stolenItem := stealFrom.items.removeAt(0) - child.items = append(child.items, n.items[i]) - n.items[i] = stolenItem - if len(stealFrom.children) > 0 { - child.children = append(child.children, stealFrom.children.removeAt(0)) - } - } else { - if i >= len(n.items) { - i-- - } - child := n.mutableChild(i) - // merge with right child - mergeItem := n.items.removeAt(i) - mergeChild := n.children.removeAt(i + 1) - child.items = append(child.items, mergeItem) - child.items = append(child.items, mergeChild.items...) - child.children = append(child.children, mergeChild.children...) - n.cow.freeNode(mergeChild) - } - return n.remove(item, minItems, typ, ctx) -} - -type direction int - -const ( - descend = direction(-1) - ascend = direction(+1) -) - -// iterate provides a simple method for iterating over elements in the tree. -// -// When ascending, the 'start' should be less than 'stop' and when descending, -// the 'start' should be greater than 'stop'. Setting 'includeStart' to true -// will force the iterator to include the first item when it equals 'start', -// thus creating a "greaterOrEqual" or "lessThanEqual" rather than just a -// "greaterThan" or "lessThan" queries. -func (n *node) iterate(dir direction, start, stop Item, includeStart bool, hit bool, iter ItemIterator, ctx interface{}) (bool, bool) { - var ok bool - switch dir { - case ascend: - for i := 0; i < len(n.items); i++ { - if start != nil && n.items[i].Less(start, ctx) { - continue - } - if len(n.children) > 0 { - if hit, ok = n.children[i].iterate(dir, start, stop, includeStart, hit, iter, ctx); !ok { - return hit, false - } - } - if !includeStart && !hit && start != nil && !start.Less(n.items[i], ctx) { - hit = true - continue - } - hit = true - if stop != nil && !n.items[i].Less(stop, ctx) { - return hit, false - } - if !iter(n.items[i]) { - return hit, false - } - } - if len(n.children) > 0 { - if hit, ok = n.children[len(n.children)-1].iterate(dir, start, stop, includeStart, hit, iter, ctx); !ok { - return hit, false - } - } - case descend: - for i := len(n.items) - 1; i >= 0; i-- { - if start != nil && !n.items[i].Less(start, ctx) { - if !includeStart || hit || start.Less(n.items[i], ctx) { - continue - } - } - if len(n.children) > 0 { - if hit, ok = n.children[i+1].iterate(dir, start, stop, includeStart, hit, iter, ctx); !ok { - return hit, false - } - } - if stop != nil && !stop.Less(n.items[i], ctx) { - return hit, false // continue - } - hit = true - if !iter(n.items[i]) { - return hit, false - } - } - if len(n.children) > 0 { - if hit, ok = n.children[0].iterate(dir, start, stop, includeStart, hit, iter, ctx); !ok { - return hit, false - } - } - } - return hit, true -} - -// Used for testing/debugging purposes. -func (n *node) print(w io.Writer, level int) { - fmt.Fprintf(w, "%sNODE:%v\n", strings.Repeat(" ", level), n.items) - for _, c := range n.children { - c.print(w, level+1) - } -} - -// BTree is an implementation of a B-Tree. -// -// BTree stores Item instances in an ordered structure, allowing easy insertion, -// removal, and iteration. -// -// Write operations are not safe for concurrent mutation by multiple -// goroutines, but Read operations are. -type BTree struct { - degree int - length int - root *node - ctx interface{} - cow *copyOnWriteContext -} - -// copyOnWriteContext pointers determine node ownership... a tree with a write -// context equivalent to a node's write context is allowed to modify that node. -// A tree whose write context does not match a node's is not allowed to modify -// it, and must create a new, writable copy (IE: it's a Clone). -// -// When doing any write operation, we maintain the invariant that the current -// node's context is equal to the context of the tree that requested the write. -// We do this by, before we descend into any node, creating a copy with the -// correct context if the contexts don't match. -// -// Since the node we're currently visiting on any write has the requesting -// tree's context, that node is modifiable in place. Children of that node may -// not share context, but before we descend into them, we'll make a mutable -// copy. -type copyOnWriteContext struct { - freelist *FreeList -} - -// Clone clones the btree, lazily. Clone should not be called concurrently, -// but the original tree (t) and the new tree (t2) can be used concurrently -// once the Clone call completes. -// -// The internal tree structure of b is marked read-only and shared between t and -// t2. Writes to both t and t2 use copy-on-write logic, creating new nodes -// whenever one of b's original nodes would have been modified. Read operations -// should have no performance degredation. Write operations for both t and t2 -// will initially experience minor slow-downs caused by additional allocs and -// copies due to the aforementioned copy-on-write logic, but should converge to -// the original performance characteristics of the original tree. -func (t *BTree) Clone() (t2 *BTree) { - // Create two entirely new copy-on-write contexts. - // This operation effectively creates three trees: - // the original, shared nodes (old b.cow) - // the new b.cow nodes - // the new out.cow nodes - cow1, cow2 := *t.cow, *t.cow - out := *t - t.cow = &cow1 - out.cow = &cow2 - return &out -} - -// maxItems returns the max number of items to allow per node. -func (t *BTree) maxItems() int { - return t.degree*2 - 1 -} - -// minItems returns the min number of items to allow per node (ignored for the -// root node). -func (t *BTree) minItems() int { - return t.degree - 1 -} - -func (c *copyOnWriteContext) newNode() (n *node) { - n = c.freelist.newNode() - n.cow = c - return -} - -func (c *copyOnWriteContext) freeNode(n *node) { - if n.cow == c { - // clear to allow GC - n.items.truncate(0) - n.children.truncate(0) - n.cow = nil - c.freelist.freeNode(n) - } -} - -// ReplaceOrInsert adds the given item to the tree. If an item in the tree -// already equals the given one, it is removed from the tree and returned. -// Otherwise, nil is returned. -// -// nil cannot be added to the tree (will panic). -func (t *BTree) ReplaceOrInsert(item Item) Item { - if item == nil { - panic("nil item being added to BTree") - } - if t.root == nil { - t.root = t.cow.newNode() - t.root.items = append(t.root.items, item) - t.length++ - return nil - } else { - t.root = t.root.mutableFor(t.cow) - if len(t.root.items) >= t.maxItems() { - item2, second := t.root.split(t.maxItems() / 2) - oldroot := t.root - t.root = t.cow.newNode() - t.root.items = append(t.root.items, item2) - t.root.children = append(t.root.children, oldroot, second) - } - } - out := t.root.insert(item, t.maxItems(), t.ctx) - if out == nil { - t.length++ - } - return out -} - -// Delete removes an item equal to the passed in item from the tree, returning -// it. If no such item exists, returns nil. -func (t *BTree) Delete(item Item) Item { - return t.deleteItem(item, removeItem, t.ctx) -} - -// DeleteMin removes the smallest item in the tree and returns it. -// If no such item exists, returns nil. -func (t *BTree) DeleteMin() Item { - return t.deleteItem(nil, removeMin, t.ctx) -} - -// DeleteMax removes the largest item in the tree and returns it. -// If no such item exists, returns nil. -func (t *BTree) DeleteMax() Item { - return t.deleteItem(nil, removeMax, t.ctx) -} - -func (t *BTree) deleteItem(item Item, typ toRemove, ctx interface{}) Item { - if t.root == nil || len(t.root.items) == 0 { - return nil - } - t.root = t.root.mutableFor(t.cow) - out := t.root.remove(item, t.minItems(), typ, ctx) - if len(t.root.items) == 0 && len(t.root.children) > 0 { - oldroot := t.root - t.root = t.root.children[0] - t.cow.freeNode(oldroot) - } - if out != nil { - t.length-- - } - return out -} - -// AscendRange calls the iterator for every value in the tree within the range -// [greaterOrEqual, lessThan), until iterator returns false. -func (t *BTree) AscendRange(greaterOrEqual, lessThan Item, iterator ItemIterator) { - if t.root == nil { - return - } - t.root.iterate(ascend, greaterOrEqual, lessThan, true, false, iterator, t.ctx) -} - -// AscendLessThan calls the iterator for every value in the tree within the range -// [first, pivot), until iterator returns false. -func (t *BTree) AscendLessThan(pivot Item, iterator ItemIterator) { - if t.root == nil { - return - } - t.root.iterate(ascend, nil, pivot, false, false, iterator, t.ctx) -} - -// AscendGreaterOrEqual calls the iterator for every value in the tree within -// the range [pivot, last], until iterator returns false. -func (t *BTree) AscendGreaterOrEqual(pivot Item, iterator ItemIterator) { - if t.root == nil { - return - } - t.root.iterate(ascend, pivot, nil, true, false, iterator, t.ctx) -} - -// Ascend calls the iterator for every value in the tree within the range -// [first, last], until iterator returns false. -func (t *BTree) Ascend(iterator ItemIterator) { - if t.root == nil { - return - } - t.root.iterate(ascend, nil, nil, false, false, iterator, t.ctx) -} - -// DescendRange calls the iterator for every value in the tree within the range -// [lessOrEqual, greaterThan), until iterator returns false. -func (t *BTree) DescendRange(lessOrEqual, greaterThan Item, iterator ItemIterator) { - if t.root == nil { - return - } - t.root.iterate(descend, lessOrEqual, greaterThan, true, false, iterator, t.ctx) -} - -// DescendLessOrEqual calls the iterator for every value in the tree within the range -// [pivot, first], until iterator returns false. -func (t *BTree) DescendLessOrEqual(pivot Item, iterator ItemIterator) { - if t.root == nil { - return - } - t.root.iterate(descend, pivot, nil, true, false, iterator, t.ctx) -} - -// DescendGreaterThan calls the iterator for every value in the tree within -// the range (pivot, last], until iterator returns false. -func (t *BTree) DescendGreaterThan(pivot Item, iterator ItemIterator) { - if t.root == nil { - return - } - t.root.iterate(descend, nil, pivot, false, false, iterator, t.ctx) -} - -// Descend calls the iterator for every value in the tree within the range -// [last, first], until iterator returns false. -func (t *BTree) Descend(iterator ItemIterator) { - if t.root == nil { - return - } - t.root.iterate(descend, nil, nil, false, false, iterator, t.ctx) -} - -// Get looks for the key item in the tree, returning it. It returns nil if -// unable to find that item. -func (t *BTree) Get(key Item) Item { - if t.root == nil { - return nil - } - return t.root.get(key, t.ctx) -} - -// Min returns the smallest item in the tree, or nil if the tree is empty. -func (t *BTree) Min() Item { - return min(t.root) -} - -// Max returns the largest item in the tree, or nil if the tree is empty. -func (t *BTree) Max() Item { - return max(t.root) -} - -// Has returns true if the given key is in the tree. -func (t *BTree) Has(key Item) bool { - return t.Get(key) != nil -} - -// Len returns the number of items currently in the tree. -func (t *BTree) Len() int { - return t.length -} - -// Context returns the context of the tree. -func (t *BTree) Context() interface{} { - return t.ctx -} - -// SetContext will replace the context of the tree. -func (t *BTree) SetContext(ctx interface{}) { - t.ctx = ctx -} - -// Int implements the Item interface for integers. -type Int int - -// Less returns true if int(a) < int(b). -func (a Int) Less(b Item, ctx interface{}) bool { - return a < b.(Int) -} - -type stackItem struct { - n *node // current node - i int // index of the next child/item. -} - -// Cursor represents an iterator that can traverse over all items in the tree -// in sorted order. -// -// Changing data while traversing a cursor may result in unexpected items to -// be returned. You must reposition your cursor after mutating data. -type Cursor struct { - t *BTree - stack []stackItem -} - -// Cursor returns a new cursor used to traverse over items in the tree. -func (t *BTree) Cursor() *Cursor { - return &Cursor{t: t} -} - -// First moves the cursor to the first item in the tree and returns that item. -func (c *Cursor) First() Item { - c.stack = c.stack[:0] - n := c.t.root - if n == nil { - return nil - } - c.stack = append(c.stack, stackItem{n: n}) - for len(n.children) > 0 { - n = n.children[0] - c.stack = append(c.stack, stackItem{n: n}) - } - if len(n.items) == 0 { - return nil - } - return n.items[0] -} - -// Next moves the cursor to the next item and returns that item. -func (c *Cursor) Next() Item { - if len(c.stack) == 0 { - return nil - } - si := len(c.stack) - 1 - c.stack[si].i++ - n := c.stack[si].n - i := c.stack[si].i - if i == len(n.children)+len(n.items) { - c.stack = c.stack[:len(c.stack)-1] - return c.Next() - } - if len(n.children) == 0 { - if i >= len(n.items) { - c.stack = c.stack[:len(c.stack)-1] - return c.Next() - } - return n.items[i] - } else if i%2 == 1 { - return n.items[i/2] - } - c.stack = append(c.stack, stackItem{n: n.children[i/2], i: -1}) - return c.Next() - -} - -// Last moves the cursor to the last item in the tree and returns that item. -func (c *Cursor) Last() Item { - c.stack = c.stack[:0] - n := c.t.root - if n == nil { - return nil - } - c.stack = append(c.stack, stackItem{n: n, i: len(n.children) + len(n.items) - 1}) - for len(n.children) > 0 { - n = n.children[len(n.children)-1] - c.stack = append(c.stack, stackItem{n: n, i: len(n.children) + len(n.items) - 1}) - } - if len(n.items) == 0 { - return nil - } - return n.items[len(n.items)-1] -} - -// Prev moves the cursor to the previous item and returns that item. -func (c *Cursor) Prev() Item { - if len(c.stack) == 0 { - return nil - } - si := len(c.stack) - 1 - c.stack[si].i-- - n := c.stack[si].n - i := c.stack[si].i - if i == -1 { - c.stack = c.stack[:len(c.stack)-1] - return c.Prev() - } - if len(n.children) == 0 { - return n.items[i] - } else if i%2 == 1 { - return n.items[i/2] - } - child := n.children[i/2] - c.stack = append(c.stack, stackItem{n: child, - i: len(child.children) + len(child.items)}) - return c.Prev() -} - -// Seek moves the cursor to provided item and returns that item. -// If the item does not exist then the next item is returned. -func (c *Cursor) Seek(pivot Item) Item { - c.stack = c.stack[:0] - n := c.t.root - for n != nil { - i, found := n.items.find(pivot, c.t.ctx) - c.stack = append(c.stack, stackItem{n: n}) - if found { - if len(n.children) == 0 { - c.stack[len(c.stack)-1].i = i - } else { - c.stack[len(c.stack)-1].i = i*2 + 1 - } - return n.items[i] - } - if len(n.children) == 0 { - if i == len(n.items) { - c.stack[len(c.stack)-1].i = i + 1 - return c.Next() - } - c.stack[len(c.stack)-1].i = i - return n.items[i] - } - c.stack[len(c.stack)-1].i = i * 2 - n = n.children[i] + n = n.children[n.numItems] } return nil } + +// Min returns the minimum item in tree. +// Returns nil if the tree has no items. +func (tr *BTree) Min() interface{} { + if tr.root == nil { + return nil + } + n := tr.root + for { + if n.leaf { + return n.items[0] + } + n = n.children[0] + } +} + +// Max returns the maximum item in tree. +// Returns nil if the tree has no items. +func (tr *BTree) Max() interface{} { + if tr.root == nil { + return nil + } + n := tr.root + for { + if n.leaf { + return n.items[n.numItems-1] + } + n = n.children[n.numItems] + } +} + +// PopMin removes the minimum item in tree and returns it. +// Returns nil if the tree has no items. +func (tr *BTree) PopMin() interface{} { + if tr.root == nil { + return nil + } + tr.lnode = nil + n := tr.root + for { + if n.leaf { + item := n.items[0] + if n.numItems == minItems { + return tr.Delete(item) + } + copy(n.items[:], n.items[1:]) + n.items[n.numItems-1] = nil + n.numItems-- + tr.length-- + return item + } + n = n.children[0] + } +} + +// PopMax removes the minimum item in tree and returns it. +// Returns nil if the tree has no items. +func (tr *BTree) PopMax() interface{} { + if tr.root == nil { + return nil + } + tr.lnode = nil + n := tr.root + for { + if n.leaf { + item := n.items[n.numItems-1] + if n.numItems == minItems { + return tr.Delete(item) + } + n.items[n.numItems-1] = nil + n.numItems-- + tr.length-- + return item + } + n = n.children[n.numItems] + } +} + +// Height returns the height of the tree. +// Returns zero if tree has no items. +func (tr *BTree) Height() int { + var height int + if tr.root != nil { + n := tr.root + for { + height++ + if n.leaf { + break + } + n = n.children[n.numItems] + } + } + return height +} + +// Walk iterates over all items in tree, in order. +// The items param will contain one or more items. +func (tr *BTree) Walk(iter func(item []interface{})) { + if tr.root != nil { + tr.root.walk(iter) + } +} + +func (n *node) walk(iter func(item []interface{})) { + if n.leaf { + iter(n.items[:n.numItems]) + } else { + for i := int16(0); i < n.numItems; i++ { + n.children[i].walk(iter) + iter(n.items[i : i+1]) + } + n.children[n.numItems].walk(iter) + } +} diff --git a/vendor/github.com/tidwall/buntdb/.travis.yml b/vendor/github.com/tidwall/buntdb/.travis.yml index 4f2ee4d9..10f5fa48 100644 --- a/vendor/github.com/tidwall/buntdb/.travis.yml +++ b/vendor/github.com/tidwall/buntdb/.travis.yml @@ -1 +1,4 @@ language: go + +go: + - 1.15.x diff --git a/vendor/github.com/tidwall/buntdb/README.md b/vendor/github.com/tidwall/buntdb/README.md index 31798093..88f6d19b 100644 --- a/vendor/github.com/tidwall/buntdb/README.md +++ b/vendor/github.com/tidwall/buntdb/README.md @@ -576,7 +576,7 @@ var config buntdb.Config if err := db.ReadConfig(&config); err != nil{ log.Fatal(err) } -if err := db.WriteConfig(config); err != nil{ +if err := db.SetConfig(config); err != nil{ log.Fatal(err) } ``` diff --git a/vendor/github.com/tidwall/buntdb/buntdb.go b/vendor/github.com/tidwall/buntdb/buntdb.go index 35f85200..ca2152eb 100644 --- a/vendor/github.com/tidwall/buntdb/buntdb.go +++ b/vendor/github.com/tidwall/buntdb/buntdb.go @@ -1,7 +1,7 @@ // Package buntdb implements a low-level in-memory key/value store in pure Go. // It persists to disk, is ACID compliant, and uses locking for multiple -// readers and a single writer. Bunt is ideal for projects that need -// a dependable database, and favor speed over data size. +// readers and a single writer. Bunt is ideal for projects that need a +// dependable database, and favor speed over data size. package buntdb import ( @@ -122,10 +122,11 @@ type Config struct { // has been expired. OnExpired func(keys []string) - // OnExpiredSync will be called inside the same transaction that is performing - // the deletion of expired items. If OnExpired is present then this callback - // will not be called. If this callback is present, then the deletion of the - // timeed-out item is the explicit responsibility of this callback. + // OnExpiredSync will be called inside the same transaction that is + // performing the deletion of expired items. If OnExpired is present then + // this callback will not be called. If this callback is present, then the + // deletion of the timeed-out item is the explicit responsibility of this + // callback. OnExpiredSync func(key, value string, tx *Tx) error } @@ -142,8 +143,8 @@ const btreeDegrees = 64 func Open(path string) (*DB, error) { db := &DB{} // initialize trees and indexes - db.keys = btree.New(btreeDegrees, nil) - db.exps = btree.New(btreeDegrees, &exctx{db}) + db.keys = btree.New(lessCtx(nil)) + db.exps = btree.New(lessCtx(&exctx{db})) db.idxs = make(map[string]*index) // initialize default configuration db.config = Config{ @@ -204,7 +205,7 @@ func (db *DB) Save(wr io.Writer) error { // use a buffered writer and flush every 4MB var buf []byte // iterated through every item in the database and write to the buffer - db.keys.Ascend(func(item btree.Item) bool { + btreeAscend(db.keys, func(item interface{}) bool { dbi := item.(*dbItem) buf = dbi.writeSetTo(buf) if len(buf) > 1024*1024*4 { @@ -285,7 +286,7 @@ func (idx *index) clearCopy() *index { } // initialize with empty trees if nidx.less != nil { - nidx.btr = btree.New(btreeDegrees, nidx) + nidx.btr = btree.New(lessCtx(nidx)) } if nidx.rect != nil { nidx.rtr = rtree.New(nidx) @@ -297,20 +298,20 @@ func (idx *index) clearCopy() *index { func (idx *index) rebuild() { // initialize trees if idx.less != nil { - idx.btr = btree.New(btreeDegrees, idx) + idx.btr = btree.New(lessCtx(idx)) } if idx.rect != nil { idx.rtr = rtree.New(idx) } // iterate through all keys and fill the index - idx.db.keys.Ascend(func(item btree.Item) bool { + btreeAscend(idx.db.keys, func(item interface{}) bool { dbi := item.(*dbItem) if !idx.match(dbi.key) { - // does not match the pattern, conintue + // does not match the pattern, continue return true } if idx.less != nil { - idx.btr.ReplaceOrInsert(dbi) + idx.btr.Set(dbi) } if idx.rect != nil { idx.rtr.Insert(dbi) @@ -334,8 +335,6 @@ func (idx *index) rebuild() { // less function to handle the content format and comparison. // There are some default less function that can be used such as // IndexString, IndexBinary, etc. -// -// Deprecated: Use Transactions func (db *DB) CreateIndex(name, pattern string, less ...func(a, b string) bool) error { return db.Update(func(tx *Tx) error { @@ -347,8 +346,6 @@ func (db *DB) CreateIndex(name, pattern string, // The items are ordered in an b-tree and can be retrieved using the // Ascend* and Descend* methods. // If a previous index with the same name exists, that index will be deleted. -// -// Deprecated: Use Transactions func (db *DB) ReplaceIndex(name, pattern string, less ...func(a, b string) bool) error { return db.Update(func(tx *Tx) error { @@ -381,8 +378,6 @@ func (db *DB) ReplaceIndex(name, pattern string, // Thus min[0] must be less-than-or-equal-to max[0]. // The IndexRect is a default function that can be used for the rect // parameter. -// -// Deprecated: Use Transactions func (db *DB) CreateSpatialIndex(name, pattern string, rect func(item string) (min, max []float64)) error { return db.Update(func(tx *Tx) error { @@ -394,8 +389,6 @@ func (db *DB) CreateSpatialIndex(name, pattern string, // The items are organized in an r-tree and can be retrieved using the // Intersects method. // If a previous index with the same name exists, that index will be deleted. -// -// Deprecated: Use Transactions func (db *DB) ReplaceSpatialIndex(name, pattern string, rect func(item string) (min, max []float64)) error { return db.Update(func(tx *Tx) error { @@ -415,8 +408,6 @@ func (db *DB) ReplaceSpatialIndex(name, pattern string, } // DropIndex removes an index. -// -// Deprecated: Use Transactions func (db *DB) DropIndex(name string) error { return db.Update(func(tx *Tx) error { return tx.DropIndex(name) @@ -424,8 +415,6 @@ func (db *DB) DropIndex(name string) error { } // Indexes returns a list of index names. -// -// Deprecated: Use Transactions func (db *DB) Indexes() ([]string, error) { var names []string var err = db.View(func(tx *Tx) error { @@ -468,7 +457,7 @@ func (db *DB) SetConfig(config Config) error { // will be replaced with the new one, and return the previous item. func (db *DB) insertIntoDatabase(item *dbItem) *dbItem { var pdbi *dbItem - prev := db.keys.ReplaceOrInsert(item) + prev := db.keys.Set(item) if prev != nil { // A previous item was removed from the keys tree. Let's // fully delete this item from all indexes. @@ -491,7 +480,7 @@ func (db *DB) insertIntoDatabase(item *dbItem) *dbItem { if item.opts != nil && item.opts.ex { // The new item has eviction options. Add it to the // expires tree - db.exps.ReplaceOrInsert(item) + db.exps.Set(item) } for _, idx := range db.idxs { if !idx.match(item.key) { @@ -499,7 +488,7 @@ func (db *DB) insertIntoDatabase(item *dbItem) *dbItem { } if idx.btr != nil { // Add new item to btree index. - idx.btr.ReplaceOrInsert(item) + idx.btr.Set(item) } if idx.rtr != nil { // Add new item to rtree index. @@ -569,9 +558,9 @@ func (db *DB) backgroundManager() { } } // produce a list of expired items that need removing - db.exps.AscendLessThan(&dbItem{ + btreeAscendLessThan(db.exps, &dbItem{ opts: &dbItemOpts{ex: true, exat: time.Now()}, - }, func(item btree.Item) bool { + }, func(item interface{}) bool { expired = append(expired, item.(*dbItem)) return true }) @@ -686,8 +675,8 @@ func (db *DB) Shrink() error { } done = true var n int - db.keys.AscendGreaterOrEqual(&dbItem{key: pivot}, - func(item btree.Item) bool { + btreeAscendGreaterOrEqual(db.keys, &dbItem{key: pivot}, + func(item interface{}) bool { dbi := item.(*dbItem) // 1000 items or 64MB buffer if n > 1000 || len(buf) > 64*1024*1024 { @@ -861,7 +850,7 @@ func (db *DB) readLoad(rd io.Reader, modTime time.Time) error { if len(parts) == 0 { continue } - if (parts[0][0] == 's' || parts[0][1] == 'S') && + if (parts[0][0] == 's' || parts[0][0] == 'S') && (parts[0][1] == 'e' || parts[0][1] == 'E') && (parts[0][2] == 't' || parts[0][2] == 'T') { // SET @@ -891,7 +880,7 @@ func (db *DB) readLoad(rd io.Reader, modTime time.Time) error { } else { db.insertIntoDatabase(&dbItem{key: parts[1], val: parts[2]}) } - } else if (parts[0][0] == 'd' || parts[0][1] == 'D') && + } else if (parts[0][0] == 'd' || parts[0][0] == 'D') && (parts[0][1] == 'e' || parts[0][1] == 'E') && (parts[0][2] == 'l' || parts[0][2] == 'L') { // DEL @@ -899,10 +888,10 @@ func (db *DB) readLoad(rd io.Reader, modTime time.Time) error { return ErrInvalid } db.deleteFromDatabase(&dbItem{key: parts[1]}) - } else if (parts[0][0] == 'f' || parts[0][1] == 'F') && + } else if (parts[0][0] == 'f' || parts[0][0] == 'F') && strings.ToLower(parts[0]) == "flushdb" { - db.keys = btree.New(btreeDegrees, nil) - db.exps = btree.New(btreeDegrees, &exctx{db}) + db.keys = btree.New(lessCtx(nil)) + db.exps = btree.New(lessCtx(&exctx{db})) db.idxs = make(map[string]*index) } else { return ErrInvalid @@ -1037,8 +1026,8 @@ func (tx *Tx) DeleteAll() error { } // now reset the live database trees - tx.db.keys = btree.New(btreeDegrees, nil) - tx.db.exps = btree.New(btreeDegrees, &exctx{tx.db}) + tx.db.keys = btree.New(lessCtx(nil)) + tx.db.exps = btree.New(lessCtx(&exctx{tx.db})) tx.db.idxs = make(map[string]*index) // finally re-create the indexes @@ -1276,8 +1265,7 @@ func (dbi *dbItem) expiresAt() time.Time { // to note that the ctx parameter is used to help with determine which // formula to use on an item. Each b-tree should use a different ctx when // sharing the same item. -func (dbi *dbItem) Less(item btree.Item, ctx interface{}) bool { - dbi2 := item.(*dbItem) +func (dbi *dbItem) Less(dbi2 *dbItem, ctx interface{}) bool { switch ctx := ctx.(type) { case *exctx: // The expires b-tree formula @@ -1307,6 +1295,12 @@ func (dbi *dbItem) Less(item btree.Item, ctx interface{}) bool { return dbi.key < dbi2.key } +func lessCtx(ctx interface{}) func(a, b interface{}) bool { + return func(a, b interface{}) bool { + return a.(*dbItem).Less(b.(*dbItem), ctx) + } +} + // Rect converts a string to a rectangle. // An invalid rectangle will cause a panic. func (dbi *dbItem) Rect(ctx interface{}) (min, max []float64) { @@ -1510,7 +1504,7 @@ func (tx *Tx) scan(desc, gt, lt bool, index, start, stop string, return ErrTxClosed } // wrap a btree specific iterator around the user-defined iterator. - iter := func(item btree.Item) bool { + iter := func(item interface{}) bool { dbi := item.(*dbItem) return iterator(dbi.key, dbi.val) } @@ -1554,26 +1548,26 @@ func (tx *Tx) scan(desc, gt, lt bool, index, start, stop string, if desc { if gt { if lt { - tr.DescendRange(itemA, itemB, iter) + btreeDescendRange(tr, itemA, itemB, iter) } else { - tr.DescendGreaterThan(itemA, iter) + btreeDescendGreaterThan(tr, itemA, iter) } } else if lt { - tr.DescendLessOrEqual(itemA, iter) + btreeDescendLessOrEqual(tr, itemA, iter) } else { - tr.Descend(iter) + btreeDescend(tr, iter) } } else { if gt { if lt { - tr.AscendRange(itemA, itemB, iter) + btreeAscendRange(tr, itemA, itemB, iter) } else { - tr.AscendGreaterOrEqual(itemA, iter) + btreeAscendGreaterOrEqual(tr, itemA, iter) } } else if lt { - tr.AscendLessThan(itemA, iter) + btreeAscendLessThan(tr, itemA, iter) } else { - tr.Ascend(iter) + btreeAscend(tr, iter) } } return nil @@ -2026,7 +2020,8 @@ func (tx *Tx) createIndex(name string, pattern string, if tx.wc.rbkeys == nil { // store the index in the rollback map. if _, ok := tx.wc.rollbackIndexes[name]; !ok { - // we use nil to indicate that the index should be removed upon rollback. + // we use nil to indicate that the index should be removed upon + // rollback. tx.wc.rollbackIndexes[name] = nil } } @@ -2056,8 +2051,8 @@ func (tx *Tx) DropIndex(name string) error { if tx.wc.rbkeys == nil { // store the index in the rollback map. if _, ok := tx.wc.rollbackIndexes[name]; !ok { - // we use a non-nil copy of the index without the data to indicate that the - // index should be rebuilt upon rollback. + // we use a non-nil copy of the index without the data to indicate + // that the index should be rebuilt upon rollback. tx.wc.rollbackIndexes[name] = idx.clearCopy() } } @@ -2193,3 +2188,67 @@ func IndexJSONCaseSensitive(path string) func(a, b string) bool { func Desc(less func(a, b string) bool) func(a, b string) bool { return func(a, b string) bool { return less(b, a) } } + +//// Wrappers around btree Ascend/Descend + +func bLT(tr *btree.BTree, a, b interface{}) bool { return tr.Less(a, b) } +func bGT(tr *btree.BTree, a, b interface{}) bool { return tr.Less(b, a) } + +// func bLTE(tr *btree.BTree, a, b interface{}) bool { return !tr.Less(b, a) } +// func bGTE(tr *btree.BTree, a, b interface{}) bool { return !tr.Less(a, b) } + +// Ascend + +func btreeAscend(tr *btree.BTree, iter func(item interface{}) bool) { + tr.Ascend(nil, iter) +} + +func btreeAscendLessThan(tr *btree.BTree, pivot interface{}, + iter func(item interface{}) bool, +) { + tr.Ascend(nil, func(item interface{}) bool { + return bLT(tr, item, pivot) && iter(item) + }) +} + +func btreeAscendGreaterOrEqual(tr *btree.BTree, pivot interface{}, + iter func(item interface{}) bool, +) { + tr.Ascend(pivot, iter) +} + +func btreeAscendRange(tr *btree.BTree, greaterOrEqual, lessThan interface{}, + iter func(item interface{}) bool, +) { + tr.Ascend(greaterOrEqual, func(item interface{}) bool { + return bLT(tr, item, lessThan) && iter(item) + }) +} + +// Descend + +func btreeDescend(tr *btree.BTree, iter func(item interface{}) bool) { + tr.Descend(nil, iter) +} + +func btreeDescendGreaterThan(tr *btree.BTree, pivot interface{}, + iter func(item interface{}) bool, +) { + tr.Descend(nil, func(item interface{}) bool { + return bGT(tr, item, pivot) && iter(item) + }) +} + +func btreeDescendRange(tr *btree.BTree, lessOrEqual, greaterThan interface{}, + iter func(item interface{}) bool, +) { + tr.Descend(lessOrEqual, func(item interface{}) bool { + return bGT(tr, item, greaterThan) && iter(item) + }) +} + +func btreeDescendLessOrEqual(tr *btree.BTree, pivot interface{}, + iter func(item interface{}) bool, +) { + tr.Descend(pivot, iter) +} diff --git a/vendor/github.com/tidwall/buntdb/go.mod b/vendor/github.com/tidwall/buntdb/go.mod new file mode 100644 index 00000000..225704cd --- /dev/null +++ b/vendor/github.com/tidwall/buntdb/go.mod @@ -0,0 +1,12 @@ +module github.com/tidwall/buntdb + +go 1.15 + +require ( + github.com/tidwall/btree v0.2.2 + github.com/tidwall/gjson v1.6.1 + github.com/tidwall/grect v0.0.0-20161006141115-ba9a043346eb + github.com/tidwall/match v1.0.1 + github.com/tidwall/rtree v0.0.0-20201027154624-32188eeb08a8 + github.com/tidwall/tinyqueue v0.0.0-20180302190814-1e39f5511563 // indirect +) diff --git a/vendor/github.com/tidwall/buntdb/go.sum b/vendor/github.com/tidwall/buntdb/go.sum new file mode 100644 index 00000000..6c801950 --- /dev/null +++ b/vendor/github.com/tidwall/buntdb/go.sum @@ -0,0 +1,14 @@ +github.com/tidwall/btree v0.2.2 h1:VVo0JW/tdidNdQzNsDR4wMbL3heaxA1DGleyzQ3/niY= +github.com/tidwall/btree v0.2.2/go.mod h1:huei1BkDWJ3/sLXmO+bsCNELL+Bp2Kks9OLyQFkzvA8= +github.com/tidwall/gjson v1.6.1 h1:LRbvNuNuvAiISWg6gxLEFuCe72UKy5hDqhxW/8183ws= +github.com/tidwall/gjson v1.6.1/go.mod h1:BaHyNc5bjzYkPqgLq7mdVzeiRtULKULXLgZFKsxEHI0= +github.com/tidwall/grect v0.0.0-20161006141115-ba9a043346eb h1:5NSYaAdrnblKByzd7XByQEJVT8+9v0W/tIY0Oo4OwrE= +github.com/tidwall/grect v0.0.0-20161006141115-ba9a043346eb/go.mod h1:lKYYLFIr9OIgdgrtgkZ9zgRxRdvPYsExnYBsEAd8W5M= +github.com/tidwall/match v1.0.1 h1:PnKP62LPNxHKTwvHHZZzdOAOCtsJTjo6dZLCwpKm5xc= +github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E= +github.com/tidwall/pretty v1.0.2 h1:Z7S3cePv9Jwm1KwS0513MRaoUe3S01WPbLNV40pwWZU= +github.com/tidwall/pretty v1.0.2/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= +github.com/tidwall/rtree v0.0.0-20201027154624-32188eeb08a8 h1:BsKSRhu0TDB6Snq8SutN9KQHc6vqHEXJTcAFwyGNius= +github.com/tidwall/rtree v0.0.0-20201027154624-32188eeb08a8/go.mod h1:/h+UnNGt0IhNNJLkGikcdcJqm66zGD/uJGMRxK/9+Ao= +github.com/tidwall/tinyqueue v0.0.0-20180302190814-1e39f5511563 h1:Otn9S136ELckZ3KKDyCkxapfufrqDqwmGjcHfAyXRrE= +github.com/tidwall/tinyqueue v0.0.0-20180302190814-1e39f5511563/go.mod h1:mLqSmt7Dv/CNneF2wfcChfN1rvapyQr01LGKnKex0DQ= diff --git a/vendor/github.com/tidwall/rtree/base/rtree.go b/vendor/github.com/tidwall/rtree/base/rtree.go index 4a66235f..0b64ca06 100644 --- a/vendor/github.com/tidwall/rtree/base/rtree.go +++ b/vendor/github.com/tidwall/rtree/base/rtree.go @@ -183,6 +183,7 @@ type treeItem struct { item interface{} } +//go:nocheckptr func (item *treeItem) unsafeNode() *treeNode { return (*treeNode)(unsafe.Pointer(item)) } diff --git a/vendor/github.com/tidwall/tinybtree/btree.go b/vendor/github.com/tidwall/tinybtree/btree.go index 91463760..10221933 100644 --- a/vendor/github.com/tidwall/tinybtree/btree.go +++ b/vendor/github.com/tidwall/tinybtree/btree.go @@ -1,6 +1,6 @@ package tinybtree -const maxItems = 31 // use an odd number +const maxItems = 255 const minItems = maxItems * 40 / 100 type item struct { @@ -23,19 +23,24 @@ type BTree struct { } func (n *node) find(key string) (index int, found bool) { - i, j := 0, n.numItems - for i < j { - h := i + (j-i)/2 - if key >= n.items[h].key { - i = h + 1 + low := 0 + high := n.numItems - 1 + for low <= high { + mid := low + ((high+1)-low)/2 + if key >= n.items[mid].key { + low = mid + 1 } else { - j = h + high = mid - 1 } } - if i > 0 && n.items[i-1].key >= key { - return i - 1, true + if low > 0 && n.items[low-1].key == key { + index = low - 1 + found = true + } else { + index = low + found = false } - return i, false + return index, found } // Set or replace a value for a key diff --git a/vendor/modules.txt b/vendor/modules.txt index 950a8796..70a25fba 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -92,9 +92,9 @@ github.com/pierrec/lz4/internal/xxh32 github.com/rcrowley/go-metrics # github.com/streadway/amqp v1.0.0 github.com/streadway/amqp -# github.com/tidwall/btree v0.0.0-20191029221954-400434d76274 +# github.com/tidwall/btree v0.2.2 github.com/tidwall/btree -# github.com/tidwall/buntdb v1.1.0 +# github.com/tidwall/buntdb v1.1.4 github.com/tidwall/buntdb # github.com/tidwall/geoindex v1.1.0 github.com/tidwall/geoindex @@ -121,12 +121,12 @@ github.com/tidwall/redcon github.com/tidwall/resp # github.com/tidwall/rhh v1.1.0 github.com/tidwall/rhh -# github.com/tidwall/rtree v0.0.0-20180113144539-6cd427091e0e +# github.com/tidwall/rtree v0.0.0-20201027154624-32188eeb08a8 github.com/tidwall/rtree github.com/tidwall/rtree/base # github.com/tidwall/sjson v1.1.1 github.com/tidwall/sjson -# github.com/tidwall/tinybtree v0.0.0-20181217131827-de5932d649b5 +# github.com/tidwall/tinybtree v1.0.1 github.com/tidwall/tinybtree # github.com/tidwall/tinyqueue v0.0.0-20180302190814-1e39f5511563 github.com/tidwall/tinyqueue