From 008416e4dd0fd53e9f07f6b5d33deeaa35b6e4e4 Mon Sep 17 00:00:00 2001 From: Shivaram Lingamneni Date: Sun, 8 Nov 2020 17:55:22 -0500 Subject: [PATCH] upgrade buntdb and dependencies --- go.mod | 2 +- go.sum | 10 + vendor/github.com/tidwall/btree/LICENSE | 214 +-- vendor/github.com/tidwall/btree/README.md | 104 +- vendor/github.com/tidwall/btree/btree.go | 1495 +++++++---------- vendor/github.com/tidwall/buntdb/.travis.yml | 3 + vendor/github.com/tidwall/buntdb/README.md | 2 +- vendor/github.com/tidwall/buntdb/buntdb.go | 155 +- vendor/github.com/tidwall/buntdb/go.mod | 8 +- vendor/github.com/tidwall/buntdb/go.sum | 16 +- vendor/github.com/tidwall/gjson/README.md | 6 +- vendor/github.com/tidwall/gjson/SYNTAX.md | 14 +- vendor/github.com/tidwall/gjson/gjson.go | 370 ++-- vendor/github.com/tidwall/gjson/gjson_gae.go | 18 - vendor/github.com/tidwall/gjson/gjson_ngae.go | 81 - vendor/github.com/tidwall/gjson/go.mod | 2 +- vendor/github.com/tidwall/gjson/go.sum | 4 +- vendor/github.com/tidwall/pretty/README.md | 2 +- vendor/github.com/tidwall/pretty/pretty.go | 34 +- vendor/github.com/tidwall/rtree/base/rtree.go | 1 + vendor/modules.txt | 10 +- 21 files changed, 985 insertions(+), 1566 deletions(-) delete mode 100644 vendor/github.com/tidwall/gjson/gjson_gae.go delete mode 100644 vendor/github.com/tidwall/gjson/gjson_ngae.go diff --git a/go.mod b/go.mod index ccd1a431..b01118a6 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,7 @@ require ( github.com/oragono/confusables v0.0.0-20190624102032-fe1cf31a24b0 github.com/oragono/go-ident v0.0.0-20200511222032-830550b1d775 github.com/stretchr/testify v1.4.0 // indirect - github.com/tidwall/buntdb v1.1.2 + github.com/tidwall/buntdb v1.1.4 github.com/toorop/go-dkim v0.0.0-20200526084421-76378ae5207e golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a golang.org/x/sys v0.0.0-20200905004654-be1d3432aa8f // indirect diff --git a/go.sum b/go.sum index 3e112983..e165499a 100644 --- a/go.sum +++ b/go.sum @@ -42,18 +42,28 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= 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/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.2 h1:noCrqQXL9EKMtcdwJcmuVKSEjqu1ua99RHHgbLTEHRo= github.com/tidwall/buntdb v1.1.2/go.mod h1:xAzi36Hir4FarpSHyfuZ6JzPJdjRZ8QlLZSntE2mqlI= +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/gjson v1.3.4 h1:On5waDnyKKk3SWE4EthbjjirAWXp43xx5cKCUZY1eZw= github.com/tidwall/gjson v1.3.4/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls= +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.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4= github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= +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-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/tinyqueue v0.0.0-20180302190814-1e39f5511563 h1:Otn9S136ELckZ3KKDyCkxapfufrqDqwmGjcHfAyXRrE= github.com/tidwall/tinyqueue v0.0.0-20180302190814-1e39f5511563/go.mod h1:mLqSmt7Dv/CNneF2wfcChfN1rvapyQr01LGKnKex0DQ= github.com/toorop/go-dkim v0.0.0-20200526084421-76378ae5207e h1:uZTp+hhFm+PCH0t0Px5oE+QYlVTwVJ+XKNQr7ct4Q7w= 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 9b8d28b1..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) @@ -456,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. @@ -479,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) { @@ -487,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. @@ -557,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 }) @@ -674,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 { @@ -849,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 @@ -879,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 @@ -887,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 @@ -1025,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 @@ -1264,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 @@ -1295,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) { @@ -1498,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) } @@ -1542,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 @@ -2014,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 } } @@ -2044,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() } } @@ -2181,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 index f4597f10..225704cd 100644 --- a/vendor/github.com/tidwall/buntdb/go.mod +++ b/vendor/github.com/tidwall/buntdb/go.mod @@ -1,12 +1,12 @@ module github.com/tidwall/buntdb -go 1.13 +go 1.15 require ( - github.com/tidwall/btree v0.0.0-20191029221954-400434d76274 - github.com/tidwall/gjson v1.3.4 + 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-20180113144539-6cd427091e0e + 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 index fc5a01cd..6c801950 100644 --- a/vendor/github.com/tidwall/buntdb/go.sum +++ b/vendor/github.com/tidwall/buntdb/go.sum @@ -1,14 +1,14 @@ -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/gjson v1.3.4 h1:On5waDnyKKk3SWE4EthbjjirAWXp43xx5cKCUZY1eZw= -github.com/tidwall/gjson v1.3.4/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls= +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.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4= -github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= -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/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/gjson/README.md b/vendor/github.com/tidwall/gjson/README.md index cab0f9fa..4108deb3 100644 --- a/vendor/github.com/tidwall/gjson/README.md +++ b/vendor/github.com/tidwall/gjson/README.md @@ -193,11 +193,15 @@ we'll get `children` array and reverse the order: "children|@reverse|0" >> "Jack" ``` -There are currently three built-in modifiers: +There are currently the following built-in modifiers: - `@reverse`: Reverse an array or the members of an object. - `@ugly`: Remove all whitespace from a json document. - `@pretty`: Make the json document more human readable. +- `@this`: Returns the current element. It can be used to retrieve the root element. +- `@valid`: Ensure the json document is valid. +- `@flatten`: Flattens an array. +- `@join`: Joins multiple objects into a single object. ### Modifier arguments diff --git a/vendor/github.com/tidwall/gjson/SYNTAX.md b/vendor/github.com/tidwall/gjson/SYNTAX.md index a57a4d66..5ea0407f 100644 --- a/vendor/github.com/tidwall/gjson/SYNTAX.md +++ b/vendor/github.com/tidwall/gjson/SYNTAX.md @@ -77,6 +77,14 @@ Special purpose characters, such as `.`, `*`, and `?` can be escaped with `\`. fav\.movie "Deer Hunter" ``` +You'll also need to make sure that the `\` character is correctly escaped when hardcoding a path in source code. + +```go +res := gjson.Get(json, "fav\\.movie") // must escape the slash +res := gjson.Get(json, `fav\.movie`) // no need to escape the slash + +``` + ### Arrays The `#` character allows for digging into JSON Arrays. @@ -181,11 +189,15 @@ children.@reverse ["Jack","Alex","Sara"] children.@reverse.0 "Jack" ``` -There are currently three built-in modifiers: +There are currently the following built-in modifiers: - `@reverse`: Reverse an array or the members of an object. - `@ugly`: Remove all whitespace from JSON. - `@pretty`: Make the JSON more human readable. +- `@this`: Returns the current element. It can be used to retrieve the root element. +- `@valid`: Ensure the json document is valid. +- `@flatten`: Flattens an array. +- `@join`: Joins multiple objects into a single object. #### Modifier arguments diff --git a/vendor/github.com/tidwall/gjson/gjson.go b/vendor/github.com/tidwall/gjson/gjson.go index 787d3276..0b6dcb09 100644 --- a/vendor/github.com/tidwall/gjson/gjson.go +++ b/vendor/github.com/tidwall/gjson/gjson.go @@ -2,17 +2,14 @@ package gjson import ( - "encoding/base64" "encoding/json" - "errors" "reflect" "strconv" "strings" - "sync" - "sync/atomic" "time" "unicode/utf16" "unicode/utf8" + "unsafe" "github.com/tidwall/match" "github.com/tidwall/pretty" @@ -464,11 +461,13 @@ func ParseBytes(json []byte) Result { } func squash(json string) string { - // expects that the lead character is a '[' or '{' or '(' + // expects that the lead character is a '[' or '{' or '(' or '"' // squash the value, ignoring all nested arrays and objects. - // the first '[' or '{' or '(', has already been read - depth := 1 - for i := 1; i < len(json); i++ { + var i, depth int + if json[0] != '"' { + i, depth = 1, 1 + } + for ; i < len(json); i++ { if json[i] >= '"' && json[i] <= '}' { switch json[i] { case '"': @@ -495,6 +494,9 @@ func squash(json string) string { break } } + if depth == 0 { + return json[:i+1] + } case '{', '[', '(': depth++ case '}', ']', ')': @@ -1984,7 +1986,7 @@ func runeit(json string) rune { } // unescape unescapes a string -func unescape(json string) string { //, error) { +func unescape(json string) string { var str = make([]byte, 0, len(json)) for i := 0; i < len(json); i++ { switch { @@ -2194,145 +2196,6 @@ func GetManyBytes(json []byte, path ...string) []Result { return res } -var fieldsmu sync.RWMutex -var fields = make(map[string]map[string]int) - -func assign(jsval Result, goval reflect.Value) { - if jsval.Type == Null { - return - } - switch goval.Kind() { - default: - case reflect.Ptr: - if !goval.IsNil() { - newval := reflect.New(goval.Elem().Type()) - assign(jsval, newval.Elem()) - goval.Elem().Set(newval.Elem()) - } else { - newval := reflect.New(goval.Type().Elem()) - assign(jsval, newval.Elem()) - goval.Set(newval) - } - case reflect.Struct: - fieldsmu.RLock() - sf := fields[goval.Type().String()] - fieldsmu.RUnlock() - if sf == nil { - fieldsmu.Lock() - sf = make(map[string]int) - for i := 0; i < goval.Type().NumField(); i++ { - f := goval.Type().Field(i) - tag := strings.Split(f.Tag.Get("json"), ",")[0] - if tag != "-" { - if tag != "" { - sf[tag] = i - sf[f.Name] = i - } else { - sf[f.Name] = i - } - } - } - fields[goval.Type().String()] = sf - fieldsmu.Unlock() - } - jsval.ForEach(func(key, value Result) bool { - if idx, ok := sf[key.Str]; ok { - f := goval.Field(idx) - if f.CanSet() { - assign(value, f) - } - } - return true - }) - case reflect.Slice: - if goval.Type().Elem().Kind() == reflect.Uint8 && - jsval.Type == String { - data, _ := base64.StdEncoding.DecodeString(jsval.String()) - goval.Set(reflect.ValueOf(data)) - } else { - jsvals := jsval.Array() - slice := reflect.MakeSlice(goval.Type(), len(jsvals), len(jsvals)) - for i := 0; i < len(jsvals); i++ { - assign(jsvals[i], slice.Index(i)) - } - goval.Set(slice) - } - case reflect.Array: - i, n := 0, goval.Len() - jsval.ForEach(func(_, value Result) bool { - if i == n { - return false - } - assign(value, goval.Index(i)) - i++ - return true - }) - case reflect.Map: - if goval.Type().Key().Kind() == reflect.String && - goval.Type().Elem().Kind() == reflect.Interface { - goval.Set(reflect.ValueOf(jsval.Value())) - } - case reflect.Interface: - goval.Set(reflect.ValueOf(jsval.Value())) - case reflect.Bool: - goval.SetBool(jsval.Bool()) - case reflect.Float32, reflect.Float64: - goval.SetFloat(jsval.Float()) - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, - reflect.Int64: - goval.SetInt(jsval.Int()) - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, - reflect.Uint64: - goval.SetUint(jsval.Uint()) - case reflect.String: - goval.SetString(jsval.String()) - } - if len(goval.Type().PkgPath()) > 0 { - v := goval.Addr() - if v.Type().NumMethod() > 0 { - if u, ok := v.Interface().(json.Unmarshaler); ok { - u.UnmarshalJSON([]byte(jsval.Raw)) - } - } - } -} - -var validate uintptr = 1 - -// UnmarshalValidationEnabled provides the option to disable JSON validation -// during the Unmarshal routine. Validation is enabled by default. -// -// Deprecated: Use encoder/json.Unmarshal instead -func UnmarshalValidationEnabled(enabled bool) { - if enabled { - atomic.StoreUintptr(&validate, 1) - } else { - atomic.StoreUintptr(&validate, 0) - } -} - -// Unmarshal loads the JSON data into the value pointed to by v. -// -// This function works almost identically to json.Unmarshal except that -// gjson.Unmarshal will automatically attempt to convert JSON values to any Go -// type. For example, the JSON string "100" or the JSON number 100 can be -// equally assigned to Go string, int, byte, uint64, etc. This rule applies to -// all types. -// -// Deprecated: Use encoder/json.Unmarshal instead -func Unmarshal(data []byte, v interface{}) error { - if atomic.LoadUintptr(&validate) == 1 { - _, ok := validpayload(data, 0) - if !ok { - return errors.New("invalid json") - } - } - if v := reflect.ValueOf(v); v.Kind() == reflect.Ptr { - assign(ParseBytes(data), v) - } - return nil -} - func validpayload(data []byte, i int) (outi int, ok bool) { for ; i < len(data); i++ { switch data[i] { @@ -2713,7 +2576,7 @@ func execModifier(json, path string) (pathOut, res string, ok bool) { case '{', '[', '"': res := Parse(pathOut) if res.Exists() { - _, args = parseSquash(pathOut, 0) + args = squash(pathOut) pathOut = pathOut[len(args):] parsedArgs = true } @@ -2734,6 +2597,15 @@ func execModifier(json, path string) (pathOut, res string, ok bool) { return pathOut, res, false } +// unwrap removes the '[]' or '{}' characters around json +func unwrap(json string) string { + json = trim(json) + if len(json) >= 2 && json[0] == '[' || json[0] == '{' { + json = json[1 : len(json)-1] + } + return json +} + // DisableModifiers will disable the modifier syntax var DisableModifiers = false @@ -2741,6 +2613,10 @@ var modifiers = map[string]func(json, arg string) string{ "pretty": modPretty, "ugly": modUgly, "reverse": modReverse, + "this": modThis, + "flatten": modFlatten, + "join": modJoin, + "valid": modValid, } // AddModifier binds a custom modifier command to the GJSON syntax. @@ -2778,6 +2654,11 @@ func modPretty(json, arg string) string { return bytesString(pretty.Pretty(stringBytes(json))) } +// @this returns the current element. Can be used to retrieve the root element. +func modThis(json, arg string) string { + return json +} + // @ugly modifier removes all whitespace. func modUgly(json, arg string) string { return bytesString(pretty.Ugly(stringBytes(json))) @@ -2824,3 +2705,194 @@ func modReverse(json, arg string) string { } return json } + +// @flatten an array with child arrays. +// [1,[2],[3,4],[5,[6,7]]] -> [1,2,3,4,5,[6,7]] +// The {"deep":true} arg can be provide for deep flattening. +// [1,[2],[3,4],[5,[6,7]]] -> [1,2,3,4,5,6,7] +// The original json is returned when the json is not an array. +func modFlatten(json, arg string) string { + res := Parse(json) + if !res.IsArray() { + return json + } + var deep bool + if arg != "" { + Parse(arg).ForEach(func(key, value Result) bool { + if key.String() == "deep" { + deep = value.Bool() + } + return true + }) + } + var out []byte + out = append(out, '[') + var idx int + res.ForEach(func(_, value Result) bool { + if idx > 0 { + out = append(out, ',') + } + if value.IsArray() { + if deep { + out = append(out, unwrap(modFlatten(value.Raw, arg))...) + } else { + out = append(out, unwrap(value.Raw)...) + } + } else { + out = append(out, value.Raw...) + } + idx++ + return true + }) + out = append(out, ']') + return bytesString(out) +} + +// @join multiple objects into a single object. +// [{"first":"Tom"},{"last":"Smith"}] -> {"first","Tom","last":"Smith"} +// The arg can be "true" to specify that duplicate keys should be preserved. +// [{"first":"Tom","age":37},{"age":41}] -> {"first","Tom","age":37,"age":41} +// Without preserved keys: +// [{"first":"Tom","age":37},{"age":41}] -> {"first","Tom","age":41} +// The original json is returned when the json is not an object. +func modJoin(json, arg string) string { + res := Parse(json) + if !res.IsArray() { + return json + } + var preserve bool + if arg != "" { + Parse(arg).ForEach(func(key, value Result) bool { + if key.String() == "preserve" { + preserve = value.Bool() + } + return true + }) + } + var out []byte + out = append(out, '{') + if preserve { + // Preserve duplicate keys. + var idx int + res.ForEach(func(_, value Result) bool { + if !value.IsObject() { + return true + } + if idx > 0 { + out = append(out, ',') + } + out = append(out, unwrap(value.Raw)...) + idx++ + return true + }) + } else { + // Deduplicate keys and generate an object with stable ordering. + var keys []Result + kvals := make(map[string]Result) + res.ForEach(func(_, value Result) bool { + if !value.IsObject() { + return true + } + value.ForEach(func(key, value Result) bool { + k := key.String() + if _, ok := kvals[k]; !ok { + keys = append(keys, key) + } + kvals[k] = value + return true + }) + return true + }) + for i := 0; i < len(keys); i++ { + if i > 0 { + out = append(out, ',') + } + out = append(out, keys[i].Raw...) + out = append(out, ':') + out = append(out, kvals[keys[i].String()].Raw...) + } + } + out = append(out, '}') + return bytesString(out) +} + +// @valid ensures that the json is valid before moving on. An empty string is +// returned when the json is not valid, otherwise it returns the original json. +func modValid(json, arg string) string { + if !Valid(json) { + return "" + } + return json +} + +// getBytes casts the input json bytes to a string and safely returns the +// results as uniquely allocated data. This operation is intended to minimize +// copies and allocations for the large json string->[]byte. +func getBytes(json []byte, path string) Result { + var result Result + if json != nil { + // unsafe cast to string + result = Get(*(*string)(unsafe.Pointer(&json)), path) + // safely get the string headers + rawhi := *(*reflect.StringHeader)(unsafe.Pointer(&result.Raw)) + strhi := *(*reflect.StringHeader)(unsafe.Pointer(&result.Str)) + // create byte slice headers + rawh := reflect.SliceHeader{Data: rawhi.Data, Len: rawhi.Len} + strh := reflect.SliceHeader{Data: strhi.Data, Len: strhi.Len} + if strh.Data == 0 { + // str is nil + if rawh.Data == 0 { + // raw is nil + result.Raw = "" + } else { + // raw has data, safely copy the slice header to a string + result.Raw = string(*(*[]byte)(unsafe.Pointer(&rawh))) + } + result.Str = "" + } else if rawh.Data == 0 { + // raw is nil + result.Raw = "" + // str has data, safely copy the slice header to a string + result.Str = string(*(*[]byte)(unsafe.Pointer(&strh))) + } else if strh.Data >= rawh.Data && + int(strh.Data)+strh.Len <= int(rawh.Data)+rawh.Len { + // Str is a substring of Raw. + start := int(strh.Data - rawh.Data) + // safely copy the raw slice header + result.Raw = string(*(*[]byte)(unsafe.Pointer(&rawh))) + // substring the raw + result.Str = result.Raw[start : start+strh.Len] + } else { + // safely copy both the raw and str slice headers to strings + result.Raw = string(*(*[]byte)(unsafe.Pointer(&rawh))) + result.Str = string(*(*[]byte)(unsafe.Pointer(&strh))) + } + } + return result +} + +// fillIndex finds the position of Raw data and assigns it to the Index field +// of the resulting value. If the position cannot be found then Index zero is +// used instead. +func fillIndex(json string, c *parseContext) { + if len(c.value.Raw) > 0 && !c.calcd { + jhdr := *(*reflect.StringHeader)(unsafe.Pointer(&json)) + rhdr := *(*reflect.StringHeader)(unsafe.Pointer(&(c.value.Raw))) + c.value.Index = int(rhdr.Data - jhdr.Data) + if c.value.Index < 0 || c.value.Index >= len(json) { + c.value.Index = 0 + } + } +} + +func stringBytes(s string) []byte { + return *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{ + Data: (*reflect.StringHeader)(unsafe.Pointer(&s)).Data, + Len: len(s), + Cap: len(s), + })) +} + +func bytesString(b []byte) string { + return *(*string)(unsafe.Pointer(&b)) +} diff --git a/vendor/github.com/tidwall/gjson/gjson_gae.go b/vendor/github.com/tidwall/gjson/gjson_gae.go deleted file mode 100644 index 95869039..00000000 --- a/vendor/github.com/tidwall/gjson/gjson_gae.go +++ /dev/null @@ -1,18 +0,0 @@ -//+build appengine js - -package gjson - -func getBytes(json []byte, path string) Result { - return Get(string(json), path) -} -func fillIndex(json string, c *parseContext) { - // noop. Use zero for the Index value. -} - -func stringBytes(s string) []byte { - return []byte(s) -} - -func bytesString(b []byte) string { - return string(b) -} diff --git a/vendor/github.com/tidwall/gjson/gjson_ngae.go b/vendor/github.com/tidwall/gjson/gjson_ngae.go deleted file mode 100644 index bc608b53..00000000 --- a/vendor/github.com/tidwall/gjson/gjson_ngae.go +++ /dev/null @@ -1,81 +0,0 @@ -//+build !appengine -//+build !js - -package gjson - -import ( - "reflect" - "unsafe" -) - -// getBytes casts the input json bytes to a string and safely returns the -// results as uniquely allocated data. This operation is intended to minimize -// copies and allocations for the large json string->[]byte. -func getBytes(json []byte, path string) Result { - var result Result - if json != nil { - // unsafe cast to string - result = Get(*(*string)(unsafe.Pointer(&json)), path) - // safely get the string headers - rawhi := *(*reflect.StringHeader)(unsafe.Pointer(&result.Raw)) - strhi := *(*reflect.StringHeader)(unsafe.Pointer(&result.Str)) - // create byte slice headers - rawh := reflect.SliceHeader{Data: rawhi.Data, Len: rawhi.Len} - strh := reflect.SliceHeader{Data: strhi.Data, Len: strhi.Len} - if strh.Data == 0 { - // str is nil - if rawh.Data == 0 { - // raw is nil - result.Raw = "" - } else { - // raw has data, safely copy the slice header to a string - result.Raw = string(*(*[]byte)(unsafe.Pointer(&rawh))) - } - result.Str = "" - } else if rawh.Data == 0 { - // raw is nil - result.Raw = "" - // str has data, safely copy the slice header to a string - result.Str = string(*(*[]byte)(unsafe.Pointer(&strh))) - } else if strh.Data >= rawh.Data && - int(strh.Data)+strh.Len <= int(rawh.Data)+rawh.Len { - // Str is a substring of Raw. - start := int(strh.Data - rawh.Data) - // safely copy the raw slice header - result.Raw = string(*(*[]byte)(unsafe.Pointer(&rawh))) - // substring the raw - result.Str = result.Raw[start : start+strh.Len] - } else { - // safely copy both the raw and str slice headers to strings - result.Raw = string(*(*[]byte)(unsafe.Pointer(&rawh))) - result.Str = string(*(*[]byte)(unsafe.Pointer(&strh))) - } - } - return result -} - -// fillIndex finds the position of Raw data and assigns it to the Index field -// of the resulting value. If the position cannot be found then Index zero is -// used instead. -func fillIndex(json string, c *parseContext) { - if len(c.value.Raw) > 0 && !c.calcd { - jhdr := *(*reflect.StringHeader)(unsafe.Pointer(&json)) - rhdr := *(*reflect.StringHeader)(unsafe.Pointer(&(c.value.Raw))) - c.value.Index = int(rhdr.Data - jhdr.Data) - if c.value.Index < 0 || c.value.Index >= len(json) { - c.value.Index = 0 - } - } -} - -func stringBytes(s string) []byte { - return *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{ - Data: (*reflect.StringHeader)(unsafe.Pointer(&s)).Data, - Len: len(s), - Cap: len(s), - })) -} - -func bytesString(b []byte) string { - return *(*string)(unsafe.Pointer(&b)) -} diff --git a/vendor/github.com/tidwall/gjson/go.mod b/vendor/github.com/tidwall/gjson/go.mod index d851688c..c287095f 100644 --- a/vendor/github.com/tidwall/gjson/go.mod +++ b/vendor/github.com/tidwall/gjson/go.mod @@ -4,5 +4,5 @@ go 1.12 require ( github.com/tidwall/match v1.0.1 - github.com/tidwall/pretty v1.0.0 + github.com/tidwall/pretty v1.0.2 ) diff --git a/vendor/github.com/tidwall/gjson/go.sum b/vendor/github.com/tidwall/gjson/go.sum index a4a2d872..75e1dd7b 100644 --- a/vendor/github.com/tidwall/gjson/go.sum +++ b/vendor/github.com/tidwall/gjson/go.sum @@ -1,4 +1,4 @@ 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.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4= -github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= +github.com/tidwall/pretty v1.0.2 h1:Z7S3cePv9Jwm1KwS0513MRaoUe3S01WPbLNV40pwWZU= +github.com/tidwall/pretty v1.0.2/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= diff --git a/vendor/github.com/tidwall/pretty/README.md b/vendor/github.com/tidwall/pretty/README.md index d2b8864d..09884692 100644 --- a/vendor/github.com/tidwall/pretty/README.md +++ b/vendor/github.com/tidwall/pretty/README.md @@ -1,7 +1,7 @@ # Pretty [![Build Status](https://img.shields.io/travis/tidwall/pretty.svg?style=flat-square)](https://travis-ci.org/tidwall/prettty) [![Coverage Status](https://img.shields.io/badge/coverage-100%25-brightgreen.svg?style=flat-square)](http://gocover.io/github.com/tidwall/pretty) -[![GoDoc](https://img.shields.io/badge/api-reference-blue.svg?style=flat-square)](https://godoc.org/github.com/tidwall/pretty) +[![GoDoc](https://img.shields.io/badge/api-reference-blue.svg?style=flat-square)](https://pkg.go.dev/github.com/tidwall/pretty) Pretty is a Go package that provides [fast](#performance) methods for formatting JSON for human readability, or to compact JSON for smaller payloads. diff --git a/vendor/github.com/tidwall/pretty/pretty.go b/vendor/github.com/tidwall/pretty/pretty.go index 0a922d03..2951c610 100644 --- a/vendor/github.com/tidwall/pretty/pretty.go +++ b/vendor/github.com/tidwall/pretty/pretty.go @@ -318,21 +318,25 @@ func hexp(p byte) byte { } // TerminalStyle is for terminals -var TerminalStyle = &Style{ - Key: [2]string{"\x1B[94m", "\x1B[0m"}, - String: [2]string{"\x1B[92m", "\x1B[0m"}, - Number: [2]string{"\x1B[93m", "\x1B[0m"}, - True: [2]string{"\x1B[96m", "\x1B[0m"}, - False: [2]string{"\x1B[96m", "\x1B[0m"}, - Null: [2]string{"\x1B[91m", "\x1B[0m"}, - Append: func(dst []byte, c byte) []byte { - if c < ' ' && (c != '\r' && c != '\n' && c != '\t' && c != '\v') { - dst = append(dst, "\\u00"...) - dst = append(dst, hexp((c>>4)&0xF)) - return append(dst, hexp((c)&0xF)) - } - return append(dst, c) - }, +var TerminalStyle *Style + +func init() { + TerminalStyle = &Style{ + Key: [2]string{"\x1B[94m", "\x1B[0m"}, + String: [2]string{"\x1B[92m", "\x1B[0m"}, + Number: [2]string{"\x1B[93m", "\x1B[0m"}, + True: [2]string{"\x1B[96m", "\x1B[0m"}, + False: [2]string{"\x1B[96m", "\x1B[0m"}, + Null: [2]string{"\x1B[91m", "\x1B[0m"}, + Append: func(dst []byte, c byte) []byte { + if c < ' ' && (c != '\r' && c != '\n' && c != '\t' && c != '\v') { + dst = append(dst, "\\u00"...) + dst = append(dst, hexp((c>>4)&0xF)) + return append(dst, hexp((c)&0xF)) + } + return append(dst, c) + }, + } } // Color will colorize the json. The style parma is used for customizing 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/modules.txt b/vendor/modules.txt index 561000f8..efeb27f7 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -37,20 +37,20 @@ github.com/oragono/confusables github.com/oragono/go-ident # github.com/stretchr/testify v1.4.0 ## explicit -# 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.2 +# github.com/tidwall/buntdb v1.1.4 ## explicit github.com/tidwall/buntdb -# github.com/tidwall/gjson v1.3.4 +# github.com/tidwall/gjson v1.6.1 github.com/tidwall/gjson # github.com/tidwall/grect v0.0.0-20161006141115-ba9a043346eb github.com/tidwall/grect # github.com/tidwall/match v1.0.1 github.com/tidwall/match -# github.com/tidwall/pretty v1.0.0 +# github.com/tidwall/pretty v1.0.2 github.com/tidwall/pretty -# 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/tinyqueue v0.0.0-20180302190814-1e39f5511563