Files
db/heromodels/examples/grid4_example/example.rs
2025-09-16 14:18:08 +02:00

67 lines
2.0 KiB
Rust

use heromodels::db::hero::OurDB;
use heromodels::db::{Collection, Db};
use heromodels::models::grid4::node::node_index::{country, nodegroupid, pubkey};
use heromodels::models::grid4::node::{ComputeSlice, DeviceInfo, Node};
use std::sync::Arc;
fn main() {
// Create a temp OurDB
let ts = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos();
let path = format!("/tmp/grid4_example_{}", ts);
let _ = std::fs::remove_dir_all(&path);
let db = Arc::new(OurDB::new(&path, true).expect("create OurDB"));
let nodes = db.collection::<Node>().expect("open node collection");
// Build a node
let cs = ComputeSlice::new()
.nodeid(1)
.slice_id(1)
.mem_gb(64.0)
.storage_gb(1024.0)
.passmark(8000)
.vcores(24)
.gpus(2)
.price_cc(0.5);
let dev = DeviceInfo {
vendor: "ACME".into(),
..Default::default()
};
let n = Node::new()
.nodegroupid(7)
.uptime(98)
.add_compute_slice(cs)
.devices(dev)
.country("BE")
.pubkey("PUB_NODE_X")
.build();
// Store
let (id, stored) = nodes.set(&n).expect("store node");
println!("Stored node id={id} pubkey={} country={}", stored.pubkey, stored.country);
// Query by indexes
let by_country = nodes.get::<country, _>("BE").expect("query country");
println!("Found {} nodes in country=BE", by_country.len());
let by_group = nodes.get::<nodegroupid, _>(&7).expect("query group");
println!("Found {} nodes in group=7", by_group.len());
let by_key = nodes.get::<pubkey, _>("PUB_NODE_X").expect("query pubkey");
println!("Found {} with pubkey PUB_NODE_X", by_key.len());
// Update
let updated = stored.clone().country("NL");
let (_, back) = nodes.set(&updated).expect("update node");
println!("Updated node country={}", back.country);
// Delete
nodes.delete_by_id(id).expect("delete node");
println!("Deleted node id={id}");
}