You've already forked nyx
mirror of
https://github.com/rls-moe/nyx
synced 2025-08-20 12:04:16 +00:00
MVP, no mod tools or anything but it works
This commit is contained in:
202
vendor/github.com/tidwall/btree/LICENSE
generated
vendored
Normal file
202
vendor/github.com/tidwall/btree/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
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.
|
107
vendor/github.com/tidwall/btree/README.md
generated
vendored
Normal file
107
vendor/github.com/tidwall/btree/README.md
generated
vendored
Normal file
@@ -0,0 +1,107 @@
|
||||
BTree implementation for Go
|
||||
===========================
|
||||
|
||||

|
||||
[](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
|
||||
*/
|
||||
```
|
968
vendor/github.com/tidwall/btree/btree.go
generated
vendored
Normal file
968
vendor/github.com/tidwall/btree/btree.go
generated
vendored
Normal file
@@ -0,0 +1,968 @@
|
||||
// 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.
|
||||
|
||||
// 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"
|
||||
)
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// 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)}
|
||||
}
|
||||
|
||||
func (f *FreeList) newNode() (n *node) {
|
||||
f.mu.Lock()
|
||||
index := len(f.freelist) - 1
|
||||
if index < 0 {
|
||||
f.mu.Unlock()
|
||||
return new(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
|
||||
}
|
||||
|
||||
// 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):]
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
func (n *node) mutableFor(cow *copyOnWriteContext) *node {
|
||||
if n.cow == cow {
|
||||
return n
|
||||
}
|
||||
out := cow.newNode()
|
||||
if cap(out.items) >= len(n.items) {
|
||||
out.items = out.items[:len(n.items)]
|
||||
} else {
|
||||
out.items = make(items, len(n.items), cap(n.items))
|
||||
}
|
||||
copy(out.items, n.items)
|
||||
// Copy children
|
||||
if cap(out.children) >= len(n.children) {
|
||||
out.children = out.children[:len(n.children)]
|
||||
} else {
|
||||
out.children = make(children, len(n.children), cap(n.children))
|
||||
}
|
||||
copy(out.children, n.children)
|
||||
return out
|
||||
}
|
||||
|
||||
func (n *node) mutableChild(i int) *node {
|
||||
c := n.children[i].mutableFor(n.cow)
|
||||
n.children[i] = c
|
||||
return c
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
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
|
||||
}
|
||||
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)
|
||||
}
|
||||
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)
|
||||
}
|
||||
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())
|
||||
}
|
||||
} 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
|
||||
}
|
||||
|
||||
// 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]
|
||||
}
|
||||
return nil
|
||||
}
|
20
vendor/github.com/tidwall/buntdb/LICENSE
generated
vendored
Normal file
20
vendor/github.com/tidwall/buntdb/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 Josh Baker
|
||||
|
||||
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:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
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.
|
606
vendor/github.com/tidwall/buntdb/README.md
generated
vendored
Normal file
606
vendor/github.com/tidwall/buntdb/README.md
generated
vendored
Normal file
@@ -0,0 +1,606 @@
|
||||
<p align="center">
|
||||
<img
|
||||
src="logo.png"
|
||||
width="307" height="150" border="0" alt="BuntDB">
|
||||
<br>
|
||||
<a href="https://travis-ci.org/tidwall/buntdb"><img src="https://img.shields.io/travis/tidwall/buntdb.svg?style=flat-square" alt="Build Status"></a>
|
||||
<a href="http://gocover.io/github.com/tidwall/buntdb"><img src="https://img.shields.io/badge/coverage-95%25-brightgreen.svg?style=flat-square" alt="Code Coverage"></a>
|
||||
<a href="https://goreportcard.com/report/github.com/tidwall/buntdb"><img src="https://goreportcard.com/badge/github.com/tidwall/buntdb?style=flat-square" alt="Go Report Card"></a>
|
||||
<a href="https://godoc.org/github.com/tidwall/buntdb"><img src="https://img.shields.io/badge/api-reference-blue.svg?style=flat-square" alt="GoDoc"></a>
|
||||
</p>
|
||||
|
||||
====
|
||||
|
||||
BuntDB is 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. It supports custom indexes and geospatial
|
||||
data. It's ideal for projects that need a dependable database and favor
|
||||
speed over data size.
|
||||
|
||||
The desire to create BuntDB stems from the need for a new embeddable
|
||||
database for [Tile38](https://github.com/tidwall/tile38) and [SummitDB](https://github.com/tidwall/summitdb).
|
||||
|
||||
Features
|
||||
========
|
||||
|
||||
- In-memory database for [fast reads and writes](#performance)
|
||||
- Embeddable with a [simple API](https://godoc.org/github.com/tidwall/buntdb)
|
||||
- [Spatial indexing](#spatial-indexes) for up to 20 dimensions; Useful for Geospatial data
|
||||
- Index fields inside [JSON](#json-indexes) documents
|
||||
- [Collate i18n Indexes](#collate-i18n-indexes) using the optional [collate package](https://github.com/tidwall/collate)
|
||||
- Create [custom indexes](#custom-indexes) for any data type
|
||||
- Support for [multi value indexes](#multi-value-index); Similar to a SQL multi column index
|
||||
- [Built-in types](#built-in-types) that are easy to get up & running; String, Uint, Int, Float
|
||||
- Flexible [iteration](#iterating) of data; ascending, descending, and ranges
|
||||
- [Durable append-only file](#append-only-file) format for persistence
|
||||
- Option to evict old items with an [expiration](#data-expiration) TTL
|
||||
- Tight codebase, under 2K loc using the `cloc` command
|
||||
- ACID semantics with locking [transactions](#transactions) that support rollbacks
|
||||
|
||||
|
||||
Getting Started
|
||||
===============
|
||||
|
||||
## Installing
|
||||
|
||||
To start using BuntDB, install Go and run `go get`:
|
||||
|
||||
```sh
|
||||
$ go get -u github.com/tidwall/buntdb
|
||||
```
|
||||
|
||||
This will retrieve the library.
|
||||
|
||||
|
||||
## Opening a database
|
||||
|
||||
The primary object in BuntDB is a `DB`. To open or create your
|
||||
database, use the `buntdb.Open()` function:
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/tidwall/buntdb"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Open the data.db file. It will be created if it doesn't exist.
|
||||
db, err := buntdb.Open("data.db")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
It's also possible to open a database that does not persist to disk by using `:memory:` as the path of the file.
|
||||
|
||||
```go
|
||||
buntdb.Open(":memory:") // Open a file that does not persist to disk.
|
||||
```
|
||||
|
||||
## Transactions
|
||||
All reads and writes must be performed from inside a transaction. BuntDB can have one write transaction opened at a time, but can have many concurrent read transactions. Each transaction maintains a stable view of the database. In other words, once a transaction has begun, the data for that transaction cannot be changed by other transactions.
|
||||
|
||||
Transactions run in a function that exposes a `Tx` object, which represents the transaction state. While inside a transaction, all database operations should be performed using this object. You should never access the origin `DB` object while inside a transaction. Doing so may have side-effects, such as blocking your application.
|
||||
|
||||
When a transaction fails, it will roll back, and revert all changes that occurred to the database during that transaction. There's a single return value that you can use to close the transaction. For read/write transactions, returning an error this way will force the transaction to roll back. When a read/write transaction succeeds all changes are persisted to disk.
|
||||
|
||||
### Read-only Transactions
|
||||
A read-only transaction should be used when you don't need to make changes to the data. The advantage of a read-only transaction is that there can be many running concurrently.
|
||||
|
||||
```go
|
||||
err := db.View(func(tx *buntdb.Tx) error {
|
||||
...
|
||||
return nil
|
||||
})
|
||||
```
|
||||
|
||||
### Read/write Transactions
|
||||
A read/write transaction is used when you need to make changes to your data. There can only be one read/write transaction running at a time. So make sure you close it as soon as you are done with it.
|
||||
|
||||
```go
|
||||
err := db.Update(func(tx *buntdb.Tx) error {
|
||||
...
|
||||
return nil
|
||||
})
|
||||
```
|
||||
|
||||
## Setting and getting key/values
|
||||
|
||||
To set a value you must open a read/write transaction:
|
||||
|
||||
```go
|
||||
err := db.Update(func(tx *buntdb.Tx) error {
|
||||
_, _, err := tx.Set("mykey", "myvalue", nil)
|
||||
return err
|
||||
})
|
||||
```
|
||||
|
||||
|
||||
To get the value:
|
||||
|
||||
```go
|
||||
err := db.View(func(tx *buntdb.Tx) error {
|
||||
val, err := tx.Get("mykey")
|
||||
if err != nil{
|
||||
return err
|
||||
}
|
||||
fmt.Printf("value is %s\n", val)
|
||||
return nil
|
||||
})
|
||||
```
|
||||
|
||||
Getting non-existent values will case an `ErrNotFound` error.
|
||||
|
||||
### Iterating
|
||||
All keys/value pairs are ordered in the database by the key. To iterate over the keys:
|
||||
|
||||
```go
|
||||
err := db.View(func(tx *buntdb.Tx) error {
|
||||
err := tx.Ascend("", func(key, value string) bool{
|
||||
fmt.Printf("key: %s, value: %s\n", key, value)
|
||||
})
|
||||
return err
|
||||
})
|
||||
```
|
||||
|
||||
There is also `AscendGreaterOrEqual`, `AscendLessThan`, `AscendRange`, `Descend`, `DescendLessOrEqual`, `DescendGreaterThan`, and `DescendRange`. Please see the [documentation](https://godoc.org/github.com/tidwall/buntdb) for more information on these functions.
|
||||
|
||||
|
||||
## Custom Indexes
|
||||
Initially all data is stored in a single [B-tree](https://en.wikipedia.org/wiki/B-tree) with each item having one key and one value. All of these items are ordered by the key. This is great for quickly getting a value from a key or [iterating](#iterating) over the keys. Feel free to peruse the [B-tree implementation](https://github.com/tidwall/btree).
|
||||
|
||||
You can also create custom indexes that allow for ordering and [iterating](#iterating) over values. A custom index also uses a B-tree, but it's more flexible because it allows for custom ordering.
|
||||
|
||||
For example, let's say you want to create an index for ordering names:
|
||||
|
||||
```go
|
||||
db.CreateIndex("names", "*", buntdb.IndexString)
|
||||
```
|
||||
|
||||
This will create an index named `names` which stores and sorts all values. The second parameter is a pattern that is used to filter on keys. A `*` wildcard argument means that we want to accept all keys. `IndexString` is a built-in function that performs case-insensitive ordering on the values
|
||||
|
||||
Now you can add various names:
|
||||
|
||||
```go
|
||||
db.Update(func(tx *buntdb.Tx) error {
|
||||
tx.Set("user:0:name", "tom", nil)
|
||||
tx.Set("user:1:name", "Randi", nil)
|
||||
tx.Set("user:2:name", "jane", nil)
|
||||
tx.Set("user:4:name", "Janet", nil)
|
||||
tx.Set("user:5:name", "Paula", nil)
|
||||
tx.Set("user:6:name", "peter", nil)
|
||||
tx.Set("user:7:name", "Terri", nil)
|
||||
return nil
|
||||
})
|
||||
```
|
||||
|
||||
Finally you can iterate over the index:
|
||||
|
||||
```go
|
||||
db.View(func(tx *buntdb.Tx) error {
|
||||
tx.Ascend("names", func(key, val string) bool {
|
||||
fmt.Printf(buf, "%s %s\n", key, val)
|
||||
return true
|
||||
})
|
||||
return nil
|
||||
})
|
||||
```
|
||||
The output should be:
|
||||
```
|
||||
user:2:name jane
|
||||
user:4:name Janet
|
||||
user:5:name Paula
|
||||
user:6:name peter
|
||||
user:1:name Randi
|
||||
user:7:name Terri
|
||||
user:0:name tom
|
||||
```
|
||||
|
||||
The pattern parameter can be used to filter on keys like this:
|
||||
|
||||
```go
|
||||
db.CreateIndex("names", "user:*", buntdb.IndexString)
|
||||
```
|
||||
|
||||
Now only items with keys that have the prefix `user:` will be added to the `names` index.
|
||||
|
||||
|
||||
### Built-in types
|
||||
Along with `IndexString`, there is also `IndexInt`, `IndexUint`, and `IndexFloat`.
|
||||
These are built-in types for indexing. You can choose to use these or create your own.
|
||||
|
||||
So to create an index that is numerically ordered on an age key, we could use:
|
||||
|
||||
```go
|
||||
db.CreateIndex("ages", "user:*:age", buntdb.IndexInt)
|
||||
```
|
||||
|
||||
And then add values:
|
||||
|
||||
```go
|
||||
db.Update(func(tx *buntdb.Tx) error {
|
||||
tx.Set("user:0:age", "35", nil)
|
||||
tx.Set("user:1:age", "49", nil)
|
||||
tx.Set("user:2:age", "13", nil)
|
||||
tx.Set("user:4:age", "63", nil)
|
||||
tx.Set("user:5:age", "8", nil)
|
||||
tx.Set("user:6:age", "3", nil)
|
||||
tx.Set("user:7:age", "16", nil)
|
||||
return nil
|
||||
})
|
||||
```
|
||||
|
||||
```go
|
||||
db.View(func(tx *buntdb.Tx) error {
|
||||
tx.Ascend("ages", func(key, val string) bool {
|
||||
fmt.Printf(buf, "%s %s\n", key, val)
|
||||
return true
|
||||
})
|
||||
return nil
|
||||
})
|
||||
```
|
||||
|
||||
The output should be:
|
||||
```
|
||||
user:6:name 3
|
||||
user:5:name 8
|
||||
user:2:name 13
|
||||
user:7:name 16
|
||||
user:0:name 35
|
||||
user:1:name 49
|
||||
user:4:name 63
|
||||
```
|
||||
|
||||
## Spatial Indexes
|
||||
BuntDB has support for spatial indexes by storing rectangles in an [R-tree](https://en.wikipedia.org/wiki/R-tree). An R-tree is organized in a similar manner as a [B-tree](https://en.wikipedia.org/wiki/B-tree), and both are balanced trees. But, an R-tree is special because it can operate on data that is in multiple dimensions. This is super handy for Geospatial applications.
|
||||
|
||||
To create a spatial index use the `CreateSpatialIndex` function:
|
||||
|
||||
```go
|
||||
db.CreateSpatialIndex("fleet", "fleet:*:pos", buntdb.IndexRect)
|
||||
```
|
||||
|
||||
Then `IndexRect` is a built-in function that converts rect strings to a format that the R-tree can use. It's easy to use this function out of the box, but you might find it better to create a custom one that renders from a different format, such as [Well-known text](https://en.wikipedia.org/wiki/Well-known_text) or [GeoJSON](http://geojson.org/).
|
||||
|
||||
To add some lon,lat points to the `fleet` index:
|
||||
|
||||
```go
|
||||
db.Update(func(tx *buntdb.Tx) error {
|
||||
tx.Set("fleet:0:pos", "[-115.567 33.532]", nil)
|
||||
tx.Set("fleet:1:pos", "[-116.671 35.735]", nil)
|
||||
tx.Set("fleet:2:pos", "[-113.902 31.234]", nil)
|
||||
return nil
|
||||
})
|
||||
```
|
||||
|
||||
And then you can run the `Intersects` function on the index:
|
||||
|
||||
```go
|
||||
db.View(func(tx *buntdb.Tx) error {
|
||||
tx.Intersects("fleet", "[-117 30],[-112 36]", func(key, val string) bool {
|
||||
...
|
||||
return true
|
||||
})
|
||||
return nil
|
||||
})
|
||||
```
|
||||
|
||||
This will get all three positions.
|
||||
|
||||
### Spatial bracket syntax
|
||||
|
||||
The bracket syntax `[-117 30],[-112 36]` is unique to BuntDB, and it's how the built-in rectangles are processed. But, you are not limited to this syntax. Whatever Rect function you choose to use during `CreateSpatialIndex` will be used to process the parameter, in this case it's `IndexRect`.
|
||||
|
||||
- **2D rectangle:** `[10 15],[20 25]`
|
||||
*Min XY: "10x15", Max XY: "20x25"*
|
||||
|
||||
- **3D rectangle:** `[10 15 12],[20 25 18]`
|
||||
*Min XYZ: "10x15x12", Max XYZ: "20x25x18"*
|
||||
|
||||
- **2D point:** `[10 15]`
|
||||
*XY: "10x15"*
|
||||
|
||||
- **LatLon point:** `[-112.2693 33.5123]`
|
||||
*LatLon: "33.5123 -112.2693"*
|
||||
|
||||
- **LatLon bounding box:** `[-112.26 33.51],[-112.18 33.67]`
|
||||
*Min LatLon: "33.51 -112.26", Max LatLon: "33.67 -112.18"*
|
||||
|
||||
**Notice:** The longitude is the Y axis and is on the left, and latitude is the X axis and is on the right.
|
||||
|
||||
You can also represent `Infinity` by using `-inf` and `+inf`.
|
||||
For example, you might have the following points (`[X Y M]` where XY is a point and M is a timestamp):
|
||||
```
|
||||
[3 9 1]
|
||||
[3 8 2]
|
||||
[4 8 3]
|
||||
[4 7 4]
|
||||
[5 7 5]
|
||||
[5 6 6]
|
||||
```
|
||||
|
||||
You can then do a search for all points with `M` between 2-4 by calling `Intersects`.
|
||||
|
||||
```go
|
||||
tx.Intersects("points", "[-inf -inf 2],[+inf +inf 4]", func(key, val string) bool {
|
||||
println(val)
|
||||
return true
|
||||
})
|
||||
```
|
||||
|
||||
Which will return:
|
||||
|
||||
```
|
||||
[3 8 2]
|
||||
[4 8 3]
|
||||
[4 7 4]
|
||||
```
|
||||
|
||||
## JSON Indexes
|
||||
Indexes can be created on individual fields inside JSON documents. BuntDB uses [GJSON](https://github.com/tidwall/gjson) under the hood.
|
||||
|
||||
For example:
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/tidwall/buntdb"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db, _ := buntdb.Open(":memory:")
|
||||
db.CreateIndex("last_name", "*", buntdb.IndexJSON("name.last"))
|
||||
db.CreateIndex("age", "*", buntdb.IndexJSON("age"))
|
||||
db.Update(func(tx *buntdb.Tx) error {
|
||||
tx.Set("1", `{"name":{"first":"Tom","last":"Johnson"},"age":38}`, nil)
|
||||
tx.Set("2", `{"name":{"first":"Janet","last":"Prichard"},"age":47}`, nil)
|
||||
tx.Set("3", `{"name":{"first":"Carol","last":"Anderson"},"age":52}`, nil)
|
||||
tx.Set("4", `{"name":{"first":"Alan","last":"Cooper"},"age":28}`, nil)
|
||||
return nil
|
||||
})
|
||||
db.View(func(tx *buntdb.Tx) error {
|
||||
fmt.Println("Order by last name")
|
||||
tx.Ascend("last_name", func(key, value string) bool {
|
||||
fmt.Printf("%s: %s\n", key, value)
|
||||
return true
|
||||
})
|
||||
fmt.Println("Order by age")
|
||||
tx.Ascend("age", func(key, value string) bool {
|
||||
fmt.Printf("%s: %s\n", key, value)
|
||||
return true
|
||||
})
|
||||
fmt.Println("Order by age range 30-50")
|
||||
tx.AscendRange("age", `{"age":30}`, `{"age":50}`, func(key, value string) bool {
|
||||
fmt.Printf("%s: %s\n", key, value)
|
||||
return true
|
||||
})
|
||||
return nil
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
Results:
|
||||
|
||||
```
|
||||
Order by last name
|
||||
3: {"name":{"first":"Carol","last":"Anderson"},"age":52}
|
||||
4: {"name":{"first":"Alan","last":"Cooper"},"age":28}
|
||||
1: {"name":{"first":"Tom","last":"Johnson"},"age":38}
|
||||
2: {"name":{"first":"Janet","last":"Prichard"},"age":47}
|
||||
|
||||
Order by age
|
||||
4: {"name":{"first":"Alan","last":"Cooper"},"age":28}
|
||||
1: {"name":{"first":"Tom","last":"Johnson"},"age":38}
|
||||
2: {"name":{"first":"Janet","last":"Prichard"},"age":47}
|
||||
3: {"name":{"first":"Carol","last":"Anderson"},"age":52}
|
||||
|
||||
Order by age range 30-50
|
||||
1: {"name":{"first":"Tom","last":"Johnson"},"age":38}
|
||||
2: {"name":{"first":"Janet","last":"Prichard"},"age":47}
|
||||
```
|
||||
|
||||
## Multi Value Index
|
||||
With BuntDB it's possible to join multiple values on a single index.
|
||||
This is similar to a [multi column index](http://dev.mysql.com/doc/refman/5.7/en/multiple-column-indexes.html) in a traditional SQL database.
|
||||
|
||||
In this example we are creating a multi value index on "name.last" and "age":
|
||||
|
||||
```go
|
||||
db, _ := buntdb.Open(":memory:")
|
||||
db.CreateIndex("last_name_age", "*", buntdb.IndexJSON("name.last"), buntdb.IndexJSON("age"))
|
||||
db.Update(func(tx *buntdb.Tx) error {
|
||||
tx.Set("1", `{"name":{"first":"Tom","last":"Johnson"},"age":38}`, nil)
|
||||
tx.Set("2", `{"name":{"first":"Janet","last":"Prichard"},"age":47}`, nil)
|
||||
tx.Set("3", `{"name":{"first":"Carol","last":"Anderson"},"age":52}`, nil)
|
||||
tx.Set("4", `{"name":{"first":"Alan","last":"Cooper"},"age":28}`, nil)
|
||||
tx.Set("5", `{"name":{"first":"Sam","last":"Anderson"},"age":51}`, nil)
|
||||
tx.Set("6", `{"name":{"first":"Melinda","last":"Prichard"},"age":44}`, nil)
|
||||
return nil
|
||||
})
|
||||
db.View(func(tx *buntdb.Tx) error {
|
||||
tx.Ascend("last_name_age", func(key, value string) bool {
|
||||
fmt.Printf("%s: %s\n", key, value)
|
||||
return true
|
||||
})
|
||||
return nil
|
||||
})
|
||||
|
||||
// Output:
|
||||
// 5: {"name":{"first":"Sam","last":"Anderson"},"age":51}
|
||||
// 3: {"name":{"first":"Carol","last":"Anderson"},"age":52}
|
||||
// 4: {"name":{"first":"Alan","last":"Cooper"},"age":28}
|
||||
// 1: {"name":{"first":"Tom","last":"Johnson"},"age":38}
|
||||
// 6: {"name":{"first":"Melinda","last":"Prichard"},"age":44}
|
||||
// 2: {"name":{"first":"Janet","last":"Prichard"},"age":47}
|
||||
```
|
||||
|
||||
## Descending Ordered Index
|
||||
Any index can be put in descending order by wrapping it's less function with `buntdb.Desc`.
|
||||
|
||||
```go
|
||||
db.CreateIndex("last_name_age", "*",
|
||||
buntdb.IndexJSON("name.last"),
|
||||
buntdb.Desc(buntdb.IndexJSON("age")))
|
||||
```
|
||||
|
||||
This will create a multi value index where the last name is ascending and the age is descending.
|
||||
|
||||
## Collate i18n Indexes
|
||||
|
||||
Using the external [collate package](https://github.com/tidwall/collate) it's possible to create
|
||||
indexes that are sorted by the specified language. This is similar to the [SQL COLLATE keyword](https://msdn.microsoft.com/en-us/library/ms174596.aspx) found in traditional databases.
|
||||
|
||||
To install:
|
||||
|
||||
```
|
||||
go get -u github.com/tidwall/collate
|
||||
```
|
||||
|
||||
For example:
|
||||
|
||||
```go
|
||||
import "github.com/tidwall/collate"
|
||||
|
||||
// To sort case-insensitive in French.
|
||||
db.CreateIndex("name", "*", collate.IndexString("FRENCH_CI"))
|
||||
|
||||
// To specify that numbers should sort numerically ("2" < "12")
|
||||
// and use a comma to represent a decimal point.
|
||||
db.CreateIndex("amount", "*", collate.IndexString("FRENCH_NUM"))
|
||||
```
|
||||
|
||||
There's also support for Collation on JSON indexes:
|
||||
|
||||
```go
|
||||
db.CreateIndex("last_name", "*", collate.IndexJSON("CHINESE_CI", "name.last"))
|
||||
```
|
||||
|
||||
Check out the [collate project](https://github.com/tidwall/collate) for more information.
|
||||
|
||||
## Data Expiration
|
||||
Items can be automatically evicted by using the `SetOptions` object in the `Set` function to set a `TTL`.
|
||||
|
||||
```go
|
||||
db.Update(func(tx *buntdb.Tx) error {
|
||||
tx.Set("mykey", "myval", &buntdb.SetOptions{Expires:true, TTL:time.Second})
|
||||
return nil
|
||||
})
|
||||
```
|
||||
|
||||
Now `mykey` will automatically be deleted after one second. You can remove the TTL by setting the value again with the same key/value, but with the options parameter set to nil.
|
||||
|
||||
## Append-only File
|
||||
|
||||
BuntDB uses an AOF (append-only file) which is a log of all database changes that occur from operations like `Set()` and `Delete()`.
|
||||
|
||||
The format of this file looks like:
|
||||
```
|
||||
set key:1 value1
|
||||
set key:2 value2
|
||||
set key:1 value3
|
||||
del key:2
|
||||
...
|
||||
```
|
||||
|
||||
When the database opens again, it will read back the aof file and process each command in exact order.
|
||||
This read process happens one time when the database opens.
|
||||
From there on the file is only appended.
|
||||
|
||||
As you may guess this log file can grow large over time.
|
||||
There's a background routine that automatically shrinks the log file when it gets too large.
|
||||
There is also a `Shrink()` function which will rewrite the aof file so that it contains only the items in the database.
|
||||
The shrink operation does not lock up the database so read and write transactions can continue while shrinking is in process.
|
||||
|
||||
### Durability and fsync
|
||||
|
||||
By default BuntDB executes an `fsync` once every second on the [aof file](#append-only-file). Which simply means that there's a chance that up to one second of data might be lost. If you need higher durability then there's an optional database config setting `Config.SyncPolicy` which can be set to `Always`.
|
||||
|
||||
The `Config.SyncPolicy` has the following options:
|
||||
|
||||
- `Never` - fsync is managed by the operating system, less safe
|
||||
- `EverySecond` - fsync every second, fast and safer, this is the default
|
||||
- `Always` - fsync after every write, very durable, slower
|
||||
|
||||
## Config
|
||||
|
||||
Here are some configuration options that can be use to change various behaviors of the database.
|
||||
|
||||
- **SyncPolicy** adjusts how often the data is synced to disk. This value can be Never, EverySecond, or Always. Default is EverySecond.
|
||||
- **AutoShrinkPercentage** is used by the background process to trigger a shrink of the aof file when the size of the file is larger than the percentage of the result of the previous shrunk file. For example, if this value is 100, and the last shrink process resulted in a 100mb file, then the new aof file must be 200mb before a shrink is triggered. Default is 100.
|
||||
- **AutoShrinkMinSize** defines the minimum size of the aof file before an automatic shrink can occur. Default is 32MB.
|
||||
- **AutoShrinkDisabled** turns off automatic background shrinking. Default is false.
|
||||
|
||||
To update the configuration you should call `ReadConfig` followed by `SetConfig`. For example:
|
||||
|
||||
```go
|
||||
|
||||
var config buntdb.Config
|
||||
if err := db.ReadConfig(&config); err != nil{
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err := db.WriteConfig(config); err != nil{
|
||||
log.Fatal(err)
|
||||
}
|
||||
```
|
||||
|
||||
## Performance
|
||||
|
||||
How fast is BuntDB?
|
||||
|
||||
Here are some example [benchmarks](https://github.com/tidwall/raft-buntdb#raftstore-performance-comparison) when using BuntDB in a Raft Store implementation.
|
||||
|
||||
You can also run the standard Go benchmark tool from the project root directory:
|
||||
|
||||
```
|
||||
go test --bench=.
|
||||
```
|
||||
|
||||
### BuntDB-Benchmark
|
||||
|
||||
There's a [custom utility](https://github.com/tidwall/buntdb-benchmark) that was created specifically for benchmarking BuntDB.
|
||||
|
||||
*These are the results from running the benchmarks on a MacBook Pro 15" 2.8 GHz Intel Core i7:*
|
||||
|
||||
```
|
||||
$ buntdb-benchmark -q
|
||||
GET: 4609604.74 operations per second
|
||||
SET: 248500.33 operations per second
|
||||
ASCEND_100: 2268998.79 operations per second
|
||||
ASCEND_200: 1178388.14 operations per second
|
||||
ASCEND_400: 679134.20 operations per second
|
||||
ASCEND_800: 348445.55 operations per second
|
||||
DESCEND_100: 2313821.69 operations per second
|
||||
DESCEND_200: 1292738.38 operations per second
|
||||
DESCEND_400: 675258.76 operations per second
|
||||
DESCEND_800: 337481.67 operations per second
|
||||
SPATIAL_SET: 134824.60 operations per second
|
||||
SPATIAL_INTERSECTS_100: 939491.47 operations per second
|
||||
SPATIAL_INTERSECTS_200: 561590.40 operations per second
|
||||
SPATIAL_INTERSECTS_400: 306951.15 operations per second
|
||||
SPATIAL_INTERSECTS_800: 159673.91 operations per second
|
||||
```
|
||||
|
||||
To install this utility:
|
||||
|
||||
```
|
||||
go get github.com/tidwall/buntdb-benchmark
|
||||
```
|
||||
|
||||
|
||||
|
||||
## Contact
|
||||
Josh Baker [@tidwall](http://twitter.com/tidwall)
|
||||
|
||||
## License
|
||||
|
||||
BuntDB source code is available under the MIT [License](/LICENSE).
|
2062
vendor/github.com/tidwall/buntdb/buntdb.go
generated
vendored
Normal file
2062
vendor/github.com/tidwall/buntdb/buntdb.go
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
BIN
vendor/github.com/tidwall/buntdb/logo.png
generated
vendored
Normal file
BIN
vendor/github.com/tidwall/buntdb/logo.png
generated
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 94 KiB |
20
vendor/github.com/tidwall/gjson/LICENSE
generated
vendored
Normal file
20
vendor/github.com/tidwall/gjson/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 Josh Baker
|
||||
|
||||
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:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
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.
|
369
vendor/github.com/tidwall/gjson/README.md
generated
vendored
Normal file
369
vendor/github.com/tidwall/gjson/README.md
generated
vendored
Normal file
@@ -0,0 +1,369 @@
|
||||
<p align="center">
|
||||
<img
|
||||
src="logo.png"
|
||||
width="240" height="78" border="0" alt="GJSON">
|
||||
<br>
|
||||
<a href="https://travis-ci.org/tidwall/gjson"><img src="https://img.shields.io/travis/tidwall/gjson.svg?style=flat-square" alt="Build Status"></a><!--
|
||||
<a href="http://gocover.io/github.com/tidwall/gjson"><img src="https://img.shields.io/badge/coverage-97%25-brightgreen.svg?style=flat-square" alt="Code Coverage"></a>
|
||||
-->
|
||||
<a href="https://godoc.org/github.com/tidwall/gjson"><img src="https://img.shields.io/badge/api-reference-blue.svg?style=flat-square" alt="GoDoc"></a>
|
||||
</p>
|
||||
|
||||
<p align="center">get a json value quickly</a></p>
|
||||
|
||||
GJSON is a Go package that provides a [very fast](#performance) and simple way to get a value from a json document. The purpose for this library it to give efficient json indexing for the [BuntDB](https://github.com/tidwall/buntdb) project.
|
||||
|
||||
Getting Started
|
||||
===============
|
||||
|
||||
## Installing
|
||||
|
||||
To start using GJSON, install Go and run `go get`:
|
||||
|
||||
```sh
|
||||
$ go get -u github.com/tidwall/gjson
|
||||
```
|
||||
|
||||
This will retrieve the library.
|
||||
|
||||
## Get a value
|
||||
Get searches json for the specified path. A path is in dot syntax, such as "name.last" or "age". This function expects that the json is well-formed and validates. Invalid json will not panic, but it may return back unexpected results. When the value is found it's returned immediately.
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import "github.com/tidwall/gjson"
|
||||
|
||||
const json = `{"name":{"first":"Janet","last":"Prichard"},"age":47}`
|
||||
|
||||
func main() {
|
||||
value := gjson.Get(json, "name.last")
|
||||
println(value.String())
|
||||
}
|
||||
```
|
||||
|
||||
This will print:
|
||||
|
||||
```
|
||||
Prichard
|
||||
```
|
||||
*There's also the [GetMany](#get-multiple-values-at-once) function to get multiple values at once, and [GetBytes](#working-with-bytes) for working with JSON byte slices.*
|
||||
|
||||
## Path Syntax
|
||||
|
||||
A path is a series of keys separated by a dot.
|
||||
A key may contain special wildcard characters '\*' and '?'.
|
||||
To access an array value use the index as the key.
|
||||
To get the number of elements in an array or to access a child path, use the '#' character.
|
||||
The dot and wildcard characters can be escaped with '\'.
|
||||
|
||||
```json
|
||||
{
|
||||
"name": {"first": "Tom", "last": "Anderson"},
|
||||
"age":37,
|
||||
"children": ["Sara","Alex","Jack"],
|
||||
"fav.movie": "Deer Hunter",
|
||||
"friends": [
|
||||
{"first": "Dale", "last": "Murphy", "age": 44},
|
||||
{"first": "Roger", "last": "Craig", "age": 68},
|
||||
{"first": "Jane", "last": "Murphy", "age": 47}
|
||||
]
|
||||
}
|
||||
```
|
||||
```
|
||||
"name.last" >> "Anderson"
|
||||
"age" >> 37
|
||||
"children" >> ["Sara","Alex","Jack"]
|
||||
"children.#" >> 3
|
||||
"children.1" >> "Alex"
|
||||
"child*.2" >> "Jack"
|
||||
"c?ildren.0" >> "Sara"
|
||||
"fav\.movie" >> "Deer Hunter"
|
||||
"friends.#.first" >> ["Dale","Roger","Jane"]
|
||||
"friends.1.last" >> "Craig"
|
||||
```
|
||||
|
||||
You can also query an array for the first match by using `#[...]`, or find all matches with `#[...]#`.
|
||||
Queries support the `==`, `!=`, `<`, `<=`, `>`, `>=` comparison operators and the simple pattern matching `%` operator.
|
||||
|
||||
```
|
||||
friends.#[last=="Murphy"].first >> "Dale"
|
||||
friends.#[last=="Murphy"]#.first >> ["Dale","Jane"]
|
||||
friends.#[age>45]#.last >> ["Craig","Murphy"]
|
||||
friends.#[first%"D*"].last >> "Murphy"
|
||||
```
|
||||
|
||||
## Result Type
|
||||
|
||||
GJSON supports the json types `string`, `number`, `bool`, and `null`.
|
||||
Arrays and Objects are returned as their raw json types.
|
||||
|
||||
The `Result` type holds one of these:
|
||||
|
||||
```
|
||||
bool, for JSON booleans
|
||||
float64, for JSON numbers
|
||||
string, for JSON string literals
|
||||
nil, for JSON null
|
||||
```
|
||||
|
||||
To directly access the value:
|
||||
|
||||
```go
|
||||
result.Type // can be String, Number, True, False, Null, or JSON
|
||||
result.Str // holds the string
|
||||
result.Num // holds the float64 number
|
||||
result.Raw // holds the raw json
|
||||
result.Index // index of raw value in original json, zero means index unknown
|
||||
```
|
||||
|
||||
There are a variety of handy functions that work on a result:
|
||||
|
||||
```go
|
||||
result.Value() interface{}
|
||||
result.Int() int64
|
||||
result.Uint() uint64
|
||||
result.Float() float64
|
||||
result.String() string
|
||||
result.Bool() bool
|
||||
result.Array() []gjson.Result
|
||||
result.Map() map[string]gjson.Result
|
||||
result.Get(path string) Result
|
||||
result.ForEach(iterator func(key, value Result) bool)
|
||||
result.Less(token Result, caseSensitive bool) bool
|
||||
```
|
||||
|
||||
The `result.Value()` function returns an `interface{}` which requires type assertion and is one of the following Go types:
|
||||
|
||||
|
||||
|
||||
The `result.Array()` function returns back an array of values.
|
||||
If the result represents a non-existent value, then an empty array will be returned.
|
||||
If the result is not a JSON array, the return value will be an array containing one result.
|
||||
|
||||
```go
|
||||
boolean >> bool
|
||||
number >> float64
|
||||
string >> string
|
||||
null >> nil
|
||||
array >> []interface{}
|
||||
object >> map[string]interface{}
|
||||
```
|
||||
|
||||
## Get nested array values
|
||||
|
||||
Suppose you want all the last names from the following json:
|
||||
|
||||
```json
|
||||
{
|
||||
"programmers": [
|
||||
{
|
||||
"firstName": "Janet",
|
||||
"lastName": "McLaughlin",
|
||||
}, {
|
||||
"firstName": "Elliotte",
|
||||
"lastName": "Hunter",
|
||||
}, {
|
||||
"firstName": "Jason",
|
||||
"lastName": "Harold",
|
||||
}
|
||||
]
|
||||
}`
|
||||
```
|
||||
|
||||
You would use the path "programmers.#.lastName" like such:
|
||||
|
||||
```go
|
||||
result := gjson.Get(json, "programmers.#.lastName")
|
||||
for _,name := range result.Array() {
|
||||
println(name.String())
|
||||
}
|
||||
```
|
||||
|
||||
You can also query an object inside an array:
|
||||
|
||||
```go
|
||||
name := gjson.Get(json, `programmers.#[lastName="Hunter"].firstName`)
|
||||
println(name.String()) // prints "Elliotte"
|
||||
```
|
||||
|
||||
## Iterate through an object or array
|
||||
|
||||
The `ForEach` function allows for quickly iterating through an object or array.
|
||||
The key and value are passed to the iterator function for objects.
|
||||
Only the value is passed for arrays.
|
||||
Returning `false` from an iterator will stop iteration.
|
||||
|
||||
```go
|
||||
result := gjson.Get(json, "programmers")
|
||||
result.ForEach(func(key, value gjson.Result) bool{
|
||||
println(value.String())
|
||||
return true // keep iterating
|
||||
})
|
||||
```
|
||||
|
||||
## Simple Parse and Get
|
||||
|
||||
There's a `Parse(json)` function that will do a simple parse, and `result.Get(path)` that will search a result.
|
||||
|
||||
For example, all of these will return the same result:
|
||||
|
||||
```go
|
||||
gjson.Parse(json).Get("name").Get("last")
|
||||
gjson.Get(json, "name").Get("last")
|
||||
gjson.Get(json, "name.last")
|
||||
```
|
||||
|
||||
## Check for the existence of a value
|
||||
|
||||
Sometimes you just want to know if a value exists.
|
||||
|
||||
```go
|
||||
value := gjson.Get(json, "name.last")
|
||||
if !value.Exists() {
|
||||
println("no last name")
|
||||
} else {
|
||||
println(value.String())
|
||||
}
|
||||
|
||||
// Or as one step
|
||||
if gjson.Get(json, "name.last").Exists(){
|
||||
println("has a last name")
|
||||
}
|
||||
```
|
||||
|
||||
## Unmarshal to a map
|
||||
|
||||
To unmarshal to a `map[string]interface{}`:
|
||||
|
||||
```go
|
||||
m, ok := gjson.Parse(json).Value().(map[string]interface{})
|
||||
if !ok{
|
||||
// not a map
|
||||
}
|
||||
```
|
||||
|
||||
## Working with Bytes
|
||||
|
||||
If your JSON is contained in a `[]byte` slice, there's the [GetBytes](https://godoc.org/github.com/tidwall/gjson#GetBytes) function. This is preferred over `Get(string(data), path)`.
|
||||
|
||||
```go
|
||||
var json []byte = ...
|
||||
result := gjson.GetBytes(json, path)
|
||||
```
|
||||
|
||||
If you are using the `gjson.GetBytes(json, path)` function and you want to avoid converting `result.Raw` to a `[]byte`, then you can use this pattern:
|
||||
|
||||
```go
|
||||
var json []byte = ...
|
||||
result := gjson.GetBytes(json, path)
|
||||
var raw []byte
|
||||
if result.Index > 0 {
|
||||
raw = json[result.Index:result.Index+len(result.Raw)]
|
||||
} else {
|
||||
raw = []byte(result.Raw)
|
||||
}
|
||||
```
|
||||
|
||||
This is a best-effort no allocation sub slice of the original json. This method utilizes the `result.Index` field, which is the position of the raw data in the original json. It's possible that the value of `result.Index` equals zero, in which case the `result.Raw` is converted to a `[]byte`.
|
||||
|
||||
## Get multiple values at once
|
||||
|
||||
The `GetMany` function can be used to get multiple values at the same time, and is optimized to scan over a JSON payload once.
|
||||
|
||||
```go
|
||||
results := gjson.GetMany(json, "name.first", "name.last", "age")
|
||||
```
|
||||
|
||||
The return value is a `[]Result`, which will always contain exactly the same number of items as the input paths.
|
||||
|
||||
## Performance
|
||||
|
||||
Benchmarks of GJSON alongside [encoding/json](https://golang.org/pkg/encoding/json/),
|
||||
[ffjson](https://github.com/pquerna/ffjson),
|
||||
[EasyJSON](https://github.com/mailru/easyjson),
|
||||
and [jsonparser](https://github.com/buger/jsonparser)
|
||||
|
||||
```
|
||||
BenchmarkGJSONGet-8 15000000 333 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGJSONUnmarshalMap-8 900000 4188 ns/op 1920 B/op 26 allocs/op
|
||||
BenchmarkJSONUnmarshalMap-8 600000 8908 ns/op 3048 B/op 69 allocs/op
|
||||
BenchmarkJSONUnmarshalStruct-8 600000 9026 ns/op 1832 B/op 69 allocs/op
|
||||
BenchmarkJSONDecoder-8 300000 14339 ns/op 4224 B/op 184 allocs/op
|
||||
BenchmarkFFJSONLexer-8 1500000 3156 ns/op 896 B/op 8 allocs/op
|
||||
BenchmarkEasyJSONLexer-8 3000000 938 ns/op 613 B/op 6 allocs/op
|
||||
BenchmarkJSONParserGet-8 3000000 442 ns/op 21 B/op 0 allocs/op
|
||||
```
|
||||
|
||||
Benchmarks for the `GetMany` function:
|
||||
|
||||
```
|
||||
BenchmarkGJSONGetMany4Paths-8 4000000 319 ns/op 112 B/op 0 allocs/op
|
||||
BenchmarkGJSONGetMany8Paths-8 8000000 218 ns/op 56 B/op 0 allocs/op
|
||||
BenchmarkGJSONGetMany16Paths-8 16000000 160 ns/op 56 B/op 0 allocs/op
|
||||
BenchmarkGJSONGetMany32Paths-8 32000000 130 ns/op 64 B/op 0 allocs/op
|
||||
BenchmarkGJSONGetMany64Paths-8 64000000 117 ns/op 64 B/op 0 allocs/op
|
||||
BenchmarkGJSONGetMany128Paths-8 128000000 109 ns/op 64 B/op 0 allocs/op
|
||||
```
|
||||
|
||||
JSON document used:
|
||||
|
||||
```json
|
||||
{
|
||||
"widget": {
|
||||
"debug": "on",
|
||||
"window": {
|
||||
"title": "Sample Konfabulator Widget",
|
||||
"name": "main_window",
|
||||
"width": 500,
|
||||
"height": 500
|
||||
},
|
||||
"image": {
|
||||
"src": "Images/Sun.png",
|
||||
"hOffset": 250,
|
||||
"vOffset": 250,
|
||||
"alignment": "center"
|
||||
},
|
||||
"text": {
|
||||
"data": "Click Here",
|
||||
"size": 36,
|
||||
"style": "bold",
|
||||
"vOffset": 100,
|
||||
"alignment": "center",
|
||||
"onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Each operation was rotated though one of the following search paths:
|
||||
|
||||
```
|
||||
widget.window.name
|
||||
widget.image.hOffset
|
||||
widget.text.onMouseUp
|
||||
```
|
||||
|
||||
For the `GetMany` benchmarks these paths are used:
|
||||
|
||||
```
|
||||
widget.window.name
|
||||
widget.image.hOffset
|
||||
widget.text.onMouseUp
|
||||
widget.window.title
|
||||
widget.image.alignment
|
||||
widget.text.style
|
||||
widget.window.height
|
||||
widget.image.src
|
||||
widget.text.data
|
||||
widget.text.size
|
||||
```
|
||||
|
||||
*These benchmarks were run on a MacBook Pro 15" 2.8 GHz Intel Core i7 using Go 1.7.*
|
||||
|
||||
## Contact
|
||||
Josh Baker [@tidwall](http://twitter.com/tidwall)
|
||||
|
||||
## License
|
||||
|
||||
GJSON source code is available under the MIT [License](/LICENSE).
|
1946
vendor/github.com/tidwall/gjson/gjson.go
generated
vendored
Normal file
1946
vendor/github.com/tidwall/gjson/gjson.go
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
BIN
vendor/github.com/tidwall/gjson/logo.png
generated
vendored
Normal file
BIN
vendor/github.com/tidwall/gjson/logo.png
generated
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 16 KiB |
20
vendor/github.com/tidwall/grect/LICENSE.md
generated
vendored
Normal file
20
vendor/github.com/tidwall/grect/LICENSE.md
generated
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 Josh Baker
|
||||
|
||||
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:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
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.
|
25
vendor/github.com/tidwall/grect/README.md
generated
vendored
Normal file
25
vendor/github.com/tidwall/grect/README.md
generated
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
GRECT
|
||||
====
|
||||
|
||||
Quickly get the outer rectangle for GeoJSON, WKT, WKB.
|
||||
|
||||
```go
|
||||
r := grect.Get(`{
|
||||
"type": "Polygon",
|
||||
"coordinates": [
|
||||
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0],
|
||||
[100.0, 1.0], [100.0, 0.0] ]
|
||||
]
|
||||
}`)
|
||||
fmt.Printf("%v %v\n", r.Min, r.Max)
|
||||
// Output:
|
||||
// [100 0] [101 1]
|
||||
```
|
||||
|
||||
## Contact
|
||||
Josh Baker [@tidwall](http://twitter.com/tidwall)
|
||||
|
||||
## License
|
||||
|
||||
GRECT source code is available under the MIT [License](/LICENSE).
|
||||
|
337
vendor/github.com/tidwall/grect/grect.go
generated
vendored
Normal file
337
vendor/github.com/tidwall/grect/grect.go
generated
vendored
Normal file
@@ -0,0 +1,337 @@
|
||||
package grect
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
type Rect struct {
|
||||
Min, Max []float64
|
||||
}
|
||||
|
||||
func (r Rect) String() string {
|
||||
diff := len(r.Min) != len(r.Max)
|
||||
if !diff {
|
||||
for i := 0; i < len(r.Min); i++ {
|
||||
if r.Min[i] != r.Max[i] {
|
||||
diff = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
var buf []byte
|
||||
buf = append(buf, '[')
|
||||
for i, v := range r.Min {
|
||||
if i > 0 {
|
||||
buf = append(buf, ' ')
|
||||
}
|
||||
buf = append(buf, strconv.FormatFloat(v, 'f', -1, 64)...)
|
||||
}
|
||||
if diff {
|
||||
buf = append(buf, ']', ',', '[')
|
||||
for i, v := range r.Max {
|
||||
if i > 0 {
|
||||
buf = append(buf, ' ')
|
||||
}
|
||||
buf = append(buf, strconv.FormatFloat(v, 'f', -1, 64)...)
|
||||
}
|
||||
}
|
||||
buf = append(buf, ']')
|
||||
return string(buf)
|
||||
}
|
||||
|
||||
func normalize(min, max []float64) (nmin, nmax []float64) {
|
||||
if len(max) == 0 {
|
||||
return min, min
|
||||
} else if len(max) != len(min) {
|
||||
if len(max) < len(min) {
|
||||
max = append(max, min[len(max):]...)
|
||||
} else if len(min) < len(max) {
|
||||
min = append(min, max[len(min):]...)
|
||||
}
|
||||
}
|
||||
match := true
|
||||
for i := 0; i < len(min); i++ {
|
||||
if min[i] != max[i] {
|
||||
if match {
|
||||
match = false
|
||||
}
|
||||
if min[i] > max[i] {
|
||||
min[i], max[i] = max[i], min[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
if match {
|
||||
return min, min
|
||||
}
|
||||
return min, max
|
||||
}
|
||||
|
||||
func Get(s string) Rect {
|
||||
var i int
|
||||
var ws bool
|
||||
var min, max []float64
|
||||
for ; i < len(s); i++ {
|
||||
switch s[i] {
|
||||
default:
|
||||
continue
|
||||
case ' ', '\t', '\r', '\n':
|
||||
ws = true
|
||||
continue
|
||||
case '[':
|
||||
min, max, i = getRect(s, i)
|
||||
case '{':
|
||||
min, max, i = getGeoJSON(s, i)
|
||||
case 0x00, 0x01:
|
||||
if !ws {
|
||||
// return parseWKB(s, i)
|
||||
}
|
||||
case 'p', 'P', 'l', 'L', 'm', 'M', 'g', 'G':
|
||||
min, max, i = getWKT(s, i)
|
||||
}
|
||||
break
|
||||
}
|
||||
min, max = normalize(min, max)
|
||||
return Rect{Min: min, Max: max}
|
||||
}
|
||||
|
||||
func getRect(s string, i int) (min, max []float64, ri int) {
|
||||
a := s[i:]
|
||||
parts := strings.Split(a, ",")
|
||||
for i := 0; i < len(parts) && i < 2; i++ {
|
||||
part := parts[i]
|
||||
if len(part) > 0 && (part[0] <= ' ' || part[len(part)-1] <= ' ') {
|
||||
part = strings.TrimSpace(part)
|
||||
}
|
||||
if len(part) >= 2 && part[0] == '[' && part[len(part)-1] == ']' {
|
||||
pieces := strings.Split(part[1:len(part)-1], " ")
|
||||
if i == 0 {
|
||||
min = make([]float64, 0, len(pieces))
|
||||
} else {
|
||||
max = make([]float64, 0, len(pieces))
|
||||
}
|
||||
for j := 0; j < len(pieces); j++ {
|
||||
piece := pieces[j]
|
||||
if piece != "" {
|
||||
n, _ := strconv.ParseFloat(piece, 64)
|
||||
if i == 0 {
|
||||
min = append(min, n)
|
||||
} else {
|
||||
max = append(max, n)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// normalize
|
||||
if len(parts) == 1 {
|
||||
max = min
|
||||
} else {
|
||||
min, max = normalize(min, max)
|
||||
}
|
||||
|
||||
return min, max, len(s)
|
||||
}
|
||||
|
||||
func union(min1, max1, min2, max2 []float64) (umin, umax []float64) {
|
||||
for i := 0; i < len(min1) || i < len(min2); i++ {
|
||||
if i >= len(min1) {
|
||||
// just copy min2
|
||||
umin = append(umin, min2[i])
|
||||
umax = append(umax, max2[i])
|
||||
} else if i >= len(min2) {
|
||||
// just copy min1
|
||||
umin = append(umin, min1[i])
|
||||
umax = append(umax, max1[i])
|
||||
} else {
|
||||
if min1[i] < min2[i] {
|
||||
umin = append(umin, min1[i])
|
||||
} else {
|
||||
umin = append(umin, min2[i])
|
||||
}
|
||||
if max1[i] > max2[i] {
|
||||
umax = append(umax, max1[i])
|
||||
} else {
|
||||
umax = append(umax, max2[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
return umin, umax
|
||||
}
|
||||
|
||||
func getWKT(s string, i int) (min, max []float64, ri int) {
|
||||
switch s[i] {
|
||||
default:
|
||||
for ; i < len(s); i++ {
|
||||
if s[i] == ',' {
|
||||
return nil, nil, i
|
||||
}
|
||||
if s[i] == '(' {
|
||||
return getWKTAny(s, i)
|
||||
}
|
||||
}
|
||||
return nil, nil, i
|
||||
case 'g', 'G':
|
||||
if len(s)-i < 18 {
|
||||
return nil, nil, i
|
||||
}
|
||||
return getWKTGeometryCollection(s, i+18)
|
||||
}
|
||||
}
|
||||
|
||||
func getWKTAny(s string, i int) (min, max []float64, ri int) {
|
||||
min, max = make([]float64, 0, 4), make([]float64, 0, 4)
|
||||
var depth int
|
||||
var ni int
|
||||
var idx int
|
||||
loop:
|
||||
for ; i < len(s); i++ {
|
||||
switch s[i] {
|
||||
default:
|
||||
if ni == 0 {
|
||||
ni = i
|
||||
}
|
||||
case '(':
|
||||
depth++
|
||||
case ')', ' ', '\t', '\r', '\n', ',':
|
||||
if ni != 0 {
|
||||
n, _ := strconv.ParseFloat(s[ni:i], 64)
|
||||
if idx >= len(min) {
|
||||
min = append(min, n)
|
||||
max = append(max, n)
|
||||
} else {
|
||||
if n < min[idx] {
|
||||
min[idx] = n
|
||||
} else if n > max[idx] {
|
||||
max[idx] = n
|
||||
}
|
||||
}
|
||||
idx++
|
||||
ni = 0
|
||||
}
|
||||
switch s[i] {
|
||||
case ')':
|
||||
idx = 0
|
||||
depth--
|
||||
if depth == 0 {
|
||||
i++
|
||||
break loop
|
||||
}
|
||||
case ',':
|
||||
idx = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
return min, max, i
|
||||
}
|
||||
|
||||
func getWKTGeometryCollection(s string, i int) (min, max []float64, ri int) {
|
||||
var depth int
|
||||
for ; i < len(s); i++ {
|
||||
if s[i] == ',' || s[i] == ')' {
|
||||
// do not increment the index
|
||||
return nil, nil, i
|
||||
}
|
||||
if s[i] == '(' {
|
||||
depth++
|
||||
i++
|
||||
break
|
||||
}
|
||||
}
|
||||
next:
|
||||
for ; i < len(s); i++ {
|
||||
switch s[i] {
|
||||
case 'p', 'P', 'l', 'L', 'm', 'M', 'g', 'G':
|
||||
var min2, max2 []float64
|
||||
min2, max2, i = getWKT(s, i)
|
||||
min, max = union(min, max, min2, max2)
|
||||
for ; i < len(s); i++ {
|
||||
if s[i] == ',' {
|
||||
i++
|
||||
goto next
|
||||
}
|
||||
if s[i] == ')' {
|
||||
i++
|
||||
goto done
|
||||
}
|
||||
}
|
||||
case ' ', '\t', '\r', '\n':
|
||||
continue
|
||||
default:
|
||||
goto end_early
|
||||
}
|
||||
}
|
||||
end_early:
|
||||
// just balance the parens
|
||||
for ; i < len(s); i++ {
|
||||
if s[i] == '(' {
|
||||
depth++
|
||||
} else if s[i] == ')' {
|
||||
depth--
|
||||
if depth == 0 {
|
||||
i++
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
done:
|
||||
return min, max, i
|
||||
}
|
||||
func getGeoJSON(s string, i int) (min, max []float64, ri int) {
|
||||
json := s[i:]
|
||||
switch gjson.Get(json, "type").String() {
|
||||
default:
|
||||
min, max = getMinMaxBrackets(gjson.Get(json, "coordinates").Raw)
|
||||
case "Feature":
|
||||
min, max, _ = getGeoJSON(gjson.Get(json, "geometry").String(), 0)
|
||||
case "FeatureCollection":
|
||||
for _, json := range gjson.Get(json, "features").Array() {
|
||||
nmin, nmax, _ := getGeoJSON(json.String(), 0)
|
||||
min, max = union(min, max, nmin, nmax)
|
||||
}
|
||||
case "GeometryCollection":
|
||||
for _, json := range gjson.Get(json, "geometries").Array() {
|
||||
nmin, nmax, _ := getGeoJSON(json.String(), 0)
|
||||
min, max = union(min, max, nmin, nmax)
|
||||
}
|
||||
}
|
||||
return min, max, len(json)
|
||||
}
|
||||
|
||||
func getMinMaxBrackets(s string) (min, max []float64) {
|
||||
var ni int
|
||||
var idx int
|
||||
for i := 0; i < len(s); i++ {
|
||||
switch s[i] {
|
||||
default:
|
||||
if ni == 0 {
|
||||
ni = i
|
||||
}
|
||||
case '[', ',', ']', ' ', '\t', '\r', '\n':
|
||||
if ni > 0 {
|
||||
n, _ := strconv.ParseFloat(s[ni:i], 64)
|
||||
if idx >= len(min) {
|
||||
min = append(min, n)
|
||||
max = append(max, n)
|
||||
} else {
|
||||
if n < min[idx] {
|
||||
min[idx] = n
|
||||
} else if n > max[idx] {
|
||||
max[idx] = n
|
||||
}
|
||||
}
|
||||
ni = 0
|
||||
idx++
|
||||
}
|
||||
if s[i] == ']' {
|
||||
idx = 0
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
20
vendor/github.com/tidwall/match/LICENSE
generated
vendored
Normal file
20
vendor/github.com/tidwall/match/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 Josh Baker
|
||||
|
||||
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:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
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.
|
31
vendor/github.com/tidwall/match/README.md
generated
vendored
Normal file
31
vendor/github.com/tidwall/match/README.md
generated
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
Match
|
||||
=====
|
||||
<a href="https://travis-ci.org/tidwall/match"><img src="https://img.shields.io/travis/tidwall/match.svg?style=flat-square" alt="Build Status"></a>
|
||||
<a href="https://godoc.org/github.com/tidwall/match"><img src="https://img.shields.io/badge/api-reference-blue.svg?style=flat-square" alt="GoDoc"></a>
|
||||
|
||||
Match is a very simple pattern matcher where '*' matches on any
|
||||
number characters and '?' matches on any one character.
|
||||
Installing
|
||||
----------
|
||||
|
||||
```
|
||||
go get -u github.com/tidwall/match
|
||||
```
|
||||
|
||||
Example
|
||||
-------
|
||||
|
||||
```go
|
||||
match.Match("hello", "*llo")
|
||||
match.Match("jello", "?ello")
|
||||
match.Match("hello", "h*o")
|
||||
```
|
||||
|
||||
|
||||
Contact
|
||||
-------
|
||||
Josh Baker [@tidwall](http://twitter.com/tidwall)
|
||||
|
||||
License
|
||||
-------
|
||||
Redcon source code is available under the MIT [License](/LICENSE).
|
192
vendor/github.com/tidwall/match/match.go
generated
vendored
Normal file
192
vendor/github.com/tidwall/match/match.go
generated
vendored
Normal file
@@ -0,0 +1,192 @@
|
||||
// Match provides a simple pattern matcher with unicode support.
|
||||
package match
|
||||
|
||||
import "unicode/utf8"
|
||||
|
||||
// Match returns true if str matches pattern. This is a very
|
||||
// simple wildcard match where '*' matches on any number characters
|
||||
// and '?' matches on any one character.
|
||||
|
||||
// pattern:
|
||||
// { term }
|
||||
// term:
|
||||
// '*' matches any sequence of non-Separator characters
|
||||
// '?' matches any single non-Separator character
|
||||
// c matches character c (c != '*', '?', '\\')
|
||||
// '\\' c matches character c
|
||||
//
|
||||
func Match(str, pattern string) bool {
|
||||
if pattern == "*" {
|
||||
return true
|
||||
}
|
||||
return deepMatch(str, pattern)
|
||||
}
|
||||
func deepMatch(str, pattern string) bool {
|
||||
for len(pattern) > 0 {
|
||||
if pattern[0] > 0x7f {
|
||||
return deepMatchRune(str, pattern)
|
||||
}
|
||||
switch pattern[0] {
|
||||
default:
|
||||
if len(str) == 0 {
|
||||
return false
|
||||
}
|
||||
if str[0] > 0x7f {
|
||||
return deepMatchRune(str, pattern)
|
||||
}
|
||||
if str[0] != pattern[0] {
|
||||
return false
|
||||
}
|
||||
case '?':
|
||||
if len(str) == 0 {
|
||||
return false
|
||||
}
|
||||
case '*':
|
||||
return deepMatch(str, pattern[1:]) ||
|
||||
(len(str) > 0 && deepMatch(str[1:], pattern))
|
||||
}
|
||||
str = str[1:]
|
||||
pattern = pattern[1:]
|
||||
}
|
||||
return len(str) == 0 && len(pattern) == 0
|
||||
}
|
||||
|
||||
func deepMatchRune(str, pattern string) bool {
|
||||
var sr, pr rune
|
||||
var srsz, prsz int
|
||||
|
||||
// read the first rune ahead of time
|
||||
if len(str) > 0 {
|
||||
if str[0] > 0x7f {
|
||||
sr, srsz = utf8.DecodeRuneInString(str)
|
||||
} else {
|
||||
sr, srsz = rune(str[0]), 1
|
||||
}
|
||||
} else {
|
||||
sr, srsz = utf8.RuneError, 0
|
||||
}
|
||||
if len(pattern) > 0 {
|
||||
if pattern[0] > 0x7f {
|
||||
pr, prsz = utf8.DecodeRuneInString(pattern)
|
||||
} else {
|
||||
pr, prsz = rune(pattern[0]), 1
|
||||
}
|
||||
} else {
|
||||
pr, prsz = utf8.RuneError, 0
|
||||
}
|
||||
// done reading
|
||||
for pr != utf8.RuneError {
|
||||
switch pr {
|
||||
default:
|
||||
if srsz == utf8.RuneError {
|
||||
return false
|
||||
}
|
||||
if sr != pr {
|
||||
return false
|
||||
}
|
||||
case '?':
|
||||
if srsz == utf8.RuneError {
|
||||
return false
|
||||
}
|
||||
case '*':
|
||||
return deepMatchRune(str, pattern[prsz:]) ||
|
||||
(srsz > 0 && deepMatchRune(str[srsz:], pattern))
|
||||
}
|
||||
str = str[srsz:]
|
||||
pattern = pattern[prsz:]
|
||||
// read the next runes
|
||||
if len(str) > 0 {
|
||||
if str[0] > 0x7f {
|
||||
sr, srsz = utf8.DecodeRuneInString(str)
|
||||
} else {
|
||||
sr, srsz = rune(str[0]), 1
|
||||
}
|
||||
} else {
|
||||
sr, srsz = utf8.RuneError, 0
|
||||
}
|
||||
if len(pattern) > 0 {
|
||||
if pattern[0] > 0x7f {
|
||||
pr, prsz = utf8.DecodeRuneInString(pattern)
|
||||
} else {
|
||||
pr, prsz = rune(pattern[0]), 1
|
||||
}
|
||||
} else {
|
||||
pr, prsz = utf8.RuneError, 0
|
||||
}
|
||||
// done reading
|
||||
}
|
||||
|
||||
return srsz == 0 && prsz == 0
|
||||
}
|
||||
|
||||
var maxRuneBytes = func() []byte {
|
||||
b := make([]byte, 4)
|
||||
if utf8.EncodeRune(b, '\U0010FFFF') != 4 {
|
||||
panic("invalid rune encoding")
|
||||
}
|
||||
return b
|
||||
}()
|
||||
|
||||
// Allowable parses the pattern and determines the minimum and maximum allowable
|
||||
// values that the pattern can represent.
|
||||
// When the max cannot be determined, 'true' will be returned
|
||||
// for infinite.
|
||||
func Allowable(pattern string) (min, max string) {
|
||||
if pattern == "" || pattern[0] == '*' {
|
||||
return "", ""
|
||||
}
|
||||
|
||||
minb := make([]byte, 0, len(pattern))
|
||||
maxb := make([]byte, 0, len(pattern))
|
||||
var wild bool
|
||||
for i := 0; i < len(pattern); i++ {
|
||||
if pattern[i] == '*' {
|
||||
wild = true
|
||||
break
|
||||
}
|
||||
if pattern[i] == '?' {
|
||||
minb = append(minb, 0)
|
||||
maxb = append(maxb, maxRuneBytes...)
|
||||
} else {
|
||||
minb = append(minb, pattern[i])
|
||||
maxb = append(maxb, pattern[i])
|
||||
}
|
||||
}
|
||||
if wild {
|
||||
r, n := utf8.DecodeLastRune(maxb)
|
||||
if r != utf8.RuneError {
|
||||
if r < utf8.MaxRune {
|
||||
r++
|
||||
if r > 0x7f {
|
||||
b := make([]byte, 4)
|
||||
nn := utf8.EncodeRune(b, r)
|
||||
maxb = append(maxb[:len(maxb)-n], b[:nn]...)
|
||||
} else {
|
||||
maxb = append(maxb[:len(maxb)-n], byte(r))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return string(minb), string(maxb)
|
||||
/*
|
||||
return
|
||||
if wild {
|
||||
r, n := utf8.DecodeLastRune(maxb)
|
||||
if r != utf8.RuneError {
|
||||
if r < utf8.MaxRune {
|
||||
infinite = true
|
||||
} else {
|
||||
r++
|
||||
if r > 0x7f {
|
||||
b := make([]byte, 4)
|
||||
nn := utf8.EncodeRune(b, r)
|
||||
maxb = append(maxb[:len(maxb)-n], b[:nn]...)
|
||||
} else {
|
||||
maxb = append(maxb[:len(maxb)-n], byte(r))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return string(minb), string(maxb), infinite
|
||||
*/
|
||||
}
|
19
vendor/github.com/tidwall/rtree/LICENSE
generated
vendored
Normal file
19
vendor/github.com/tidwall/rtree/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
Copyright (c) 2016 Josh Baker
|
||||
|
||||
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:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
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.
|
21
vendor/github.com/tidwall/rtree/README.md
generated
vendored
Normal file
21
vendor/github.com/tidwall/rtree/README.md
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
RTree implementation for Go
|
||||
===========================
|
||||
|
||||
[](https://travis-ci.org/tidwall/rtree)
|
||||
[](https://godoc.org/github.com/tidwall/rtree)
|
||||
|
||||
This package provides an in-memory R-Tree implementation for Go, useful as a spatial data structure.
|
||||
It has support for 1-20 dimensions, and can store and search multidimensions interchangably in the same tree.
|
||||
|
||||
Authors
|
||||
-------
|
||||
* 1983 Original algorithm and test code by Antonin Guttman and Michael Stonebraker, UC Berkely
|
||||
* 1994 ANCI C ported from original test code by Melinda Green
|
||||
* 1995 Sphere volume fix for degeneracy problem submitted by Paul Brook
|
||||
* 2004 Templated C++ port by Greg Douglas
|
||||
* 2016 Go port by Josh Baker
|
||||
|
||||
License
|
||||
-------
|
||||
RTree source code is available under the MIT License.
|
||||
|
14013
vendor/github.com/tidwall/rtree/rtree.go
generated
vendored
Normal file
14013
vendor/github.com/tidwall/rtree/rtree.go
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user