Skip to content

Commit

Permalink
Add ormsgpack unpacker
Browse files Browse the repository at this point in the history
  • Loading branch information
TLCFEM committed Oct 14, 2024
1 parent 391a74c commit 69d67fd
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 18 deletions.
26 changes: 20 additions & 6 deletions src/msglc/unpacker.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

from abc import abstractmethod
import msgpack
import msgspec


class Unpacker:
Expand All @@ -33,9 +32,24 @@ def decode(self, data):
return self._unpacker.unpack()


class MsgspecUnpacker(Unpacker):
def __init__(self):
self._unpacker = msgspec.msgpack.Decoder()
try:
import msgspec

def decode(self, data):
return self._unpacker.decode(data)
class MsgspecUnpacker(Unpacker):
def __init__(self):
self._unpacker = msgspec.msgpack.Decoder()

def decode(self, data):
return self._unpacker.decode(data)
except ImportError:
MsgspecUnpacker = MsgpackUnpacker

try:
import ormsgpack

class OrmsgpackUnpacker(Unpacker):
def decode(self, data):
return ormsgpack.unpackb(data)

except ImportError:
pass
8 changes: 7 additions & 1 deletion tests/test_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@
import pytest

from msglc import dump, config
from msglc.generate import generate_random_json, find_all_paths, goto_path, generate, compare
from msglc.generate import (
generate_random_json,
find_all_paths,
goto_path,
generate,
compare,
)
from msglc.reader import LazyStats, LazyReader
from msglc.unpacker import MsgpackUnpacker, MsgspecUnpacker

Expand Down
63 changes: 52 additions & 11 deletions tests/test_msglc.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@ def test_msglc(monkeypatch, tmpdir, json_before, json_after, target, size, cache

with MockIO(target, "rb", 0, 500 * 2**20) as buffer:
with LazyReader(buffer, counter=stats, cached=cached) as reader:
assert reader.read("glossary/GlossDiv/GlossList/GlossEntry/GlossDef/GlossSeeAlso/1") == "XML"
assert (
reader.read(
"glossary/GlossDiv/GlossList/GlossEntry/GlossDef/GlossSeeAlso/1"
)
== "XML"
)
assert reader.read("glossary/empty_list") == []
assert reader.read("glossary/none_list/0") is None
assert reader.read() == json_after
Expand All @@ -104,7 +109,9 @@ def test_msglc(monkeypatch, tmpdir, json_before, json_after, target, size, cache
for x, _ in dict_container.items():
assert x in ["title", "GlossList"]

list_container = reader.read("glossary/GlossDiv/GlossList/GlossEntry/GlossDef/GlossSeeAlso")
list_container = reader.read(
"glossary/GlossDiv/GlossList/GlossEntry/GlossDef/GlossSeeAlso"
)
assert len(list_container) == 2
for x in list_container:
assert x in ["GML", "XML"]
Expand All @@ -117,7 +124,9 @@ def test_msglc(monkeypatch, tmpdir, json_before, json_after, target, size, cache
@pytest.mark.parametrize("size", [0, 8192])
@pytest.mark.parametrize("cached", [True, False])
@pytest.mark.asyncio
async def test_async_msglc(monkeypatch, tmpdir, json_before, json_after, target, size, cached):
async def test_async_msglc(
monkeypatch, tmpdir, json_before, json_after, target, size, cached
):
monkeypatch.setattr(config, "small_obj_optimization_threshold", size)

with tmpdir.as_cwd():
Expand All @@ -137,7 +146,10 @@ async def test_async_msglc(monkeypatch, tmpdir, json_before, json_after, target,
with MockIO(target, "rb", 0, 500 * 2**20) as buffer:
with LazyReader(buffer, counter=stats, cached=cached) as reader:
assert (
await reader.async_read("glossary/GlossDiv/GlossList/GlossEntry/GlossDef/GlossSeeAlso/1") == "XML"
await reader.async_read(
"glossary/GlossDiv/GlossList/GlossEntry/GlossDef/GlossSeeAlso/1"
)
== "XML"
)
assert await reader.async_read("glossary/empty_list") == []
assert await reader.async_read("glossary/none_list/0") is None
Expand All @@ -153,7 +165,9 @@ async def test_async_msglc(monkeypatch, tmpdir, json_before, json_after, target,
for x, _ in dict_container.items():
assert x in ["title", "GlossList"]

list_container = await reader.async_read("glossary/GlossDiv/GlossList/GlossEntry/GlossDef/GlossSeeAlso")
list_container = await reader.async_read(
"glossary/GlossDiv/GlossList/GlossEntry/GlossDef/GlossSeeAlso"
)
assert len(list_container) == 2
for x in list_container:
assert x in ["GML", "XML"]
Expand Down Expand Up @@ -225,18 +239,33 @@ def test_combine_archives(tmpdir, json_after, target):
with LazyWriter("test_dict.msg") as writer:
writer.write(json_after)

combine("combined_a.msg", [FileInfo("test_list.msg", "first_inner"), FileInfo("test_dict.msg", "second_inner")])
combine(
"combined_a.msg",
[
FileInfo("test_list.msg", "first_inner"),
FileInfo("test_dict.msg", "second_inner"),
],
)

if isinstance(target, BytesIO):
target.seek(0)

combine(target, [FileInfo("combined_a.msg", "first_outer"), FileInfo("combined_a.msg", "second_outer")])
combine(
target,
[
FileInfo("combined_a.msg", "first_outer"),
FileInfo("combined_a.msg", "second_outer"),
],
)

if isinstance(target, BytesIO):
target.seek(0)

with LazyReader(target) as reader:
assert reader.read("first_outer//second_inner/glossary/title") == "example glossary"
assert (
reader.read("first_outer//second_inner/glossary/title")
== "example glossary"
)
assert reader.read("second_outer/first_inner/2") == 2
assert reader.read("second_outer/first_inner/-1") == 29
assert reader.read("second_outer/first_inner/0:2") == [0, 1]
Expand All @@ -245,7 +274,10 @@ def test_combine_archives(tmpdir, json_after, target):
assert reader.read("second_outer/first_inner/24:2:30") == [24, 26, 28]
assert reader.read("second_outer/first_inner/:2:5") == [0, 2, 4]
assert reader.read("second_outer/first_inner/24:2:") == [24, 26, 28]
assert reader.visit("first_outer//second_inner/glossary/title") == "example glossary"
assert (
reader.visit("first_outer//second_inner/glossary/title")
== "example glossary"
)
assert reader.visit("second_outer/first_inner/2") == 2
assert reader.visit("second_outer/first_inner/-1") == 29
assert reader.visit("second_outer/first_inner/0:2") == [0, 1]
Expand Down Expand Up @@ -288,9 +320,18 @@ def test_combine_archives(tmpdir, json_after, target):
assert reader[1]["second_inner"] == inner_reader

with pytest.raises(ValueError):
combine(target, [FileInfo("combined_a.msg", "some_name"), FileInfo("combined_a.msg")])
combine(
target,
[FileInfo("combined_a.msg", "some_name"), FileInfo("combined_a.msg")],
)
with pytest.raises(ValueError):
combine(target, [FileInfo("combined_a.msg", "some_name"), FileInfo("combined_a.msg", "some_name")])
combine(
target,
[
FileInfo("combined_a.msg", "some_name"),
FileInfo("combined_a.msg", "some_name"),
],
)
with pytest.raises(ValueError):
combine(target, [FileInfo("combined_aa.msg"), FileInfo("combined_a.msg")])

Expand Down

0 comments on commit 69d67fd

Please sign in to comment.