Skip to content

Commit 1d2add7

Browse files
authored
fix mongo id (#2317)
1 parent 4d483fd commit 1d2add7

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

optimade/utils.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,17 @@ def insert_from_jsonl(jsonl_path: Path, create_default_index: bool = False) -> N
120120

121121

122122
def mongo_id_for_database(database_id: str, database_type: str) -> str:
123-
"""Produce a MongoDB ObjectId for a database"""
123+
"""Produce a deterministic MongoDB ObjectId for a database"""
124+
import hashlib
125+
124126
from bson.objectid import ObjectId
125127

126-
oid = f"{database_id}{database_type}"
127-
if len(oid) > 12:
128-
oid = oid[:12]
129-
elif len(oid) < 12:
130-
oid = f"{oid}{'0' * (12 - len(oid))}"
128+
combined = f"{database_id}{database_type}"
129+
130+
hash_obj = hashlib.md5(combined.encode("utf-8"))
131+
hash_bytes = hash_obj.digest()[:12] # ObjectId requires 12 bytes
131132

132-
return str(ObjectId(oid.encode("UTF-8")))
133+
return str(ObjectId(hash_bytes))
133134

134135

135136
def get_providers(add_mongo_id: bool = False) -> list:

0 commit comments

Comments
 (0)