#![allow(clippy::unwrap_used, clippy::expect_used)]
use chronix::prelude::*;
use chronix::{Chronix, fields, tags};
use std::sync::Arc;
const NS_TAG: &str = "__namespace__";
fn open(dir: &tempfile::TempDir) -> Arc<Chronix> {
let config = ChronixConfig::builder()
.data_dir(dir.path())
.build()
.unwrap();
Arc::new(Chronix::open(config).unwrap())
}
fn seed(db: &Arc<Chronix>) {
for i in 0..10i64 {
let key = SeriesKey::new(
"m",
tags! { "host" => format!("h{}", i % 2).as_str(), NS_TAG => "default" },
)
.unwrap();
db.insert(&Point::new(key, fields! { "usage" => i as f64 }, 1_000 + i).unwrap())
.unwrap();
}
}
fn count(db: &Arc<Chronix>, plan: &chronix_query::plan::QueryPlan) -> usize {
db.execute_iter(plan)
.unwrap()
.map(|b| b.unwrap().num_rows())
.sum()
}
#[test]
fn a_namespaced_scan_sees_flushed_data() {
let dir = tempfile::tempdir().unwrap();
let db = open(&dir);
seed(&db);
let plan = db
.query()
.measurement("m")
.range(0, i64::MAX)
.namespace("default")
.build()
.unwrap();
assert_eq!(count(&db, &plan), 10, "memtable scan sees every row");
db.flush().unwrap();
let via_execute = db.execute(&plan).unwrap().num_rows();
let via_iter = count(&db, &plan);
assert_eq!(
(via_execute, via_iter),
(10, 10),
"the same query must see the same rows once they are in a segment"
);
}
#[test]
fn a_namespaced_scan_with_a_tag_filter_sees_flushed_data() {
let dir = tempfile::tempdir().unwrap();
let db = open(&dir);
seed(&db);
db.flush().unwrap();
let plan = db
.query()
.measurement("m")
.range(0, i64::MAX)
.namespace("default")
.tag("host", "h0")
.build()
.unwrap();
assert_eq!(count(&db, &plan), 5);
}
#[test]
fn a_partial_tag_filter_still_matches() {
let dir = tempfile::tempdir().unwrap();
let db = open(&dir);
for i in 0..10i64 {
let key = SeriesKey::new(
"m",
tags! { "host" => format!("h{}", i % 2).as_str(), "region" => "eu" },
)
.unwrap();
db.insert(&Point::new(key, fields! { "usage" => i as f64 }, 1_000 + i).unwrap())
.unwrap();
}
db.flush().unwrap();
let plan = db
.query()
.measurement("m")
.range(0, i64::MAX)
.tag("region", "eu")
.build()
.unwrap();
assert_eq!(
db.execute(&plan).unwrap().num_rows(),
10,
"filtering on one of two tags must match every row that carries it"
);
let plan = db
.query()
.measurement("m")
.range(0, i64::MAX)
.tag("host", "h0")
.build()
.unwrap();
assert_eq!(
db.execute(&plan).unwrap().num_rows(),
5,
"and the other partial filter must match its half"
);
let plan = db
.query()
.measurement("m")
.range(0, i64::MAX)
.tag("host", "h0")
.tag("region", "eu")
.build()
.unwrap();
assert_eq!(db.execute(&plan).unwrap().num_rows(), 5);
}