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
#![allow(clippy::unwrap_used, clippy::expect_used)] // examples favour brevity
//! # Quickstart
//!
//! Open a database, write a point, read it back — with the builder and with
//! SQL. This is the example on the front page of the docs; CI compiles and
//! runs it on every change.
//!
//! ```sh
//! cargo run -p chronix --example quickstart
//! ```
use chronix::prelude::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let dir = tempfile::tempdir()?;
// A data directory is the whole deployment. It is file-locked, so a
// second process cannot open it by accident. `open_small` is the
// gateway preset; `Chronix::open(ChronixConfig::builder()…)` is the
// general form.
let db = Chronix::open_small(dir.path())?;
// A point is a series key (measurement + tags), some fields, and a
// timestamp in **nanoseconds**.
let key = SeriesKey::new("cpu", tags! { "host" => "web-01" })?;
let point = Point::new(
key,
fields! { "usage_idle" => 95.5 },
1_700_000_000_000_000_000,
)?;
db.insert(&point)?;
// Query with the builder — an Arrow RecordBatch back.
let plan = db.query().measurement("cpu").build()?;
let batch = db.execute(&plan)?;
println!("wrote 1 point to `cpu`, read it back with the builder:\n");
println!("{}", arrow::util::pretty::pretty_format_batches(&[batch])?);
// …or with SQL. Every measurement is a table, `_time` is the timestamp.
let batches = db.sql("SELECT _time, host, usage_idle FROM cpu")?;
println!("\nand with SQL:\n");
println!("{}", arrow::util::pretty::pretty_format_batches(&batches)?);
// `close()` flushes and records the WAL floor, so the next open replays
// nothing. Dropping the last handle does the same.
db.close()?;
Ok(())
}