oxirs-wasm 0.2.4

WebAssembly bindings for OxiRS - Run RDF/SPARQL in the browser
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
//! # Storage Adapter
//!
//! WASM storage abstraction layer providing a key-value store with namespace
//! isolation and optional TTL (time-to-live) expiry.
//!
//! Three backend variants are defined (`InMemory`, `LocalStorage`,
//! `SessionStorage`); in a native / test context all three are simulated with
//! an in-process `HashMap`.  In a real WASM build the `LocalStorage` and
//! `SessionStorage` variants would delegate to the respective Web Storage APIs.
//!
//! ## Example
//!
//! ```rust
//! use oxirs_wasm::storage_adapter::{StorageAdapter, StorageBackend};
//!
//! let mut adapter = StorageAdapter::new(StorageBackend::InMemory, "ns");
//! adapter.set("key", b"value".to_vec(), 0).expect("set");
//! let v = adapter.get("key", 0).expect("get");
//! assert_eq!(v, b"value");
//! ```

use std::collections::HashMap;

// ─── Backend ──────────────────────────────────────────────────────────────────

/// The underlying storage mechanism.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StorageBackend {
    /// Pure in-process hash map (always available).
    InMemory,
    /// Browser `localStorage` (simulated in tests).
    LocalStorage,
    /// Browser `sessionStorage` (simulated in tests).
    SessionStorage,
}

// ─── Entry ────────────────────────────────────────────────────────────────────

/// A single stored entry.
#[derive(Debug, Clone)]
pub struct StorageEntry {
    /// The logical key (without namespace prefix).
    pub key: String,
    /// The stored bytes.
    pub value: Vec<u8>,
    /// Creation time in milliseconds since Unix epoch.
    pub created_at: u64,
    /// Optional TTL in milliseconds; `None` means the entry never expires.
    pub ttl_ms: Option<u64>,
}

impl StorageEntry {
    /// Returns the expiry time in milliseconds since Unix epoch, or `None` if
    /// the entry has no TTL.
    pub fn expires_at(&self) -> Option<u64> {
        self.ttl_ms.map(|t| self.created_at + t)
    }

    /// Returns `true` when the entry has expired relative to `now_ms`.
    pub fn is_expired(&self, now_ms: u64) -> bool {
        self.expires_at().is_some_and(|exp| now_ms > exp)
    }
}

// ─── Errors ───────────────────────────────────────────────────────────────────

/// Errors returned by [`StorageAdapter`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StorageError {
    /// The requested key was not found in this namespace.
    KeyNotFound(String),
    /// The requested namespace does not exist (future use).
    NamespaceNotFound(String),
    /// The entry exists but has expired.
    Expired(String),
    /// Serialization / deserialization failure.
    SerializeError(String),
    /// Storage quota would be exceeded.
    QuotaExceeded,
}

impl std::fmt::Display for StorageError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            StorageError::KeyNotFound(k) => write!(f, "Key not found: {k}"),
            StorageError::NamespaceNotFound(ns) => write!(f, "Namespace not found: {ns}"),
            StorageError::Expired(k) => write!(f, "Entry expired: {k}"),
            StorageError::SerializeError(msg) => write!(f, "Serialize error: {msg}"),
            StorageError::QuotaExceeded => write!(f, "Storage quota exceeded"),
        }
    }
}

impl std::error::Error for StorageError {}

// ─── Adapter ──────────────────────────────────────────────────────────────────

/// A namespaced key-value store backed by one of the [`StorageBackend`]s.
///
/// Keys stored in the adapter are logically scoped to the namespace; callers
/// use bare keys and the adapter handles prefixing internally.
pub struct StorageAdapter {
    /// The backend variant.
    backend: StorageBackend,
    /// Namespace prefix used to isolate this adapter's keys.
    namespace: String,
    /// Internal storage: bare key → entry.
    store: HashMap<String, StorageEntry>,
}

impl StorageAdapter {
    /// Create a new adapter with the given backend and namespace.
    pub fn new(backend: StorageBackend, namespace: &str) -> Self {
        Self {
            backend,
            namespace: namespace.to_string(),
            store: HashMap::new(),
        }
    }

    /// Return the namespace this adapter operates in.
    pub fn namespace(&self) -> &str {
        &self.namespace
    }

    /// Return the backend variant.
    pub fn backend(&self) -> &StorageBackend {
        &self.backend
    }

    // ── Write operations ──────────────────────────────────────────────────────

    /// Store `value` under `key` with no expiry.
    pub fn set(&mut self, key: &str, value: Vec<u8>, now_ms: u64) -> Result<(), StorageError> {
        let entry = StorageEntry {
            key: key.to_string(),
            value,
            created_at: now_ms,
            ttl_ms: None,
        };
        self.store.insert(key.to_string(), entry);
        Ok(())
    }

    /// Store `value` under `key` with an expiry of `ttl_ms` milliseconds from
    /// `now_ms`.
    pub fn set_with_ttl(
        &mut self,
        key: &str,
        value: Vec<u8>,
        now_ms: u64,
        ttl_ms: u64,
    ) -> Result<(), StorageError> {
        let entry = StorageEntry {
            key: key.to_string(),
            value,
            created_at: now_ms,
            ttl_ms: Some(ttl_ms),
        };
        self.store.insert(key.to_string(), entry);
        Ok(())
    }

    // ── Read operations ───────────────────────────────────────────────────────

    /// Retrieve the value stored under `key`.
    ///
    /// Returns [`StorageError::Expired`] if the entry's TTL has elapsed, and
    /// [`StorageError::KeyNotFound`] if the key does not exist.
    pub fn get(&self, key: &str, now_ms: u64) -> Result<Vec<u8>, StorageError> {
        match self.store.get(key) {
            None => Err(StorageError::KeyNotFound(key.to_string())),
            Some(entry) => {
                if entry.is_expired(now_ms) {
                    Err(StorageError::Expired(key.to_string()))
                } else {
                    Ok(entry.value.clone())
                }
            }
        }
    }

    /// Return a reference to the [`StorageEntry`] for `key`, or `None`.
    ///
    /// Does **not** perform expiry checking — use [`Self::get`] for normal access.
    pub fn get_entry(&self, key: &str) -> Option<&StorageEntry> {
        self.store.get(key)
    }

    // ── Delete operations ─────────────────────────────────────────────────────

    /// Delete the entry for `key`.  Returns `true` if the key existed.
    pub fn delete(&mut self, key: &str) -> bool {
        self.store.remove(key).is_some()
    }

    /// Remove all entries in this namespace.
    pub fn clear_namespace(&mut self) {
        self.store.clear();
    }

    // ── Query operations ──────────────────────────────────────────────────────

    /// Return all keys in this namespace whose bare key starts with `prefix`.
    ///
    /// The returned keys are the bare keys (without the namespace prefix).
    /// The order is unspecified.
    pub fn list_keys(&self, prefix: &str) -> Vec<String> {
        let mut keys: Vec<String> = self
            .store
            .keys()
            .filter(|k| k.starts_with(prefix))
            .cloned()
            .collect();
        keys.sort();
        keys
    }

    /// Purge all expired entries (those whose TTL has elapsed as of `now_ms`).
    ///
    /// Returns the number of entries removed.
    pub fn purge_expired(&mut self, now_ms: u64) -> usize {
        let before = self.store.len();
        self.store.retain(|_k, entry| !entry.is_expired(now_ms));
        before - self.store.len()
    }

    // ── Stats ─────────────────────────────────────────────────────────────────

    /// Return the number of entries currently stored (including expired ones
    /// that have not yet been purged).
    pub fn key_count(&self) -> usize {
        self.store.len()
    }

    /// Return the total size in bytes of all stored values (including expired
    /// entries that have not been purged).
    pub fn storage_size_bytes(&self) -> usize {
        self.store.values().map(|e| e.value.len()).sum()
    }
}

// ─── Tests ────────────────────────────────────────────────────────────────────

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

    fn mem_adapter(ns: &str) -> StorageAdapter {
        StorageAdapter::new(StorageBackend::InMemory, ns)
    }

    // ── StorageEntry ──────────────────────────────────────────────────────────

    #[test]
    fn test_entry_no_ttl_never_expires() {
        let e = StorageEntry {
            key: "k".to_string(),
            value: b"v".to_vec(),
            created_at: 0,
            ttl_ms: None,
        };
        assert!(!e.is_expired(u64::MAX));
        assert!(e.expires_at().is_none());
    }

    #[test]
    fn test_entry_expires_at_correct_time() {
        let e = StorageEntry {
            key: "k".to_string(),
            value: b"v".to_vec(),
            created_at: 1_000,
            ttl_ms: Some(500),
        };
        assert_eq!(e.expires_at(), Some(1_500));
        assert!(!e.is_expired(1_499));
        assert!(!e.is_expired(1_500)); // exact boundary — not yet expired
        assert!(e.is_expired(1_501));
    }

    #[test]
    fn test_entry_clone() {
        let e = StorageEntry {
            key: "x".to_string(),
            value: vec![1, 2, 3],
            created_at: 10,
            ttl_ms: Some(20),
        };
        let c = e.clone();
        assert_eq!(c.key, e.key);
        assert_eq!(c.value, e.value);
    }

    // ── StorageError ──────────────────────────────────────────────────────────

    #[test]
    fn test_error_key_not_found_display() {
        let e = StorageError::KeyNotFound("foo".to_string());
        assert!(e.to_string().contains("foo"));
    }

    #[test]
    fn test_error_expired_display() {
        let e = StorageError::Expired("bar".to_string());
        assert!(e.to_string().contains("bar"));
    }

    #[test]
    fn test_error_quota_exceeded_display() {
        let e = StorageError::QuotaExceeded;
        assert!(e.to_string().contains("quota"));
    }

    #[test]
    fn test_error_serialize_display() {
        let e = StorageError::SerializeError("bad bytes".to_string());
        assert!(e.to_string().contains("bad bytes"));
    }

    // ── set / get ─────────────────────────────────────────────────────────────

    #[test]
    fn test_set_and_get() {
        let mut a = mem_adapter("ns");
        a.set("hello", b"world".to_vec(), 0).expect("set");
        let v = a.get("hello", 0).expect("get");
        assert_eq!(v, b"world");
    }

    #[test]
    fn test_get_missing_key_returns_not_found() {
        let a = mem_adapter("ns");
        let err = a.get("missing", 0).expect_err("should fail");
        assert_eq!(err, StorageError::KeyNotFound("missing".to_string()));
    }

    #[test]
    fn test_set_overwrites_existing() {
        let mut a = mem_adapter("ns");
        a.set("k", b"v1".to_vec(), 0).expect("first set");
        a.set("k", b"v2".to_vec(), 1).expect("second set");
        let v = a.get("k", 2).expect("get");
        assert_eq!(v, b"v2");
    }

    #[test]
    fn test_set_multiple_keys() {
        let mut a = mem_adapter("ns");
        a.set("a", b"1".to_vec(), 0).expect("a");
        a.set("b", b"2".to_vec(), 0).expect("b");
        a.set("c", b"3".to_vec(), 0).expect("c");
        assert_eq!(a.key_count(), 3);
    }

    // ── set_with_ttl ──────────────────────────────────────────────────────────

    #[test]
    fn test_set_with_ttl_before_expiry() {
        let mut a = mem_adapter("ns");
        a.set_with_ttl("k", b"v".to_vec(), 0, 1_000)
            .expect("set_with_ttl");
        let v = a.get("k", 999).expect("get before expiry");
        assert_eq!(v, b"v");
    }

    #[test]
    fn test_set_with_ttl_at_boundary_not_expired() {
        let mut a = mem_adapter("ns");
        a.set_with_ttl("k", b"v".to_vec(), 0, 1_000).expect("set");
        let v = a.get("k", 1_000).expect("get at boundary");
        assert_eq!(v, b"v");
    }

    #[test]
    fn test_set_with_ttl_after_expiry() {
        let mut a = mem_adapter("ns");
        a.set_with_ttl("k", b"v".to_vec(), 0, 500).expect("set");
        let err = a.get("k", 501).expect_err("should be expired");
        assert_eq!(err, StorageError::Expired("k".to_string()));
    }

    #[test]
    fn test_set_with_ttl_zero_expires_immediately() {
        let mut a = mem_adapter("ns");
        a.set_with_ttl("k", b"v".to_vec(), 0, 0).expect("set");
        // created_at=0, ttl=0 → expires_at=0 → now=1 is after → expired
        let err = a.get("k", 1).expect_err("zero-ttl should expire");
        assert_eq!(err, StorageError::Expired("k".to_string()));
    }

    // ── delete ────────────────────────────────────────────────────────────────

    #[test]
    fn test_delete_existing_key_returns_true() {
        let mut a = mem_adapter("ns");
        a.set("k", b"v".to_vec(), 0).expect("set");
        assert!(a.delete("k"));
    }

    #[test]
    fn test_delete_missing_key_returns_false() {
        let mut a = mem_adapter("ns");
        assert!(!a.delete("nope"));
    }

    #[test]
    fn test_delete_removes_key_from_store() {
        let mut a = mem_adapter("ns");
        a.set("k", b"v".to_vec(), 0).expect("set");
        a.delete("k");
        let err = a.get("k", 0).expect_err("key should be gone");
        assert_eq!(err, StorageError::KeyNotFound("k".to_string()));
    }

    // ── clear_namespace ───────────────────────────────────────────────────────

    #[test]
    fn test_clear_namespace_removes_all_keys() {
        let mut a = mem_adapter("ns");
        a.set("a", b"1".to_vec(), 0).expect("a");
        a.set("b", b"2".to_vec(), 0).expect("b");
        a.clear_namespace();
        assert_eq!(a.key_count(), 0);
    }

    #[test]
    fn test_clear_namespace_idempotent_on_empty() {
        let mut a = mem_adapter("ns");
        a.clear_namespace();
        a.clear_namespace(); // second call should not panic
        assert_eq!(a.key_count(), 0);
    }

    // ── list_keys ─────────────────────────────────────────────────────────────

    #[test]
    fn test_list_keys_empty_prefix_returns_all() {
        let mut a = mem_adapter("ns");
        a.set("foo", b"1".to_vec(), 0).expect("foo");
        a.set("bar", b"2".to_vec(), 0).expect("bar");
        let keys = a.list_keys("");
        assert_eq!(keys.len(), 2);
        // sorted
        assert_eq!(keys[0], "bar");
        assert_eq!(keys[1], "foo");
    }

    #[test]
    fn test_list_keys_prefix_filter() {
        let mut a = mem_adapter("ns");
        a.set("user:alice", b"1".to_vec(), 0).expect("a");
        a.set("user:bob", b"2".to_vec(), 0).expect("b");
        a.set("config:x", b"3".to_vec(), 0).expect("c");
        let keys = a.list_keys("user:");
        assert_eq!(keys.len(), 2);
        assert!(keys.contains(&"user:alice".to_string()));
        assert!(keys.contains(&"user:bob".to_string()));
    }

    #[test]
    fn test_list_keys_no_match_returns_empty() {
        let mut a = mem_adapter("ns");
        a.set("alpha", b"1".to_vec(), 0).expect("set");
        let keys = a.list_keys("beta");
        assert!(keys.is_empty());
    }

    #[test]
    fn test_list_keys_sorted_alphabetically() {
        let mut a = mem_adapter("ns");
        for ch in ['z', 'a', 'm', 'b'] {
            a.set(&ch.to_string(), b"v".to_vec(), 0).expect("set");
        }
        let keys = a.list_keys("");
        assert_eq!(keys, vec!["a", "b", "m", "z"]);
    }

    // ── purge_expired ─────────────────────────────────────────────────────────

    #[test]
    fn test_purge_expired_removes_only_stale_entries() {
        let mut a = mem_adapter("ns");
        a.set_with_ttl("stale", b"s".to_vec(), 0, 100)
            .expect("stale");
        a.set("fresh", b"f".to_vec(), 0).expect("fresh");
        let removed = a.purge_expired(200);
        assert_eq!(removed, 1);
        assert_eq!(a.key_count(), 1);
        assert!(a.get("fresh", 200).is_ok());
    }

    #[test]
    fn test_purge_expired_returns_zero_when_none_expired() {
        let mut a = mem_adapter("ns");
        a.set("a", b"1".to_vec(), 0).expect("a");
        a.set("b", b"2".to_vec(), 0).expect("b");
        let removed = a.purge_expired(999);
        assert_eq!(removed, 0);
        assert_eq!(a.key_count(), 2);
    }

    #[test]
    fn test_purge_expired_removes_all_when_all_stale() {
        let mut a = mem_adapter("ns");
        a.set_with_ttl("x", b"1".to_vec(), 0, 10).expect("x");
        a.set_with_ttl("y", b"2".to_vec(), 0, 10).expect("y");
        let removed = a.purge_expired(100);
        assert_eq!(removed, 2);
        assert_eq!(a.key_count(), 0);
    }

    #[test]
    fn test_purge_expired_idempotent() {
        let mut a = mem_adapter("ns");
        a.set_with_ttl("k", b"v".to_vec(), 0, 5).expect("set");
        a.purge_expired(100);
        let removed2 = a.purge_expired(200);
        assert_eq!(removed2, 0);
    }

    // ── key_count ─────────────────────────────────────────────────────────────

    #[test]
    fn test_key_count_empty() {
        let a = mem_adapter("ns");
        assert_eq!(a.key_count(), 0);
    }

    #[test]
    fn test_key_count_after_operations() {
        let mut a = mem_adapter("ns");
        a.set("a", b"1".to_vec(), 0).expect("a");
        a.set("b", b"2".to_vec(), 0).expect("b");
        assert_eq!(a.key_count(), 2);
        a.delete("a");
        assert_eq!(a.key_count(), 1);
    }

    // ── storage_size_bytes ────────────────────────────────────────────────────

    #[test]
    fn test_storage_size_bytes_empty() {
        let a = mem_adapter("ns");
        assert_eq!(a.storage_size_bytes(), 0);
    }

    #[test]
    fn test_storage_size_bytes_accumulates() {
        let mut a = mem_adapter("ns");
        a.set("a", vec![0u8; 10], 0).expect("a");
        a.set("b", vec![0u8; 20], 0).expect("b");
        assert_eq!(a.storage_size_bytes(), 30);
    }

    #[test]
    fn test_storage_size_bytes_decreases_after_delete() {
        let mut a = mem_adapter("ns");
        a.set("a", vec![0u8; 10], 0).expect("a");
        a.set("b", vec![0u8; 5], 0).expect("b");
        a.delete("a");
        assert_eq!(a.storage_size_bytes(), 5);
    }

    // ── namespace isolation ───────────────────────────────────────────────────

    #[test]
    fn test_namespace_isolation_same_key_different_adapters() {
        let mut a1 = StorageAdapter::new(StorageBackend::InMemory, "ns1");
        let mut a2 = StorageAdapter::new(StorageBackend::InMemory, "ns2");

        a1.set("key", b"val1".to_vec(), 0).expect("a1 set");
        a2.set("key", b"val2".to_vec(), 0).expect("a2 set");

        assert_eq!(a1.get("key", 0).expect("a1 get"), b"val1");
        assert_eq!(a2.get("key", 0).expect("a2 get"), b"val2");

        a1.delete("key");
        // a2 is unaffected
        assert_eq!(a2.get("key", 0).expect("a2 still has key"), b"val2");
    }

    #[test]
    fn test_clear_namespace_does_not_affect_other_adapter() {
        let mut a1 = StorageAdapter::new(StorageBackend::InMemory, "ns1");
        let mut a2 = StorageAdapter::new(StorageBackend::InMemory, "ns2");

        a1.set("x", b"1".to_vec(), 0).expect("a1");
        a2.set("x", b"2".to_vec(), 0).expect("a2");

        a1.clear_namespace();
        assert_eq!(a1.key_count(), 0);
        assert_eq!(a2.key_count(), 1);
    }

    // ── backend variants ──────────────────────────────────────────────────────

    #[test]
    fn test_backend_local_storage_simulated() {
        let mut a = StorageAdapter::new(StorageBackend::LocalStorage, "ns");
        a.set("k", b"v".to_vec(), 0).expect("set");
        assert_eq!(a.get("k", 0).expect("get"), b"v");
    }

    #[test]
    fn test_backend_session_storage_simulated() {
        let mut a = StorageAdapter::new(StorageBackend::SessionStorage, "ns");
        a.set("k", b"v".to_vec(), 0).expect("set");
        assert_eq!(a.get("k", 0).expect("get"), b"v");
    }

    #[test]
    fn test_backend_accessor() {
        let a = StorageAdapter::new(StorageBackend::InMemory, "ns");
        assert_eq!(a.backend(), &StorageBackend::InMemory);
    }

    #[test]
    fn test_namespace_accessor() {
        let a = StorageAdapter::new(StorageBackend::InMemory, "myns");
        assert_eq!(a.namespace(), "myns");
    }

    // ── get_entry ─────────────────────────────────────────────────────────────

    #[test]
    fn test_get_entry_returns_none_for_missing() {
        let a = mem_adapter("ns");
        assert!(a.get_entry("missing").is_none());
    }

    #[test]
    fn test_get_entry_returns_entry_with_ttl() {
        let mut a = mem_adapter("ns");
        a.set_with_ttl("k", b"v".to_vec(), 100, 200).expect("set");
        let entry = a.get_entry("k").expect("entry should exist");
        assert_eq!(entry.created_at, 100);
        assert_eq!(entry.ttl_ms, Some(200));
        assert_eq!(entry.expires_at(), Some(300));
    }
}