GitHub

ConnectionWrites

API reference for ConnectionWrites.

A connection's writes, kept out of its own change streams.

Functions

__init__

__init__() -> None
def __init__(self) -> None:
    self._streams: dict[tuple[str, str], set[int]] = {}
    self._states: dict[int, _ConnectionStream] = {}

track

track[E, R](*, public_key, store_id, entry_ids, write) -> Effect[tuple[WriteAck, ...], E, R]

Run write, so the connection's streams skip what it stores.

ParameterTypeDescription
public_keystr
store_idStoreId
entry_idsSequence[EntryId]
writeEffect[tuple[WriteAck, ...], E, R]

Returns Effect[tuple[WriteAck, ...], E, R]

def track[E, R](
    self,
    *,
    public_key: str,
    store_id: StoreId,
    entry_ids: Sequence[EntryId],
    write: Effect[tuple[WriteAck, ...], E, R],
) -> Effect[tuple[WriteAck, ...], E, R]:
    """Run ``write``, so the connection's streams skip what it stores."""
    require_effect(write, "write")
    open_streams = self._streams.get((public_key, store_id))
    if not open_streams or not entry_ids:
        return write
    streams = [self._states[key] for key in open_streams]
    started = [stream.position for stream in streams]
    ids = [_uuid.stringify(entry_id) for entry_id in entry_ids]
    # Track before writing: storage can publish an entry before the
    # write returns. Each occurrence counts, as an id can be stored
    # more than once.
    for stream in streams:
        for id in ids:
            stream.ids[id] = stream.ids.get(id, 0) + 1

    def settle(exit: Exit[tuple[WriteAck, ...], E]) -> Effect[None]:
        def run() -> None:
            for stream, position in zip(streams, started, strict=True):
                if not isinstance(exit, Success):
                    for id in ids:
                        _release(stream.ids, id)
                    continue
                # Release occurrences the stream had passed when the write began.
                for ack in exit.value:
                    if ack.sequence <= position:
                        _release(stream.ids, _uuid.stringify(ack.entry_id))

        return sync(run)

    return write.on_exit(settle)

filter

filter[A, E, R](*, public_key, store_id, entry_ids, sequence, stream) -> Stream[A, E, R]

stream without the items this connection wrote.

ParameterTypeDescription
public_keystr
store_idStoreId
entry_idsCallable[[A], Sequence[EntryId]]
sequenceCallable[[A], int]
streamStream[A, E, R]

Returns Stream[A, E, R]

def filter[A, E, R](
    self,
    *,
    public_key: str,
    store_id: StoreId,
    entry_ids: Callable[[A], Sequence[EntryId]],
    sequence: Callable[[A], int],
    stream: Stream[A, E, R],
) -> Stream[A, E, R]:
    """``stream`` without the items this connection wrote."""
    require_stream(stream, "stream")

    def make() -> Stream[A, E, R]:
        key = (public_key, store_id)
        state = _ConnectionStream()
        self._states[id(state)] = state
        open_streams = self._streams.setdefault(key, set())
        open_streams.add(id(state))

        def keep(item: A) -> bool:
            position = sequence(item)
            state.position = max(state.position, position)
            if not state.ids:
                return True
            # An item is written by one request: all of it is ours or none.
            ids = [_uuid.stringify(entry_id) for entry_id in entry_ids(item)]
            if not all(id in state.ids for id in ids):
                return True
            for id in ids:
                _release(state.ids, id)
            return False

        def close() -> None:
            open_streams.discard(id(state))
            del self._states[id(state)]
            if not open_streams:
                self._streams.pop(key, None)

        return stream.filter(keep).ensuring(sync(close))

    return stream_suspend(make)