getting_started/
getting_started.rs1use spatio::{Point, SetOptions, Spatio};
2use std::time::Duration;
3
4fn main() -> Result<(), Box<dyn std::error::Error>> {
5 println!("Spatio - Getting Started Example");
6 println!("=================================");
7
8 let db = Spatio::memory()?;
10 println!("Created in-memory database");
11
12 db.insert("user:123", b"John Doe", None)?;
14 let value = db.get("user:123")?.unwrap();
15 println!(
16 "Basic storage: user:123 = {}",
17 String::from_utf8_lossy(&value)
18 );
19
20 let ttl_options = SetOptions::with_ttl(Duration::from_secs(5));
22 db.insert("session:abc", b"expires_soon", Some(ttl_options))?;
23 println!("Stored session data with 5-second TTL");
24
25 let new_york = Point::new(40.7128, -74.0060);
27 let london = Point::new(51.5074, -0.1278);
28 let tokyo = Point::new(35.6762, 139.6503);
29
30 db.insert_point("cities", &new_york, b"New York", None)?;
32 db.insert_point("cities", &london, b"London", None)?;
33 db.insert_point("cities", &tokyo, b"Tokyo", None)?;
34 println!("Stored geographic points for major cities");
35
36 let distance_km = new_york.distance_to(&london) / 1000.0;
38 println!("Distance NYC ↔ London: {:.0} km", distance_km);
39
40 let nearby = db.find_nearby("cities", &new_york, 2_000_000.0, 5)?;
42 println!("Found {} cities within 2000km of NYC", nearby.len());
43 for (point, data) in &nearby {
44 println!(
45 " - {} at ({:.2}, {:.2})",
46 String::from_utf8_lossy(data),
47 point.lat,
48 point.lon
49 );
50 }
51
52 db.atomic(|batch| {
54 batch.insert("sensor:temperature", b"22.5C", None)?;
55 batch.insert("sensor:humidity", b"65%", None)?;
56 batch.insert("sensor:pressure", b"1013.25 hPa", None)?;
57 Ok(())
58 })?;
59 println!("Performed atomic batch insert of sensor data");
60
61 let vehicle_path = vec![
63 (Point::new(40.7128, -74.0060), 1640995200), (Point::new(40.7150, -74.0040), 1640995260), (Point::new(40.7172, -74.0020), 1640995320), (Point::new(40.7194, -74.0000), 1640995380), ];
68
69 db.insert_trajectory("vehicle:truck001", &vehicle_path, None)?;
70 println!(
71 "Stored vehicle trajectory with {} waypoints",
72 vehicle_path.len()
73 );
74
75 let path_segment = db.query_trajectory("vehicle:truck001", 1640995200, 1640995320)?;
77 println!(
78 "Retrieved {} waypoints for first 2 minutes",
79 path_segment.len()
80 );
81
82 let stats = db.stats()?;
84 println!("Database contains {} keys", stats.key_count);
85
86 println!("\nWaiting 6 seconds to demonstrate TTL...");
88 std::thread::sleep(Duration::from_secs(6));
89
90 match db.get("session:abc")? {
92 Some(_) => println!("TTL data still exists (unexpected)"),
93 None => println!("TTL data expired as expected"),
94 }
95
96 println!("\nGetting started example completed successfully!");
97 println!("\nKey features demonstrated:");
98 println!("- Simple key-value storage");
99 println!("- Automatic spatial indexing for points");
100 println!("- Nearby point queries");
101 println!("- Trajectory tracking over time");
102 println!("- TTL (time-to-live) support");
103 println!("- Atomic batch operations");
104
105 println!("\nNext steps:");
106 println!("- Try the 'spatial_queries' example for more spatial operations");
107 println!("- See 'trajectory_tracking' for advanced movement analysis");
108
109 Ok(())
110}