218 lines
7.5 KiB
Go
218 lines
7.5 KiB
Go
package buntdb
|
|
|
|
import "github.com/tidwall/match"
|
|
|
|
// AscendKeys allows for iterating through keys based on the specified pattern.
|
|
func (tx *Tx) AscendKeys(pattern string,
|
|
iterator func(key, value string) bool) error {
|
|
if pattern == "" {
|
|
return nil
|
|
}
|
|
if pattern[0] == '*' {
|
|
if pattern == "*" {
|
|
return tx.Ascend("", iterator)
|
|
}
|
|
return tx.Ascend("", func(key, value string) bool {
|
|
if match.Match(key, pattern) {
|
|
if !iterator(key, value) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
})
|
|
}
|
|
min, max := match.Allowable(pattern)
|
|
return tx.AscendGreaterOrEqual("", min, func(key, value string) bool {
|
|
if key > max {
|
|
return false
|
|
}
|
|
if match.Match(key, pattern) {
|
|
if !iterator(key, value) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
})
|
|
}
|
|
|
|
// DescendKeys allows for iterating through keys based on the specified pattern.
|
|
func (tx *Tx) DescendKeys(pattern string,
|
|
iterator func(key, value string) bool) error {
|
|
if pattern == "" {
|
|
return nil
|
|
}
|
|
if pattern[0] == '*' {
|
|
if pattern == "*" {
|
|
return tx.Descend("", iterator)
|
|
}
|
|
return tx.Descend("", func(key, value string) bool {
|
|
if match.Match(key, pattern) {
|
|
if !iterator(key, value) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
})
|
|
}
|
|
min, max := match.Allowable(pattern)
|
|
return tx.DescendLessOrEqual("", max, func(key, value string) bool {
|
|
if key < min {
|
|
return false
|
|
}
|
|
if match.Match(key, pattern) {
|
|
if !iterator(key, value) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
})
|
|
}
|
|
|
|
// Ascend calls the iterator for every item in the database within the range
|
|
// [first, last], until iterator returns false.
|
|
// When an index is provided, the results will be ordered by the item values
|
|
// as specified by the less() function of the defined index.
|
|
// When an index is not provided, the results will be ordered by the item key.
|
|
// An invalid index will return an error.
|
|
func (tx *Tx) Ascend(index string,
|
|
iterator func(key, value string) bool) error {
|
|
return tx.scan(false, false, false, index, "", "", iterator)
|
|
}
|
|
|
|
// AscendGreaterOrEqual calls the iterator for every item in the database within
|
|
// the range [pivot, last], until iterator returns false.
|
|
// When an index is provided, the results will be ordered by the item values
|
|
// as specified by the less() function of the defined index.
|
|
// When an index is not provided, the results will be ordered by the item key.
|
|
// An invalid index will return an error.
|
|
func (tx *Tx) AscendGreaterOrEqual(index, pivot string,
|
|
iterator func(key, value string) bool) error {
|
|
return tx.scan(false, true, false, index, pivot, "", iterator)
|
|
}
|
|
|
|
// AscendLessThan calls the iterator for every item in the database within the
|
|
// range [first, pivot), until iterator returns false.
|
|
// When an index is provided, the results will be ordered by the item values
|
|
// as specified by the less() function of the defined index.
|
|
// When an index is not provided, the results will be ordered by the item key.
|
|
// An invalid index will return an error.
|
|
func (tx *Tx) AscendLessThan(index, pivot string,
|
|
iterator func(key, value string) bool) error {
|
|
return tx.scan(false, false, true, index, pivot, "", iterator)
|
|
}
|
|
|
|
// AscendRange calls the iterator for every item in the database within
|
|
// the range [greaterOrEqual, lessThan), until iterator returns false.
|
|
// When an index is provided, the results will be ordered by the item values
|
|
// as specified by the less() function of the defined index.
|
|
// When an index is not provided, the results will be ordered by the item key.
|
|
// An invalid index will return an error.
|
|
func (tx *Tx) AscendRange(index, greaterOrEqual, lessThan string,
|
|
iterator func(key, value string) bool) error {
|
|
return tx.scan(
|
|
false, true, true, index, greaterOrEqual, lessThan, iterator,
|
|
)
|
|
}
|
|
|
|
// Descend calls the iterator for every item in the database within the range
|
|
// [last, first], until iterator returns false.
|
|
// When an index is provided, the results will be ordered by the item values
|
|
// as specified by the less() function of the defined index.
|
|
// When an index is not provided, the results will be ordered by the item key.
|
|
// An invalid index will return an error.
|
|
func (tx *Tx) Descend(index string,
|
|
iterator func(key, value string) bool) error {
|
|
return tx.scan(true, false, false, index, "", "", iterator)
|
|
}
|
|
|
|
// DescendGreaterThan calls the iterator for every item in the database within
|
|
// the range [last, pivot), until iterator returns false.
|
|
// When an index is provided, the results will be ordered by the item values
|
|
// as specified by the less() function of the defined index.
|
|
// When an index is not provided, the results will be ordered by the item key.
|
|
// An invalid index will return an error.
|
|
func (tx *Tx) DescendGreaterThan(index, pivot string,
|
|
iterator func(key, value string) bool) error {
|
|
return tx.scan(true, true, false, index, pivot, "", iterator)
|
|
}
|
|
|
|
// DescendLessOrEqual calls the iterator for every item in the database within
|
|
// the range [pivot, first], until iterator returns false.
|
|
// When an index is provided, the results will be ordered by the item values
|
|
// as specified by the less() function of the defined index.
|
|
// When an index is not provided, the results will be ordered by the item key.
|
|
// An invalid index will return an error.
|
|
func (tx *Tx) DescendLessOrEqual(index, pivot string,
|
|
iterator func(key, value string) bool) error {
|
|
return tx.scan(true, false, true, index, pivot, "", iterator)
|
|
}
|
|
|
|
// DescendRange calls the iterator for every item in the database within
|
|
// the range [lessOrEqual, greaterThan), until iterator returns false.
|
|
// When an index is provided, the results will be ordered by the item values
|
|
// as specified by the less() function of the defined index.
|
|
// When an index is not provided, the results will be ordered by the item key.
|
|
// An invalid index will return an error.
|
|
func (tx *Tx) DescendRange(index, lessOrEqual, greaterThan string,
|
|
iterator func(key, value string) bool) error {
|
|
return tx.scan(
|
|
true, true, true, index, lessOrEqual, greaterThan, iterator,
|
|
)
|
|
}
|
|
|
|
// AscendEqual calls the iterator for every item in the database that equals
|
|
// pivot, until iterator returns false.
|
|
// When an index is provided, the results will be ordered by the item values
|
|
// as specified by the less() function of the defined index.
|
|
// When an index is not provided, the results will be ordered by the item key.
|
|
// An invalid index will return an error.
|
|
func (tx *Tx) AscendEqual(index, pivot string,
|
|
iterator func(key, value string) bool) error {
|
|
var err error
|
|
var less func(a, b string) bool
|
|
if index != "" {
|
|
less, err = tx.GetLess(index)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return tx.AscendGreaterOrEqual(index, pivot, func(key, value string) bool {
|
|
if less == nil {
|
|
if key != pivot {
|
|
return false
|
|
}
|
|
} else if less(pivot, value) {
|
|
return false
|
|
}
|
|
return iterator(key, value)
|
|
})
|
|
}
|
|
|
|
// DescendEqual calls the iterator for every item in the database that equals
|
|
// pivot, until iterator returns false.
|
|
// When an index is provided, the results will be ordered by the item values
|
|
// as specified by the less() function of the defined index.
|
|
// When an index is not provided, the results will be ordered by the item key.
|
|
// An invalid index will return an error.
|
|
func (tx *Tx) DescendEqual(index, pivot string,
|
|
iterator func(key, value string) bool) error {
|
|
var err error
|
|
var less func(a, b string) bool
|
|
if index != "" {
|
|
less, err = tx.GetLess(index)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return tx.DescendLessOrEqual(index, pivot, func(key, value string) bool {
|
|
if less == nil {
|
|
if key != pivot {
|
|
return false
|
|
}
|
|
} else if less(value, pivot) {
|
|
return false
|
|
}
|
|
return iterator(key, value)
|
|
})
|
|
}
|