Skip to content

Commit

Permalink
update rust examples
Browse files Browse the repository at this point in the history
  • Loading branch information
atimin committed Dec 3, 2024
1 parent c8b02d4 commit 3d67005
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 24 deletions.
2 changes: 1 addition & 1 deletion docs/examples/py/src/data_management_delete_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async def main():
assert removed_records == 2

# You can also delete all records with a specific label
await bucket.remove_query("py-example", ts, ts + 2, where={"&key1": {"$eq": "value1"}})
await bucket.remove_query("py-example", ts, ts + 2, when={"&key1": {"$eq": "value1"}})

# Or each N-th record
await bucket.remove_query("py-example", ts, ts + 2, each_n=2)
Expand Down
3 changes: 2 additions & 1 deletion docs/examples/rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ edition = "2021"

[dependencies]
reduct-rs= { git = "https://github.com/reductstore/reduct-rs.git"}
serde_json = "1.0.133"
tokio = { version = "1", features = ["rt-multi-thread"]}
bytes = "1.6.0"
futures = "0.3.30"
futures-util = "0.3.30"
futures-util = "0.3.30"
11 changes: 4 additions & 7 deletions docs/examples/rs/examples/data_management_delete_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::time::{Duration, SystemTime};

use bytes::Bytes;
use reduct_rs::{ErrorCode, RecordBuilder, ReductClient, ReductError};
use serde_json::json;
use tokio;

#[tokio::main]
Expand All @@ -24,7 +25,7 @@ async fn main() -> Result<(), ReductError> {

bucket
.write_record("rs-example")
.timestamp(timestamp + Duration::from_secs(1))
.timestamp(timestamp + Duration::from_secs(1))
.data(Bytes::from("Some more binary data"))
.send()
.await?;
Expand All @@ -41,15 +42,11 @@ async fn main() -> Result<(), ReductError> {
// You can also delete all records with a specific label
bucket
.remove_query("rs-example")
.add_include("label1", "value1")
.when(json!({"&key1": {"$eq": "value1"}}))
.send()
.await?;

// Or each N-th record
bucket
.remove_query("rs-example")
.each_n(2)
.send()
.await?;
bucket.remove_query("rs-example").each_n(2).send().await?;
Ok(())
}
8 changes: 5 additions & 3 deletions docs/examples/rs/examples/data_management_delete_records.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async fn main() -> Result<(), ReductError> {

bucket
.write_record("rs-example")
.timestamp(timestamp + Duration::from_secs(1))
.timestamp(timestamp + Duration::from_secs(1))
.data(Bytes::from("Some more binary data"))
.send()
.await?;
Expand All @@ -39,8 +39,10 @@ async fn main() -> Result<(), ReductError> {
// Delete a batch of records
let batch = bucket.remove_batch("rs-example");
let errors = batch
.add_timestamp(timestamp) // was already deleted, so this error will be in the errors map
.add_timestamp(timestamp + Duration::from_secs(1)).send().await?;
.add_timestamp(timestamp) // was already deleted, so this error will be in the errors map
.add_timestamp(timestamp + Duration::from_secs(1))
.send()
.await?;

assert_eq!(errors.len(), 1);
assert_eq!(errors.values().next().unwrap().status, ErrorCode::NotFound);
Expand Down
15 changes: 10 additions & 5 deletions docs/examples/rs/examples/data_management_update_labels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async fn main() -> Result<(), ReductError> {

bucket
.write_record("rs-example")
.timestamp(timestamp + Duration::from_secs(1))
.timestamp(timestamp + Duration::from_secs(1))
.add_label("key1", "value1")
.add_label("key2", "value2")
.data(Bytes::from("Some more binary data"))
Expand All @@ -41,24 +41,29 @@ async fn main() -> Result<(), ReductError> {
.update_label("key1", "value3")
.send()
.await?;
let record = bucket.read_record("rs-example").timestamp(timestamp).send().await?;
let record = bucket
.read_record("rs-example")
.timestamp(timestamp)
.send()
.await?;
assert_eq!(record.labels().get("key1"), Some(&"value3".to_string()));
assert_eq!(record.labels().get("key2"), None);

// Update labels in a batch
let record1 = RecordBuilder::new()
.timestamp(timestamp)
.add_label("key1", "value1")
.add_label("key2", "") // Remove label "key2"
.add_label("key2", "") // Remove label "key2"
.build();

let record2 = RecordBuilder::new()
.timestamp(timestamp + Duration::from_secs(1))
.add_label("key1", "value1")
.add_label("key2", "") // Remove label "key2"
.add_label("key2", "") // Remove label "key2"
.build();

let errors = bucket.update_batch("rs-example")
let errors = bucket
.update_batch("rs-example")
.add_records(vec![record1, record2])
.send()
.await?;
Expand Down
7 changes: 5 additions & 2 deletions docs/examples/rs/examples/data_querying_filter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use futures::StreamExt;
use reduct_rs::{ReductClient, ReductError};
use serde_json::json;
use tokio;

#[tokio::main]
Expand All @@ -18,8 +19,10 @@ async fn main() -> Result<(), ReductError> {
// Query 10 photos from "imdb" entry which taken in 2006 but don't contain "Rowan Atkinson"
let query = bucket
.query("imdb")
.add_include("photo_taken", "2006")
.add_exclude("name", "b'Rowan Atkinson'")
.when(json!({
"&photo_taken": {"$gt": 2006},
"&name": {"$ne": "b'Rowan Atkinson'"},
}))
.limit(10)
.send()
.await?;
Expand Down
13 changes: 8 additions & 5 deletions docs/examples/rs/examples/quick_start.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bytes::Bytes;
use futures_util::stream::StreamExt;
use reduct_rs::{QuotaType, ReductClient, ReductError};
use serde_json::json;
use std::pin::pin;
use std::time::{Duration, SystemTime};
use tokio;
Expand All @@ -22,27 +22,30 @@ async fn main() -> Result<(), ReductError> {
.send()
.await?;

// 3. Write some data with timestamps in the 'sensor-1' entry
// 3. Write some data with timestamps and labels to the 'entry-1' entry
let start = SystemTime::now();
bucket
.write_record("sensor-1")
.data("Record #1")
.data("<Blob data>")
.timestamp(start)
.add_label("score", 10)
.send()
.await?;

bucket
.write_record("sensor-1")
.data("Record #2")
.data("<Blob data>")
.timestamp(start + Duration::from_secs(1))
.add_label("score", 20)
.send()
.await?;

// 4. Query the data by time range
// 4. Query the data by time range and condition
let query = bucket
.query("sensor-1")
.start(start)
.stop(start + Duration::from_secs(2))
.when(json!({"&score": {"$gt": 15}}))
.send()
.await?;

Expand Down

0 comments on commit 3d67005

Please sign in to comment.