use couch_rs::document::DocumentCollection;
use serde_json::Value;
use std::{fs::File, io::prelude::*, time::SystemTime};
use tokio::sync::{
mpsc,
mpsc::{Receiver, Sender},
};
const TEST_DB: &str = "test_db";
#[tokio::main]
async fn main() {
println!("Connecting...");
let now = SystemTime::now();
let (tx, mut rx): (Sender<DocumentCollection<Value>>, Receiver<DocumentCollection<Value>>) = mpsc::channel(100);
let t = tokio::spawn(async move {
let client = couch_rs::Client::new_local_test().unwrap();
let db = client.db(TEST_DB).await.unwrap();
if let Err(err) = db.get_all_batched(tx, 0, 0).await {
println!("error during batch read: {err:?}");
}
});
let mut file = File::create("test_db.json").unwrap();
while let Some(all_docs) = rx.recv().await {
println!("Received {} docs", all_docs.total_rows);
for row in all_docs.rows {
file.write_all(serde_json::to_string(&row).unwrap().as_bytes()).unwrap();
}
}
file.sync_all().unwrap();
let elapsed = now.elapsed().unwrap_or_default();
println!("{} ms", elapsed.as_millis());
t.await.unwrap();
}