Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for newly added Query.SeekPrefix #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,36 @@ func (a *accessor) Query(q dsq.Query) (dsq.Results, error) {
return a.queryNew(q)
}

// rangeForQuery returns a new Range struct that starts and bounds a query,
// based on Query.Prefix and Query.SeekPrefix
func rangeForQuery(q *dsq.Query) *util.Range {
var rnge *util.Range

if len(q.Prefix) > 0 {
rnge = util.BytesPrefix([]byte(q.Prefix))
}

if len(q.SeekPrefix) > 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See ipfs/go-ds-badger#47 (comment). We should probably fail here if we're applying any order other than "order by key".

if rnge == nil {
limit := [1]byte{0xff}
rnge = &util.Range{
Limit: limit[:],
}
}
rnge.Start = []byte(q.SeekPrefix)
}

return rnge
}

func (a *accessor) queryNew(q dsq.Query) (dsq.Results, error) {
if len(q.Filters) > 0 ||
len(q.Orders) > 0 ||
q.Limit > 0 ||
q.Offset > 0 {
return a.queryOrig(q)
}
var rnge *util.Range
if q.Prefix != "" {
rnge = util.BytesPrefix([]byte(q.Prefix))
}
rnge := rangeForQuery(&q)
i := a.ldb.NewIterator(rnge, nil)
return dsq.ResultsFromIterator(q, dsq.Iterator{
Next: func() (dsq.Result, bool) {
Expand Down Expand Up @@ -185,10 +204,7 @@ func (a *accessor) queryOrig(q dsq.Query) (dsq.Results, error) {
}

func (a *accessor) runQuery(worker goprocess.Process, qrb *dsq.ResultBuilder) {
var rnge *util.Range
if qrb.Query.Prefix != "" {
rnge = util.BytesPrefix([]byte(qrb.Query.Prefix))
}
rnge := rangeForQuery(&qrb.Query)
i := a.ldb.NewIterator(rnge, nil)
defer i.Release()

Expand Down