rong_storage 0.1.1

Storage module for RongJS
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
use super::*;
use redb::{Database, ReadableDatabase, ReadableTable};
use rong::{
    FromJSObj, HostError, IntoJSIteratorExt, JSContext, JSDate, JSObject, JSResult, JSValue,
    JsonToJSValue, function::Optional, js_class, js_export, js_method,
};
use std::cell::RefCell;
use std::fs;
use std::path::PathBuf;
use std::rc::Rc;

#[derive(FromJSObj, Default)]
pub struct StorageOptionsInput {
    #[rename = "maxKeySize"]
    max_key_size: Option<u32>,
    #[rename = "maxValueSize"]
    max_value_size: Option<u32>,
    #[rename = "maxDataSize"]
    max_data_size: Option<u32>,
}

#[derive(Clone, Debug, Default)]
pub struct StorageOptions {
    pub max_key_size: Option<u32>,
    pub max_value_size: Option<u32>,
    pub max_data_size: Option<u32>,
}

impl From<StorageOptionsInput> for StorageOptions {
    fn from(input: StorageOptionsInput) -> Self {
        Self {
            max_key_size: input.max_key_size,
            max_value_size: input.max_value_size,
            max_data_size: input.max_data_size,
        }
    }
}

#[derive(Clone, Debug)]
pub(crate) struct StorageConfig {
    max_key_size: usize,
    max_value_size: usize,
    max_user_data_size: usize,
}

impl StorageConfig {
    fn from_options(options: StorageOptions) -> JSResult<Self> {
        Ok(Self {
            max_key_size: Self::validate_limit(
                "maxKeySize",
                options.max_key_size,
                DEFAULT_MAX_KEY_SIZE,
            )?,
            max_value_size: Self::validate_limit(
                "maxValueSize",
                options.max_value_size,
                DEFAULT_MAX_VALUE_SIZE,
            )?,
            max_user_data_size: Self::validate_limit(
                "maxDataSize",
                options.max_data_size,
                DEFAULT_MAX_USER_DATA_SIZE,
            )?,
        })
    }

    fn validate_limit(field: &str, value: Option<u32>, default_value: usize) -> JSResult<usize> {
        let value = value.map(|v| v as usize).unwrap_or(default_value);
        if value == 0 {
            return Err(HostError::new(
                rong::error::E_INVALID_ARG,
                format!("{field} must be greater than 0"),
            )
            .with_name("TypeError")
            .into());
        }
        Ok(value)
    }

    fn limit_size_u32(&self) -> u32 {
        self.max_user_data_size.min(u32::MAX as usize) as u32
    }
}

impl Default for StorageConfig {
    fn default() -> Self {
        Self {
            max_key_size: DEFAULT_MAX_KEY_SIZE,
            max_value_size: DEFAULT_MAX_VALUE_SIZE,
            max_user_data_size: DEFAULT_MAX_USER_DATA_SIZE,
        }
    }
}

#[js_export]
pub struct Storage {
    db: Rc<RefCell<Option<Database>>>,
    #[allow(dead_code)]
    db_path: PathBuf,
    config: StorageConfig,
}

impl Storage {
    #[cfg_attr(not(test), allow(dead_code))]
    pub fn open<P: Into<PathBuf>>(path: P) -> JSResult<Self> {
        Self::open_with_options(path, StorageOptions::default())
    }

    pub fn open_with_options<P: Into<PathBuf>>(path: P, options: StorageOptions) -> JSResult<Self> {
        Self::open_with_path(path.into(), options)
    }

    pub(crate) fn open_with_path(path: PathBuf, options: StorageOptions) -> JSResult<Self> {
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent).map_err(|e| {
                HostError::new(
                    rong::error::E_IO,
                    format!("Failed to create directory {:?}: {}", parent, e),
                )
            })?;
        }

        let db = Database::create(&path).map_err(|e| {
            HostError::new(
                rong::error::E_IO,
                format!("Failed to open database at {:?}: {}", path, e),
            )
        })?;

        Self::ensure_storage_table(&db)?;

        let config = StorageConfig::from_options(options)?;

        Ok(Self {
            db: Rc::new(RefCell::new(Some(db))),
            db_path: path,
            config,
        })
    }

    /// Close the underlying database for this Storage instance.
    ///
    /// This will drop the shared Database handle for all clones that share
    /// the same internal Rc. After calling close, any subsequent operation
    /// on this Storage (or its clones) will fail with a TypeError.
    pub fn close(&self) {
        let mut db_opt = self.db.borrow_mut();
        if db_opt.is_some() {
            *db_opt = None;
        }
    }

    fn with_db<F, T>(&self, f: F) -> JSResult<T>
    where
        F: FnOnce(&Database) -> JSResult<T>,
    {
        let db_opt = self.db.borrow();
        let db = db_opt.as_ref().ok_or_else(|| {
            HostError::new(rong::error::E_INVALID_STATE, "Storage database is closed")
                .with_name("TypeError")
        })?;
        f(db)
    }

    fn ensure_storage_table(db: &Database) -> JSResult<()> {
        let write_txn = db.begin_write().map_err(|e| {
            HostError::new(
                rong::error::E_IO,
                format!("Failed to begin write transaction: {}", e),
            )
        })?;

        {
            // This will create the table if it doesn't exist, or do nothing if it does
            write_txn.open_table(STORAGE_TABLE).map_err(|e| {
                HostError::new(
                    rong::error::E_IO,
                    format!("Failed to create/open storage table: {}", e),
                )
            })?;
        }

        write_txn.commit().map_err(|e| {
            HostError::new(
                rong::error::E_IO,
                format!("Failed to commit table creation: {}", e),
            )
        })?;

        Ok(())
    }
}

#[js_class]
impl Storage {
    #[js_method(constructor)]
    pub fn new(path: String, options: Optional<StorageOptionsInput>) -> JSResult<Self> {
        let opts = options.0.map(StorageOptions::from).unwrap_or_default();
        Self::open_with_options(PathBuf::from(path), opts)
    }

    /// Set a key-value pair in storage
    #[js_method]
    pub async fn set(&self, key: String, value: JSValue) -> JSResult<()> {
        let cfg = &self.config;

        // Validate key size
        if key.len() > cfg.max_key_size {
            return Err(HostError::new(
                rong::error::E_OUT_OF_RANGE,
                format!(
                    "Key size exceeds maximum limit of {} bytes",
                    cfg.max_key_size
                ),
            )
            .with_name("RangeError")
            .into());
        }

        // Convert value to JSON string to preserve type information
        let value_str = if value.is_string() {
            // For strings, store as JSON string to preserve type
            let s: String = value.clone().try_into().map_err(|_| {
                HostError::new(rong::error::E_INVALID_ARG, "Failed to convert string value")
                    .with_name("TypeError")
            })?;
            serde_json::to_string(&s).map_err(|e| {
                HostError::new(
                    rong::error::E_INTERNAL,
                    format!("Failed to serialize string: {}", e),
                )
            })?
        } else if value.is_number() {
            // First get as f64 to avoid truncation issues
            let f: f64 = value.clone().try_into().map_err(|_| {
                HostError::new(rong::error::E_INVALID_ARG, "Failed to convert number value")
                    .with_name("TypeError")
            })?;

            // Check if it's actually an integer (no fractional part)
            if f.fract() == 0.0 {
                // It's an integer, try to fit in appropriate integer type
                if f >= i32::MIN as f64 && f <= i32::MAX as f64 {
                    // Fits in i32
                    serde_json::to_string(&(f as i32)).map_err(|e| {
                        HostError::new(
                            rong::error::E_INTERNAL,
                            format!("Failed to serialize i32: {}", e),
                        )
                    })?
                } else if f >= 0.0 && f <= u32::MAX as f64 {
                    // Fits in u32
                    serde_json::to_string(&(f as u32)).map_err(|e| {
                        HostError::new(
                            rong::error::E_INTERNAL,
                            format!("Failed to serialize u32: {}", e),
                        )
                    })?
                } else {
                    // Large integer, store as f64
                    serde_json::to_string(&f).map_err(|e| {
                        HostError::new(
                            rong::error::E_INTERNAL,
                            format!("Failed to serialize large integer as f64: {}", e),
                        )
                    })?
                }
            } else {
                // It's a floating point number
                serde_json::to_string(&f).map_err(|e| {
                    HostError::new(
                        rong::error::E_INTERNAL,
                        format!("Failed to serialize f64: {}", e),
                    )
                })?
            }
        } else if value.is_bigint() {
            // Handle BigInt values (i64/u64)
            if let Ok(i) = value.clone().try_into::<i64>() {
                serde_json::to_string(&i).map_err(|e| {
                    HostError::new(
                        rong::error::E_INTERNAL,
                        format!("Failed to serialize bigint i64: {}", e),
                    )
                })?
            } else if let Ok(u) = value.clone().try_into::<u64>() {
                serde_json::to_string(&u).map_err(|e| {
                    HostError::new(
                        rong::error::E_INTERNAL,
                        format!("Failed to serialize bigint u64: {}", e),
                    )
                })?
            } else {
                return Err(
                    HostError::new(rong::error::E_INVALID_ARG, "Invalid bigint value")
                        .with_name("TypeError")
                        .into(),
                );
            }
        } else if value.is_boolean() {
            let b: bool = value.clone().try_into().map_err(|_| {
                HostError::new(
                    rong::error::E_INVALID_ARG,
                    "Failed to convert boolean value",
                )
                .with_name("TypeError")
            })?;
            serde_json::to_string(&b).map_err(|e| {
                HostError::new(
                    rong::error::E_INTERNAL,
                    format!("Failed to serialize boolean: {}", e),
                )
            })?
        } else if value.is_null() {
            "null".to_string()
        } else if value.is_undefined() {
            return Err(HostError::new(
                rong::error::E_INVALID_ARG,
                "Cannot store undefined values",
            )
            .with_name("TypeError")
            .into());
        } else if let Ok(date) = value.clone().try_into::<JSDate>() {
            // Handle Date objects by storing timestamp with type marker
            let timestamp = date.get_time().map_err(|e| {
                HostError::new(
                    rong::error::E_INVALID_ARG,
                    format!("Failed to get Date timestamp: {}", e),
                )
                .with_name("TypeError")
            })?;
            serde_json::to_string(&serde_json::json!({
                "__type": "Date",
                "timestamp": timestamp
            }))
            .map_err(|e| {
                HostError::new(
                    rong::error::E_INTERNAL,
                    format!("Failed to serialize Date: {}", e),
                )
            })?
        } else if let Ok(obj) = value.clone().try_into::<JSObject>() {
            // Handle objects by converting to JSON string
            obj.json_stringify().map_err(|e| {
                HostError::new(
                    rong::error::E_INVALID_ARG,
                    format!("Failed to stringify object: {}", e),
                )
                .with_name("TypeError")
            })?
        } else if let Ok(s) = value.clone().try_into::<String>() {
            // Fallback: convert to string
            serde_json::to_string(&s).map_err(|e| {
                HostError::new(
                    rong::error::E_INTERNAL,
                    format!("Failed to serialize fallback string: {}", e),
                )
            })?
        } else {
            return Err(HostError::new(
                rong::error::E_INVALID_ARG,
                "Value cannot be converted to a storable type",
            )
            .with_name("TypeError")
            .into());
        };

        // Validate value size
        if value_str.len() > cfg.max_value_size {
            return Err(HostError::new(
                rong::error::E_OUT_OF_RANGE,
                format!(
                    "Value size exceeds maximum limit of {} bytes",
                    cfg.max_value_size
                ),
            )
            .with_name("RangeError")
            .into());
        }

        self.with_db(|db| {
            // Check total storage size before adding new data
            let read_txn = db.begin_read().map_err(|e| {
                HostError::new(
                    rong::error::E_IO,
                    format!("Failed to begin read transaction: {}", e),
                )
            })?;

            let table = read_txn
                .open_table(STORAGE_TABLE)
                .map_err(|e| {
                    HostError::new(rong::error::E_IO, format!("Failed to open table: {}", e))
                })?;

            let mut current_size = 0;
            let mut existing_key_size = 0;

            // Calculate current storage size and check if key already exists
            let iter = table
                .iter()
                .map_err(|e| {
                    HostError::new(rong::error::E_IO, format!("Failed to create iterator: {}", e))
                })?;

            for item in iter {
                let (existing_key, existing_value) = item
                    .map_err(|e| {
                        HostError::new(rong::error::E_IO, format!("Failed to read item: {}", e))
                    })?;

                let key_size = existing_key.value().len();
                let value_size = existing_value.value().len();

                if existing_key.value().as_bytes() == key.as_bytes() {
                    existing_key_size = key_size + value_size;
                }
                current_size += key_size + value_size;
            }

            drop(table);
            drop(read_txn);

            // Calculate new size after this operation
            let new_entry_size = key.len() + value_str.len();
            let new_total_size = current_size - existing_key_size + new_entry_size;

            if new_total_size > cfg.max_user_data_size {
                return Err(HostError::new(rong::error::E_OUT_OF_RANGE, format!(
                    "Storage size would exceed maximum limit of {} bytes (current: {}, new entry: {})",
                    cfg.max_user_data_size,
                    current_size - existing_key_size,
                    new_entry_size
                )).with_name("RangeError").into());
            }

            // Store in database
            let write_txn = db.begin_write().map_err(|e| {
                HostError::new(
                    rong::error::E_IO,
                    format!("Failed to begin write transaction: {}", e),
                )
            })?;

            {
                let mut table = write_txn
                    .open_table(STORAGE_TABLE)
                    .map_err(|e| {
                        HostError::new(rong::error::E_IO, format!("Failed to open table: {}", e))
                    })?;

                table
                    .insert(key.as_str(), value_str.as_bytes())
                    .map_err(|e| {
                        HostError::new(rong::error::E_IO, format!("Failed to insert value: {}", e))
                    })?;
            }

            write_txn.commit().map_err(|e| {
                HostError::new(
                    rong::error::E_IO,
                    format!("Failed to commit transaction: {}", e),
                )
            })?;

            Ok(())
        })
    }

    /// Get a value from storage
    #[js_method]
    pub async fn get(&self, ctx: JSContext, key: String) -> JSResult<JSValue> {
        self.with_db(|db| {
            let read_txn = db.begin_read().map_err(|e| {
                HostError::new(
                    rong::error::E_IO,
                    format!("Failed to begin read transaction: {}", e),
                )
            })?;

            let table = read_txn.open_table(STORAGE_TABLE).map_err(|e| {
                HostError::new(rong::error::E_IO, format!("Failed to open table: {}", e))
            })?;

            match table.get(key.as_str()) {
                Ok(Some(value)) => {
                    let value_str = String::from_utf8(value.value().to_vec()).map_err(|e| {
                        HostError::new(
                            rong::error::E_INVALID_DATA,
                            format!("Failed to decode value as UTF-8: {}", e),
                        )
                    })?;

                    // Parse JSON back to appropriate JavaScript type
                    match serde_json::from_str::<serde_json::Value>(&value_str) {
                        Ok(json_value) => {
                            match json_value {
                                serde_json::Value::String(s) => Ok(JSValue::from(&ctx, s)),
                                serde_json::Value::Number(n) => {
                                    // Let JSValue::from handle the intelligent number conversion
                                    if let Some(i) = n.as_i64() {
                                        Ok(JSValue::from(&ctx, i))
                                    } else if let Some(u) = n.as_u64() {
                                        Ok(JSValue::from(&ctx, u))
                                    } else if let Some(f) = n.as_f64() {
                                        Ok(JSValue::from(&ctx, f))
                                    } else {
                                        Ok(JSValue::from(&ctx, value_str))
                                    }
                                }
                                serde_json::Value::Bool(b) => Ok(JSValue::from(&ctx, b)),
                                serde_json::Value::Null => Ok(JSValue::null(&ctx)),
                                serde_json::Value::Object(ref obj) => {
                                    // Check if this is a Date object
                                    if obj.get("__type")
                                        == Some(&serde_json::Value::String("Date".to_string()))
                                    {
                                        if let Some(timestamp) =
                                            obj.get("timestamp").and_then(|v| v.as_f64())
                                        {
                                            let date = JSDate::new(&ctx, timestamp);
                                            Ok(date.into_js_value())
                                        } else {
                                            Err(HostError::new(
                                                rong::error::E_INVALID_DATA,
                                                "Invalid Date object: missing timestamp",
                                            )
                                            .into())
                                        }
                                    } else {
                                        // Regular object, parse using JavaScript's JSON.parse
                                        value_str.as_str().json_to_js_value(&ctx)
                                    }
                                }
                                serde_json::Value::Array(_) => {
                                    // For arrays, parse them back using JavaScript's JSON.parse
                                    value_str.as_str().json_to_js_value(&ctx)
                                }
                            }
                        }
                        Err(_) => {
                            // If not valid JSON, return as string
                            Ok(JSValue::from(&ctx, value_str))
                        }
                    }
                }
                Ok(None) => Ok(JSValue::undefined(&ctx)),
                Err(e) => Err(HostError::new(
                    rong::error::E_IO,
                    format!("Failed to get value: {}", e),
                )
                .into()),
            }
        })
    }

    /// Delete a key from storage
    #[js_method]
    pub async fn delete(&self, key: String) -> JSResult<()> {
        self.with_db(|db| {
            let write_txn = db.begin_write().map_err(|e| {
                HostError::new(
                    rong::error::E_IO,
                    format!("Failed to begin write transaction: {}", e),
                )
            })?;

            {
                let mut table = write_txn.open_table(STORAGE_TABLE).map_err(|e| {
                    HostError::new(rong::error::E_IO, format!("Failed to open table: {}", e))
                })?;

                table.remove(key.as_str()).map_err(|e| {
                    HostError::new(rong::error::E_IO, format!("Failed to remove key: {}", e))
                })?;
            }

            write_txn.commit().map_err(|e| {
                HostError::new(
                    rong::error::E_IO,
                    format!("Failed to commit transaction: {}", e),
                )
            })?;

            Ok(())
        })
    }

    /// Clear all data from storage
    #[js_method]
    pub async fn clear(&self) -> JSResult<()> {
        self.with_db(|db| {
            let write_txn = db.begin_write().map_err(|e| {
                HostError::new(
                    rong::error::E_IO,
                    format!("Failed to begin write transaction: {}", e),
                )
            })?;

            {
                let mut table = write_txn.open_table(STORAGE_TABLE).map_err(|e| {
                    HostError::new(rong::error::E_IO, format!("Failed to open table: {}", e))
                })?;

                // Remove all entries
                let keys: Vec<String> = table
                    .iter()
                    .map_err(|e| {
                        HostError::new(rong::error::E_IO, format!("Failed to iterate table: {}", e))
                    })?
                    .map(|item| {
                        item.map(|(key, _)| key.value().to_string()).map_err(|e| {
                            HostError::new(rong::error::E_IO, format!("Failed to read key: {}", e))
                        })
                    })
                    .collect::<Result<Vec<_>, _>>()?;

                for key in keys {
                    table.remove(key.as_str()).map_err(|e| {
                        HostError::new(rong::error::E_IO, format!("Failed to remove key: {}", e))
                    })?;
                }
            }

            write_txn.commit().map_err(|e| {
                HostError::new(
                    rong::error::E_IO,
                    format!("Failed to commit transaction: {}", e),
                )
            })?;

            Ok(())
        })
    }

    /// Storage list function that returns an iterator
    #[js_method]
    pub async fn list(&self, ctx: JSContext, prefix: Optional<String>) -> JSResult<JSValue> {
        self.with_db(|db| {
            let read_txn = db.begin_read().map_err(|e| {
                HostError::new(
                    rong::error::E_IO,
                    format!("Failed to begin read transaction: {}", e),
                )
            })?;

            let table = read_txn.open_table(STORAGE_TABLE).map_err(|e| {
                HostError::new(rong::error::E_IO, format!("Failed to open table: {}", e))
            })?;

            let iter = table.iter().map_err(|e| {
                HostError::new(
                    rong::error::E_IO,
                    format!("Failed to create iterator: {}", e),
                )
            })?;

            let mut keys = Vec::new();
            for item in iter {
                let (key, _) = item.map_err(|e| {
                    HostError::new(rong::error::E_IO, format!("Failed to read item: {}", e))
                })?;
                let key_str = key.value().to_string();

                // Apply prefix filter if provided
                if let Some(ref prefix_str) = prefix.0 {
                    if key_str.starts_with(prefix_str) {
                        keys.push(key_str);
                    }
                } else {
                    keys.push(key_str);
                }
            }

            // Convert to JS iterator and then to JSValue
            let iter = keys.to_js_iter(&ctx)?;
            Ok(JSValue::from(&ctx, iter))
        })
    }

    /// Storage info function
    #[js_method]
    pub async fn info(&self) -> JSResult<StorageInfo> {
        self.with_db(|db| {
            let read_txn = db.begin_read().map_err(|e| {
                HostError::new(
                    rong::error::E_IO,
                    format!("Failed to begin read transaction: {}", e),
                )
            })?;

            let table = read_txn.open_table(STORAGE_TABLE).map_err(|e| {
                HostError::new(rong::error::E_IO, format!("Failed to open table: {}", e))
            })?;

            let mut current_size = 0;
            let mut key_count = 0;
            let iter = table.iter().map_err(|e| {
                HostError::new(
                    rong::error::E_IO,
                    format!("Failed to create iterator: {}", e),
                )
            })?;

            for item in iter {
                let (key, value) = item.map_err(|e| {
                    HostError::new(rong::error::E_IO, format!("Failed to read item: {}", e))
                })?;

                current_size += key.value().len() + value.value().len();
                key_count += 1;
            }

            Ok(StorageInfo {
                current_size: current_size as u32,
                limit_size: self.config.limit_size_u32(),
                key_count: key_count as u32,
            })
        })
    }
}