Skip to content

Commit

Permalink
Replace elide_data_returned with count timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
ml-evs committed Sep 18, 2023
1 parent 066ac8a commit 9791fb8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
7 changes: 4 additions & 3 deletions optimade/server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,10 @@ class ServerConfig(BaseSettings):
None, description="Host settings to pass through to the `Elasticsearch` class."
)

elide_data_returned: bool = Field(
False,
description="Whether to skip counting all the results for every query (to set the `data_returned` field), as this may be too strenuous for large databases. Currently only supports MongoDB.",
mongo_count_timeout: int = Field(
5,
description="""Number of seconds to allow MongoDB to perform a full database count before falling back to `null`.
This operation can require a full COLLSCAN for empty queries which can be prohibitively slow if the database does not fit into the active set, hence a timeout can drastically speed-up response times.""",
)

mongo_database: str = Field(
Expand Down
5 changes: 3 additions & 2 deletions optimade/server/entry_collections/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,11 @@ def count(self, **kwargs: Any) -> int:
for k in list(kwargs.keys()):
if k not in ("filter", "skip", "limit", "hint", "maxTimeMS"):
del kwargs[k]
if "filter" not in kwargs: # "filter" is needed for count_documents()
kwargs["filter"] = {}
if "filter" not in kwargs:
return self.collection.estimated_document_count()
else:
if "maxTimeMS" not in kwargs:
kwargs["maxTimeMS"] = 1000 * CONFIG.mongo_count_timeout
return self.collection.count_documents(**kwargs)

def insert(self, data: List[EntryResource]) -> None:
Expand Down

0 comments on commit 9791fb8

Please sign in to comment.