Skip to content

Commit 1a47add

Browse files
committed
relocate list total calculation to support resumptionToken content decision
1 parent 57735d7 commit 1a47add

File tree

2 files changed

+9
-16
lines changed

2 files changed

+9
-16
lines changed

lib/oai/provider/model/activerecord_caching_wrapper.rb

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,7 @@ def next_set(token_string)
7878
raise ResumptionTokenException.new unless @limit
7979

8080
token = ResumptionToken.parse(token_string)
81-
total = model.where(token_conditions(token)).count
82-
83-
if token.last * @limit + @limit < total
84-
select_partial(token)
85-
else
86-
select_partial(token).tap { |list| list.instance_variable_set(:@token, list.token.next(nil)) }
87-
end
81+
select_partial(token)
8882
end
8983

9084
# select a subset of the result set, and return it with a
@@ -102,10 +96,13 @@ def select_partial(token)
10296

10397
raise ResumptionTokenException.new unless oaitoken
10498

99+
total = model.where(token_conditions(token)).count
100+
# token offset should be nil if this is the last set
101+
offset = (token.last * @limit + @limit >= total) ? nil : token.last + 1
105102
PartialResult.new(
106103
hydrate_records(
107104
oaitoken.entries.limit(@limit).offset(token.last * @limit)),
108-
token.next(token.last + 1)
105+
token.next(offset)
109106
)
110107
end
111108

lib/oai/provider/model/activerecord_wrapper.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,7 @@ def next_set(find_scope, token_string)
129129
raise OAI::ResumptionTokenException.new unless @limit
130130

131131
token = ResumptionToken.parse(token_string)
132-
total = find_scope.where(token_conditions(token)).count
133-
134-
if @limit < total
135-
select_partial(find_scope, token)
136-
else # end of result set
137-
select_partial(find_scope, token).tap { |list| list.instance_variable_set(:@token, list.token.next(nil)) }
138-
end
132+
select_partial(find_scope, token)
139133
end
140134

141135
# select a subset of the result set, and return it with a
@@ -145,8 +139,10 @@ def select_partial(find_scope, token)
145139
.limit(@limit)
146140
.order("#{identifier_field} asc")
147141
raise OAI::ResumptionTokenException.new unless records
148-
offset = records.last.send(identifier_field)
149142

143+
total = find_scope.where(token_conditions(token)).count
144+
# token offset should be nil if this is the last set
145+
offset = (@limit >= total) ? nil : records.last.send(identifier_field)
150146
PartialResult.new(records, token.next(offset))
151147
end
152148

0 commit comments

Comments
 (0)