added GetLess and GetRect tx functions

This commit is contained in:
Josh Baker 2016-09-05 14:29:50 -07:00
parent 301cfc2ead
commit 6cd540fffb
2 changed files with 78 additions and 2 deletions

View File

@ -1279,6 +1279,37 @@ type SetOptions struct {
TTL time.Duration TTL time.Duration
} }
// GetLess returns the less function for an index. This is handy for
// doing ad-hoc compares inside a transaction.
// Returns ErrNotFound if the index is not found or there is no less
// function bound to the index
func (tx *Tx) GetLess(index string) (func(a, b string) bool, error) {
if tx.db == nil {
return nil, ErrTxClosed
}
idx, ok := tx.db.idxs[index]
if !ok || idx.less == nil {
return nil, ErrNotFound
}
return idx.less, nil
}
// GetRect returns the rect function for an index. This is handy for
// doing ad-hoc searches inside a transaction.
// Returns ErrNotFound if the index is not found or there is no rect
// function bound to the index
func (tx *Tx) GetRect(index string) (func(s string) (min, max []float64),
error) {
if tx.db == nil {
return nil, ErrTxClosed
}
idx, ok := tx.db.idxs[index]
if !ok || idx.rect == nil {
return nil, ErrNotFound
}
return idx.rect, nil
}
// Set inserts or replaces an item in the database based on the key. // Set inserts or replaces an item in the database based on the key.
// The opt params may be used for additional functionality such as forcing // The opt params may be used for additional functionality such as forcing
// the item to be evicted at a specified time. When the return value // the item to be evicted at a specified time. When the return value

View File

@ -436,9 +436,33 @@ func TestVariousTx(t *testing.T) {
if err := db.CreateIndex("blank", "*", nil); err != nil { if err := db.CreateIndex("blank", "*", nil); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if err := db.CreateIndex("real", "*", IndexInt); err != nil {
t.Fatal(err)
}
// test scanning // test scanning
if err := db.Update(func(tx *Tx) error { if err := db.Update(func(tx *Tx) error {
_, _, err := tx.Set("nothing", "here", nil) less, err := tx.GetLess("junk")
if err != ErrNotFound {
t.Fatalf("expecting a not found, got %v", err)
}
if less != nil {
t.Fatal("expecting nil, got a less function")
}
less, err = tx.GetLess("blank")
if err != ErrNotFound {
t.Fatalf("expecting a not found, got %v", err)
}
if less != nil {
t.Fatal("expecting nil, got a less function")
}
less, err = tx.GetLess("real")
if err != nil {
return err
}
if less == nil {
t.Fatal("expecting a less function, got nil")
}
_, _, err = tx.Set("nothing", "here", nil)
return err return err
}); err != nil { }); err != nil {
t.Fatal(err) t.Fatal(err)
@ -543,6 +567,27 @@ func TestVariousTx(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
err = db.Update(func(tx *Tx) error { err = db.Update(func(tx *Tx) error {
rect, err := tx.GetRect("spat")
if err != nil {
return err
}
if rect == nil {
t.Fatal("expecting a rect function, got nil")
}
rect, err = tx.GetRect("junk")
if err != ErrNotFound {
t.Fatalf("expecting a not found, got %v", err)
}
if rect != nil {
t.Fatal("expecting nil, got a rect function")
}
rect, err = tx.GetRect("na")
if err != ErrNotFound {
t.Fatalf("expecting a not found, got %v", err)
}
if rect != nil {
t.Fatal("expecting nil, got a rect function")
}
if _, _, err := tx.Set("rect:1", "[10 10],[20 20]", nil); err != nil { if _, _, err := tx.Set("rect:1", "[10 10],[20 20]", nil); err != nil {
return err return err
} }
@ -553,7 +598,7 @@ func TestVariousTx(t *testing.T) {
return err return err
} }
s := "" s := ""
err := tx.Intersects("spat", "[5 5],[13 13]", func(key, val string) bool { err = tx.Intersects("spat", "[5 5],[13 13]", func(key, val string) bool {
s += key + ":" + val + "\n" s += key + ":" + val + "\n"
return true return true
}) })