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

Add line-level deduplication for NDJSONReader. #50

Merged
merged 1 commit into from
Dec 2, 2024
Merged
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
11 changes: 8 additions & 3 deletions dabapush/Reader/NDJSONReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,15 @@ def __init__(self, config: "NDJSONReaderConfiguration") -> None:

def read(self) -> Iterator[Record]:
"""reads multiple NDJSON files and emits them line by line"""

for file_record in self.records:
yield from file_record.split(
func=read_and_split, flatten_records=self.config.flatten_dicts
filtered_records = filter(
lambda x: x not in self.back_log,
file_record.split(
func=read_and_split, flatten_records=self.config.flatten_dicts
),
)
yield from filtered_records


class NDJSONReaderConfiguration(ReaderConfiguration):
Expand All @@ -63,7 +68,7 @@ class NDJSONReaderConfiguration(ReaderConfiguration):
Attributes
----------
flatten_dicts: bool
wether to flatten those nested dictioniaries
whether to flatten those nested dicts

"""

Expand Down
2 changes: 1 addition & 1 deletion dabapush/Reader/Reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def records(self) -> Iterator[Record]:
"""Generator for all files matching the pattern in the read_path."""
if self.log_path.exists():
with self.log_path.open("rt", encoding="utf8") as f:
self.back_log = (ujson.loads(_) for _ in f.readlines())
self.back_log = [Record(**ujson.loads(_)) for _ in f.readlines()]
else:
self.log_path.touch()

Expand Down
29 changes: 26 additions & 3 deletions tests/Reader/test_NDJSONReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ def data():
return [{"key": "value"} for _ in range(20)]


def test_read(tmp_path: Path, data): # pylint: disable=W0621
def test_read(isolated_test_dir: Path, data): # pylint: disable=W0621
"""Should read the data from the file."""
reader = NDJSONReader(
NDJSONReaderConfiguration("test", read_path=str(tmp_path.resolve()))
NDJSONReaderConfiguration("test", read_path=str(isolated_test_dir.resolve()))
)
file_path = tmp_path / "test.ndjson"
file_path = isolated_test_dir / "test.ndjson"
with file_path.open("wt") as file:
for line in data:
json.dump(line, file)
Expand All @@ -30,3 +30,26 @@ def test_read(tmp_path: Path, data): # pylint: disable=W0621
for n, record in enumerate(records):
assert record.processed_at
assert record.payload == data[n]


def test_read_with_backlog(isolated_test_dir: Path, data): # pylint: disable=W0621
"""Should only read the new data."""
reader = NDJSONReaderConfiguration(
"test", read_path=str(isolated_test_dir.resolve())
).get_instance()
file_path = isolated_test_dir / "test.ndjson"
with file_path.open("wt") as file:
for line in data:
json.dump(line, file)
file.write("\n")

records = list(reader.read())
assert len(records) == 20

reader2 = NDJSONReaderConfiguration(
"test", read_path=str(isolated_test_dir.resolve())
).get_instance()

records = list(reader2.read())
assert len(records) == 0
assert len(reader2.back_log) == 20
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""Common fixtures for tests."""

import pytest


@pytest.fixture()
def isolated_test_dir(monkeypatch, tmp_path):
"""Create an isolated test tmp-directory."""
monkeypatch.chdir(tmp_path)
yield tmp_path