mirror of https://github.com/tidwall/buntdb.git
replace indexes
This commit is contained in:
parent
7cc7ccf899
commit
2d071d5cbe
|
@ -38,7 +38,7 @@ Features
|
||||||
- Flexible [iteration](#iterating) of data; ascending, descending, and ranges
|
- Flexible [iteration](#iterating) of data; ascending, descending, and ranges
|
||||||
- [Durable append-only file](#append-only-file) format for persistence.
|
- [Durable append-only file](#append-only-file) format for persistence.
|
||||||
- Option to evict old items with an [expiration](#data-expiration) TTL
|
- Option to evict old items with an [expiration](#data-expiration) TTL
|
||||||
- Tight codebase, ~1K loc using the `cloc` command
|
- Tight codebase, under 2K loc using the `cloc` command
|
||||||
- ACID semantics with locking [transactions](#transactions) that support rollbacks
|
- ACID semantics with locking [transactions](#transactions) that support rollbacks
|
||||||
|
|
||||||
Getting Started
|
Getting Started
|
||||||
|
|
35
buntdb.go
35
buntdb.go
|
@ -225,7 +225,6 @@ type index struct {
|
||||||
rtr *rtree.RTree // contains the items
|
rtr *rtree.RTree // contains the items
|
||||||
name string // name of the index
|
name string // name of the index
|
||||||
pattern string // a required key pattern
|
pattern string // a required key pattern
|
||||||
uc bool // unicode pattern
|
|
||||||
less func(a, b string) bool // less comparison function
|
less func(a, b string) bool // less comparison function
|
||||||
rect func(item string) (min, max []float64) // rect from string function
|
rect func(item string) (min, max []float64) // rect from string function
|
||||||
db *DB // the origin database
|
db *DB // the origin database
|
||||||
|
@ -248,7 +247,16 @@ type index struct {
|
||||||
// IndexString, IndexBinary, etc.
|
// IndexString, IndexBinary, etc.
|
||||||
func (db *DB) CreateIndex(name, pattern string,
|
func (db *DB) CreateIndex(name, pattern string,
|
||||||
less ...func(a, b string) bool) error {
|
less ...func(a, b string) bool) error {
|
||||||
return db.createIndex(name, pattern, less, nil)
|
return db.createIndex(false, name, pattern, less, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReplaceIndex builds a new index and populates it with items.
|
||||||
|
// The items are ordered in an b-tree and can be retrieved using the
|
||||||
|
// Ascend* and Descend* methods.
|
||||||
|
// If a previous index with the same name exists, that index will be deleted.
|
||||||
|
func (db *DB) ReplaceIndex(name, pattern string,
|
||||||
|
less ...func(a, b string) bool) error {
|
||||||
|
return db.createIndex(true, name, pattern, less, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateSpatialIndex builds a new index and populates it with items.
|
// CreateSpatialIndex builds a new index and populates it with items.
|
||||||
|
@ -267,11 +275,21 @@ func (db *DB) CreateIndex(name, pattern string,
|
||||||
// parameter.
|
// parameter.
|
||||||
func (db *DB) CreateSpatialIndex(name, pattern string,
|
func (db *DB) CreateSpatialIndex(name, pattern string,
|
||||||
rect func(item string) (min, max []float64)) error {
|
rect func(item string) (min, max []float64)) error {
|
||||||
return db.createIndex(name, pattern, nil, rect)
|
return db.createIndex(false, name, pattern, nil, rect)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReplaceSpatialIndex builds a new index and populates it with items.
|
||||||
|
// The items are organized in an r-tree and can be retrieved using the
|
||||||
|
// Intersects method.
|
||||||
|
// If a previous index with the same name exists, that index will be deleted.
|
||||||
|
func (db *DB) ReplaceSpatialIndex(name, pattern string,
|
||||||
|
rect func(item string) (min, max []float64)) error {
|
||||||
|
return db.createIndex(true, name, pattern, nil, rect)
|
||||||
}
|
}
|
||||||
|
|
||||||
// createIndex is called by CreateIndex() and CreateSpatialIndex()
|
// createIndex is called by CreateIndex() and CreateSpatialIndex()
|
||||||
func (db *DB) createIndex(
|
func (db *DB) createIndex(
|
||||||
|
replace bool,
|
||||||
name string,
|
name string,
|
||||||
pattern string,
|
pattern string,
|
||||||
lessers []func(a, b string) bool,
|
lessers []func(a, b string) bool,
|
||||||
|
@ -286,8 +304,12 @@ func (db *DB) createIndex(
|
||||||
return ErrIndexExists
|
return ErrIndexExists
|
||||||
}
|
}
|
||||||
if _, ok := db.idxs[name]; ok {
|
if _, ok := db.idxs[name]; ok {
|
||||||
|
if replace {
|
||||||
|
delete(db.idxs, name)
|
||||||
|
} else {
|
||||||
return ErrIndexExists
|
return ErrIndexExists
|
||||||
}
|
}
|
||||||
|
}
|
||||||
var less func(a, b string) bool
|
var less func(a, b string) bool
|
||||||
switch len(lessers) {
|
switch len(lessers) {
|
||||||
default:
|
default:
|
||||||
|
@ -314,19 +336,12 @@ func (db *DB) createIndex(
|
||||||
rect: rect,
|
rect: rect,
|
||||||
db: db,
|
db: db,
|
||||||
}
|
}
|
||||||
for i := 0; i < len(pattern); i++ {
|
|
||||||
if pattern[i] > 0x7f {
|
|
||||||
idx.uc = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if less != nil {
|
if less != nil {
|
||||||
idx.btr = btree.New(btreeDegrees, idx)
|
idx.btr = btree.New(btreeDegrees, idx)
|
||||||
}
|
}
|
||||||
if rect != nil {
|
if rect != nil {
|
||||||
idx.rtr = rtree.New(idx)
|
idx.rtr = rtree.New(idx)
|
||||||
}
|
}
|
||||||
|
|
||||||
db.keys.Ascend(func(item btree.Item) bool {
|
db.keys.Ascend(func(item btree.Item) bool {
|
||||||
dbi := item.(*dbItem)
|
dbi := item.(*dbItem)
|
||||||
if !match.Match(dbi.key, idx.pattern) {
|
if !match.Match(dbi.key, idx.pattern) {
|
||||||
|
|
Loading…
Reference in New Issue