Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
caosdb-pylib
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
caosdb
Software
caosdb-pylib
Commits
b122bdf1
Commit
b122bdf1
authored
9 months ago
by
Florian Spreckelsen
Browse files
Options
Downloads
Patches
Plain Diff
DOC: Add documentation of paginated queries.
parent
f508c8fc
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!142
F doc page length
Pipeline
#52331
failed
9 months ago
Stage: code_style
Stage: linting
Stage: test
Stage: deploy
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/doc/tutorials/index.rst
+1
-0
1 addition, 0 deletions
src/doc/tutorials/index.rst
src/doc/tutorials/paginated_queries.rst
+63
-0
63 additions, 0 deletions
src/doc/tutorials/paginated_queries.rst
with
64 additions
and
0 deletions
src/doc/tutorials/index.rst
+
1
−
0
View file @
b122bdf1
...
...
@@ -15,6 +15,7 @@ advanced usage of the Python client.
Data-Insertion
errors
Entity-Getters
paginated_queries
caching
data-model-interface
complex_data_models
...
...
This diff is collapsed.
Click to expand it.
src/doc/tutorials/paginated_queries.rst
0 → 100644
+
63
−
0
View file @
b122bdf1
Query pagination
================
When retrieving many entities, you may not want to retrieve all at once, e.g.,
for performance reasons or to prevent connection timeouts, but rather in a
chunked way. For that purpose, there is the ``page_length`` parameter in the
:py:meth:`~linkahead.common.models.execute_query` function. If this is set to a
non-zero integer, the behavior of the function changes in that it returns a
Python `generator <https://docs.python.org/3/glossary.html#term-generator>`_
which can be used, e.g., in loops or in list comprehension. The generator yields
a :py:class:`~linkahead.common.models.Container` containing the next
``page_length`` many entities from the query result.
The following example illustrates this on the demo server.
.. code-block:: python
import linkahead as db
# 10 at the time of writing of this example
print(db.execute_query("FIND MusicalInstrument"))
# Retrieve in pages of length 5 and iterate over the pages
for page in db.execute_query("FIND MusicalInstrument", page_length=5):
# each page is a container
print(type(page))
# exactly page_length=5 for the first N-1 pages,
# and possibly less for the last page
print(len(page))
# the items on each page are subclasses of Entity
print(type(page[0]))
# The id of the first entity on the page is different for all pages
print(page[0].id)
# You can use this in a list comprehension to fill a container
container_paginated = db.Container().extend(
[ent for page in db.execute_query("FIND MusicalInstrument", page_length=5) for ent in page]
)
# The result is the same as in the unpaginated case, but the
# following can cause connection timeouts in case of very large
# retrievals
container_at_once = db.execute_query("FIND MusicalInstrument")
for ent1, ent2 in zip(container_paginated, container_at_once):
print(ent1.id == ent2.id) # always true
As you can see, you can iterate over a paginated query and then access the
entities on each page during the iteration.
.. note::
The ``page_length`` keyword is ignored for ``COUNT`` queries where
:py:meth:`~linkahead.common.models.execute_query` always returns the integer
result and in case of ``unique=True`` where always exactly one
:py:class:`~linkahead.common.models.Entity` is returned.
.. warning::
Be careful when combining query pagination with insert, update, or delete
operations. If your database changes while iterating over a paginated query,
the client will raise a
:py:exc:`~linkahead.exceptions.PagingConsistencyError` since the server
can't guarantee that the query results haven't changed in the meantime.
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment