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 create_persistent_subscription( stub: persistent_pb2_grpc.PersistentSubscriptionsStub, stream: str, group_name: str, resolve_link_to_s: bool = False, from_revision: Union[int, str] = constants.START, extra_statistics: bool = False, message_timeout_ms: int = 30000, checkpoint_after_ms: int = 2000, max_retry_count: int = 10, min_checkpoint_count: int = 10, max_checkpoint_count: int = 1000, max_subscriber_count: Union[str, int] = 0, live_buffer_size: int = 20, history_buffer_size: int = 500, strategy: str = "ROUND_ROBIN", **kwargs) -> persistent_pb2.CreateResp: """Creates a persistent subscription.""" request = persistent_pb2.CreateReq() options = persistent_pb2.CreateReq.Options() identifier = shared_pb2.StreamIdentifier() request_settings = persistent_pb2.CreateReq.Settings() request_settings.resolve_links = resolve_link_to_s if isinstance(from_revision, int): request_settings.revision = from_revision elif from_revision == constants.START: request_settings.revision = 0 request_settings.extra_statistics = extra_statistics request_settings.message_timeout_ms = message_timeout_ms request_settings.checkpoint_after_ms = checkpoint_after_ms request_settings.max_retry_count = max_retry_count request_settings.min_checkpoint_count = min_checkpoint_count request_settings.max_checkpoint_count = max_checkpoint_count if isinstance(max_subscriber_count, int): request_settings.max_subscriber_count = max_subscriber_count elif max_checkpoint_count == "UNLIMITED": request_settings.max_subscriber_count = 0 request_settings.live_buffer_size = live_buffer_size request_settings.history_buffer_size = history_buffer_size if strategy == "DISPATCH_TO_SINGLE": request_settings.named_consumer_strategy = ( persistent_pb2.CreateReq.DispatchToSingle) elif strategy == "PINNED": request_settings.named_consumer_strategy = persistent_pb2.CreateReq.Pinned elif strategy == "ROUND_ROBIN": request_settings.named_consumer_strategy = persistent_pb2.CreateReq.RoundRobin identifier.streamName = stream.encode() options.group_name = group_name options.stream_identifier.CopyFrom(identifier) options.settings.CopyFrom(request_settings) request.options.CopyFrom(options) response = stub.Create(request, **kwargs) return response
def delete_persistent_subscription( stub: persistent_pb2_grpc.PersistentSubscriptionsStub, stream: str, group: str ) -> persistent_pb2.DeleteResp: """Deletes a persistent subscription.""" request = persistent_pb2.DeleteReq() options = persistent_pb2.DeleteReq.Options() identifier = shared_pb2.StreamIdentifier() identifier.streamName = stream.encode() options.stream_identifier.CopyFrom(identifier) options.group_name = group request.options.CopyFrom(options) response = stub.Delete(request) return response
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 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
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
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