diff --git a/datastore.go b/datastore.go index 7412c6c..53a74ab 100644 --- a/datastore.go +++ b/datastore.go @@ -1,7 +1,6 @@ package dsbbolt import ( - "fmt" "os" "bytes" @@ -94,10 +93,26 @@ func (d *Datastore) Query(q query.Query) (query.Results, error) { resBuilder := query.NewResultBuilder(q) if err := d.db.View(func(tx *bbolt.Tx) error { cursor := tx.Bucket(d.bucket).Cursor() + if q.Prefix == "" { + for k, _ := cursor.First(); k != nil; k, _ = cursor.Next() { + result := query.Result{} + key := datastore.NewKey(string(k)) + result.Entry.Key = key.String() + if !q.KeysOnly { + result.Entry.Value, result.Error = d.Get(key) + } + select { + case resBuilder.Output <- result: + default: + continue + } + } + return nil + } pref := []byte(q.Prefix) for k, _ := cursor.Seek(pref); bytes.HasPrefix(k, pref); k, _ = cursor.Next() { result := query.Result{} - key := datastore.NewKey(fmt.Sprintf("%v", k)) + key := datastore.NewKey(string(k)) result.Entry.Key = key.String() if !q.KeysOnly { result.Entry.Value, result.Error = d.Get(key) diff --git a/datastore_test.go b/datastore_test.go index 13a77d2..7d5d7c0 100644 --- a/datastore_test.go +++ b/datastore_test.go @@ -62,7 +62,7 @@ func Test_Datastore(t *testing.T) { } else if size != len([]byte("hello world")) { t.Fatal("incorrect data size") } - + // test a query where we specify a search key rs, err := ds.Query(query.Query{Prefix: key.String()}) if err != nil { t.Fatal(err) @@ -74,6 +74,18 @@ func Test_Datastore(t *testing.T) { for _, v := range res { fmt.Printf("%+v\n", v) } + // test a query where we dont specify a search key + rs, err = ds.Query(query.Query{}) + if err != nil { + t.Fatal(err) + } + res, err = rs.Rest() + if err != nil { + t.Fatal(err) + } + for _, v := range res { + fmt.Printf("%+v\n", v) + } if err := ds.Delete(key); err != nil { t.Fatal(err) }