Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy load zstandard #827

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion qcportal/qcportal/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
except ImportError:
import pydantic
from pydantic import BaseModel, Extra, validator, PrivateAttr, Field
import zstandard

from .serialization import serialize, deserialize

Expand All @@ -35,12 +34,16 @@


def compress_for_cache(data: Any) -> sqlite3.Binary:
import zstandard

serialized_data = serialize(data, "msgpack")
compressed_data = zstandard.compress(serialized_data, level=1)
return sqlite3.Binary(compressed_data)


def decompress_from_cache(data: sqlite3.Binary, value_type) -> Any:
import zstandard

decompressed_data = zstandard.decompress(bytes(data))
deserialized_data = deserialize(decompressed_data, "msgpack")
return pydantic.parse_obj_as(value_type, deserialized_data)
Expand Down
3 changes: 2 additions & 1 deletion qcportal/qcportal/compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from typing import Optional, Tuple, Any

import msgpack
import zstandard


class CompressionEnum(str, Enum):
Expand Down Expand Up @@ -60,6 +59,8 @@ def compress(
# ZStandard compression
# By default, use level = 6 for larger data (>15MB or so)
elif compression_type == CompressionEnum.zstd:
import zstandard

if compression_level is None:
if len(data) > 15 * 1048576:
compression_level = 6
Expand Down
Loading