1use std::ffi::{c_char, c_int, c_uint, c_void};
3
4pub type Wal = crate::ffi::libsql_wal;
5use crate::ffi::{libsql_wal_methods, sqlite3, sqlite3_file, sqlite3_vfs, PgHdr};
6
7pub type XWalLimitFn = extern "C" fn(wal: *mut Wal, limit: i64);
9pub type XWalBeginReadTransactionFn = extern "C" fn(wal: *mut Wal, changed: *mut c_int) -> c_int;
10pub type XWalEndReadTransaction = extern "C" fn(wal: *mut Wal);
11pub type XWalFindFrameFn = extern "C" fn(wal: *mut Wal, pgno: u32, frame: *mut u32) -> c_int;
12pub type XWalReadFrameFn =
13 extern "C" fn(wal: *mut Wal, frame: u32, n_out: c_int, p_out: *mut u8) -> c_int;
14pub type XWalDbsizeFn = extern "C" fn(wal: *mut Wal) -> u32;
15pub type XWalBeginWriteTransactionFn = extern "C" fn(wal: *mut Wal) -> c_int;
16pub type XWalEndWriteTransactionFn = extern "C" fn(wal: *mut Wal) -> c_int;
17pub type XWalSavepointFn = extern "C" fn(wal: *mut Wal, wal_data: *mut u32);
18pub type XWalSavePointUndoFn = unsafe extern "C" fn(wal: *mut Wal, wal_data: *mut u32) -> c_int;
19pub type XWalCheckpointFn = unsafe extern "C" fn(
20 wal: *mut Wal,
21 db: *mut sqlite3,
22 emode: c_int,
23 busy_handler: Option<unsafe extern "C" fn(busy_param: *mut c_void) -> c_int>,
24 busy_arg: *mut c_void,
25 sync_flags: c_int,
26 n_buf: c_int,
27 z_buf: *mut u8,
28 frames_in_wal: *mut c_int,
29 backfilled_frames: *mut c_int,
30) -> c_int;
31pub type XWalCallbackFn = extern "C" fn(wal: *mut Wal) -> c_int;
32pub type XWalExclusiveModeFn = extern "C" fn(wal: *mut Wal, op: c_int) -> c_int;
33pub type XWalHeapMemoryFn = extern "C" fn(wal: *mut Wal) -> c_int;
34pub type XWalFileFn = extern "C" fn(wal: *mut Wal) -> *mut sqlite3_file;
35pub type XWalDbFn = extern "C" fn(wal: *mut Wal, db: *mut sqlite3);
36pub type XWalPathNameLenFn = extern "C" fn(orig_len: c_int) -> c_int;
37pub type XWalGetPathNameFn = extern "C" fn(buf: *mut c_char, orig: *const c_char, orig_len: c_int);
38pub type XWalPreMainDbOpen =
39 extern "C" fn(methods: *mut libsql_wal_methods, path: *const c_char) -> c_int;
40pub type XWalOpenFn = extern "C" fn(
41 vfs: *mut sqlite3_vfs,
42 file: *mut sqlite3_file,
43 wal_name: *const c_char,
44 no_shm_mode: c_int,
45 max_size: i64,
46 methods: *mut libsql_wal_methods,
47 wal: *mut *mut Wal,
48) -> c_int;
49pub type XWalCloseFn = extern "C" fn(
50 wal: *mut Wal,
51 db: *mut sqlite3,
52 sync_flags: c_int,
53 n_buf: c_int,
54 z_buf: *mut u8,
55) -> c_int;
56pub type XWalFrameFn = unsafe extern "C" fn(
57 wal: *mut Wal,
58 page_size: c_int,
59 page_headers: *mut PgHdr,
60 size_after: u32,
61 is_commit: c_int,
62 sync_flags: c_int,
63) -> c_int;
64pub type XWalUndoFn = unsafe extern "C" fn(
65 wal: *mut Wal,
66 func: Option<unsafe extern "C" fn(*mut c_void, c_uint) -> c_int>,
67 ctx: *mut c_void,
68) -> c_int;
69
70pub type XAccessFn = unsafe extern "C" fn(
72 vfs: *mut sqlite3_vfs,
73 name: *const c_char,
74 flags: c_int,
75 res: *mut c_int,
76) -> c_int;
77pub type XDeleteFn =
78 unsafe extern "C" fn(vfs: *mut sqlite3_vfs, name: *const c_char, sync_dir: c_int) -> c_int;
79pub type XFullPathNameFn = unsafe extern "C" fn(
80 vfs: *mut sqlite3_vfs,
81 name: *const c_char,
82 n: c_int,
83 out: *mut c_char,
84) -> c_int;
85pub type XOpenFn = unsafe extern "C" fn(
86 vfs: *mut sqlite3_vfs,
87 name: *const c_char,
88 file: *mut sqlite3_file,
89 flags: c_int,
90 out_flags: *mut c_int,
91) -> c_int;
92pub type XDlOpenFn =
93 unsafe extern "C" fn(vfs: *mut sqlite3_vfs, name: *const c_char) -> *const c_void;
94pub type XDlErrorFn = unsafe extern "C" fn(vfs: *mut sqlite3_vfs, n: c_int, msg: *mut c_char);
95pub type XDlSymFn = unsafe extern "C" fn(
96 vfs: *mut sqlite3_vfs,
97 arg: *mut c_void,
98 symbol: *const c_char,
99) -> unsafe extern "C" fn();
100pub type XDlCloseFn = unsafe extern "C" fn(vfs: *mut sqlite3_vfs, arg: *mut c_void);
101pub type XRandomnessFn =
102 unsafe extern "C" fn(vfs: *mut sqlite3_vfs, n_bytes: c_int, out: *mut c_char) -> c_int;
103pub type XSleepFn = unsafe extern "C" fn(vfs: *mut sqlite3_vfs, ms: c_int) -> c_int;
104pub type XCurrentTimeFn = unsafe extern "C" fn(vfs: *mut sqlite3_vfs, time: *mut f64) -> c_int;
105pub type XGetLastErrorFn =
106 unsafe extern "C" fn(vfs: *mut sqlite3_vfs, n: c_int, buf: *mut c_char) -> c_int;
107pub type XCurrentTimeInt64 = unsafe extern "C" fn(vfs: *mut sqlite3_vfs, time: *mut i64) -> c_int;
108pub type XCloseFn = unsafe extern "C" fn(file_ptr: *mut sqlite3_file) -> c_int;
109pub type XReadFn = unsafe extern "C" fn(
110 file_ptr: *mut sqlite3_file,
111 buf: *mut c_char,
112 n: c_int,
113 off: i64,
114) -> c_int;
115pub type XWriteFn = unsafe extern "C" fn(
116 file_ptr: *mut sqlite3_file,
117 buf: *const c_char,
118 n: c_int,
119 off: i64,
120) -> c_int;
121pub type XTruncateFn = unsafe extern "C" fn(file_ptr: *mut sqlite3_file, size: i64) -> c_int;
122pub type XSyncFn = unsafe extern "C" fn(file_ptr: *mut sqlite3_file, flags: c_int) -> c_int;
123pub type XFileSizeFn = unsafe extern "C" fn(file_ptr: *mut sqlite3_file, size: *mut i64) -> c_int;
124pub type XLockFn = unsafe extern "C" fn(file_ptr: *mut sqlite3_file, lock: c_int) -> c_int;
125pub type XUnlockFn = unsafe extern "C" fn(file_ptr: *mut sqlite3_file, lock: c_int) -> c_int;
126pub type XCheckReservedLockFn =
127 unsafe extern "C" fn(file_ptr: *mut sqlite3_file, res: *mut c_int) -> c_int;
128pub type XFileControlFn =
129 unsafe extern "C" fn(file_ptr: *mut sqlite3_file, op: c_int, arg: *mut c_void) -> c_int;
130pub type XSectorSizeFn = unsafe extern "C" fn(file_ptr: *mut sqlite3_file) -> c_int;
131pub type XDeviceCharacteristicsFn = unsafe extern "C" fn(file_ptr: *mut sqlite3_file) -> c_int;
132
133pub struct PageHdrIter {
134 current_ptr: *const PgHdr,
135 page_size: usize,
136}
137
138impl PageHdrIter {
139 pub fn new(current_ptr: *const PgHdr, page_size: usize) -> Self {
140 Self {
141 current_ptr,
142 page_size,
143 }
144 }
145}
146
147impl std::iter::Iterator for PageHdrIter {
148 type Item = (u32, &'static [u8]);
149
150 fn next(&mut self) -> Option<Self::Item> {
151 if self.current_ptr.is_null() {
152 return None;
153 }
154 let current_hdr: &PgHdr = unsafe { &*self.current_ptr };
155 let raw_data =
156 unsafe { std::slice::from_raw_parts(current_hdr.pData as *const u8, self.page_size) };
157 let item = Some((current_hdr.pgno, raw_data));
158 self.current_ptr = current_hdr.pDirty;
159 item
160 }
161}