Skip to content

Commit

Permalink
Improve MongoDB interface:
Browse files Browse the repository at this point in the history
- provide method for creating indexes
- use unordered batch inserts
- refactor abstract insert method to be clearer
  • Loading branch information
ml-evs committed Nov 17, 2024
1 parent 482896d commit 97f4e50
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
2 changes: 1 addition & 1 deletion optimade/server/entry_collections/elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def __len__(self):
"""Returns the total number of entries in the collection."""
return Search(using=self.client, index=self.name).execute().hits.total.value

def insert(self, data: list[EntryResource]) -> None:
def insert(self, data: list[EntryResource | dict]) -> None:
"""Add the given entries to the underlying database.
Warning:
Expand Down
19 changes: 17 additions & 2 deletions optimade/server/entry_collections/entry_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,16 @@ def __len__(self) -> int:
"""Returns the total number of entries in the collection."""

@abstractmethod
def insert(self, data: list[EntryResource]) -> None:
def insert(self, data: list[EntryResource | dict]) -> None:
"""Add the given entries to the underlying database.
Warning:
No validation is performed on the incoming data, this data
should have been mapped to the appropriate format before
insertion.
Arguments:
data: The entry resource objects to add to the database.
data: The entries to add to the database.
"""

Expand Down Expand Up @@ -274,6 +279,16 @@ def all_fields(self) -> set[str]:

return self._all_fields

def create_index(self, fields: str | set[str], unique: bool = False) -> None:
"""Create an index on the given fields.
Arguments:
fields: The fields, or single field, to index.
unique: Whether or not the index should be unique.
"""
raise NotImplementedError

def get_attribute_fields(self) -> set[str]:
"""Get the set of attribute fields
Expand Down
23 changes: 19 additions & 4 deletions optimade/server/entry_collections/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,32 @@ def count(self, **kwargs: Any) -> int | None:
except ExecutionTimeout:
return None

def insert(self, data: list[EntryResource]) -> None:
def insert(self, data: list[EntryResource | dict]) -> None:
"""Add the given entries to the underlying database.
Warning:
No validation is performed on the incoming data.
No validation is performed on the incoming data, this data
should have been mapped to the appropriate format before
insertion.
Arguments:
data: The entry resource objects to add to the database.
data: The entries to add to the database.
"""
self.collection.insert_many(data)
self.collection.insert_many(data, ordered=False)

def create_index(self, fields: str | set[str], unique: bool = False) -> None:
"""Create an index on the given fields.
Arguments:
fields: The fields, or single field, to index.
unique: Whether or not the index should be unique.
"""
if isinstance(fields, str):
fields = {fields}

self.collection.create_indexes(fields, unique=unique, background=True)

def handle_query_params(
self, params: EntryListingQueryParams | SingleEntryQueryParams
Expand Down

0 comments on commit 97f4e50

Please sign in to comment.