metaspn-store 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ from metaspn_store.store import FileSystemStore
2
+
3
+ __all__ = ["FileSystemStore"]
metaspn_store/store.py ADDED
@@ -0,0 +1,147 @@
1
+ from __future__ import annotations
2
+
3
+ import json
4
+ from datetime import date, datetime, timedelta, timezone
5
+ from pathlib import Path
6
+ from typing import Any, Iterator
7
+
8
+ from metaspn_schemas import EmissionEnvelope, EntityRef, SignalEnvelope
9
+
10
+
11
+ def _ensure_utc(value: datetime) -> datetime:
12
+ if value.tzinfo is None:
13
+ return value.replace(tzinfo=timezone.utc)
14
+ return value.astimezone(timezone.utc)
15
+
16
+
17
+ def _iter_days(start_day: date, end_day: date) -> Iterator[date]:
18
+ current = start_day
19
+ while current <= end_day:
20
+ yield current
21
+ current += timedelta(days=1)
22
+
23
+
24
+ class FileSystemStore:
25
+ """Append-only filesystem store for SignalEnvelope and EmissionEnvelope."""
26
+
27
+ def __init__(self, workspace: str | Path) -> None:
28
+ self.workspace = Path(workspace)
29
+ self.store_root = self.workspace / "store"
30
+ self.signals_dir = self.store_root / "signals"
31
+ self.emissions_dir = self.store_root / "emissions"
32
+ self.snapshots_dir = self.store_root / "snapshots"
33
+ self._ensure_dirs()
34
+
35
+ def _ensure_dirs(self) -> None:
36
+ self.signals_dir.mkdir(parents=True, exist_ok=True)
37
+ self.emissions_dir.mkdir(parents=True, exist_ok=True)
38
+ self.snapshots_dir.mkdir(parents=True, exist_ok=True)
39
+
40
+ def write_signal(self, signal: SignalEnvelope) -> Path:
41
+ """Append a signal to its UTC day partition and return written file path."""
42
+ if not signal.signal_id:
43
+ raise ValueError("signal_id is required for stable IDs")
44
+ if not signal.schema_version:
45
+ raise ValueError("schema_version is required")
46
+
47
+ signal_ts = _ensure_utc(signal.timestamp)
48
+ destination = self.signals_dir / f"{signal_ts.date().isoformat()}.jsonl"
49
+ with destination.open("a", encoding="utf-8") as handle:
50
+ handle.write(json.dumps(signal.to_dict(), sort_keys=True, separators=(",", ":")))
51
+ handle.write("\n")
52
+ return destination
53
+
54
+ def write_emission(self, emission: EmissionEnvelope) -> Path:
55
+ """Append an emission to its UTC day partition and return written file path."""
56
+ if not emission.emission_id:
57
+ raise ValueError("emission_id is required for stable IDs")
58
+ if not emission.schema_version:
59
+ raise ValueError("schema_version is required")
60
+
61
+ emission_ts = _ensure_utc(emission.timestamp)
62
+ destination = self.emissions_dir / f"{emission_ts.date().isoformat()}.jsonl"
63
+ with destination.open("a", encoding="utf-8") as handle:
64
+ handle.write(json.dumps(emission.to_dict(), sort_keys=True, separators=(",", ":")))
65
+ handle.write("\n")
66
+ return destination
67
+
68
+ def write_snapshot(
69
+ self,
70
+ name: str,
71
+ snapshot_state: dict[str, Any],
72
+ snapshot_time: datetime | None = None,
73
+ ) -> Path:
74
+ """Write a point-in-time snapshot JSON document."""
75
+ ts = _ensure_utc(snapshot_time or datetime.now(timezone.utc))
76
+ timestamp_token = ts.strftime("%Y-%m-%dT%H%M%SZ")
77
+ destination = self.snapshots_dir / f"{name}__{timestamp_token}.json"
78
+ with destination.open("w", encoding="utf-8") as handle:
79
+ json.dump(snapshot_state, handle, sort_keys=True, separators=(",", ":"))
80
+ handle.write("\n")
81
+ return destination
82
+
83
+ def iter_signals(
84
+ self,
85
+ start: datetime,
86
+ end: datetime,
87
+ entity_ref: EntityRef | None = None,
88
+ sources: list[str] | None = None,
89
+ ) -> Iterator[SignalEnvelope]:
90
+ """Stream signals in [start, end], optionally filtered by entity_ref and source."""
91
+ start_utc = _ensure_utc(start)
92
+ end_utc = _ensure_utc(end)
93
+ if end_utc < start_utc:
94
+ raise ValueError("end must be greater than or equal to start")
95
+
96
+ source_set = set(sources) if sources else None
97
+ for day in _iter_days(start_utc.date(), end_utc.date()):
98
+ partition = self.signals_dir / f"{day.isoformat()}.jsonl"
99
+ if not partition.exists():
100
+ continue
101
+ with partition.open("r", encoding="utf-8") as handle:
102
+ for line in handle:
103
+ if not line.strip():
104
+ continue
105
+ record = json.loads(line)
106
+ signal = SignalEnvelope.from_dict(record)
107
+ signal_ts = _ensure_utc(signal.timestamp)
108
+ if signal_ts < start_utc or signal_ts > end_utc:
109
+ continue
110
+ if source_set is not None and signal.source not in source_set:
111
+ continue
112
+ if entity_ref is not None and entity_ref not in signal.entity_refs:
113
+ continue
114
+ yield signal
115
+
116
+ def iter_emissions(
117
+ self,
118
+ start: datetime,
119
+ end: datetime,
120
+ entity_ref: EntityRef | None = None,
121
+ emission_types: list[str] | None = None,
122
+ ) -> Iterator[EmissionEnvelope]:
123
+ """Stream emissions in [start, end], optionally filtered by entity_ref and type."""
124
+ start_utc = _ensure_utc(start)
125
+ end_utc = _ensure_utc(end)
126
+ if end_utc < start_utc:
127
+ raise ValueError("end must be greater than or equal to start")
128
+
129
+ emission_type_set = set(emission_types) if emission_types else None
130
+ for day in _iter_days(start_utc.date(), end_utc.date()):
131
+ partition = self.emissions_dir / f"{day.isoformat()}.jsonl"
132
+ if not partition.exists():
133
+ continue
134
+ with partition.open("r", encoding="utf-8") as handle:
135
+ for line in handle:
136
+ if not line.strip():
137
+ continue
138
+ record = json.loads(line)
139
+ emission = EmissionEnvelope.from_dict(record)
140
+ emission_ts = _ensure_utc(emission.timestamp)
141
+ if emission_ts < start_utc or emission_ts > end_utc:
142
+ continue
143
+ if emission_type_set is not None and emission.emission_type not in emission_type_set:
144
+ continue
145
+ if entity_ref is not None and entity_ref not in emission.entity_refs:
146
+ continue
147
+ yield emission
@@ -0,0 +1,56 @@
1
+ Metadata-Version: 2.4
2
+ Name: metaspn-store
3
+ Version: 0.1.0
4
+ Summary: Minimal append-only durability layer and replay API for MetaSPN signals and emissions
5
+ Author: MetaSPN
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/MetaSPN/metaspn-store
8
+ Project-URL: Repository, https://github.com/MetaSPN/metaspn-store
9
+ Project-URL: Issues, https://github.com/MetaSPN/metaspn-store/issues
10
+ Keywords: metaspn,event-store,replay,append-only,jsonl
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3 :: Only
15
+ Classifier: Operating System :: OS Independent
16
+ Requires-Python: >=3.10
17
+ Description-Content-Type: text/markdown
18
+ License-File: LICENSE
19
+ Requires-Dist: metaspn-schemas>=0.1.0
20
+ Provides-Extra: dev
21
+ Requires-Dist: pytest>=8; extra == "dev"
22
+ Requires-Dist: build>=1.2.0; extra == "dev"
23
+ Requires-Dist: twine>=5.0.0; extra == "dev"
24
+ Requires-Dist: wheel>=0.43.0; extra == "dev"
25
+ Dynamic: license-file
26
+
27
+ # metaspn-store
28
+
29
+ `metaspn-store` provides a minimal append-only event store for MetaSPN signals and emissions.
30
+
31
+ ## Features
32
+ - Filesystem JSONL backend (partitioned by UTC date)
33
+ - Append-only writes for signals and emissions
34
+ - Snapshot writes for deterministic state rebuild checkpoints
35
+ - Streaming replay by time window, entity reference, and source
36
+
37
+ ## Layout
38
+ ```text
39
+ workspace/
40
+ store/
41
+ signals/
42
+ 2026-02-05.jsonl
43
+ emissions/
44
+ 2026-02-05.jsonl
45
+ snapshots/
46
+ system_state__2026-02-05T120000Z.json
47
+ ```
48
+
49
+ ## Release
50
+ ```bash
51
+ python -m pip install -e ".[dev]"
52
+ pytest -q
53
+ python -m build
54
+ python -m twine check dist/*
55
+ python -m twine upload dist/*
56
+ ```
@@ -0,0 +1,7 @@
1
+ metaspn_store/__init__.py,sha256=tSRu9VXvRBaw4OzBS0aKqHQiNPAtk-zbdJVl5vjPIKc,79
2
+ metaspn_store/store.py,sha256=bWmGjLTuAYN2DZCo68opb9tfFnbgRd5ZyMh7RM_YO04,6285
3
+ metaspn_store-0.1.0.dist-info/licenses/LICENSE,sha256=a9HFx7YHIoXQV8KLxqBH1uEJ6Ok6-yGkFu0_4fi2Zu0,1064
4
+ metaspn_store-0.1.0.dist-info/METADATA,sha256=c0FsJ-N3dotjJKEyeWn0GsY6vah7BKcDkJvQdpyYzGk,1695
5
+ metaspn_store-0.1.0.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
6
+ metaspn_store-0.1.0.dist-info/top_level.txt,sha256=gPVQAufLeLBcsT2XvIbX1dGZf5ocXX2hQQrf9erF9Kc,14
7
+ metaspn_store-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.10.2)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 MetaSPN
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ metaspn_store