rusqlite-gpkg 0.0.8

GeoPackage reader/writer built on top of rusqlite
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
//! Single-file hybrid VFS for wasm.
//!
//! - Writes to the main database file are forwarded to a user-provided writer.
//! - Writes to sidecar files (`-wal`, `-shm`, `-journal`) stay in memory.
//! - This VFS intentionally does not validate filename intent.

use crate::{Gpkg, GpkgError, Result as CrateResult};
use sqlite_wasm_rs::utils::{
    OsCallback, RegisterVfsError, SQLiteIoMethods, SQLiteVfs, SQLiteVfsFile, VfsError, VfsFile,
    VfsResult, VfsStore,
    ffi::{
        SQLITE_IOERR, SQLITE_IOERR_DELETE, SQLITE_IOERR_READ, SQLITE_IOERR_WRITE, SQLITE_OK,
        sqlite3_file, sqlite3_vfs,
    },
    register_vfs,
};
use std::cell::RefCell;
use std::collections::HashMap;
use std::io::{Seek, SeekFrom, Write};
use std::path::Path;
use std::rc::Rc;
use std::time::Duration;

trait HybridWriter: Write + Seek {}
impl<T: Write + Seek> HybridWriter for T {}

type SharedWriter = Rc<RefCell<WriterState>>;
type SharedFiles = Rc<RefCell<HashMap<String, HybridFile>>>;
type HybridAppData = RefCell<HybridState>;

struct WriterState {
    writer: Box<dyn HybridWriter>,
    /// Last known cursor position. `None` means unknown — initial state,
    /// after a writer replacement, or after a failed seek/write that may
    /// have left the cursor at an indeterminate offset.
    pos: Option<u64>,
}

impl WriterState {
    fn new(writer: Box<dyn HybridWriter>) -> Self {
        Self { writer, pos: None }
    }

    /// Write `buf` at `offset`, skipping the seek when the cursor is already
    /// there. SQLite emits long runs of contiguous page writes; each `seek`
    /// otherwise forces `BufWriter::flush_buf` plus, on OPFS, a synchronous
    /// `get_size` JS round-trip.
    fn write_at(&mut self, buf: &[u8], offset: u64) -> std::io::Result<()> {
        if self.pos != Some(offset) {
            self.pos = None;
            self.writer.seek(SeekFrom::Start(offset))?;
            self.pos = Some(offset);
        }
        match self.writer.write_all(buf) {
            Ok(()) => {
                self.pos = Some(offset + buf.len() as u64);
                Ok(())
            }
            Err(e) => {
                self.pos = None;
                Err(e)
            }
        }
    }

    fn flush(&mut self) -> std::io::Result<()> {
        self.writer.flush()
    }

    fn replace(&mut self, writer: Box<dyn HybridWriter>) {
        self.writer = writer;
        self.pos = None;
    }
}

thread_local! {
    static DEFAULT_HYBRID_VFS: RefCell<Option<HybridVfsHandle>> = const { RefCell::new(None) };
}

/// Builder that holds the writer used for main database file writes.
pub struct HybridVfsBuilder {
    writer: Box<dyn HybridWriter>,
}

#[derive(Clone)]
pub struct HybridVfsHandle {
    vfs_name: String,
    writer: SharedWriter,
    files: SharedFiles,
}

impl HybridVfsBuilder {
    /// Create a single-file hybrid VFS builder.
    pub fn new<W: Write + Seek + 'static>(writer: W) -> Self {
        Self {
            writer: Box::new(writer),
        }
    }

    /// Register the VFS with sqlite.
    pub fn register(
        self,
        vfs_name: &str,
        default_vfs: bool,
    ) -> Result<*mut sqlite3_vfs, RegisterVfsError> {
        let state = HybridState {
            files: Rc::new(RefCell::new(HashMap::new())),
            writer: Rc::new(RefCell::new(WriterState::new(self.writer))),
        };
        register_vfs::<HybridIoMethods, HybridVfsImpl>(vfs_name, RefCell::new(state), default_vfs)
    }

    /// Register a reusable Hybrid VFS and return a handle that can replace writers.
    pub fn register_reusable(
        self,
        vfs_name: &str,
        default_vfs: bool,
    ) -> Result<HybridVfsHandle, RegisterVfsError> {
        let writer: SharedWriter = Rc::new(RefCell::new(WriterState::new(self.writer)));
        let files: SharedFiles = Rc::new(RefCell::new(HashMap::new()));
        let state = HybridState {
            files: files.clone(),
            writer: writer.clone(),
        };
        register_vfs::<HybridIoMethods, HybridVfsImpl>(vfs_name, RefCell::new(state), default_vfs)?;
        Ok(HybridVfsHandle {
            vfs_name: vfs_name.to_string(),
            writer,
            files,
        })
    }

    /// Convenience helper for wasm: register/reuse a default hybrid VFS and open a GeoPackage.
    ///
    /// On first use, this registers a process-local default VFS. On subsequent calls,
    /// it reuses the same registration, replaces the writer, and clears the in-memory
    /// file map so SQLite sees a fresh database. Any `Gpkg` instances from a previous
    /// call must be dropped before calling this again.
    pub fn open_gpkg<P: AsRef<Path>>(self, sqlite_filename: P) -> CrateResult<Gpkg> {
        let writer = self.writer;
        let handle = DEFAULT_HYBRID_VFS.with(|slot| -> CrateResult<HybridVfsHandle> {
            let mut slot = slot.borrow_mut();
            if let Some(handle) = slot.as_ref() {
                handle.replace_boxed_writer(writer);
                handle.clear_files();
                return Ok(handle.clone());
            }

            let vfs = HybridVfsBuilder { writer }
                .register_reusable("hybrid-opfs-default", false)
                .map_err(|e| GpkgError::Vfs(format!("{e}")))?;
            *slot = Some(vfs.clone());
            Ok(vfs)
        })?;

        handle.open_gpkg(sqlite_filename)
    }
}

impl HybridVfsHandle {
    /// Replace the writer used for main database file writes.
    pub fn replace_writer<W: Write + Seek + 'static>(&self, writer: W) {
        self.replace_boxed_writer(Box::new(writer));
    }

    fn replace_boxed_writer(&self, writer: Box<dyn HybridWriter>) {
        self.writer.borrow_mut().replace(writer);
    }

    /// Drop every in-memory file tracked by this VFS so the next `open_gpkg`
    /// starts from an empty database. Calling this while a `Gpkg` from a prior
    /// open is still alive will leave that connection with dangling references.
    fn clear_files(&self) {
        self.files.borrow_mut().clear();
    }

    /// Open a GeoPackage using this registered Hybrid VFS.
    pub fn open_gpkg<P: AsRef<Path>>(&self, sqlite_filename: P) -> CrateResult<Gpkg> {
        Gpkg::open_with_vfs(sqlite_filename, &self.vfs_name)
    }
}

// Adapted from sqlite-wasm-rs example code:
// https://github.com/Spxg/sqlite-wasm-rs/blob/master/examples/implement-a-vfs/src/lib.rs
#[derive(Default)]
struct MemFile(Vec<u8>);

impl VfsFile for MemFile {
    fn read(&self, buf: &mut [u8], offset: usize) -> VfsResult<bool> {
        let end = offset.saturating_add(buf.len());
        if self.0.len() <= offset {
            buf.fill(0);
            return Ok(false);
        }

        let read_end = end.min(self.0.len());
        let read_size = read_end - offset;
        buf[..read_size].copy_from_slice(&self.0[offset..read_end]);
        if read_size < buf.len() {
            buf[read_size..].fill(0);
            return Ok(false);
        }
        Ok(true)
    }

    fn write(&mut self, buf: &[u8], offset: usize) -> VfsResult<()> {
        let end = offset.saturating_add(buf.len());
        if end > self.0.len() {
            self.0.resize(end, 0);
        }
        self.0[offset..end].copy_from_slice(buf);
        Ok(())
    }

    fn truncate(&mut self, size: usize) -> VfsResult<()> {
        self.0.truncate(size);
        Ok(())
    }

    fn flush(&mut self) -> VfsResult<()> {
        Ok(())
    }

    fn size(&self) -> VfsResult<usize> {
        Ok(self.0.len())
    }
}

struct MainFile {
    data: Vec<u8>,
    writer: SharedWriter,
}

impl MainFile {
    fn new(writer: SharedWriter) -> Self {
        Self {
            data: Vec::new(),
            writer,
        }
    }
}

impl VfsFile for MainFile {
    fn read(&self, buf: &mut [u8], offset: usize) -> VfsResult<bool> {
        let end = offset.saturating_add(buf.len());
        if self.data.len() <= offset {
            buf.fill(0);
            return Ok(false);
        }

        let read_end = end.min(self.data.len());
        let read_size = read_end - offset;
        buf[..read_size].copy_from_slice(&self.data[offset..read_end]);
        if read_size < buf.len() {
            buf[read_size..].fill(0);
            return Ok(false);
        }
        Ok(true)
    }

    fn write(&mut self, buf: &[u8], offset: usize) -> VfsResult<()> {
        let end = offset.saturating_add(buf.len());
        if end > self.data.len() {
            self.data.resize(end, 0);
        }
        self.data[offset..end].copy_from_slice(buf);

        self.writer
            .borrow_mut()
            .write_at(buf, offset as u64)
            .map_err(|e| VfsError::new(SQLITE_IOERR_WRITE, e.to_string()))?;
        Ok(())
    }

    fn truncate(&mut self, size: usize) -> VfsResult<()> {
        self.data.truncate(size);
        Ok(())
    }

    fn flush(&mut self) -> VfsResult<()> {
        self.writer
            .borrow_mut()
            .flush()
            .map_err(|e| VfsError::new(SQLITE_IOERR, e.to_string()))
    }

    fn size(&self) -> VfsResult<usize> {
        Ok(self.data.len())
    }
}

enum HybridFile {
    Main(MainFile),
    Mem(MemFile),
}

impl VfsFile for HybridFile {
    fn read(&self, buf: &mut [u8], offset: usize) -> VfsResult<bool> {
        match self {
            HybridFile::Main(file) => file.read(buf, offset),
            HybridFile::Mem(file) => file.read(buf, offset),
        }
    }

    fn write(&mut self, buf: &[u8], offset: usize) -> VfsResult<()> {
        match self {
            HybridFile::Main(file) => file.write(buf, offset),
            HybridFile::Mem(file) => file.write(buf, offset),
        }
    }

    fn truncate(&mut self, size: usize) -> VfsResult<()> {
        match self {
            HybridFile::Main(file) => file.truncate(size),
            HybridFile::Mem(file) => file.truncate(size),
        }
    }

    fn flush(&mut self) -> VfsResult<()> {
        match self {
            HybridFile::Main(file) => file.flush(),
            HybridFile::Mem(file) => file.flush(),
        }
    }

    fn size(&self) -> VfsResult<usize> {
        match self {
            HybridFile::Main(file) => file.size(),
            HybridFile::Mem(file) => file.size(),
        }
    }
}

struct HybridState {
    files: SharedFiles,
    writer: SharedWriter,
}

fn is_main_sqlite_file(name: &str) -> bool {
    !name.ends_with("-wal") && !name.ends_with("-shm") && !name.ends_with("-journal")
}

struct HybridStore;

impl VfsStore<HybridFile, HybridAppData> for HybridStore {
    fn add_file(vfs: *mut sqlite3_vfs, file: &str, _flags: i32) -> VfsResult<()> {
        let app_data = unsafe { Self::app_data(vfs) };
        let state = app_data.borrow();
        let item = if is_main_sqlite_file(file) {
            HybridFile::Main(MainFile::new(state.writer.clone()))
        } else {
            HybridFile::Mem(MemFile::default())
        };
        state.files.borrow_mut().insert(file.to_string(), item);
        Ok(())
    }

    fn contains_file(vfs: *mut sqlite3_vfs, file: &str) -> VfsResult<bool> {
        let app_data = unsafe { Self::app_data(vfs) };
        let state = app_data.borrow();
        Ok(state.files.borrow().contains_key(file))
    }

    fn delete_file(vfs: *mut sqlite3_vfs, file: &str) -> VfsResult<()> {
        let app_data = unsafe { Self::app_data(vfs) };
        let state = app_data.borrow();
        if state.files.borrow_mut().remove(file).is_none() {
            return Err(VfsError::new(
                SQLITE_IOERR_DELETE,
                format!("{file} not found"),
            ));
        }
        Ok(())
    }

    fn with_file<F: Fn(&HybridFile) -> VfsResult<i32>>(
        vfs_file: &SQLiteVfsFile,
        f: F,
    ) -> VfsResult<i32> {
        let app_data = unsafe { Self::app_data(vfs_file.vfs) };
        let state = app_data.borrow();
        let files = state.files.borrow();
        let name = unsafe { vfs_file.name() };
        match files.get(name) {
            Some(file) => f(file),
            None => Err(VfsError::new(
                SQLITE_IOERR_READ,
                format!("{name} not found"),
            )),
        }
    }

    fn with_file_mut<F: Fn(&mut HybridFile) -> VfsResult<i32>>(
        vfs_file: &SQLiteVfsFile,
        f: F,
    ) -> VfsResult<i32> {
        let app_data = unsafe { Self::app_data(vfs_file.vfs) };
        let state = app_data.borrow();
        let mut files = state.files.borrow_mut();
        let name = unsafe { vfs_file.name() };
        match files.get_mut(name) {
            Some(file) => f(file),
            None => Err(VfsError::new(
                SQLITE_IOERR_WRITE,
                format!("{name} not found"),
            )),
        }
    }
}

struct HybridIoMethods;

impl SQLiteIoMethods for HybridIoMethods {
    type File = HybridFile;
    type AppData = HybridAppData;
    type Store = HybridStore;

    const VERSION: ::std::os::raw::c_int = 1;

    unsafe extern "C" fn xCheckReservedLock(
        _p_file: *mut sqlite3_file,
        p_res_out: *mut ::std::os::raw::c_int,
    ) -> ::std::os::raw::c_int {
        if !p_res_out.is_null() {
            unsafe {
                *p_res_out = 1;
            }
        }
        SQLITE_OK
    }
}

struct HybridVfsImpl;

impl SQLiteVfs<HybridIoMethods> for HybridVfsImpl {
    const VERSION: ::std::os::raw::c_int = 1;

    fn sleep(dur: Duration) {
        sqlite_wasm_rs::WasmOsCallback::sleep(dur);
    }

    fn random(buf: &mut [u8]) {
        sqlite_wasm_rs::WasmOsCallback::random(buf);
    }

    fn epoch_timestamp_in_ms() -> i64 {
        sqlite_wasm_rs::WasmOsCallback::epoch_timestamp_in_ms()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::{self, Cursor, Seek, Write};

    /// Test sink that delegates `Write + Seek` to a shared `Cursor<Vec<u8>>`,
    /// so the test can keep a clone to inspect the resulting bytes after the
    /// `Box<dyn HybridWriter>` has swallowed the concrete type.
    struct SharedCursor(Rc<RefCell<Cursor<Vec<u8>>>>);

    impl Write for SharedCursor {
        fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
            self.0.borrow_mut().write(buf)
        }
        fn flush(&mut self) -> io::Result<()> {
            self.0.borrow_mut().flush()
        }
    }

    impl Seek for SharedCursor {
        fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
            self.0.borrow_mut().seek(pos)
        }
    }

    #[test]
    fn identifies_main_sqlite_file_by_suffix() {
        assert!(is_main_sqlite_file("data.sqlite"));
        assert!(!is_main_sqlite_file("data.sqlite-wal"));
        assert!(is_main_sqlite_file("data.gpkg"));
    }

    #[test]
    fn mem_file_read_pads_with_zero_when_beyond_end() {
        let mut file = MemFile::default();
        file.write(&[1, 2, 3], 0).expect("write should succeed");

        let mut buf = [9_u8; 5];
        let complete = file.read(&mut buf, 1).expect("read should succeed");

        assert!(!complete);
        assert_eq!(buf, [2, 3, 0, 0, 0]);
    }

    #[test]
    fn mem_file_supports_offset_write_and_truncate() {
        let mut file = MemFile::default();
        file.write(&[10, 20], 2).expect("write should succeed");
        assert_eq!(file.size().expect("size should succeed"), 4);

        let mut buf = [0_u8; 4];
        let complete = file.read(&mut buf, 0).expect("read should succeed");
        assert!(complete);
        assert_eq!(buf, [0, 0, 10, 20]);

        file.truncate(3).expect("truncate should succeed");
        assert_eq!(file.size().expect("size should succeed"), 3);
    }

    #[test]
    fn handle_clear_files_drops_entries_visible_to_state() {
        let writer: SharedWriter = Rc::new(RefCell::new(WriterState::new(Box::new(Cursor::new(
            Vec::<u8>::new(),
        )))));
        let files: SharedFiles = Rc::new(RefCell::new(HashMap::new()));
        let state = HybridState {
            files: files.clone(),
            writer: writer.clone(),
        };
        state
            .files
            .borrow_mut()
            .insert("main.gpkg".to_string(), HybridFile::Mem(MemFile::default()));
        state.files.borrow_mut().insert(
            "main.gpkg-journal".to_string(),
            HybridFile::Mem(MemFile::default()),
        );

        let handle = HybridVfsHandle {
            vfs_name: "test".to_string(),
            writer,
            files,
        };

        handle.clear_files();

        assert!(state.files.borrow().is_empty());
    }

    #[test]
    fn main_file_writes_forward_to_writer_at_offset() {
        let cursor = Rc::new(RefCell::new(Cursor::new(Vec::<u8>::new())));
        let writer: SharedWriter = Rc::new(RefCell::new(WriterState::new(Box::new(SharedCursor(
            cursor.clone(),
        )))));
        let mut file = MainFile::new(writer);

        // The second write lands at offset 1, not appended at offset 3.
        file.write(&[1, 2, 3], 0).expect("write should succeed");
        file.write(&[9], 1).expect("write should succeed");
        file.flush().expect("flush should succeed");

        let mut buf = [0_u8; 4];
        let complete = file.read(&mut buf, 0).expect("read should succeed");
        assert!(!complete);
        assert_eq!(buf, [1, 9, 3, 0]);
        assert_eq!(file.size().expect("size should succeed"), 3);
        assert_eq!(cursor.borrow().get_ref().as_slice(), &[1, 9, 3]);
    }

    struct CountingSeek {
        inner: Cursor<Vec<u8>>,
        seek_count: Rc<RefCell<usize>>,
    }

    impl Write for CountingSeek {
        fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
            self.inner.write(buf)
        }
        fn flush(&mut self) -> io::Result<()> {
            self.inner.flush()
        }
    }

    impl Seek for CountingSeek {
        fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
            *self.seek_count.borrow_mut() += 1;
            self.inner.seek(pos)
        }
    }

    #[test]
    fn main_file_skips_seek_for_sequential_writes() {
        let seek_count = Rc::new(RefCell::new(0_usize));
        let writer: SharedWriter =
            Rc::new(RefCell::new(WriterState::new(Box::new(CountingSeek {
                inner: Cursor::new(Vec::<u8>::new()),
                seek_count: seek_count.clone(),
            }))));
        let mut file = MainFile::new(writer);

        file.write(&[1, 2, 3], 0).expect("first write");
        assert_eq!(*seek_count.borrow(), 1);

        // Sequential write at offset 3 should reuse the cursor position.
        file.write(&[4, 5, 6], 3).expect("sequential write");
        assert_eq!(*seek_count.borrow(), 1);

        // Non-sequential write must seek again.
        file.write(&[9], 0).expect("backward write");
        assert_eq!(*seek_count.borrow(), 2);
    }
}