#[cfg(feature = "write-support")]
fn main() -> cqlite_core::error::Result<()> {
use cqlite_core::schema::{Column, KeyColumn, TableSchema};
use cqlite_core::storage::write_engine::{
CellOperation, Mutation, PartitionKey, TableId, WriteEngine, WriteEngineConfig,
};
use cqlite_core::types::Value;
use std::collections::HashMap;
let schema = TableSchema {
keyspace: "demo".to_string(),
table: "users".to_string(),
partition_keys: vec![KeyColumn {
name: "id".to_string(),
data_type: "int".to_string(),
position: 0,
}],
clustering_keys: vec![],
columns: vec![
Column {
name: "id".to_string(),
data_type: "int".to_string(),
nullable: false,
default: None,
is_static: false,
},
Column {
name: "name".to_string(),
data_type: "text".to_string(),
nullable: true,
default: None,
is_static: false,
},
],
comments: HashMap::new(),
};
let base = std::env::temp_dir().join("cqlite-write-a-mutation");
let config = WriteEngineConfig::new(base.join("data"), base.join("wal"), schema);
let mut engine = WriteEngine::new(config)?;
let mutation = Mutation::new(
TableId::new("demo", "users"),
PartitionKey::single("id", Value::Integer(1)),
None, vec![CellOperation::Write {
column: "name".to_string(),
value: Value::Text("Alice".to_string()),
}],
1_704_067_200_000_000, None, );
engine.write(mutation)?;
let rt = tokio::runtime::Runtime::new().expect("create tokio runtime");
rt.block_on(async {
match engine.flush().await? {
Some(_info) => println!("Flushed memtable to a new SSTable in {base:?}"),
None => println!("Nothing to flush (memtable was empty)"),
}
engine.close().await
})?;
Ok(())
}
#[cfg(not(feature = "write-support"))]
fn main() {
eprintln!("This example requires the `write-support` feature.");
eprintln!(
"Run with: cargo run -p cqlite-core --example write_a_mutation --features write-support"
);
std::process::exit(1);
}