1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//! Rust client library for [HStreamDB](https://hstream.io/)
//! ## Write Data to Streams
//!
//! ```
//! use std::env;
//!
//! use hstreamdb::client::Client;
//! use hstreamdb::producer::FlushSettings;
//! use hstreamdb::{CompressionType, Payload, Record, Stream};
//! use rand::distributions::Alphanumeric;
//! use rand::{thread_rng, Rng};
//!
//! async fn produce_example() -> anyhow::Result<()> {
//! let mut client = Client::new(env::var("TEST_SERVER_ADDR")?).await?;
//!
//! let stream_name = "test_stream";
//!
//! client
//! .create_stream(Stream {
//! stream_name: "test_stream".to_string(),
//! replication_factor: 3,
//! backlog_duration: 7 * 24 * 3600,
//! shard_count: 12,
//! })
//! .await?;
//! println!("{:?}", client.list_streams().await?);
//!
//! // `Appender` is cheap to clone
//! let (appender, mut producer) = client
//! .new_producer(
//! stream_name.to_string(),
//! hstreamdb_pb::CompressionType::Zstd,
//! FlushSettings {
//! len: 10,
//! size: 4000 * 20,
//! },
//! )
//! .await?;
//!
//! _ = tokio::spawn(async move {
//! let mut appender = appender;
//!
//! for _ in 0..10 {
//! for _ in 0..100 {
//! let i: u32 = rand::random();
//! let payload: Vec<u8> = thread_rng()
//! .sample_iter(&Alphanumeric)
//! .take(20)
//! .map(char::from)
//! .collect::<String>()
//! .into_bytes();
//! appender
//! .append(Record {
//! partition_key: format!("test_partition_key_{i}"),
//! payload: Payload::RawRecord(payload),
//! })
//! .unwrap();
//! }
//! }
//! drop(appender)
//! });
//!
//! // when all `Appender`s for the corresponding `Producer` have been dropped,
//! // the `Producer` will wait for all requests to be done and then stop
//! producer.start().await;
//!
//! Ok(())
//! }
//! ```
//!
//! ## Read Data from Subscriptions
//!
//! ```
//! use std::env;
//!
//! use hstreamdb::client::Client;
//! use hstreamdb::{SpecialOffset, Subscription};
//! use tokio_stream::StreamExt;
//!
//! async fn consume_example() -> anyhow::Result<()> {
//! let addr = env::var("TEST_SERVER_ADDR").unwrap();
//! let mut client = Client::new(addr).await.unwrap();
//!
//! let stream_name = "test_stream";
//! let subscription_id = "test_subscription";
//!
//! client
//! .create_subscription(Subscription {
//! subscription_id: subscription_id.to_string(),
//! stream_name: stream_name.to_string(),
//! ack_timeout_seconds: 60 * 60,
//! max_unacked_records: 1000,
//! offset: SpecialOffset::Earliest,
//! })
//! .await?;
//! println!("{:?}", client.list_subscriptions().await?);
//!
//! let mut stream = client
//! .streaming_fetch("test_consumer".to_string(), subscription_id.to_string())
//! .await
//! .unwrap();
//! let mut records = Vec::new();
//! while let Some((record, ack)) = stream.next().await {
//! println!("{record:?}");
//! records.push(record);
//! ack().unwrap();
//! if records.len() == 10 * 100 {
//! println!("done");
//! break;
//! }
//! }
//!
//! client
//! .delete_subscription(subscription_id.to_string(), true)
//! .await?;
//!
//! Ok(())
//! }
//! ```
pub use ;