Skip to content

Commit

Permalink
datastore: query cache
Browse files Browse the repository at this point in the history
  • Loading branch information
sinkuu committed May 14, 2020
1 parent e9f67ab commit ee3062f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
26 changes: 16 additions & 10 deletions swift.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ type SwiftContainer struct {
}

type QueryCache struct {
index int
name string
prefix string
index int
name string
}

func (c *QueryCache) Invalidate() {
c.prefix = ""
c.name = ""
}

Expand Down Expand Up @@ -124,12 +126,12 @@ func (s *SwiftContainer) Query(q dsq.Query) (dsq.Results, error) {

offset := q.Offset

if s.cache.name != "" && s.cache.index <= offset {
if s.cache.prefix == opts.Prefix && s.cache.name != "" && s.cache.index <= offset {
opts.Marker = s.cache.name
offset = s.cache.index - offset
}

end := q.Offset + q.Limit
end := offset + q.Limit
if end != 0 && end < opts.Limit {
opts.Limit = end
}
Expand All @@ -149,10 +151,6 @@ func (s *SwiftContainer) Query(q dsq.Query) (dsq.Results, error) {
}

if len(names) == 0 {
if q.Limit > count && (q.Limit-count) < opts.Limit {
opts.Limit = q.Limit - count
}

for len(names) == 0 || offset > 0 {
if doneFetching {
return dsq.Result{}, false
Expand Down Expand Up @@ -192,9 +190,17 @@ func (s *SwiftContainer) Query(q dsq.Query) (dsq.Results, error) {
name := names[0]
names = names[1:]

if len(names) == 0 && q.Limit > count && (q.Limit-count) < opts.Limit {
opts.Limit = q.Limit - count
}

// Cache the last item
if q.Limit != 0 && count == q.Limit {
s.cache.index = q.Offset + count
s.cache.name = name
s.cache = QueryCache{
prefix: opts.Prefix,
index: q.Offset + count,
name: name,
}
}

key := "/" + name
Expand Down
21 changes: 21 additions & 0 deletions swift_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,27 @@ func TestQuery(t *testing.T) {
"/a/b/d",
"/a/c",
}, rs)

rs, err = d.Query(dsq.Query{Prefix: "/a/", Offset: 4, Limit: 1})
if err != nil {
t.Fatal(err)
}

expectMatches(t, []string{
"/a/d",
}, rs)

rs, err = d.Query(dsq.Query{Prefix: "/a/", Offset: 1})
if err != nil {
t.Fatal(err)
}

expectMatches(t, []string{
"/a/b/c",
"/a/b/d",
"/a/c",
"/a/d",
}, rs)
}

func TestHas(t *testing.T) {
Expand Down

0 comments on commit ee3062f

Please sign in to comment.