def get_stream_subscription_request(
    stream: str,
    from_revision: Union[str, int] = constants.START,
    resolve_link_to_s: bool = False,
) -> streams_pb2.ReadReq:
    """Returns a streams_pb2.ReadReq configured for subscription operations for a generic stream."""
    request = streams_pb2.ReadReq()
    options = streams_pb2.ReadReq.Options()
    identifier = shared_pb2.StreamIdentifier()
    identifier.streamName = stream.encode()
    uuid_option = streams_pb2.ReadReq.Options.UUIDOption()
    uuid_option.string.CopyFrom(shared_pb2.Empty())
    stream_options = streams_pb2.ReadReq.Options.StreamOptions()
    stream_options.stream_identifier.CopyFrom(identifier)
    if isinstance(from_revision, int):
        stream_options.revision = from_revision
    elif from_revision == constants.START:
        stream_options.start.CopyFrom(shared_pb2.Empty())
    elif from_revision == constants.END:
        stream_options.end.CopyFrom(shared_pb2.Empty())
    options.stream.CopyFrom(stream_options)
    options.resolve_links = resolve_link_to_s
    options.subscription.CopyFrom(
        streams_pb2.ReadReq.Options.SubscriptionOptions())
    options.uuid_option.CopyFrom(uuid_option)
    options.no_filter.CopyFrom(shared_pb2.Empty())
    request.options.CopyFrom(options)
    return request
def get_all_subscription_request(
    from_position: Union[Dict[str, int], str] = constants.START,
    resolve_link_to_s: bool = False,
    filters: Dict = None,
) -> streams_pb2.ReadReq:
    """Returns a streams_pb2.ReadReq configured for subscription operations for the "$all" stream."""
    request = streams_pb2.ReadReq()
    options = streams_pb2.ReadReq.Options()
    uuid_option = streams_pb2.ReadReq.Options.UUIDOption()
    uuid_option.string.CopyFrom(shared_pb2.Empty())
    all_options = streams_pb2.ReadReq.Options.AllOptions()
    if isinstance(from_position, dict):
        grpc_pos = streams_pb2.ReadReq.Options.Position()
        grpc_pos.commit_position = from_position["commit_position"]
        grpc_pos.prepare_position = from_position["prepare_position"]
        all_options.position.CopyFrom(grpc_pos)
    elif from_position == constants.START:
        all_options.start.CopyFrom(shared_pb2.Empty())
    elif from_position == constants.END:
        all_options.end.CopyFrom(shared_pb2.Empty())
    options.all.CopyFrom(all_options)
    options.resolve_links = resolve_link_to_s
    options.subscription.CopyFrom(
        streams_pb2.ReadReq.Options.SubscriptionOptions())
    options.uuid_option.CopyFrom(uuid_option)
    if filters is not None:
        expr = streams_pb2.ReadReq.Options.FilterOptions.Expression()
        if "prefixes" in filters:
            for elm in filters["prefixes"]:
                expr.prefix.append(elm)
        if "regex" in filters:
            expr.regex = filters["regex"]
        filter_options = streams_pb2.ReadReq.Options.FilterOptions()
        filter_on = filters.get("filter_on")
        if filter_on == "STREAM_NAME":
            filter_options.stream_identifier.CopyFrom(expr)
        elif filter_on == "EVENT_TYPE":
            filter_options.event_type.CopyFrom(expr)
        max_search_window = filters.get("max_search_window")
        if max_search_window is not None:
            if max_search_window <= 0:
                raise ValueError("CheckpointInterval must be greater than 0.")
            filter_options.max = max_search_window
        else:
            filter_options.count.CopyFrom(shared_pb2.Empty())
        checkpoint_interval = filters.get("checkpoint_interval")
        if checkpoint_interval is None or checkpoint_interval <= 0:
            raise ValueError("CheckpointInterval must be greater than 0.")
        filter_options.checkpointIntervalMultiplier = checkpoint_interval
        options.filter.CopyFrom(filter_options)
    else:
        options.no_filter.CopyFrom(shared_pb2.Empty())
    request.options.CopyFrom(options)
    return request
def build_options(
    stream: str,
    expected_version: Union[str, int] = None,
) -> streams_pb2.AppendReq.Options:
    """Builds AppendReq Options."""
    options = streams_pb2.AppendReq.Options()
    stream_identifier = shared_pb2.StreamIdentifier()
    stream_identifier.streamName = stream.encode()
    if isinstance(expected_version, int):
        options.revision = expected_version
    elif expected_version == constants.NO_STREAM:
        options.no_stream.CopyFrom(shared_pb2.Empty())
    elif expected_version == constants.ANY:
        options.any.CopyFrom(shared_pb2.Empty())
    elif expected_version == constants.STREAM_EXISTS:
        options.stream_exists.CopyFrom(shared_pb2.Empty())
    options.stream_identifier.CopyFrom(stream_identifier)
    return options
Exemple #4
0
def list_continuous_projections(
        stub: projections_pb2_grpc.ProjectionsStub,
        **kwargs) -> Iterable[projections_pb2.StatisticsResp]:
    """Lists continuous projections."""
    request = projections_pb2.StatisticsReq()
    options = projections_pb2.StatisticsReq.Options()
    options.continuous.CopyFrom(shared_pb2.Empty())
    request.options.CopyFrom(options)
    response = stub.Statistics(request, **kwargs)
    return response
Exemple #5
0
def tombstone_stream(
    stub: streams_pb2_grpc.StreamsStub,
    stream: str,
    expected_version: Union[int, str],
    **kwargs,
):
    """Tombstone."""
    request = streams_pb2.TombstoneReq()
    options = streams_pb2.TombstoneReq.Options()
    expected_stream_revision = None
    if expected_version == constants.ANY:
        options.any.CopyFrom(shared_pb2.Empty())
    elif expected_version == constants.NO_STREAM:
        options.no_stream.CopyFrom(shared_pb2.Empty())
    elif expected_version == constants.STREAM_EXISTS:
        options.stream_exists.CopyFrom(shared_pb2.Empty())
    request.options.CopyFrom(options)
    response = stub.Tombstone(request, **kwargs)
    return response
def read_from_all(
    stub: streams_pb2_grpc.StreamsStub,
    from_position: Union[Dict[str, int], str] = constants.START,
    count: int = None,
    direction: str = None,
    **kwargs,
):
    request = streams_pb2.ReadReq()
    options = streams_pb2.ReadReq.Options()
    uuid_option = streams_pb2.ReadReq.Options.UUIDOption()
    uuid_option.string.CopyFrom(shared_pb2.Empty())
    all_options = streams_pb2.ReadReq.Options.AllOptions()
    if isinstance(from_position,
                  str):  # TODO: consider a better way of handling this.
        if from_position.upper() == constants.START:
            all_options.start.CopyFrom(shared_pb2.Empty())
        elif from_position.upper() == constants.END:
            all_options.end.CopyFrom(shared_pb2.Empty())
    elif isinstance(from_position, dict):
        pos = streams_pb2.ReadReq.Options.Position()
        pos.commit_position = from_position["commit_position"]
        pos.prepare_position = from_position["prepare_position"]
        all_options.position.CopyFrom(pos)
    else:
        raise ValueError(f"Invalid 'from_position': {from_position}")
    options.count = count or sys.maxsize
    options.all.CopyFrom(all_options)
    options.uuid_option.CopyFrom(uuid_option)
    options.no_filter.CopyFrom(shared_pb2.Empty())
    default_direction = ("backwards" if isinstance(from_position, str)
                         and from_position.upper() == constants.END else
                         "forwards")
    direction = direction or default_direction
    if direction == "forwards":
        options.read_direction = streams_pb2.ReadReq.Options.ReadDirection.Forwards
    elif direction == "backwards":
        options.read_direction = streams_pb2.ReadReq.Options.ReadDirection.Backwards
    else:
        raise ValueError(f"Invalid direction: {direction}")
    request.options.CopyFrom(options)
    response = stub.Read(request, **kwargs)
    return response
def delete_stream(
    stub: streams_pb2_grpc.StreamsStub,
    stream: str,
    expected_version: Union[str, int],
    **kwargs
) -> streams_pb2.DeleteResp:
    """Deletes a stream."""
    request = streams_pb2.DeleteReq()
    options = streams_pb2.DeleteReq.Options()
    stream_identifier = shared_pb2.StreamIdentifier()
    stream_identifier.streamName = stream.encode()
    if expected_version == constants.NO_STREAM:
        options.no_stream.CopyFrom(shared_pb2.Empty())
    elif expected_version == constants.ANY:
        options.any.CopyFrom(shared_pb2.Empty())
    elif expected_version == constants.STREAM_EXISTS:
        options.stream_exists.CopyFrom(shared_pb2.Empty())
    elif isinstance(int, expected_version):
        options.stream_exists = expected_version
    options.stream_identifier.CopyFrom(stream_identifier)
    request.options.CopyFrom(options)
    response = stub.Delete(request, **kwargs)
    return response
Exemple #8
0
def create_one_time_projection(
    stub: projections_pb2_grpc.ProjectionsStub,
    query: str,
    track_emitted_streams: bool = False,
    **kwargs,
) -> projections_pb2.CreateResp:
    """Creates a one time projection."""
    request = projections_pb2.CreateReq()
    options = projections_pb2.CreateReq.Options()
    options.query = query
    options.one_time.CopyFrom(shared_pb2.Empty())
    request.options.CopyFrom(options)
    response = stub.Create(request, **kwargs)
    return response
Exemple #9
0
def options_request(stream: str,
                    group_name: str,
                    buffer_size: int = 10) -> persistent_pb2.ReadReq:
    """Returns a persistent subscription options request."""
    request = persistent_pb2.ReadReq()
    options = persistent_pb2.ReadReq.Options()
    identifier = shared_pb2.StreamIdentifier()
    identifier.streamName = stream.encode()
    uuid_option = persistent_pb2.ReadReq.Options.UUIDOption()
    uuid_option.string.CopyFrom(shared_pb2.Empty())
    options.stream_identifier.CopyFrom(identifier)
    options.group_name = group_name
    options.buffer_size = buffer_size
    options.uuid_option.CopyFrom(uuid_option)
    request.options.CopyFrom(options)
    return request
def update_projection(
    stub: projections_pb2_grpc.ProjectionsStub,
    name: str,
    query: str,
    track_emitted_streams: Optional[bool] = None,
    **kwargs,
) -> projections_pb2.UpdateResp:
    """Updates a projection."""
    request = projections_pb2.UpdateReq()
    options = projections_pb2.UpdateReq.Options()
    options.name = name
    options.query = query
    if track_emitted_streams is None:
        options.no_emit_options.CopyFrom(shared_pb2.Empty())
    else:
        options.emit_enabled = track_emitted_streams
    request.options.CopyFrom(options)
    response = stub.Update(request, **kwargs)
    return response
def read_from_stream(
    stub: streams_pb2_grpc.StreamsStub,
    stream: str,
    count: int,
    options: Dict,  # TODO: use from_revision as a parameter.
    **kwargs,
) -> Iterator[streams_pb2.ReadResp]:
    """Reads events from an Event Stream.

    The simplest way to read a stream forwards is to supply a stream name, direction
    and revision to start from. This can either be a stream position or an unsigned
    64 big integer. This will return an iterable yielding events from the stream.

    There are a number of additional arguments you can provide when reading a stream:

    * `max_count`: passing in the max count allows you to limit the number of events
        that returned.
    * `resolve_link_to_pos`: when using projections to create new events you can set
        whether the generated events are pointers to existing events. Setting this
        value to true will tell EventStoreDB to returne the event as well as the event
        linking to it.
    * `configure_operation_options`: this argument is generic setting class for all
        operations that can be set on all operations executed against EventStoreDB. (??)
    * `user_credentials`: the credentials used to read the data can be supplied. To be
        used by the subscription as follows. This will override the default
        credentials set on the connection.

    ### Reading from a revision.
    As well as providing a `StreamPosition` you can also provide a stream revision
    in the form of an unsigned 64 big integer.

    ### Reading backwards.
    As well as being able to read a stream forwards, you can also go backwards. When
    reading backwards is the stream position will have to be set to the end if you
    want to read all the events.

    > Tip: You can use reading backwards to find the last position in the stream. Just
    > read backwards one event and get the position.

    ### Checking for stream presence.
    Reading a stream returns a ReadStreamResult containing a ReadState. This property
    can have the value StreamNotFound and Ok. It is important to check the value of
    this field before attempting to iterate an empty stream as it will throw an
    exception.
    """
    request = streams_pb2.ReadReq()
    req_options = streams_pb2.ReadReq.Options()
    identifier = shared_pb2.StreamIdentifier()
    identifier.streamName = stream.encode()
    uuid_option = streams_pb2.ReadReq.Options.UUIDOption()
    uuid_option.string.CopyFrom(shared_pb2.Empty())
    stream_options = streams_pb2.ReadReq.Options.StreamOptions()
    stream_options.stream_identifier.CopyFrom(identifier)
    from_revision = options.get("from_revision")
    if from_revision == constants.START:
        stream_options.start.CopyFrom(shared_pb2.Empty())
    elif from_revision == constants.END:
        stream_options.end.CopyFrom(shared_pb2.Empty())
    elif isinstance(from_revision, int):
        stream_options.revision = from_revision
    req_options.stream.CopyFrom(stream_options)
    req_options.uuid_option.CopyFrom(uuid_option)
    resolve_links = options.get("resolve_link_to_s", False)
    req_options.resolve_links = resolve_links
    req_options.count = count or sys.maxsize
    req_options.no_filter.CopyFrom(shared_pb2.Empty())
    default_direction = "backwards" if from_revision == constants.END else "forwards"
    direction = options.get("direction", default_direction)
    if direction == "forwards":
        req_options.read_direction = streams_pb2.ReadReq.Options.ReadDirection.Forwards
    elif direction == "backwards":
        req_options.read_direction = streams_pb2.ReadReq.Options.ReadDirection.Backwards
    request.options.CopyFrom(req_options)
    response = stub.Read(request, **kwargs)
    return response
Exemple #12
0
def shutdown(stub: operations_pb2_grpc.OperationsStub,
             **kwargs) -> shared_pb2.Empty:
    """Shuts down EvenStoreDB."""
    return stub.Shutdown(shared_pb2.Empty(), **kwargs)
def restart_projections_subsystem(stub: projections_pb2_grpc.ProjectionsStub,
                                  **kwargs) -> shared_pb2.Empty:
    """Restart projections subsystem."""
    return stub.RestartSubsystem(shared_pb2.Empty(), **kwargs)
def read(stub: gossip_pb2_grpc.GossipStub, **kwargs) -> gossip_pb2.ClusterInfo:
    """Reads Gossip."""
    return stub.Read(shared_pb2.Empty())
Exemple #15
0
def set_node_priority(stub: operations_pb2_grpc.OperationsStub, priority: int, **kwargs) -> shared_pb2.Empty():
    """Sets node priority."""
    request = operations_pb2.SetNodePriorityReq()
    request.priority = priority
    response = stub.SetNodePriority(request, **kwargs)
    return response
def restart_persistent_subscriptions(stub: operations_pb2_grpc.OperationsStub, **kwargs) -> shared_pb2.Empty:
    """Restart persistent subscriptions."""
    return stub.RestartPersistentSubscriptions(shared_pb2.Empty(), **kwargs)
Exemple #17
0
def resign_node(stub: operations_pb2_grpc.OperationsStub,
                **kwargs) -> shared_pb2.Empty:
    """Resign Node."""
    return stub.ResignNode(shared_pb2.Empty(), **kwargs)