aerospike / aerospike-client-rust

Rust client for the Aerospike database
https://www.aerospike.com/
Other
82 stars 29 forks source link

Support for other batch operations? (write/exists/...) #52

Closed Xaelias closed 6 years ago

Xaelias commented 6 years ago

Hi!

I noticed that last year, you added support for batch-read. For the small program I'm writing, I would have loved to have batch-exists. I was just wondering if this was an easy fix for you guys, or if I need to use another language than Rust for this project.

Thanks! Alexis

jhecking commented 6 years ago

You can call Client::batch_get with Bins::None to just check whether a record exists with fetching any of the bins:

#[macro_use]
extern crate aerospike;

use std::env;

use aerospike::{BatchPolicy, BatchRead, Bins, Client, ClientPolicy, WritePolicy};

fn main() {
    let hosts = env::var("AEROSPIKE_HOSTS").unwrap_or(String::from("127.0.0.1:3000"));
    let client =
        Client::new(&ClientPolicy::default(), &hosts).expect("Failed to connect to cluster");

    let key = as_key!("test", "demo", 4242);
    let bins = [as_bin!("i", 42)];
    client
        .put(&WritePolicy::default(), &key, &bins)
        .expect("Error writing record");

    let batch = vec![
        BatchRead::new(as_key!("test", "demo", 4242), &Bins::None),
        BatchRead::new(as_key!("test", "demo", 2424), &Bins::None),
    ];
    let results = client
        .batch_get(&BatchPolicy::default(), batch)
        .expect("Error executing batch request");

    for result in results {
        println!("{} exists: {}", result.key, result.record.is_some());
    }
}
$ cargo run --example batch_exists
    Finished dev [unoptimized + debuginfo] target(s) in 0.10s
     Running `target/debug/examples/batch_exists`
<Key: ns="test", set="demo", key="4242"> exists: true
<Key: ns="test", set="demo", key="2424"> exists: false

As for batch writes (as per the issue title), the Aerospike server currently does not support this functionality.

Xaelias commented 6 years ago

You're right I'm sorry. I don't know how I got into my head that batch writes were a thing (although that would be useful I guess). Thanks for the code example. That was my second option indeed. I'm going to play with it :-) Thanks!

PS: I'll close the issue to clean things up.