|
| 1 | +// Example demonstrating the cloud IO abstraction layer |
| 2 | +// |
| 3 | +// This shows how to use fake implementations for testing and how end-users |
| 4 | +// can implement their own cloud IO solutions using the generic traits. |
| 5 | + |
| 6 | +use ironbeam::io::cloud::{ |
| 7 | + CacheIO, ComputeIO, EdgeDirection, FakeCacheIO, FakeComputeIO, FakeGraphIO, FakeIntelligenceIO, |
| 8 | + FakeKeyValueIO, FakeObjectIO, FakeQueueIO, FakeSearchIO, GraphIO, InferenceInput, |
| 9 | + IntelligenceIO, KeyValueIO, ObjectIO, QueueIO, SearchIO, SearchQuery, |
| 10 | +}; |
| 11 | +use std::collections::HashMap; |
| 12 | + |
| 13 | +fn main() { |
| 14 | + println!("=== Cloud IO Demonstration ===\n"); |
| 15 | + |
| 16 | + // Example 1: Object Storage (S3, GCS, Azure Blob) |
| 17 | + object_storage_example(); |
| 18 | + |
| 19 | + // Example 2: Key-Value Store (DynamoDB, Firestore) |
| 20 | + key_value_example(); |
| 21 | + |
| 22 | + // Example 3: Message Queue (SQS, Cloud Tasks) |
| 23 | + queue_example(); |
| 24 | + |
| 25 | + // Example 4: Cache (Redis, Memcached) |
| 26 | + cache_example(); |
| 27 | + |
| 28 | + // Example 5: Search (Elasticsearch, CloudWatch Logs) |
| 29 | + search_example(); |
| 30 | + |
| 31 | + // Example 6: Graph Database (Neptune, Neo4j) |
| 32 | + graph_example(); |
| 33 | + |
| 34 | + // Example 7: Serverless Compute (Lambda, Cloud Functions) |
| 35 | + compute_example(); |
| 36 | + |
| 37 | + // Example 8: ML Inference (SageMaker, Vertex AI) |
| 38 | + intelligence_example(); |
| 39 | +} |
| 40 | + |
| 41 | +fn object_storage_example() { |
| 42 | + println!("--- Object Storage Example ---"); |
| 43 | + let storage = FakeObjectIO::new(); |
| 44 | + |
| 45 | + // Upload some data |
| 46 | + storage |
| 47 | + .put_object("my-bucket", "data/file.txt", b"Hello, Cloud!") |
| 48 | + .expect("Failed to put object"); |
| 49 | + |
| 50 | + // List objects |
| 51 | + let objects = storage |
| 52 | + .list_objects("my-bucket", Some("data/")) |
| 53 | + .expect("Failed to list objects"); |
| 54 | + println!("Found {} objects", objects.len()); |
| 55 | + |
| 56 | + // Download data |
| 57 | + let data = storage |
| 58 | + .get_object("my-bucket", "data/file.txt") |
| 59 | + .expect("Failed to get object"); |
| 60 | + println!("Downloaded: {}", String::from_utf8_lossy(&data)); |
| 61 | + |
| 62 | + // Copy object |
| 63 | + storage |
| 64 | + .copy_object("my-bucket", "data/file.txt", "my-bucket", "backup/file.txt") |
| 65 | + .expect("Failed to copy object"); |
| 66 | + |
| 67 | + println!("✓ Object storage operations completed\n"); |
| 68 | +} |
| 69 | + |
| 70 | +fn key_value_example() { |
| 71 | + println!("--- Key-Value Store Example ---"); |
| 72 | + let kv = FakeKeyValueIO::new(); |
| 73 | + |
| 74 | + // Put some documents |
| 75 | + let mut user1 = HashMap::new(); |
| 76 | + user1.insert("name".to_string(), "Alice".to_string()); |
| 77 | + user1.insert("age".to_string(), "30".to_string()); |
| 78 | + kv.put("users", "user1", user1) |
| 79 | + .expect("Failed to put document"); |
| 80 | + |
| 81 | + let mut user2 = HashMap::new(); |
| 82 | + user2.insert("name".to_string(), "Bob".to_string()); |
| 83 | + user2.insert("age".to_string(), "25".to_string()); |
| 84 | + kv.put("users", "user2", user2) |
| 85 | + .expect("Failed to put document"); |
| 86 | + |
| 87 | + // Get a document |
| 88 | + let doc = kv.get("users", "user1").expect("Failed to get document"); |
| 89 | + if let Some(doc) = doc { |
| 90 | + println!("User: {}", doc.data.get("name").unwrap()); |
| 91 | + } |
| 92 | + |
| 93 | + // Query documents |
| 94 | + let mut filter = HashMap::new(); |
| 95 | + filter.insert("age".to_string(), "30".to_string()); |
| 96 | + let results = kv.query("users", filter).expect("Failed to query"); |
| 97 | + println!("Found {} users with age=30", results.len()); |
| 98 | + |
| 99 | + println!("✓ Key-value operations completed\n"); |
| 100 | +} |
| 101 | + |
| 102 | +fn queue_example() { |
| 103 | + println!("--- Message Queue Example ---"); |
| 104 | + let queue = FakeQueueIO::new(); |
| 105 | + |
| 106 | + // Send messages |
| 107 | + queue |
| 108 | + .send("my-queue", "Task 1", HashMap::new()) |
| 109 | + .expect("Failed to send message"); |
| 110 | + queue |
| 111 | + .send("my-queue", "Task 2", HashMap::new()) |
| 112 | + .expect("Failed to send message"); |
| 113 | + |
| 114 | + println!("Queue size: {}", queue.queue_size("my-queue").unwrap()); |
| 115 | + |
| 116 | + // Receive messages |
| 117 | + let messages = queue |
| 118 | + .receive("my-queue", 10, 30) |
| 119 | + .expect("Failed to receive messages"); |
| 120 | + println!("Received {} messages", messages.len()); |
| 121 | + |
| 122 | + for msg in &messages { |
| 123 | + println!(" - {}", msg.body); |
| 124 | + queue |
| 125 | + .delete("my-queue", &msg.receipt_handle) |
| 126 | + .expect("Failed to delete message"); |
| 127 | + } |
| 128 | + |
| 129 | + println!("✓ Queue operations completed\n"); |
| 130 | +} |
| 131 | + |
| 132 | +fn cache_example() { |
| 133 | + println!("--- Cache Example ---"); |
| 134 | + let cache = FakeCacheIO::new(); |
| 135 | + |
| 136 | + // Set some values |
| 137 | + cache |
| 138 | + .set("user:123", b"Alice", Some(3600)) |
| 139 | + .expect("Failed to set cache"); |
| 140 | + cache |
| 141 | + .set("user:456", b"Bob", None) |
| 142 | + .expect("Failed to set cache"); |
| 143 | + |
| 144 | + // Get values |
| 145 | + if let Some(value) = cache.get("user:123").expect("Failed to get cache") { |
| 146 | + println!("Cached user: {}", String::from_utf8_lossy(&value)); |
| 147 | + } |
| 148 | + |
| 149 | + // Increment counter |
| 150 | + cache |
| 151 | + .set("counter", b"10", None) |
| 152 | + .expect("Failed to set counter"); |
| 153 | + let new_val = cache.increment("counter", 5).expect("Failed to increment"); |
| 154 | + println!("Counter value: {new_val}"); |
| 155 | + |
| 156 | + println!("✓ Cache operations completed\n"); |
| 157 | +} |
| 158 | + |
| 159 | +fn search_example() { |
| 160 | + println!("--- Search/Log Example ---"); |
| 161 | + let search = FakeSearchIO::new(); |
| 162 | + |
| 163 | + // Index some documents |
| 164 | + let mut doc1 = HashMap::new(); |
| 165 | + doc1.insert("title".to_string(), "Getting started with Rust".to_string()); |
| 166 | + doc1.insert( |
| 167 | + "content".to_string(), |
| 168 | + "Rust is a systems programming language".to_string(), |
| 169 | + ); |
| 170 | + search.index("docs", "doc1", doc1).expect("Failed to index"); |
| 171 | + |
| 172 | + let mut doc2 = HashMap::new(); |
| 173 | + doc2.insert("title".to_string(), "Advanced Rust patterns".to_string()); |
| 174 | + doc2.insert( |
| 175 | + "content".to_string(), |
| 176 | + "Learn about Rust patterns".to_string(), |
| 177 | + ); |
| 178 | + search.index("docs", "doc2", doc2).expect("Failed to index"); |
| 179 | + |
| 180 | + // Search |
| 181 | + let query = SearchQuery { |
| 182 | + query: "Rust".to_string(), |
| 183 | + filters: HashMap::new(), |
| 184 | + limit: 10, |
| 185 | + offset: 0, |
| 186 | + }; |
| 187 | + let results = search.search("docs", query).expect("Failed to search"); |
| 188 | + println!("Search found {} results", results.len()); |
| 189 | + for hit in results { |
| 190 | + println!(" - {} (score: {})", hit.id, hit.score); |
| 191 | + } |
| 192 | + |
| 193 | + println!("✓ Search operations completed\n"); |
| 194 | +} |
| 195 | + |
| 196 | +fn graph_example() { |
| 197 | + println!("--- Graph Database Example ---"); |
| 198 | + let graph = FakeGraphIO::new(); |
| 199 | + |
| 200 | + // Add nodes |
| 201 | + let alice = graph |
| 202 | + .add_node(vec!["Person".to_string()], { |
| 203 | + let mut props = HashMap::new(); |
| 204 | + props.insert("name".to_string(), "Alice".to_string()); |
| 205 | + props |
| 206 | + }) |
| 207 | + .expect("Failed to add node"); |
| 208 | + |
| 209 | + let bob = graph |
| 210 | + .add_node(vec!["Person".to_string()], { |
| 211 | + let mut props = HashMap::new(); |
| 212 | + props.insert("name".to_string(), "Bob".to_string()); |
| 213 | + props |
| 214 | + }) |
| 215 | + .expect("Failed to add node"); |
| 216 | + |
| 217 | + // Add edge |
| 218 | + graph |
| 219 | + .add_edge(&alice, &bob, "KNOWS", HashMap::new()) |
| 220 | + .expect("Failed to add edge"); |
| 221 | + |
| 222 | + // Find neighbors |
| 223 | + let neighbors = graph |
| 224 | + .get_neighbors(&alice, EdgeDirection::Outgoing) |
| 225 | + .expect("Failed to get neighbors"); |
| 226 | + println!("Alice knows {} people", neighbors.len()); |
| 227 | + |
| 228 | + println!("✓ Graph operations completed\n"); |
| 229 | +} |
| 230 | + |
| 231 | +fn compute_example() { |
| 232 | + println!("--- Serverless Compute Example ---"); |
| 233 | + let compute = FakeComputeIO::new(); |
| 234 | + |
| 235 | + // Register a fake function |
| 236 | + compute.register_function("echo", <[u8]>::to_vec); |
| 237 | + |
| 238 | + // Invoke synchronously |
| 239 | + let result = compute |
| 240 | + .invoke("echo", b"Hello, Lambda!") |
| 241 | + .expect("Failed to invoke function"); |
| 242 | + println!( |
| 243 | + "Function returned: {}", |
| 244 | + String::from_utf8_lossy(&result.output) |
| 245 | + ); |
| 246 | + println!("Execution time: {}ms", result.execution_time_ms); |
| 247 | + |
| 248 | + // Invoke asynchronously |
| 249 | + let invocation_id = compute |
| 250 | + .invoke_async("echo", b"Async call") |
| 251 | + .expect("Failed to invoke async"); |
| 252 | + println!("Async invocation ID: {invocation_id}"); |
| 253 | + |
| 254 | + println!("✓ Compute operations completed\n"); |
| 255 | +} |
| 256 | + |
| 257 | +fn intelligence_example() { |
| 258 | + println!("--- ML Inference Example ---"); |
| 259 | + let ai = FakeIntelligenceIO::new(); |
| 260 | + |
| 261 | + // Register a fake model (simple echo for demonstration) |
| 262 | + ai.register_model("sentiment", |input| { |
| 263 | + format!( |
| 264 | + "{{\"sentiment\": \"positive\", \"input\": \"{}\"}}", |
| 265 | + String::from_utf8_lossy(input) |
| 266 | + ) |
| 267 | + .into_bytes() |
| 268 | + }); |
| 269 | + |
| 270 | + // Run inference |
| 271 | + let input = InferenceInput { |
| 272 | + data: b"This is a great product!".to_vec(), |
| 273 | + content_type: "text/plain".to_string(), |
| 274 | + }; |
| 275 | + |
| 276 | + let output = ai.predict("sentiment", input).expect("Failed to predict"); |
| 277 | + println!("Model output: {}", String::from_utf8_lossy(&output.data)); |
| 278 | + println!("Inference time: {}ms", output.inference_time_ms); |
| 279 | + |
| 280 | + println!("✓ Intelligence operations completed\n"); |
| 281 | +} |
0 commit comments