Skip to content

Commit

Permalink
Add orjson options in JSONStore
Browse files Browse the repository at this point in the history
  • Loading branch information
gpetretto committed May 29, 2023
1 parent 035b3ad commit d7d14e9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
14 changes: 8 additions & 6 deletions src/maggma/stores/gridfs.py
Expand Up @@ -2,7 +2,7 @@
"""
Module containing various definitions of Stores.
Stores are a default access pattern to data and provide
various utillities
various utilities
"""

import copy
Expand All @@ -11,7 +11,7 @@
from ruamel import yaml
from datetime import datetime
from pymongo.errors import ConfigurationError
from typing import Any, Dict, Iterator, List, Optional, Tuple, Union
from typing import Dict, Iterator, List, Optional, Tuple, Union

import gridfs
from monty.json import jsanitize
Expand Down Expand Up @@ -162,7 +162,9 @@ def connect(self, force_reset: bool = False):
def _collection(self):
"""Property referring to underlying pymongo collection"""
if self._coll is None:
raise StoreError("Must connect Mongo-like store before attemping to use it")
raise StoreError(
"Must connect Mongo-like store before attempting to use it"
)
return self._coll

@property
Expand Down Expand Up @@ -244,7 +246,6 @@ def query(
if properties is not None and prop_keys.issubset(set(doc.keys())):
yield {p: doc[p] for p in properties if p in doc}
else:

metadata = doc.get("metadata", {})

data = self._collection.find_one(
Expand Down Expand Up @@ -354,7 +355,7 @@ def groupby(

def ensure_index(self, key: str, unique: Optional[bool] = False) -> bool:
"""
Tries to create an index and return true if it suceeded
Tries to create an index and return true if it succeeded
Currently operators on the GridFS files collection
Args:
key: single key to index
Expand Down Expand Up @@ -447,7 +448,8 @@ def remove_docs(self, criteria: Dict):
self._collection.delete(_id)

def close(self):
self._collection.database.client.close()
self._files_store.close()
self._coll = None
if self.ssh_tunnel is not None:
self.ssh_tunnel.stop()

Expand Down
16 changes: 14 additions & 2 deletions src/maggma/stores/mongolike.py
Expand Up @@ -10,7 +10,7 @@
from itertools import chain, groupby
from socket import socket
import warnings
from typing import Dict, Iterator, List, Optional, Tuple, Union
from typing import Dict, Iterator, List, Optional, Tuple, Union, Any

import mongomock
import orjson
Expand Down Expand Up @@ -704,6 +704,8 @@ def __init__(
self,
paths: Union[str, List[str]],
read_only: bool = True,
serialization_option: int | None = None,
serialization_default: Any | None = None,
**kwargs,
):
"""
Expand All @@ -720,6 +722,10 @@ def __init__(
Note that when read_only=False, JSONStore only supports a single JSON
file. If the file does not exist, it will be automatically created
when the JSONStore is initialized.
serialization_option:
option that will be passed to the orjson.dump when saving to the json the file.
serialization_default:
default that will be passed to the orjson.dump when saving to the json the file.
"""
paths = paths if isinstance(paths, (list, tuple)) else [paths]
self.paths = paths
Expand Down Expand Up @@ -755,6 +761,8 @@ def __init__(
f.write(bytesdata.decode("utf-8"))

self.default_sort = None
self.serialization_option = serialization_option
self.serialization_default = serialization_default

super().__init__(**kwargs)

Expand Down Expand Up @@ -838,7 +846,11 @@ def update_json_file(self):
data = [d for d in self.query()]
for d in data:
d.pop("_id")
bytesdata = orjson.dumps(data)
bytesdata = orjson.dumps(
data,
option=self.serialization_option,
default=self.serialization_default,
)
f.write(bytesdata.decode("utf-8"))

def __hash__(self):
Expand Down
14 changes: 11 additions & 3 deletions tests/stores/test_gridfs.py
Expand Up @@ -6,6 +6,7 @@
import numpy as np
import numpy.testing.utils as nptu
import pytest
from maggma.core import StoreError

from maggma.stores import GridFSStore, MongoStore
from maggma.stores.gridfs import files_collection_fields, GridFSURIStore
Expand Down Expand Up @@ -218,7 +219,7 @@ def test_index(gridfsstore):

def test_gfs_metadata(gridfsstore):
"""
Ensure metadata is put back in the docuemnt
Ensure metadata is put back in the document
"""
tic = datetime(2018, 4, 12, 16)

Expand All @@ -244,7 +245,6 @@ def test_gridfsstore_from_launchpad_file(lp_file):


def test_searchable_fields(gridfsstore):

tic = datetime(2018, 4, 12, 16)

data = [
Expand All @@ -259,7 +259,6 @@ def test_searchable_fields(gridfsstore):


def test_additional_metadata(gridfsstore):

tic = datetime(2018, 4, 12, 16)

data = [
Expand Down Expand Up @@ -299,3 +298,12 @@ def test_gridfs_uri_dbname_parse():
uri_with_db = "mongodb://uuu:xxxx@host:27017"
with pytest.raises(ConfigurationError):
GridFSURIStore(uri_with_db, "test")


def test_close(gridfsstore):
assert gridfsstore.query_one() is None
gridfsstore.close()
with pytest.raises(StoreError):
gridfsstore.query_one()
# reconnect to allow the drop of the collection in the fixture
gridfsstore.connect()

0 comments on commit d7d14e9

Please sign in to comment.