|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from zarr.core.buffer import default_buffer_prototype |
| 4 | +from zarr.storage import MemoryStore |
| 5 | + |
| 6 | +PROTOTYPE = default_buffer_prototype() |
| 7 | + |
| 8 | + |
| 9 | +class IcechunkModel(MemoryStore): |
| 10 | + """MemoryStore with move, copy, and lifecycle methods for testing. |
| 11 | +
|
| 12 | + Tracks all_arrays/all_groups alongside store data so that copy() |
| 13 | + preserves them. |
| 14 | + """ |
| 15 | + |
| 16 | + spec_version: int |
| 17 | + |
| 18 | + def __init__(self) -> None: |
| 19 | + super().__init__() |
| 20 | + self.all_arrays: set[str] = set() |
| 21 | + self.all_groups: set[str] = set() |
| 22 | + self.baseline: frozenset[str] = frozenset() |
| 23 | + self.claims: set[str] = set() |
| 24 | + |
| 25 | + @classmethod |
| 26 | + def from_store(cls, store: MemoryStore) -> IcechunkModel: |
| 27 | + """Promote a plain MemoryStore to an IcechunkModel.""" |
| 28 | + model = cls() |
| 29 | + model._store_dict = store._store_dict |
| 30 | + return model |
| 31 | + |
| 32 | + def _claim_node(self, key: str) -> None: |
| 33 | + """Record which array or group a key belongs to.""" |
| 34 | + for array in self.all_arrays: |
| 35 | + if key.startswith(array + "/"): |
| 36 | + self.claims.add(array) |
| 37 | + return |
| 38 | + for group in self.all_groups: |
| 39 | + if key.startswith(group + "/"): |
| 40 | + self.claims.add(group) |
| 41 | + return |
| 42 | + |
| 43 | + async def set( |
| 44 | + self, key: str, value: object, byte_range: tuple[int, int] | None = None |
| 45 | + ) -> None: |
| 46 | + self._claim_node(key) |
| 47 | + await super().set(key, value, byte_range) |
| 48 | + |
| 49 | + async def delete(self, key: str) -> None: |
| 50 | + self._claim_node(key) |
| 51 | + await super().delete(key) |
| 52 | + |
| 53 | + async def move(self, source: str, dest: str) -> None: |
| 54 | + """Move all keys from source to dest. |
| 55 | +
|
| 56 | + Store keys always have form "node/zarr.json" or "node/c/...", never bare "node". |
| 57 | + """ |
| 58 | + all_keys = [k async for k in self.list_prefix("")] |
| 59 | + keys_to_move = [k for k in all_keys if k.startswith(source + "/")] |
| 60 | + for old_key in keys_to_move: |
| 61 | + new_key = dest + old_key[len(source) :] |
| 62 | + data = await self.get(old_key, prototype=PROTOTYPE) |
| 63 | + if data is not None: |
| 64 | + await self.set(new_key, data) |
| 65 | + await self.delete(old_key) |
| 66 | + |
| 67 | + async def copy(self) -> IcechunkModel: |
| 68 | + """Create a copy of this store (data + arrays + groups).""" |
| 69 | + new_store = IcechunkModel() |
| 70 | + new_store.spec_version = self.spec_version |
| 71 | + new_store.all_arrays = self.all_arrays.copy() |
| 72 | + new_store.all_groups = self.all_groups.copy() |
| 73 | + async for key in self.list_prefix(""): |
| 74 | + data = await self.get(key, prototype=PROTOTYPE) |
| 75 | + if data is not None: |
| 76 | + await new_store.set(key, data) |
| 77 | + return new_store |
| 78 | + |
| 79 | + # things for tracking changes relative to committed baseline |
| 80 | + @property |
| 81 | + def nodes(self) -> frozenset[str]: |
| 82 | + """Current set of all node paths in this model.""" |
| 83 | + return frozenset(self.all_arrays) | frozenset(self.all_groups) |
| 84 | + |
| 85 | + def changes(self) -> set[str]: |
| 86 | + """Paths modified since last sync: structural diff + data claims.""" |
| 87 | + return set(self.claims) | (self.nodes ^ self.baseline) |
| 88 | + |
| 89 | + def sync_baseline(self, committed) -> None: |
| 90 | + """Record committed state as this actor's baseline.""" |
| 91 | + self.baseline = frozenset(committed.all_arrays) | frozenset(committed.all_groups) |
| 92 | + |
| 93 | + # ── Lifecycle methods ───────────────────────────────────────────── |
| 94 | + |
| 95 | + async def commit(self) -> IcechunkModel: |
| 96 | + """Snapshot this model as the new committed baseline. |
| 97 | +
|
| 98 | + Returns the committed copy; caller stores it as shared state. |
| 99 | + """ |
| 100 | + committed = await self.copy() |
| 101 | + self.claims.clear() |
| 102 | + self.sync_baseline(committed) |
| 103 | + return committed |
| 104 | + |
| 105 | + async def rebase(self, committed: IcechunkModel) -> IcechunkModel: |
| 106 | + """Merge committed baseline with local changes. |
| 107 | +
|
| 108 | + Returns a new model with the merge result; caller swaps it in. |
| 109 | + """ |
| 110 | + merged = await committed.copy() |
| 111 | + my_changes = self.changes() |
| 112 | + |
| 113 | + merged.all_arrays = (committed.all_arrays - my_changes) | ( |
| 114 | + my_changes & self.all_arrays |
| 115 | + ) |
| 116 | + merged.all_groups = (committed.all_groups - my_changes) | ( |
| 117 | + my_changes & self.all_groups |
| 118 | + ) |
| 119 | + |
| 120 | + for path in my_changes: |
| 121 | + keys_to_delete = [k async for k in merged.list_prefix(path + "/")] |
| 122 | + for key in keys_to_delete: |
| 123 | + await merged.delete(key) |
| 124 | + async for key in self.list_prefix(path + "/"): |
| 125 | + data = await self.get(key, prototype=PROTOTYPE) |
| 126 | + if data is not None: |
| 127 | + await merged.set(key, data) |
| 128 | + |
| 129 | + merged.sync_baseline(committed) |
| 130 | + return merged |
| 131 | + |
| 132 | + async def new_session(self, committed: IcechunkModel) -> IcechunkModel: |
| 133 | + """Reset to the committed baseline, discarding local changes.""" |
| 134 | + fresh = await committed.copy() |
| 135 | + fresh.claims.clear() |
| 136 | + fresh.sync_baseline(committed) |
| 137 | + return fresh |
| 138 | + |
| 139 | + async def rewrite_manifests(self, committed: IcechunkModel) -> IcechunkModel: |
| 140 | + """Reset to committed baseline (rewrite_manifests doesn't change data).""" |
| 141 | + fresh = await committed.copy() |
| 142 | + fresh.claims.clear() |
| 143 | + fresh.sync_baseline(committed) |
| 144 | + return fresh |
0 commit comments