Skip to content

Commit

Permalink
Make creation of default index configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
ml-evs committed Nov 17, 2024
1 parent c266a3e commit 17be6c8
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 8 deletions.
7 changes: 7 additions & 0 deletions optimade/server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,13 @@ class ServerConfig(BaseSettings):
),
] = None

create_default_index: Annotated[bool, Field(
description=(
"Whether to create a set of default indices "
"for supporting databases after inserting JSONL data.")
),
] = False

use_real_mongo: Annotated[
bool | None,
Field(description="DEPRECATED: force usage of MongoDB over any other backend."),
Expand Down
18 changes: 16 additions & 2 deletions optimade/server/entry_collections/entry_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,15 +275,29 @@ def all_fields(self) -> set[str]:
return self._all_fields

def create_index(self, field: str, unique: bool = False) -> None:
"""Create an index on the given field.
"""Create an index on the given field, as stored in the database.
Arguments:
field: The field to index.
field: The database field to index (i.e., if different from the OPTIMADE field,
the mapper should be used to convert between the two).
unique: Whether or not the index should be unique.
"""
raise NotImplementedError

Check warning on line 286 in optimade/server/entry_collections/entry_collections.py

View check run for this annotation

Codecov / codecov/patch

optimade/server/entry_collections/entry_collections.py#L286

Added line #L286 was not covered by tests

def create_default_index(self) -> None:
"""Create the default index for the collection.
For example, a database backend could override this method to
create a unique index on the `id` field, so that it can be called
on server startup.
This method should use a mapper to convert any OPTIMADE field names
to the corresponding stored names in the database.
"""
raise NotImplementedError

def get_attribute_fields(self) -> set[str]:
"""Get the set of attribute fields
Expand Down
18 changes: 16 additions & 2 deletions optimade/server/entry_collections/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,29 @@ def insert(self, data: list[EntryResource]) -> None:
pass

Check warning on line 107 in optimade/server/entry_collections/mongo.py

View check run for this annotation

Codecov / codecov/patch

optimade/server/entry_collections/mongo.py#L106-L107

Added lines #L106 - L107 were not covered by tests

def create_index(self, field: str, unique: bool = False) -> None:
"""Create an index on the given field.
"""Create an index on the given field, as stored in the database.
If any error is raised during index creation, this method should faithfully
return it, except for the simple case where an identical index already exists.
Arguments:
field: The field to index.
field: The database field to index (i.e., if different from the OPTIMADE field,
the mapper should be used to convert between the two).
unique: Whether or not the index should be unique.
"""
self.collection.create_index(field, unique=unique, background=True)

def create_default_index(self) -> None:
"""Create the default index for the collection.
For MongoDB, the default is to create a unique index
on the `id` field. This method should obey any configured
mappers.
"""
self.create_index(self.resource_mapper.get_backend_field("id"), unique=True)

def handle_query_params(
self, params: EntryListingQueryParams | SingleEntryQueryParams
) -> dict[str, Any]:
Expand Down
2 changes: 1 addition & 1 deletion optimade/server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def load_entries(endpoint_name: str, endpoint_collection: EntryCollection):
f"Requested JSONL file does not exist: {jsonl_path}. Please specify an absolute group."
)

insert_from_jsonl(jsonl_path)
insert_from_jsonl(jsonl_path, create_default_index=CONFIG.create_default_index)

LOGGER.debug("Inserted data from JSONL file: %s", jsonl_path)
if CONFIG.insert_test_data:
Expand Down
7 changes: 4 additions & 3 deletions optimade/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
)


def insert_from_jsonl(jsonl_path: Path, make_default_index: bool = False) -> None:
def insert_from_jsonl(jsonl_path: Path, create_default_index: bool = False) -> None:
"""Insert OPTIMADE JSON lines data into the database.
Arguments:
Expand Down Expand Up @@ -101,10 +101,11 @@ def insert_from_jsonl(jsonl_path: Path, make_default_index: bool = False) -> Non
ENTRY_COLLECTIONS[entry_type].insert(batch[entry_type])
batch[entry_type] = []

if make_default_index:
# If the chosen database backend supports it, make the default indices
if create_default_index:
for entry_type in ENTRY_COLLECTIONS:
try:
ENTRY_COLLECTIONS[entry_type].create_index("id", unique=True)
ENTRY_COLLECTIONS[entry_type].create_default_index()
except NotImplementedError:
pass

Expand Down
1 change: 1 addition & 0 deletions tests/test_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"mongo_count_timeout": 0,
"index_base_url": "http://localhost:5001",
"insert_from_jsonl": "optimade/server/data/test_data.jsonl",
"create_default_index": true,
"provider_fields": {
"structures": [
"band_gap",
Expand Down

0 comments on commit 17be6c8

Please sign in to comment.