hive-gpu 0.1.6

High-performance GPU acceleration for vector operations (Metal, CUDA, wgpu)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
# 🚀 Hive-GPU Examples

**Practical examples for using hive-gpu v0.1.0 in real-world applications**

## 📚 Table of Contents

1. [Basic Usage]#basic-usage
2. [Document Search]#document-search
3. [Image Similarity]#image-similarity
4. [Recommendation System]#recommendation-system
5. [Vectorizer Integration]#vectorizer-integration
6. [Performance Optimization]#performance-optimization
7. [Error Handling]#error-handling

## ðŸŽŊ Basic Usage

### Simple Vector Operations

```rust
use hive_gpu::metal::context::MetalNativeContext;
use hive_gpu::traits::{GpuContext, GpuVectorStorage};
use hive_gpu::types::{GpuVector, GpuDistanceMetric};
use std::collections::HashMap;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize GPU context
    let context = MetalNativeContext::new()?;
    let mut storage = context.create_storage(4, GpuDistanceMetric::Cosine)?;
    
    // Create vectors
    let vectors = vec![
        GpuVector {
            id: "vector_1".to_string(),
            data: vec![1.0, 0.0, 0.0, 0.0],
            metadata: HashMap::new(),
        },
        GpuVector {
            id: "vector_2".to_string(),
            data: vec![0.0, 1.0, 0.0, 0.0],
            metadata: HashMap::new(),
        },
        GpuVector {
            id: "vector_3".to_string(),
            data: vec![0.0, 0.0, 1.0, 0.0],
            metadata: HashMap::new(),
        },
    ];
    
    // Add vectors to GPU
    storage.add_vectors(&vectors)?;
    println!("Added {} vectors", storage.vector_count());
    
    // Search for similar vectors
    let query = vec![1.0, 0.0, 0.0, 0.0];
    let results = storage.search(&query, 3)?;
    
    println!("Search results:");
    for (i, result) in results.iter().enumerate() {
        println!("{}. {} (similarity: {:.4})", i + 1, result.id, result.score);
    }
    
    Ok(())
}
```

### Batch Processing

```rust
use hive_gpu::metal::context::MetalNativeContext;
use hive_gpu::traits::{GpuContext, GpuVectorStorage};
use hive_gpu::types::{GpuVector, GpuDistanceMetric};
use std::collections::HashMap;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let context = MetalNativeContext::new()?;
    let mut storage = context.create_storage(128, GpuDistanceMetric::Cosine)?;
    
    // Generate large batch of random vectors
    let batch_size = 10000;
    let mut vectors = Vec::with_capacity(batch_size);
    
    for i in 0..batch_size {
        let data = (0..128).map(|_| rand::random::<f32>()).collect();
        vectors.push(GpuVector {
            id: format!("batch_vector_{}", i),
            data,
            metadata: HashMap::new(),
        });
    }
    
    // Add vectors in chunks for efficiency
    let chunk_size = 1000;
    for chunk in vectors.chunks(chunk_size) {
        storage.add_vectors(chunk)?;
        println!("Added {} vectors", chunk.len());
    }
    
    println!("Total vectors: {}", storage.vector_count());
    
    // Batch search
    let queries = vec![
        (0..128).map(|_| rand::random::<f32>()).collect::<Vec<f32>>(),
        (0..128).map(|_| rand::random::<f32>()).collect::<Vec<f32>>(),
        (0..128).map(|_| rand::random::<f32>()).collect::<Vec<f32>>(),
    ];
    
    for (i, query) in queries.iter().enumerate() {
        let results = storage.search(query, 5)?;
        println!("Query {}: Found {} results", i + 1, results.len());
    }
    
    Ok(())
}
```

## 📄 Document Search

### Semantic Document Search

```rust
use hive_gpu::metal::context::MetalNativeContext;
use hive_gpu::traits::{GpuContext, GpuVectorStorage};
use hive_gpu::types::{GpuVector, GpuDistanceMetric};
use std::collections::HashMap;

struct DocumentSearch {
    storage: Box<dyn GpuVectorStorage>,
}

impl DocumentSearch {
    async fn new() -> Result<Self, Box<dyn std::error::Error>> {
        let context = MetalNativeContext::new()?;
        let storage = context.create_storage(384, GpuDistanceMetric::Cosine)?;
        
        Ok(Self { storage })
    }
    
    async fn add_document(&mut self, id: &str, text: &str, embedding: Vec<f32>) -> Result<(), Box<dyn std::error::Error>> {
        let mut metadata = HashMap::new();
        metadata.insert("text".to_string(), text.to_string());
        metadata.insert("length".to_string(), text.len().to_string());
        
        let vector = GpuVector {
            id: id.to_string(),
            data: embedding,
            metadata,
        };
        
        self.storage.add_vectors(&[vector])?;
        Ok(())
    }
    
    async fn search(&self, query_embedding: &[f32], limit: usize) -> Result<Vec<SearchResult>, Box<dyn std::error::Error>> {
        let results = self.storage.search(query_embedding, limit)?;
        
        Ok(results.into_iter().map(|r| SearchResult {
            id: r.id,
            score: r.score,
        }).collect())
    }
}

#[derive(Debug)]
struct SearchResult {
    id: String,
    score: f32,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut search = DocumentSearch::new().await?;
    
    // Add documents (in practice, use real embeddings)
    let documents = vec![
        ("doc_1", "Machine learning and artificial intelligence", generate_embedding("Machine learning and artificial intelligence")),
        ("doc_2", "Deep learning neural networks", generate_embedding("Deep learning neural networks")),
        ("doc_3", "Natural language processing", generate_embedding("Natural language processing")),
        ("doc_4", "Computer vision and image recognition", generate_embedding("Computer vision and image recognition")),
        ("doc_5", "Reinforcement learning algorithms", generate_embedding("Reinforcement learning algorithms")),
    ];
    
    for (id, text, embedding) in documents {
        search.add_document(id, text, embedding).await?;
    }
    
    // Search for similar documents
    let query = "AI and machine learning";
    let query_embedding = generate_embedding(query);
    let results = search.search(&query_embedding, 3).await?;
    
    println!("Search results for: '{}'", query);
    for (i, result) in results.iter().enumerate() {
        println!("{}. {} (similarity: {:.4})", i + 1, result.id, result.score);
    }
    
    Ok(())
}

// Mock embedding function (replace with real implementation)
fn generate_embedding(text: &str) -> Vec<f32> {
    // In practice, use sentence-transformers or similar
    (0..384).map(|i| (i as f32 + text.len() as f32) * 0.01).collect()
}
```

### Advanced Document Search with Metadata

```rust
use hive_gpu::metal::context::MetalNativeContext;
use hive_gpu::traits::{GpuContext, GpuVectorStorage};
use hive_gpu::types::{GpuVector, GpuDistanceMetric};
use std::collections::HashMap;
use serde_json::Value;

struct AdvancedDocumentSearch {
    storage: Box<dyn GpuVectorStorage>,
}

impl AdvancedDocumentSearch {
    async fn new() -> Result<Self, Box<dyn std::error::Error>> {
        let context = MetalNativeContext::new()?;
        let storage = context.create_storage(512, GpuDistanceMetric::Cosine)?;
        
        Ok(Self { storage })
    }
    
    async fn add_document(&mut self, document: Document) -> Result<(), Box<dyn std::error::Error>> {
        let mut metadata = HashMap::new();
        metadata.insert("title".to_string(), document.title);
        metadata.insert("author".to_string(), document.author);
        metadata.insert("category".to_string(), document.category);
        metadata.insert("date".to_string(), document.date);
        metadata.insert("tags".to_string(), serde_json::to_string(&document.tags)?);
        
        let vector = GpuVector {
            id: document.id,
            data: document.embedding,
            metadata,
        };
        
        self.storage.add_vectors(&[vector])?;
        Ok(())
    }
    
    async fn search_with_filters(&self, query: &[f32], category: Option<&str>, limit: usize) -> Result<Vec<DocumentResult>, Box<dyn std::error::Error>> {
        let results = self.storage.search(query, limit * 2)?; // Get more results for filtering
        
        let mut filtered_results = Vec::new();
        for result in results {
            if let Some(cat) = category {
                if let Some(metadata_category) = result.metadata.get("category") {
                    if metadata_category != cat {
                        continue;
                    }
                }
            }
            
            filtered_results.push(DocumentResult {
                id: result.id,
                score: result.score,
                metadata: result.metadata,
            });
            
            if filtered_results.len() >= limit {
                break;
            }
        }
        
        Ok(filtered_results)
    }
}

#[derive(Debug)]
struct Document {
    id: String,
    title: String,
    author: String,
    category: String,
    date: String,
    tags: Vec<String>,
    embedding: Vec<f32>,
}

#[derive(Debug)]
struct DocumentResult {
    id: String,
    score: f32,
    metadata: HashMap<String, String>,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut search = AdvancedDocumentSearch::new().await?;
    
    // Add documents with rich metadata
    let documents = vec![
        Document {
            id: "doc_1".to_string(),
            title: "Introduction to Machine Learning".to_string(),
            author: "John Doe".to_string(),
            category: "AI".to_string(),
            date: "2024-01-15".to_string(),
            tags: vec!["machine-learning".to_string(), "ai".to_string()],
            embedding: generate_embedding("Introduction to Machine Learning"),
        },
        Document {
            id: "doc_2".to_string(),
            title: "Deep Learning Fundamentals".to_string(),
            author: "Jane Smith".to_string(),
            category: "AI".to_string(),
            date: "2024-01-20".to_string(),
            tags: vec!["deep-learning".to_string(), "neural-networks".to_string()],
            embedding: generate_embedding("Deep Learning Fundamentals"),
        },
        Document {
            id: "doc_3".to_string(),
            title: "Web Development Best Practices".to_string(),
            author: "Bob Johnson".to_string(),
            category: "Web".to_string(),
            date: "2024-01-25".to_string(),
            tags: vec!["web-development".to_string(), "best-practices".to_string()],
            embedding: generate_embedding("Web Development Best Practices"),
        },
    ];
    
    for document in documents {
        search.add_document(document).await?;
    }
    
    // Search with category filter
    let query = generate_embedding("machine learning algorithms");
    let results = search.search_with_filters(&query, Some("AI"), 5).await?;
    
    println!("AI category search results:");
    for (i, result) in results.iter().enumerate() {
        println!("{}. {} (similarity: {:.4})", i + 1, result.id, result.score);
        println!("   Title: {}", result.metadata.get("title").unwrap_or(&"Unknown".to_string()));
        println!("   Author: {}", result.metadata.get("author").unwrap_or(&"Unknown".to_string()));
    }
    
    Ok(())
}

fn generate_embedding(text: &str) -> Vec<f32> {
    (0..512).map(|i| (i as f32 + text.len() as f32) * 0.01).collect()
}
```

## 🖞ïļ Image Similarity

### Image Search System

```rust
use hive_gpu::metal::context::MetalNativeContext;
use hive_gpu::traits::{GpuContext, GpuVectorStorage};
use hive_gpu::types::{GpuVector, GpuDistanceMetric};
use std::collections::HashMap;

struct ImageSearch {
    storage: Box<dyn GpuVectorStorage>,
}

impl ImageSearch {
    async fn new() -> Result<Self, Box<dyn std::error::Error>> {
        let context = MetalNativeContext::new()?;
        let storage = context.create_storage(2048, GpuDistanceMetric::Cosine)?; // ResNet-50 embedding size
        
        Ok(Self { storage })
    }
    
    async fn add_image(&mut self, id: &str, path: &str, embedding: Vec<f32>) -> Result<(), Box<dyn std::error::Error>> {
        let mut metadata = HashMap::new();
        metadata.insert("path".to_string(), path.to_string());
        metadata.insert("type".to_string(), "image".to_string());
        
        let vector = GpuVector {
            id: id.to_string(),
            data: embedding,
            metadata,
        };
        
        self.storage.add_vectors(&[vector])?;
        Ok(())
    }
    
    async fn find_similar_images(&self, query_embedding: &[f32], limit: usize) -> Result<Vec<ImageResult>, Box<dyn std::error::Error>> {
        let results = self.storage.search(query_embedding, limit)?;
        
        Ok(results.into_iter().map(|r| ImageResult {
            id: r.id,
            score: r.score,
            path: r.metadata.get("path").unwrap_or(&"Unknown".to_string()).clone(),
        }).collect())
    }
}

#[derive(Debug)]
struct ImageResult {
    id: String,
    score: f32,
    path: String,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut search = ImageSearch::new().await?;
    
    // Add images (in practice, use real image embeddings)
    let images = vec![
        ("img_1", "/path/to/cat1.jpg", generate_image_embedding("cat")),
        ("img_2", "/path/to/cat2.jpg", generate_image_embedding("cat")),
        ("img_3", "/path/to/dog1.jpg", generate_image_embedding("dog")),
        ("img_4", "/path/to/dog2.jpg", generate_image_embedding("dog")),
        ("img_5", "/path/to/car1.jpg", generate_image_embedding("car")),
    ];
    
    for (id, path, embedding) in images {
        search.add_image(id, path, embedding).await?;
    }
    
    // Search for similar images
    let query_embedding = generate_image_embedding("cat");
    let results = search.find_similar_images(&query_embedding, 3).await?;
    
    println!("Similar images to 'cat':");
    for (i, result) in results.iter().enumerate() {
        println!("{}. {} (similarity: {:.4}) - {}", i + 1, result.id, result.score, result.path);
    }
    
    Ok(())
}

fn generate_image_embedding(description: &str) -> Vec<f32> {
    // In practice, use ResNet-50 or similar model
    (0..2048).map(|i| (i as f32 + description.len() as f32) * 0.01).collect()
}
```

## ðŸŽŊ Recommendation System

### Product Recommendation Engine

```rust
use hive_gpu::metal::context::MetalNativeContext;
use hive_gpu::traits::{GpuContext, GpuVectorStorage};
use hive_gpu::types::{GpuVector, GpuDistanceMetric};
use std::collections::HashMap;

struct RecommendationEngine {
    storage: Box<dyn GpuVectorStorage>,
}

impl RecommendationEngine {
    async fn new() -> Result<Self, Box<dyn std::error::Error>> {
        let context = MetalNativeContext::new()?;
        let storage = context.create_storage(100, GpuDistanceMetric::Cosine)?;
        
        Ok(Self { storage })
    }
    
    async fn add_product(&mut self, product: Product) -> Result<(), Box<dyn std::error::Error>> {
        let mut metadata = HashMap::new();
        metadata.insert("name".to_string(), product.name);
        metadata.insert("category".to_string(), product.category);
        metadata.insert("price".to_string(), product.price.to_string());
        metadata.insert("rating".to_string(), product.rating.to_string());
        
        let vector = GpuVector {
            id: product.id,
            data: product.features,
            metadata,
        };
        
        self.storage.add_vectors(&[vector])?;
        Ok(())
    }
    
    async fn get_recommendations(&self, user_preferences: &[f32], limit: usize) -> Result<Vec<ProductRecommendation>, Box<dyn std::error::Error>> {
        let results = self.storage.search(user_preferences, limit)?;
        
        Ok(results.into_iter().map(|r| ProductRecommendation {
            product_id: r.id,
            score: r.score,
            name: r.metadata.get("name").unwrap_or(&"Unknown".to_string()).clone(),
            category: r.metadata.get("category").unwrap_or(&"Unknown".to_string()).clone(),
            price: r.metadata.get("price").unwrap_or(&"0".to_string()).parse().unwrap_or(0.0),
            rating: r.metadata.get("rating").unwrap_or(&"0".to_string()).parse().unwrap_or(0.0),
        }).collect())
    }
}

#[derive(Debug)]
struct Product {
    id: String,
    name: String,
    category: String,
    price: f32,
    rating: f32,
    features: Vec<f32>,
}

#[derive(Debug)]
struct ProductRecommendation {
    product_id: String,
    score: f32,
    name: String,
    category: String,
    price: f32,
    rating: f32,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut engine = RecommendationEngine::new().await?;
    
    // Add products
    let products = vec![
        Product {
            id: "prod_1".to_string(),
            name: "Wireless Headphones".to_string(),
            category: "Electronics".to_string(),
            price: 99.99,
            rating: 4.5,
            features: generate_product_features("electronics", "audio", 99.99, 4.5),
        },
        Product {
            id: "prod_2".to_string(),
            name: "Smartphone".to_string(),
            category: "Electronics".to_string(),
            price: 699.99,
            rating: 4.8,
            features: generate_product_features("electronics", "mobile", 699.99, 4.8),
        },
        Product {
            id: "prod_3".to_string(),
            name: "Running Shoes".to_string(),
            category: "Sports".to_string(),
            price: 129.99,
            rating: 4.2,
            features: generate_product_features("sports", "footwear", 129.99, 4.2),
        },
    ];
    
    for product in products {
        engine.add_product(product).await?;
    }
    
    // Get recommendations for a user
    let user_preferences = generate_user_preferences("electronics", "high_quality", 500.0);
    let recommendations = engine.get_recommendations(&user_preferences, 3).await?;
    
    println!("Product recommendations:");
    for (i, rec) in recommendations.iter().enumerate() {
        println!("{}. {} (score: {:.4})", i + 1, rec.name, rec.score);
        println!("   Category: {}, Price: ${:.2}, Rating: {:.1}", rec.category, rec.price, rec.rating);
    }
    
    Ok(())
}

fn generate_product_features(category: &str, subcategory: &str, price: f32, rating: f32) -> Vec<f32> {
    // In practice, use real product feature extraction
    let mut features = vec![0.0; 100];
    
    // Category encoding
    match category {
        "electronics" => features[0] = 1.0,
        "sports" => features[1] = 1.0,
        _ => {}
    }
    
    // Price normalization
    features[2] = price / 1000.0;
    
    // Rating normalization
    features[3] = rating / 5.0;
    
    // Add some random features
    for i in 4..100 {
        features[i] = rand::random::<f32>();
    }
    
    features
}

fn generate_user_preferences(category: &str, quality: &str, max_price: f32) -> Vec<f32> {
    let mut preferences = vec![0.0; 100];
    
    // Category preference
    match category {
        "electronics" => preferences[0] = 1.0,
        "sports" => preferences[1] = 1.0,
        _ => {}
    }
    
    // Quality preference
    match quality {
        "high_quality" => preferences[2] = 1.0,
        "budget" => preferences[2] = 0.3,
        _ => {}
    }
    
    // Price preference
    preferences[3] = max_price / 1000.0;
    
    // Add some random preferences
    for i in 4..100 {
        preferences[i] = rand::random::<f32>();
    }
    
    preferences
}
```

## 🔗 Vectorizer Integration

### Using with Hive-Vectorizer

```rust
use vectorizer::VectorStore;
use vectorizer::models::{CollectionConfig, DistanceMetric, HnswConfig, Vector, Payload};
use serde_json::json;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create vectorizer store
    let mut store = VectorStore::new();
    
    // Configure collection with GPU acceleration
    let config = CollectionConfig {
        dimension: 512,
        metric: DistanceMetric::Cosine,
        hnsw_config: HnswConfig {
            m: 16,
            ef_construction: 200,
            ef_search: 50,
            seed: 42,
        },
    };
    
    // Create collection
    store.create_collection("documents", config)?;
    
    // Add documents
    let documents = vec![
        Vector {
            id: "doc_1".to_string(),
            data: generate_embedding("Machine learning and AI"),
            payload: Some(Payload::new(json!({
                "title": "Introduction to ML",
                "author": "John Doe",
                "category": "AI"
            }))),
        },
        Vector {
            id: "doc_2".to_string(),
            data: generate_embedding("Deep learning fundamentals"),
            payload: Some(Payload::new(json!({
                "title": "Deep Learning Guide",
                "author": "Jane Smith",
                "category": "AI"
            }))),
        },
    ];
    
    store.add_vectors("documents", documents)?;
    
    // Search
    let query = generate_embedding("artificial intelligence");
    let results = store.search("documents", &query, 5)?;
    
    println!("Search results:");
    for (i, result) in results.iter().enumerate() {
        println!("{}. {} (score: {:.4})", i + 1, result.id, result.score);
        if let Some(payload) = &result.payload {
            println!("   Title: {}", payload.data.get("title").unwrap_or(&json!("Unknown")));
            println!("   Author: {}", payload.data.get("author").unwrap_or(&json!("Unknown")));
        }
    }
    
    Ok(())
}

fn generate_embedding(text: &str) -> Vec<f32> {
    (0..512).map(|i| (i as f32 + text.len() as f32) * 0.01).collect()
}
```

## ⚡ Performance Optimization

### Efficient Batch Processing

```rust
use hive_gpu::metal::context::MetalNativeContext;
use hive_gpu::traits::{GpuContext, GpuVectorStorage};
use hive_gpu::types::{GpuVector, GpuDistanceMetric};
use std::collections::HashMap;
use tokio::task;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let context = MetalNativeContext::new()?;
    let mut storage = context.create_storage(128, GpuDistanceMetric::Cosine)?;
    
    // Generate large dataset
    let total_vectors = 100000;
    let batch_size = 1000;
    
    println!("Processing {} vectors in batches of {}", total_vectors, batch_size);
    
    let start_time = std::time::Instant::now();
    
    for batch_num in 0..(total_vectors / batch_size) {
        let mut vectors = Vec::with_capacity(batch_size);
        
        for i in 0..batch_size {
            let global_index = batch_num * batch_size + i;
            let data = (0..128).map(|_| rand::random::<f32>()).collect();
            
            vectors.push(GpuVector {
                id: format!("vector_{}", global_index),
                data,
                metadata: HashMap::new(),
            });
        }
        
        storage.add_vectors(&vectors)?;
        
        if batch_num % 10 == 0 {
            println!("Processed {} vectors", (batch_num + 1) * batch_size);
        }
    }
    
    let elapsed = start_time.elapsed();
    println!("Added {} vectors in {:.2}s", total_vectors, elapsed.as_secs_f32());
    println!("Throughput: {:.0} vectors/sec", total_vectors as f32 / elapsed.as_secs_f32());
    
    // Benchmark search performance
    let search_start = std::time::Instant::now();
    let query = (0..128).map(|_| rand::random::<f32>()).collect::<Vec<f32>>();
    let results = storage.search(&query, 10)?;
    let search_elapsed = search_start.elapsed();
    
    println!("Search completed in {:.2}Ξs", search_elapsed.as_micros());
    println!("Found {} results", results.len());
    
    Ok(())
}
```

### Memory Monitoring

```rust
use hive_gpu::metal::context::MetalNativeContext;
use hive_gpu::traits::{GpuContext, GpuVectorStorage};
use hive_gpu::types::{GpuVector, GpuDistanceMetric};
use std::collections::HashMap;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let context = MetalNativeContext::new()?;
    
    // Monitor memory usage
    let memory_stats = context.memory_stats();
    println!("Initial GPU memory: {:.2} MB used, {:.2} MB available", 
             memory_stats.used_memory_mb, memory_stats.available_memory_mb);
    
    let mut storage = context.create_storage(512, GpuDistanceMetric::Cosine)?;
    
    // Add vectors and monitor memory
    let batch_size = 1000;
    for batch in 0..10 {
        let mut vectors = Vec::with_capacity(batch_size);
        
        for i in 0..batch_size {
            let data = (0..512).map(|_| rand::random::<f32>()).collect();
            vectors.push(GpuVector {
                id: format!("batch_{}_vector_{}", batch, i),
                data,
                metadata: HashMap::new(),
            });
        }
        
        storage.add_vectors(&vectors)?;
        
        let memory_stats = context.memory_stats();
        println!("Batch {}: {:.2} MB used, {:.2} MB available", 
                 batch + 1, memory_stats.used_memory_mb, memory_stats.available_memory_mb);
    }
    
    Ok(())
}
```

## ðŸ›Ąïļ Error Handling

### Comprehensive Error Handling

```rust
use hive_gpu::metal::context::MetalNativeContext;
use hive_gpu::traits::{GpuContext, GpuVectorStorage};
use hive_gpu::types::{GpuVector, GpuDistanceMetric};
use hive_gpu::error::HiveGpuError;
use std::collections::HashMap;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize with error handling
    let context = match MetalNativeContext::new() {
        Ok(ctx) => {
            println!("✅ GPU context initialized successfully");
            ctx
        },
        Err(HiveGpuError::NoDeviceAvailable) => {
            println!("❌ No GPU device available, falling back to CPU");
            return fallback_to_cpu().await;
        },
        Err(e) => {
            println!("❌ Failed to initialize GPU: {}", e);
            return Err(e.into());
        }
    };
    
    // Create storage with error handling
    let mut storage = match context.create_storage(128, GpuDistanceMetric::Cosine) {
        Ok(storage) => {
            println!("✅ Vector storage created successfully");
            storage
        },
        Err(e) => {
            println!("❌ Failed to create storage: {}", e);
            return Err(e.into());
        }
    };
    
    // Add vectors with error handling
    let vectors = create_test_vectors();
    match storage.add_vectors(&vectors) {
        Ok(indices) => {
            println!("✅ Added {} vectors successfully", indices.len());
        },
        Err(HiveGpuError::InsufficientMemory) => {
            println!("❌ Insufficient GPU memory, reducing batch size");
            return add_vectors_in_smaller_batches(&mut storage, &vectors).await;
        },
        Err(e) => {
            println!("❌ Failed to add vectors: {}", e);
            return Err(e.into());
        }
    }
    
    // Search with error handling
    let query = vec![1.0; 128];
    match storage.search(&query, 10) {
        Ok(results) => {
            println!("✅ Search completed: {} results", results.len());
            for (i, result) in results.iter().enumerate() {
                println!("{}. {} (score: {:.4})", i + 1, result.id, result.score);
            }
        },
        Err(e) => {
            println!("❌ Search failed: {}", e);
            return Err(e.into());
        }
    }
    
    Ok(())
}

async fn fallback_to_cpu() -> Result<(), Box<dyn std::error::Error>> {
    println!("🔄 Implementing CPU fallback...");
    // Implement CPU-based vector operations
    Ok(())
}

async fn add_vectors_in_smaller_batches(
    storage: &mut Box<dyn GpuVectorStorage>, 
    vectors: &[GpuVector]
) -> Result<(), Box<dyn std::error::Error>> {
    let small_batch_size = 100;
    
    for chunk in vectors.chunks(small_batch_size) {
        storage.add_vectors(chunk)?;
        println!("Added batch of {} vectors", chunk.len());
    }
    
    Ok(())
}

fn create_test_vectors() -> Vec<GpuVector> {
    (0..1000).map(|i| GpuVector {
        id: format!("test_vector_{}", i),
        data: (0..128).map(|_| rand::random::<f32>()).collect(),
        metadata: HashMap::new(),
    }).collect()
}
```

---

**These examples demonstrate the power and flexibility of hive-gpu v0.1.0 for real-world applications! 🚀**