shardex 0.1.0

A high-performance memory-mapped vector search engine with ACID transactions and incremental updates
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
# Migration Guide: Converting to ApiThing Pattern

This guide helps you migrate from the previous Shardex API to the new ApiThing-based pattern.

## Overview of Changes

The new API uses:
- **Context objects** instead of direct index instances
- **Operation types** that implement `ApiOperation` trait
- **Parameter objects** for type-safe inputs
- **Consistent patterns** across all operations

## Benefits of the New API

### Type Safety
Parameter objects catch configuration errors at compile time:
```rust
// Old API - easy to mix up parameters
index.search(&query, 10, Some(5))?;

// New API - clear, self-documenting parameters
Search::execute(&mut context, &SearchParams::builder()
    .query_vector(query)
    .k(10)
    .slop_factor(Some(5))
    .build()?
)?;
```

### Consistency
All operations follow the same execute pattern:
```rust
use apithing::ApiOperation;

// Every operation has the same signature
let result = Operation::execute(&mut context, &parameters)?;
```

### Composability
Operations can be easily chained, tested, and composed:
```rust
// Easy to test individual operations
CreateIndex::execute(&mut context, &create_params)?;
AddPostings::execute(&mut context, &add_params)?;
let stats = GetStats::execute(&mut context, &GetStatsParams::new())?;
```

## Step-by-Step Migration

### 1. Update Dependencies

Add apithing to your `Cargo.toml`:
```toml
[dependencies]
shardex = "0.2"  # or latest version
apithing = { git = "https://github.com/swissarmyhammer/apithing" }
```

### 2. Update Imports

**Before:**
```rust
use shardex::{Shardex, ShardexConfig, ShardexImpl};
```

**After:**
```rust
use shardex::api::{
    ShardexContext, CreateIndex, AddPostings, Search, Flush, GetStats,
    CreateIndexParams, AddPostingsParams, SearchParams, FlushParams, GetStatsParams
};
use apithing::ApiOperation;
```

### 3. Convert Index Creation

**Before:**
```rust
let config = ShardexConfig::new()
    .directory_path("./my_index")
    .vector_size(384)
    .shard_size(50000);
    
let mut index = ShardexImpl::create(config)?;
```

**After:**
```rust
let mut context = ShardexContext::new();
let params = CreateIndexParams::builder()
    .directory_path("./my_index".into())
    .vector_size(384)
    .shard_size(50000)
    .build()?;
    
CreateIndex::execute(&mut context, &params)?;
```

### 4. Convert Opening Existing Index

**Before:**
```rust
let index = Shardex::open("./my_index")?;
```

**After:**
```rust
// Opening existing indexes will use OpenIndex operation (planned)
// For now, use CreateIndex which will open if it exists
let mut context = ShardexContext::new();
let params = CreateIndexParams::builder()
    .directory_path("./my_index".into())
    .build()?;  // Other params loaded from existing index
    
CreateIndex::execute(&mut context, &params)?;
```

### 5. Convert Adding Postings

**Before:**
```rust
let postings = vec![/* ... */];
index.add_postings(postings)?;
```

**After:**
```rust
let postings = vec![/* ... */];
AddPostings::execute(&mut context, &AddPostingsParams::new(postings)?)?;
```

### 6. Convert Searching

**Before:**
```rust
let results = index.search(&query_vector, 10, Some(5))?;
```

**After:**
```rust
let results = Search::execute(&mut context, &SearchParams::builder()
    .query_vector(query_vector)
    .k(10)
    .slop_factor(Some(5))
    .build()?
)?;
```

### 7. Convert Other Operations

**Flushing - Before:**
```rust
index.flush()?;
```

**Flushing - After:**
```rust
Flush::execute(&mut context, &FlushParams::new())?;
```

**Statistics - Before:**
```rust
let stats = index.get_stats()?;
```

**Statistics - After:**
```rust
let stats = GetStats::execute(&mut context, &GetStatsParams::new())?;
```

## Common Migration Patterns

### Pattern 1: Simple Index Operations

**Before:**
```rust
async fn old_index_workflow() -> Result<(), Box<dyn std::error::Error>> {
    let config = ShardexConfig::new()
        .directory_path("./index")
        .vector_size(128);
    let mut index = ShardexImpl::create(config)?;
    
    let postings = create_sample_postings();
    index.add_postings(postings)?;
    index.flush()?;
    
    let query = vec![0.1; 128];
    let results = index.search(&query, 5, None)?;
    println!("Found {} results", results.len());
    
    Ok(())
}
```

**After:**
```rust
fn new_index_workflow() -> Result<(), Box<dyn std::error::Error>> {
    use apithing::ApiOperation;
    
    let mut context = ShardexContext::new();
    
    // Create index
    let create_params = CreateIndexParams::builder()
        .directory_path("./index".into())
        .vector_size(128)
        .build()?;
    CreateIndex::execute(&mut context, &create_params)?;
    
    // Add postings
    let postings = create_sample_postings();
    AddPostings::execute(&mut context, &AddPostingsParams::new(postings)?)?;
    
    // Flush
    Flush::execute(&mut context, &FlushParams::new())?;
    
    // Search
    let query = vec![0.1; 128];
    let results = Search::execute(&mut context, &SearchParams::builder()
        .query_vector(query)
        .k(5)
        .build()?
    )?;
    println!("Found {} results", results.len());
    
    Ok(())
}
```

### Pattern 2: Configuration-Heavy Usage

**Before:**
```rust
let config = ShardexConfig::new()
    .directory_path("./optimized_index")
    .vector_size(768)
    .shard_size(100000)
    .batch_write_interval_ms(50)
    .default_slop_factor(3)
    .bloom_filter_size(4096);
    
let mut index = ShardexImpl::create(config)?;
```

**After:**
```rust
let mut context = ShardexContext::new();
let params = CreateIndexParams::builder()
    .directory_path("./optimized_index".into())
    .vector_size(768)
    .shard_size(100000)
    .batch_write_interval_ms(50)
    .default_slop_factor(3)
    .bloom_filter_size(4096)
    .build()?;
    
CreateIndex::execute(&mut context, &params)?;
```

### Pattern 3: Error Handling

**Before:**
```rust
match index.search(&query, 10, None).await {
    Ok(results) => println!("Found {} results", results.len()),
    Err(e) => eprintln!("Search failed: {}", e),
}
```

**After:**
```rust
match Search::execute(&mut context, &SearchParams::builder()
    .query_vector(query)
    .k(10)
    .build()?
) {
    Ok(results) => println!("Found {} results", results.len()),
    Err(e) => eprintln!("Search failed: {}", e),
}
```

## Document Text Operations

The new API includes enhanced document text storage operations:

### Storing Text

**New API:**
```rust
// Single document text
StoreDocumentText::execute(&mut context, &StoreDocumentTextParams {
    document_id: DocumentId::from_raw(1),
    text: "Document content".to_string(),
})?;

// Batch text storage
let entries = vec![
    DocumentTextEntry {
        document_id: DocumentId::from_raw(1),
        text: "First document".to_string(),
    },
    DocumentTextEntry {
        document_id: DocumentId::from_raw(2),
        text: "Second document".to_string(),
    },
];

BatchStoreDocumentText::execute(&mut context, &BatchStoreDocumentTextParams {
    entries,
    batch_size: 1000,
})?;
```

### Retrieving Text

**New API:**
```rust
// Get document text
let text = GetDocumentText::execute(&mut context, &GetDocumentTextParams {
    document_id: DocumentId::from_raw(1),
})?;

// Extract snippet
let snippet = ExtractSnippet::execute(&mut context, &ExtractSnippetParams {
    document_id: DocumentId::from_raw(1),
    start: 0,
    length: 100,
})?;
```

## Testing with the New API

The new API is more testable due to its composable nature:

```rust
#[test]
fn test_index_operations() -> Result<(), Box<dyn std::error::Error>> {
    let mut context = ShardexContext::new();
    
    // Test index creation
    let create_params = CreateIndexParams::builder()
        .directory_path(temp_dir())
        .vector_size(128)
        .build()?;
    CreateIndex::execute(&mut context, &create_params)?;
    
    // Test adding postings
    let postings = vec![/* test postings */];
    AddPostings::execute(&mut context, &AddPostingsParams::new(postings)?)?;
    
    // Test statistics
    let stats = GetStats::execute(&mut context, &GetStatsParams::new())?;
    assert_eq!(stats.total_postings, 1);
    
    Ok(())
}
```

## Performance Considerations

The new API maintains the same performance characteristics:

- **Context reuse**: Single context instance manages resources efficiently
- **Parameter validation**: Compile-time checks prevent runtime errors
- **Operation batching**: Batch operations available for high throughput
- **Memory management**: Same memory-mapped storage underneath

## Troubleshooting

### Common Migration Issues

1. **Missing ApiThing dependency**
   ```
   error: failed to resolve: use of undeclared crate or module `apithing`
   ```
   **Solution:** Add `apithing` to your `Cargo.toml` dependencies.

2. **Builder pattern errors**
   ```
   error: no method named `build` found for type `CreateIndexParamsBuilder`
   ```
   **Solution:** Ensure all required fields are set before calling `.build()`.

3. **Async/sync mismatch**
   The new API is synchronous by default. Remove `.await` calls:
   ```rust
   // Before
   CreateIndex::execute(&mut context, &params)?;
   
   // After  
   CreateIndex::execute(&mut context, &params)?;
   ```

### Getting Help

- Check the [API documentation]https://docs.rs/shardex
- Review [examples]examples/ in the repository
- File issues on [GitHub]https://github.com/wballard/shardex/issues

## Summary

The ApiThing pattern provides:
- **Type Safety**: Parameter objects catch errors at compile time
-**Consistency**: All operations follow the same pattern
-**Composability**: Operations can be easily chained and tested
-**Resource Efficiency**: Single context manages all resources
-**Documentation**: Self-documenting parameter objects

The migration is straightforward and the new API provides better ergonomics, safety, and maintainability while preserving all the performance benefits of the original implementation.