From 6cd540fffb4fd5f2417f8d2c80dee8f3174939a5 Mon Sep 17 00:00:00 2001 From: Josh Baker Date: Mon, 5 Sep 2016 14:29:50 -0700 Subject: [PATCH] added GetLess and GetRect tx functions --- buntdb.go | 31 +++++++++++++++++++++++++++++++ buntdb_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/buntdb.go b/buntdb.go index a66eaa9..27cc203 100644 --- a/buntdb.go +++ b/buntdb.go @@ -1279,6 +1279,37 @@ type SetOptions struct { 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. // 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 diff --git a/buntdb_test.go b/buntdb_test.go index 225e48e..4bd7d24 100644 --- a/buntdb_test.go +++ b/buntdb_test.go @@ -436,9 +436,33 @@ func TestVariousTx(t *testing.T) { if err := db.CreateIndex("blank", "*", nil); err != nil { t.Fatal(err) } + if err := db.CreateIndex("real", "*", IndexInt); err != nil { + t.Fatal(err) + } // test scanning 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 }); err != nil { t.Fatal(err) @@ -543,6 +567,27 @@ func TestVariousTx(t *testing.T) { t.Fatal(err) } 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 { return err } @@ -553,7 +598,7 @@ func TestVariousTx(t *testing.T) { return err } 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" return true })