Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get stream no io #1306

Merged
merged 2 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .config/nats.dic
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,5 @@ update_consumer_on_stream
create_consumer_strict
create_consumer_strict_on_stream
leafnodes
get_stream
get_stream_no_info
47 changes: 46 additions & 1 deletion async-nats/src/jetstream/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,10 @@ impl Context {
/// # Ok(())
/// # }
/// ```
pub async fn create_stream<S>(&self, stream_config: S) -> Result<Stream, CreateStreamError>
pub async fn create_stream<S>(
&self,
stream_config: S,
) -> Result<Stream<Info>, CreateStreamError>
where
Config: From<S>,
{
Expand Down Expand Up @@ -307,10 +310,50 @@ impl Context {
Response::Ok(info) => Ok(Stream {
context: self.clone(),
info,
name: config.name,
}),
}
}

/// Checks for [Stream] existence on the server and returns handle to it.
/// That handle can be used to manage and use [Consumer].
/// This variant does not fetch [Stream] info from the server.
/// It means it does not check if the stream actually exists.
/// If you run more operations on few streams, it is better to use [Context::get_stream] instead.
/// If you however run single operations on many streams, this method is more efficient.
///
/// # Examples
///
/// ```no_run
/// # #[tokio::main]
/// # async fn main() -> Result<(), async_nats::Error> {
/// let client = async_nats::connect("localhost:4222").await?;
/// let jetstream = async_nats::jetstream::new(client);
///
/// let stream = jetstream.get_stream("events").await?;
/// # Ok(())
/// # }
/// ```
pub async fn get_stream_no_info<T: AsRef<str>>(
&self,
stream: T,
) -> Result<Stream<()>, GetStreamError> {
let stream = stream.as_ref();
if stream.is_empty() {
return Err(GetStreamError::new(GetStreamErrorKind::EmptyName));
}

if !is_valid_name(stream) {
return Err(GetStreamError::new(GetStreamErrorKind::InvalidStreamName));
}

Ok(Stream {
context: self.clone(),
info: (),
name: stream.to_string(),
})
}

/// Checks for [Stream] existence on the server and returns handle to it.
/// That handle can be used to manage and use [Consumer].
///
Expand Down Expand Up @@ -348,6 +391,7 @@ impl Context {
Response::Ok(info) => Ok(Stream {
context: self.clone(),
info,
name: stream.to_string(),
}),
}
}
Expand Down Expand Up @@ -404,6 +448,7 @@ impl Context {
Response::Ok(info) => Ok(Stream {
context: self.clone(),
info,
name: config.name,
}),
}
}
Expand Down
Loading
Loading