Skip to content

Commit

Permalink
Create unique ID index when inserting from JSONL
Browse files Browse the repository at this point in the history
  • Loading branch information
ml-evs committed Nov 17, 2024
1 parent 43ef254 commit ef79b84
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
6 changes: 3 additions & 3 deletions optimade/server/entry_collections/entry_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,11 @@ 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.
def create_index(self, field: str, unique: bool = False) -> None:
"""Create an index on the given field.
Arguments:
fields: The fields, or single field, to index.
field: The field to index.
unique: Whether or not the index should be unique.
"""
Expand Down
11 changes: 4 additions & 7 deletions optimade/server/entry_collections/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,15 @@ def insert(self, data: list[EntryResource]) -> None:
except Exception:
pass

def create_index(self, fields: str | set[str], unique: bool = False) -> None:
"""Create an index on the given fields.
def create_index(self, field: str, unique: bool = False) -> None:
"""Create an index on the given field.
Arguments:
fields: The fields, or single field, to index.
field: The 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)
self.collection.create_index(field, unique=unique, background=True)

def handle_query_params(
self, params: EntryListingQueryParams | SingleEntryQueryParams
Expand Down
3 changes: 3 additions & 0 deletions optimade/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ def insert_from_jsonl(jsonl_path: Path) -> None:
ENTRY_COLLECTIONS[entry_type].insert(batch[entry_type])
batch[entry_type] = []

for entry_type in ENTRY_COLLECTIONS:
ENTRY_COLLECTIONS[entry_type].create_index("id", unique=True)

if bad_rows:
LOGGER.warning("Could not read %d rows from the JSONL file", bad_rows)

Expand Down

0 comments on commit ef79b84

Please sign in to comment.