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
// 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 crate::error::Result;
use crate::service::UriService;
use async_trait::async_trait;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use url::Url;

/// In-memory URI register service for testing
///
/// This implementation stores all mappings in memory using RwLock for
/// thread-safe access. It's designed for tests and should not be used
/// in production.
pub struct InMemoryUriRegister {
    uri_to_id: Arc<RwLock<HashMap<String, u64>>>,
    id_to_uri: Arc<RwLock<HashMap<u64, String>>>,
    next_id: Arc<RwLock<u64>>,
}

impl InMemoryUriRegister {
    /// Create a new in-memory URI register
    pub fn new() -> Self {
        Self {
            uri_to_id: Arc::new(RwLock::new(HashMap::new())),
            id_to_uri: Arc::new(RwLock::new(HashMap::new())),
            next_id: Arc::new(RwLock::new(1)), // Start at 1 (0 could be reserved)
        }
    }

    /// Validate that a string is a valid URI according to RFC 3986
    fn validate_uri(uri: &str) -> Result<()> {
        Url::parse(uri).map_err(|e| {
            crate::error::Error::InvalidUri(format!("Invalid URI '{}': {}", uri, e))
        })?;
        Ok(())
    }
}

impl Default for InMemoryUriRegister {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl UriService for InMemoryUriRegister {
    async fn register_uri(&self, uri: &str) -> Result<u64> {
        // Validate URI first
        Self::validate_uri(uri)?;

        // Try to get existing ID first (read lock)
        {
            let uri_to_id = self.uri_to_id.read().await;
            if let Some(&id) = uri_to_id.get(uri) {
                return Ok(id);
            }
        }

        // Need to create new ID (write lock)
        let mut uri_to_id = self.uri_to_id.write().await;
        let mut id_to_uri = self.id_to_uri.write().await;
        let mut next_id = self.next_id.write().await;

        // Check again in case another thread created it
        if let Some(&id) = uri_to_id.get(uri) {
            return Ok(id);
        }

        let id = *next_id;
        *next_id += 1;

        uri_to_id.insert(uri.to_string(), id);
        id_to_uri.insert(id, uri.to_string());

        Ok(id)
    }

    async fn register_uri_batch(&self, uris: &[String]) -> Result<Vec<u64>> {
        // Validate all URIs first
        for uri in uris {
            Self::validate_uri(uri)?;
        }

        let mut result = Vec::with_capacity(uris.len());

        // Try to get existing IDs first (read lock)
        {
            let uri_to_id = self.uri_to_id.read().await;
            for uri in uris {
                if let Some(&id) = uri_to_id.get(uri) {
                    result.push(Some(id));
                } else {
                    result.push(None);
                }
            }
        }

        // Find URIs that need new IDs
        let missing_indices: Vec<usize> = result
            .iter()
            .enumerate()
            .filter_map(|(idx, id)| if id.is_none() { Some(idx) } else { None })
            .collect();

        if missing_indices.is_empty() {
            return Ok(result.into_iter().map(|id| id.unwrap()).collect());
        }

        // Create new IDs (write lock)
        let mut uri_to_id = self.uri_to_id.write().await;
        let mut id_to_uri = self.id_to_uri.write().await;
        let mut next_id = self.next_id.write().await;

        for idx in missing_indices {
            let uri = &uris[idx];

            // Check again in case another thread created it
            if let Some(&id) = uri_to_id.get(uri) {
                result[idx] = Some(id);
                continue;
            }

            let id = *next_id;
            *next_id += 1;

            uri_to_id.insert(uri.clone(), id);
            id_to_uri.insert(id, uri.clone());
            result[idx] = Some(id);
        }

        Ok(result.into_iter().map(|id| id.unwrap()).collect())
    }

    async fn register_uri_batch_hashmap(&self, uris: &[String]) -> Result<HashMap<String, u64>> {
        // Validate all URIs first
        for uri in uris {
            Self::validate_uri(uri)?;
        }

        let mut result = HashMap::new();

        // Deduplicate input URIs
        let unique_uris: std::collections::HashSet<_> = uris.iter().collect();

        // Try to get existing IDs first (read lock)
        {
            let uri_to_id = self.uri_to_id.read().await;
            for uri in &unique_uris {
                if let Some(&id) = uri_to_id.get(*uri) {
                    result.insert((*uri).clone(), id);
                }
            }
        }

        // Find URIs that need new IDs
        let missing_uris: Vec<String> = unique_uris
            .iter()
            .filter(|uri| !result.contains_key(**uri))
            .map(|s| (*s).clone())
            .collect();

        if missing_uris.is_empty() {
            return Ok(result);
        }

        // Create new IDs (write lock)
        let mut uri_to_id = self.uri_to_id.write().await;
        let mut id_to_uri = self.id_to_uri.write().await;
        let mut next_id = self.next_id.write().await;

        for uri in missing_uris {
            // Check again in case another thread created it
            if let Some(&id) = uri_to_id.get(&uri) {
                result.insert(uri, id);
                continue;
            }

            let id = *next_id;
            *next_id += 1;

            uri_to_id.insert(uri.clone(), id);
            id_to_uri.insert(id, uri.clone());
            result.insert(uri, id);
        }

        Ok(result)
    }
}

// Note: Clone is needed for concurrent tests
impl Clone for InMemoryUriRegister {
    fn clone(&self) -> Self {
        Self {
            uri_to_id: Arc::clone(&self.uri_to_id),
            id_to_uri: Arc::clone(&self.id_to_uri),
            next_id: Arc::clone(&self.next_id),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_register_uri() {
        let register = InMemoryUriRegister::new();

        let id1 = register.register_uri("http://example.org/1").await.unwrap();
        let id2 = register.register_uri("http://example.org/2").await.unwrap();
        let id1_again = register.register_uri("http://example.org/1").await.unwrap();

        assert_eq!(id1, id1_again, "Same URI should return same ID");
        assert_ne!(id1, id2, "Different URIs should have different IDs");
    }

    #[tokio::test]
    async fn test_register_uri_batch() {
        let register = InMemoryUriRegister::new();

        let uris = vec![
            "http://example.org/1".to_string(),
            "http://example.org/2".to_string(),
            "http://example.org/3".to_string(),
        ];

        let ids = register.register_uri_batch(&uris).await.unwrap();

        assert_eq!(ids.len(), 3, "Should return 3 IDs");

        // Verify all IDs are unique
        let unique_ids: std::collections::HashSet<_> = ids.iter().copied().collect();
        assert_eq!(unique_ids.len(), 3, "All IDs should be unique");
    }

    #[tokio::test]
    async fn test_register_uri_batch_order_preservation() {
        let register = InMemoryUriRegister::new();

        let uris = vec![
            "http://example.org/1".to_string(),
            "http://example.org/2".to_string(),
            "http://example.org/3".to_string(),
        ];

        let ids = register.register_uri_batch(&uris).await.unwrap();

        // Register same URIs individually and verify order
        for (i, uri) in uris.iter().enumerate() {
            let id = register.register_uri(uri).await.unwrap();
            assert_eq!(
                ids[i], id,
                "Batch ID at index {} should match single registration",
                i
            );
        }
    }

    #[tokio::test]
    async fn test_register_uri_batch_with_duplicates() {
        let register = InMemoryUriRegister::new();

        let uris1 = vec![
            "http://example.org/1".to_string(),
            "http://example.org/2".to_string(),
        ];

        let ids1 = register.register_uri_batch(&uris1).await.unwrap();
        assert_eq!(ids1.len(), 2);

        let uris2 = vec![
            "http://example.org/2".to_string(), // Already exists
            "http://example.org/3".to_string(), // New
        ];

        let ids2 = register.register_uri_batch(&uris2).await.unwrap();
        assert_eq!(ids2.len(), 2);

        // Check that existing URI got same ID
        assert_eq!(ids1[1], ids2[0], "Existing URI should return same ID");
    }

    #[tokio::test]
    async fn test_register_uri_batch_empty() {
        let register = InMemoryUriRegister::new();

        let ids = register.register_uri_batch(&[]).await.unwrap();
        assert_eq!(ids.len(), 0, "Empty input should return empty result");
    }

    #[tokio::test]
    async fn test_concurrent_registration() {
        let register = InMemoryUriRegister::new();
        let uri = "http://example.org/concurrent";

        // Spawn multiple tasks trying to register the same URI
        let mut handles = vec![];
        for _ in 0..10 {
            let reg = register.clone();
            let handle = tokio::spawn(async move { reg.register_uri(uri).await.unwrap() });
            handles.push(handle);
        }

        // Collect all IDs
        let mut ids = vec![];
        for handle in handles {
            ids.push(handle.await.unwrap());
        }

        // All should be the same ID
        let unique_ids: std::collections::HashSet<_> = ids.into_iter().collect();
        assert_eq!(
            unique_ids.len(),
            1,
            "Concurrent registration should return same ID"
        );
    }

    #[tokio::test]
    async fn test_register_uri_batch_hashmap() {
        let register = InMemoryUriRegister::new();

        let uris = vec![
            "http://example.org/1".to_string(),
            "http://example.org/2".to_string(),
            "http://example.org/3".to_string(),
        ];

        let map = register.register_uri_batch_hashmap(&uris).await.unwrap();

        assert_eq!(map.len(), 3, "Should return 3 mappings");
        assert!(map.contains_key("http://example.org/1"));
        assert!(map.contains_key("http://example.org/2"));
        assert!(map.contains_key("http://example.org/3"));
    }

    #[tokio::test]
    async fn test_register_uri_batch_hashmap_with_duplicates() {
        let register = InMemoryUriRegister::new();

        // Input has duplicate URI
        let uris = vec![
            "http://example.org/1".to_string(),
            "http://example.org/2".to_string(),
            "http://example.org/1".to_string(), // Duplicate
        ];

        let map = register.register_uri_batch_hashmap(&uris).await.unwrap();

        // Should only have 2 entries (duplicates removed)
        assert_eq!(map.len(), 2, "Duplicates should be removed");
        assert!(map.contains_key("http://example.org/1"));
        assert!(map.contains_key("http://example.org/2"));
    }

    #[tokio::test]
    async fn test_register_uri_batch_hashmap_with_existing() {
        let register = InMemoryUriRegister::new();

        // Register some URIs first
        let uris1 = vec![
            "http://example.org/1".to_string(),
            "http://example.org/2".to_string(),
        ];
        let map1 = register.register_uri_batch_hashmap(&uris1).await.unwrap();

        // Register again with overlap
        let uris2 = vec![
            "http://example.org/2".to_string(), // Already exists
            "http://example.org/3".to_string(), // New
        ];
        let map2 = register.register_uri_batch_hashmap(&uris2).await.unwrap();

        // Existing URI should have same ID
        assert_eq!(
            map1.get("http://example.org/2"),
            map2.get("http://example.org/2")
        );
    }

    #[tokio::test]
    async fn test_register_uri_batch_hashmap_empty() {
        let register = InMemoryUriRegister::new();

        let map = register.register_uri_batch_hashmap(&[]).await.unwrap();
        assert_eq!(map.len(), 0, "Empty input should return empty map");
    }

    #[tokio::test]
    async fn test_invalid_uri_validation() {
        let register = InMemoryUriRegister::new();

        // Test various invalid URIs
        let invalid_uris = vec![
            "not a uri",
            "://missing-scheme",
            "http://",
            "",
            "just-a-string",
            "ftp://[invalid",
        ];

        for invalid_uri in invalid_uris {
            let result = register.register_uri(invalid_uri).await;
            assert!(
                result.is_err(),
                "Invalid URI '{}' should be rejected",
                invalid_uri
            );

            // Verify it's an InvalidUri error
            if let Err(e) = result {
                assert!(
                    matches!(e, crate::error::Error::InvalidUri(_)),
                    "Error should be InvalidUri, got: {:?}",
                    e
                );
            }
        }
    }

    #[tokio::test]
    async fn test_valid_uri_validation() {
        let register = InMemoryUriRegister::new();

        // Test various valid URIs
        let valid_uris = vec![
            "http://example.org",
            "https://example.org/path",
            "ftp://ftp.example.org/file.txt",
            "http://example.org:8080/path?query=value",
            "https://user:pass@example.org/path#fragment",
            "file:///path/to/file",
        ];

        for valid_uri in valid_uris {
            let result = register.register_uri(valid_uri).await;
            assert!(
                result.is_ok(),
                "Valid URI '{}' should be accepted, got error: {:?}",
                valid_uri,
                result.err()
            );
        }
    }

    #[tokio::test]
    async fn test_invalid_uri_batch_validation() {
        let register = InMemoryUriRegister::new();

        let uris = vec![
            "http://valid.org".to_string(),
            "invalid uri".to_string(), // Invalid
            "http://another-valid.org".to_string(),
        ];

        let result = register.register_uri_batch(&uris).await;
        assert!(result.is_err(), "Batch with invalid URI should fail");
    }

    #[tokio::test]
    async fn test_invalid_uri_batch_hashmap_validation() {
        let register = InMemoryUriRegister::new();

        let uris = vec![
            "http://valid.org".to_string(),
            "not-a-valid-uri".to_string(), // Invalid
        ];

        let result = register.register_uri_batch_hashmap(&uris).await;
        assert!(
            result.is_err(),
            "Batch hashmap with invalid URI should fail"
        );
    }
}