uri-register 0.3.0

A high-performance PostgreSQL-backed URI dictionary service for assigning unique integer IDs to URIs
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
// Copyright TELICENT LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use rand::seq::SliceRandom;
use rand::Rng;
use uri_register::{PostgresUriRegister, UriService};

mod common;
use common::{get_database_url, get_table_name};

/// Setup: Create a test register
async fn setup() -> PostgresUriRegister {
    let db_url = get_database_url();
    let table_name = get_table_name();
    PostgresUriRegister::new(&db_url, &table_name, 20, 10_000)
        .await
        .expect("Failed to connect to database")
}

/// Test all possible state combinations:
/// 1. URI in both cache and DB (cached)
/// 2. URI in DB but not in cache (cache miss)
/// 3. URI not in DB (new insertion)
/// 4. Duplicate URIs in input batch
#[tokio::test]
async fn test_batch_all_state_combinations() {
    let register = setup().await;
    let uuid = uuid::Uuid::new_v4();

    // Phase 1: Setup - Pre-populate DB with URIs
    let mut pre_existing_uris = vec![];
    for i in 0..30 {
        let uri = format!("http://example.org/stress/preexist/{}/{}", uuid, i);
        register
            .register_uri(&uri)
            .await
            .expect("Failed to pre-populate");
        pre_existing_uris.push(uri);
    }

    // Phase 2: Create a NEW register instance to simulate cache miss
    // (the pre-existing URIs are in DB but NOT in this new register's cache)
    let fresh_register = setup().await;

    // Phase 3: Prime cache with SOME of the pre-existing URIs
    let cached_uris = &pre_existing_uris[0..10]; // First 10 will be cached
    for uri in cached_uris {
        fresh_register
            .register_uri(uri)
            .await
            .expect("Failed to cache");
    }

    // Phase 4: Build test batch with all state combinations:
    let mut test_uris = vec![];

    // State 1: In cache AND in DB (first 10 pre-existing)
    test_uris.extend_from_slice(&pre_existing_uris[0..10]);

    // State 2: In DB but NOT in cache (next 20 pre-existing)
    test_uris.extend_from_slice(&pre_existing_uris[10..30]);

    // State 3: NOT in DB (brand new URIs)
    for i in 0..20 {
        test_uris.push(format!("http://example.org/stress/new/{}/{}", uuid, i));
    }

    // State 4: Add duplicates throughout
    test_uris.push(pre_existing_uris[5].clone()); // Duplicate from cached
    test_uris.push(pre_existing_uris[15].clone()); // Duplicate from DB-only
    test_uris.push(format!("http://example.org/stress/new/{}/5", uuid)); // Duplicate of new

    // Total: 53 URIs with mixed states and duplicates

    // Phase 5: Test register_uri_batch - ORDER PRESERVATION
    let batch_ids = fresh_register
        .register_uri_batch(&test_uris)
        .await
        .expect("Batch registration failed");

    assert_eq!(
        batch_ids.len(),
        test_uris.len(),
        "Result length must match input length"
    );

    // Phase 6: Verify correctness - check EVERY position
    for (idx, uri) in test_uris.iter().enumerate() {
        let individual_id = fresh_register
            .register_uri(uri)
            .await
            .expect("Individual registration failed");

        assert_eq!(
            batch_ids[idx], individual_id,
            "CORRECTNESS FAILURE at index {}: URI '{}' got ID {} in batch but {} individually",
            idx, uri, batch_ids[idx], individual_id
        );
    }

    // Phase 7: Test register_uri_batch_hashmap - MAPPING CORRECTNESS
    let fresh_register2 = setup().await;

    // Prime cache with same subset
    for uri in cached_uris {
        fresh_register2
            .register_uri(uri)
            .await
            .expect("Failed to cache");
    }

    let batch_map = fresh_register2
        .register_uri_batch_hashmap(&test_uris)
        .await
        .expect("Hashmap batch registration failed");

    // Verify each unique URI has correct mapping
    let unique_uris: std::collections::HashSet<_> = test_uris.iter().collect();
    assert_eq!(
        batch_map.len(),
        unique_uris.len(),
        "HashMap should contain exactly {} unique URIs",
        unique_uris.len()
    );

    for uri in unique_uris {
        let individual_id = fresh_register2
            .register_uri(uri)
            .await
            .expect("Individual registration failed");

        assert_eq!(
            batch_map.get(uri),
            Some(&individual_id),
            "CORRECTNESS FAILURE: URI '{}' mapped to {:?} in hashmap but {} individually",
            uri,
            batch_map.get(uri),
            individual_id
        );
    }

    println!(
        "✓ Tested {} URIs across all state combinations",
        test_uris.len()
    );
}

/// Stress test with randomized large batches
#[tokio::test]
async fn test_batch_large_random_distribution() {
    let register = setup().await;
    let uuid = uuid::Uuid::new_v4();
    let mut rng = rand::rng();

    // Pre-populate 100 URIs in DB
    let mut all_uris = vec![];
    for i in 0..100 {
        let uri = format!("http://example.org/stress/rand/{}/{}", uuid, i);
        register
            .register_uri(&uri)
            .await
            .expect("Failed to pre-populate");
        all_uris.push(uri);
    }

    // Create fresh register and randomly cache 30% of URIs
    let fresh_register = setup().await;
    let mut cached_indices: Vec<usize> = (0..100).collect();
    cached_indices.shuffle(&mut rng);

    for &idx in &cached_indices[0..30] {
        fresh_register
            .register_uri(&all_uris[idx])
            .await
            .expect("Failed to cache");
    }

    // Build test batch: 60 URIs with random distribution
    let mut test_uris = vec![];

    // Add 30 random URIs from pre-existing (mix of cached and uncached)
    let mut selected: Vec<usize> = (0..100).collect();
    selected.shuffle(&mut rng);
    for &idx in &selected[0..30] {
        test_uris.push(all_uris[idx].clone());
    }

    // Add 20 brand new URIs
    for i in 100..120 {
        test_uris.push(format!("http://example.org/stress/rand/{}/{}", uuid, i));
    }

    // Add 10 random duplicates
    for _ in 0..10 {
        let dup_idx = rng.random_range(0..test_uris.len());
        test_uris.push(test_uris[dup_idx].clone());
    }

    // Shuffle the entire batch
    test_uris.shuffle(&mut rng);

    println!(
        "Testing batch of {} URIs with random distribution",
        test_uris.len()
    );

    // Test register_uri_batch
    let batch_ids = fresh_register
        .register_uri_batch(&test_uris)
        .await
        .expect("Batch failed");

    assert_eq!(batch_ids.len(), test_uris.len());

    // Verify every single position
    for (idx, uri) in test_uris.iter().enumerate() {
        let individual_id = fresh_register
            .register_uri(uri)
            .await
            .expect("Individual failed");

        assert_eq!(
            batch_ids[idx], individual_id,
            "Order violation at index {} for URI '{}'",
            idx, uri
        );
    }

    // Test register_uri_batch_hashmap
    let fresh_register2 = setup().await;

    // Prime cache identically
    for &idx in &cached_indices[0..30] {
        fresh_register2
            .register_uri(&all_uris[idx])
            .await
            .expect("Failed to cache");
    }

    let batch_map = fresh_register2
        .register_uri_batch_hashmap(&test_uris)
        .await
        .expect("Hashmap batch failed");

    // Verify mappings
    let unique_uris: std::collections::HashSet<_> = test_uris.iter().collect();
    for uri in unique_uris {
        let individual_id = fresh_register2
            .register_uri(uri)
            .await
            .expect("Individual failed");

        assert_eq!(
            batch_map.get(uri),
            Some(&individual_id),
            "Mapping violation for URI '{}'",
            uri
        );
    }

    println!("✓ All {} positions verified correct", test_uris.len());
}

/// Stress test with heavy duplicate concentration
#[tokio::test]
async fn test_batch_heavy_duplicates() {
    let register = setup().await;
    let uuid = uuid::Uuid::new_v4();

    // Create 10 unique URIs
    let unique_uris: Vec<String> = (0..10)
        .map(|i| format!("http://example.org/stress/dup/{}/{}", uuid, i))
        .collect();

    // Build batch with 100 URIs but only 10 unique (heavy duplication)
    let mut test_uris = vec![];
    let mut rng = rand::rng();

    for _ in 0..100 {
        let idx = rng.random_range(0..unique_uris.len());
        test_uris.push(unique_uris[idx].clone());
    }

    println!(
        "Testing batch of {} URIs with only {} unique",
        test_uris.len(),
        unique_uris.len()
    );

    // Test register_uri_batch
    let batch_ids = register
        .register_uri_batch(&test_uris)
        .await
        .expect("Batch failed");

    assert_eq!(batch_ids.len(), 100);

    // Verify all 100 positions
    for (idx, uri) in test_uris.iter().enumerate() {
        let individual_id = register.register_uri(uri).await.expect("Individual failed");

        assert_eq!(
            batch_ids[idx], individual_id,
            "Duplicate handling error at index {}",
            idx
        );
    }

    // Verify that duplicate URIs got same ID
    let uri_to_positions: std::collections::HashMap<&String, Vec<usize>> = test_uris
        .iter()
        .enumerate()
        .fold(std::collections::HashMap::new(), |mut acc, (idx, uri)| {
            acc.entry(uri).or_default().push(idx);
            acc
        });

    for (uri, positions) in uri_to_positions {
        let first_id = batch_ids[positions[0]];
        for &pos in &positions[1..] {
            assert_eq!(
                batch_ids[pos], first_id,
                "Duplicate URI '{}' got different IDs at positions {} and {}",
                uri, positions[0], pos
            );
        }
    }

    // Test register_uri_batch_hashmap
    let fresh_register = setup().await;
    let batch_map = fresh_register
        .register_uri_batch_hashmap(&test_uris)
        .await
        .expect("Hashmap batch failed");

    assert_eq!(
        batch_map.len(),
        unique_uris.len(),
        "Should have exactly 10 unique mappings"
    );

    for uri in &unique_uris {
        let individual_id = fresh_register
            .register_uri(uri)
            .await
            .expect("Individual failed");

        assert_eq!(
            batch_map.get(uri),
            Some(&individual_id),
            "Mapping error for duplicate URI '{}'",
            uri
        );
    }

    println!("✓ Heavy duplicate scenario passed");
}

/// Test cache eviction scenarios (simulate LRU eviction)
#[tokio::test]
async fn test_batch_with_cache_pressure() {
    let register = setup().await;
    let uuid = uuid::Uuid::new_v4();

    // Register 100 URIs to potentially fill/evict cache
    let mut old_uris = vec![];
    for i in 0..100 {
        let uri = format!("http://example.org/stress/cache/{}/{}", uuid, i);
        register.register_uri(&uri).await.expect("Failed");
        old_uris.push(uri);
    }

    // Now create a batch that includes both old URIs (may be evicted) and new URIs
    let mut test_uris = vec![];

    // Include some old URIs (cache state unknown due to potential eviction)
    test_uris.extend_from_slice(&old_uris[0..30]);

    // Add new URIs
    for i in 100..150 {
        test_uris.push(format!("http://example.org/stress/cache/{}/{}", uuid, i));
    }

    // Add duplicates of old URIs
    test_uris.extend_from_slice(&old_uris[10..20]);

    // Test register_uri_batch
    let batch_ids = register
        .register_uri_batch(&test_uris)
        .await
        .expect("Batch failed");

    // Verify correctness regardless of cache state
    for (idx, uri) in test_uris.iter().enumerate() {
        let individual_id = register.register_uri(uri).await.expect("Individual failed");

        assert_eq!(
            batch_ids[idx], individual_id,
            "Cache pressure test failed at index {}",
            idx
        );
    }

    // Test register_uri_batch_hashmap
    let fresh_register = setup().await;

    // Re-register old URIs
    for uri in &old_uris {
        fresh_register.register_uri(uri).await.expect("Failed");
    }

    let batch_map = fresh_register
        .register_uri_batch_hashmap(&test_uris)
        .await
        .expect("Hashmap batch failed");

    let unique_uris: std::collections::HashSet<_> = test_uris.iter().collect();
    for uri in unique_uris {
        let individual_id = fresh_register
            .register_uri(uri)
            .await
            .expect("Individual failed");

        assert_eq!(
            batch_map.get(uri),
            Some(&individual_id),
            "Cache pressure hashmap test failed"
        );
    }

    println!("✓ Cache pressure scenario passed");
}

/// Concurrent stress test with mixed states
#[tokio::test]
async fn test_batch_concurrent_mixed_states() {
    let uuid = uuid::Uuid::new_v4();

    // Pre-populate shared DB state
    let setup_register = setup().await;
    let mut shared_uris = vec![];
    for i in 0..50 {
        let uri = format!("http://example.org/stress/concurrent/{}/{}", uuid, i);
        setup_register
            .register_uri(&uri)
            .await
            .expect("Setup failed");
        shared_uris.push(uri);
    }

    // Spawn multiple concurrent tasks with different cache states
    let mut handles = vec![];

    for task_id in 0..5 {
        let shared_uris_clone = shared_uris.clone();
        let uuid_clone = uuid;

        let handle = tokio::spawn(async move {
            let register = setup().await;

            // Each task caches a different subset (simulating distributed system)
            let cache_start = task_id * 10;
            let cache_end = cache_start + 10;
            for uri in &shared_uris_clone[cache_start..cache_end] {
                register.register_uri(uri).await.expect("Cache failed");
            }

            // Build batch with mixed states
            let mut test_uris = vec![];
            test_uris.extend_from_slice(&shared_uris_clone);

            // Add new URIs unique to this task
            for i in 0..20 {
                test_uris.push(format!(
                    "http://example.org/stress/concurrent/{}/task{}/{}",
                    uuid_clone, task_id, i
                ));
            }

            // Test both methods
            let batch_ids = register
                .register_uri_batch(&test_uris)
                .await
                .expect("Concurrent batch failed");

            let batch_map = register
                .register_uri_batch_hashmap(&test_uris)
                .await
                .expect("Concurrent hashmap failed");

            // Verify correctness
            for (idx, uri) in test_uris.iter().enumerate() {
                let individual_id = register.register_uri(uri).await.expect("Individual failed");

                assert_eq!(batch_ids[idx], individual_id, "Concurrent order error");
                assert_eq!(
                    batch_map.get(uri),
                    Some(&individual_id),
                    "Concurrent mapping error"
                );
            }

            test_uris.len()
        });

        handles.push(handle);
    }

    // Wait for all tasks
    let mut total_tested = 0;
    for handle in handles {
        total_tested += handle.await.expect("Task panicked");
    }

    println!(
        "✓ Concurrent test verified {} total URIs across 5 tasks",
        total_tested
    );
}