Switch V2 pagination back to using IDs, which should be much faster and easier on the DB
Also adds a test for the tags endpoint
This commit is contained in:
parent
b8b2c75822
commit
3161b60522
7 changed files with 105 additions and 31 deletions
|
@ -40,10 +40,10 @@ def handle_registry_v2_exception(error):
|
|||
return response
|
||||
|
||||
|
||||
_MAX_RESULTS_PER_PAGE = app.config.get('V2_PAGINATION_SIZE', 50)
|
||||
_MAX_RESULTS_PER_PAGE = app.config.get('V2_PAGINATION_SIZE', 100)
|
||||
|
||||
|
||||
def paginate(limit_kwarg_name='limit', offset_kwarg_name='offset',
|
||||
def paginate(start_id_kwarg_name='start_id', limit_kwarg_name='limit',
|
||||
callback_kwarg_name='pagination_callback'):
|
||||
"""
|
||||
Decorates a handler adding a parsed pagination token and a callback to encode a response token.
|
||||
|
@ -61,17 +61,16 @@ def paginate(limit_kwarg_name='limit', offset_kwarg_name='offset',
|
|||
next_page_token = request.args.get('next_page', request.args.get('last', None))
|
||||
|
||||
# Decrypt the next page token, if any.
|
||||
offset = 0
|
||||
start_id = None
|
||||
page_info = decrypt_page_token(next_page_token)
|
||||
if page_info is not None:
|
||||
# Note: we use offset here instead of ID >= n because one of the V2 queries is a UNION.
|
||||
offset = page_info.get('offset', 0)
|
||||
start_id = page_info.get('start_id', None)
|
||||
|
||||
def callback(num_results, response):
|
||||
if num_results < limit:
|
||||
def callback(results, response):
|
||||
if len(results) <= limit:
|
||||
return
|
||||
|
||||
next_page_token = encrypt_page_token({'offset': limit + offset})
|
||||
next_page_token = encrypt_page_token({'start_id': max([obj.id for obj in results])})
|
||||
|
||||
link_url = os.path.join(get_app_url(), url_for(request.endpoint, **request.view_args))
|
||||
link_param = urlencode({'n': limit, 'next_page': next_page_token})
|
||||
|
@ -79,12 +78,10 @@ def paginate(limit_kwarg_name='limit', offset_kwarg_name='offset',
|
|||
response.headers['Link'] = link
|
||||
|
||||
kwargs[limit_kwarg_name] = limit
|
||||
kwargs[offset_kwarg_name] = offset
|
||||
kwargs[start_id_kwarg_name] = start_id
|
||||
kwargs[callback_kwarg_name] = callback
|
||||
return func(*args, **kwargs)
|
||||
|
||||
return wrapped
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
|
|
Reference in a new issue