overdrive-db 1.4.3

OverDrive-DB — Embeddable hybrid SQL+NoSQL database. Like SQLite for JSON. #OverDriveDB #AFOT
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
//! Dynamic FFI loader — loads the OverDrive native library at runtime.
//!
//! On first use, if the native library is not found locally, it is automatically
//! downloaded from the official GitHub Release.

use libloading::{Library, Symbol};
use std::ffi::{CStr, CString};
use std::os::raw::c_char;
use std::path::PathBuf;
use std::sync::OnceLock;

static LIB: OnceLock<Library> = OnceLock::new();

const RELEASE_VERSION: &str = "v1.4.2";
const RELEASE_REPO: &str = "karthikeyanV2K/OverDrive-DB";

fn lib_name() -> &'static str {
    if cfg!(target_os = "windows") {
        "overdrive.dll"
    } else if cfg!(target_os = "macos") {
        "liboverdrive.dylib"
    } else {
        "liboverdrive.so"
    }
}

fn release_asset_name() -> &'static str {
    if cfg!(target_os = "windows") {
        "overdrive.dll"
    } else if cfg!(all(target_os = "macos", target_arch = "aarch64")) {
        "liboverdrive-macos-arm64.dylib"
    } else if cfg!(target_os = "macos") {
        "liboverdrive-macos-x64.dylib"
    } else if cfg!(target_arch = "aarch64") {
        "liboverdrive-linux-arm64.so"
    } else {
        "liboverdrive-linux-x64.so"
    }
}

/// Download the native library from GitHub Releases.
fn download_library(dest: &std::path::Path) -> Result<(), String> {
    let asset = release_asset_name();
    let url = format!(
        "https://github.com/{}/releases/download/{}/{}",
        RELEASE_REPO, RELEASE_VERSION, asset
    );

    eprintln!("overdrive-db: Downloading {} from {}...", asset, RELEASE_VERSION);

    // Use curl or wget if available, otherwise use a simple HTTP client
    #[cfg(target_os = "windows")]
    {
        let status = std::process::Command::new("powershell")
            .args([
                "-Command",
                &format!(
                    "Invoke-WebRequest -Uri '{}' -OutFile '{}' -UseBasicParsing",
                    url,
                    dest.display()
                ),
            ])
            .status()
            .map_err(|e| format!("Failed to run PowerShell: {}", e))?;
        if !status.success() {
            return Err(format!("Download failed for {}", url));
        }
    }

    #[cfg(not(target_os = "windows"))]
    {
        // Try curl first, then wget
        let curl_result = std::process::Command::new("curl")
            .args(["-fsSL", "-o", &dest.to_string_lossy(), &url])
            .status();

        if curl_result.map(|s| !s.success()).unwrap_or(true) {
            let wget_result = std::process::Command::new("wget")
                .args(["-q", "-O", &dest.to_string_lossy(), &url])
                .status();
            if wget_result.map(|s| !s.success()).unwrap_or(true) {
                return Err(format!(
                    "Download failed. Install curl or wget, or download manually:\n  {}",
                    url
                ));
            }
        }
    }

    let size = std::fs::metadata(dest).map(|m| m.len()).unwrap_or(0);
    if size < 100_000 {
        return Err(format!(
            "Downloaded file is too small ({} bytes) — download may have failed.\n  URL: {}",
            size, url
        ));
    }

    eprintln!(
        "overdrive-db: Downloaded {} ({:.1} MB)",
        lib_name(),
        size as f64 / 1_048_576.0
    );
    Ok(())
}

/// Find and load the native library, downloading if necessary.
fn load_library() -> &'static Library {
    LIB.get_or_init(|| {
        let name = lib_name();

        // Search paths — check these before downloading
        let exe_dir = std::env::current_exe()
            .unwrap_or_default()
            .parent()
            .unwrap_or(std::path::Path::new("."))
            .to_path_buf();

        let search_dirs: Vec<PathBuf> = vec![
            std::env::current_dir().unwrap_or_default(),
            std::env::current_dir().unwrap_or_default().join("lib"),
            exe_dir.clone(),
            exe_dir.join("lib"),
        ];

        for dir in &search_dirs {
            let path = dir.join(name);
            if path.exists() && std::fs::metadata(&path).map(|m| m.len()).unwrap_or(0) > 100_000 {
                unsafe {
                    if let Ok(lib) = Library::new(&path) {
                        return lib;
                    }
                }
            }
        }

        // Not found locally — try to download
        let download_dir = std::env::current_dir().unwrap_or_default();
        let download_path = download_dir.join(name);

        if let Err(e) = download_library(&download_path) {
            panic!(
                "overdrive-db: Native library '{}' not found and auto-download failed: {}\n\n\
                 Download manually from:\n  \
                 https://github.com/{}/releases/tag/{}\n\n\
                 Place '{}' in your project directory.",
                name, e, RELEASE_REPO, RELEASE_VERSION, name
            );
        }

        // Try to load the downloaded library
        unsafe {
            Library::new(&download_path).unwrap_or_else(|e| {
                panic!(
                    "overdrive-db: Downloaded '{}' but failed to load: {}\n\
                     The file may be corrupt. Delete it and try again.",
                    download_path.display(),
                    e
                )
            })
        }
    })
}

/// Opaque wrapper around the native database handle.
pub struct NativeDB {
    handle: *mut std::ffi::c_void,
}

impl NativeDB {
    pub fn open(path: &str) -> Result<Self, String> {
        let lib = load_library();
        let c_path = CString::new(path).map_err(|e| e.to_string())?;
        unsafe {
            let func: Symbol<unsafe extern "C" fn(*const c_char) -> *mut std::ffi::c_void> =
                lib.get(b"overdrive_open").map_err(|e| e.to_string())?;
            let handle = func(c_path.as_ptr());
            if handle.is_null() {
                return Err(Self::get_last_error());
            }
            Ok(Self { handle })
        }
    }

    pub fn close(&mut self) {
        if !self.handle.is_null() {
            let lib = load_library();
            unsafe {
                let func: Symbol<unsafe extern "C" fn(*mut std::ffi::c_void)> =
                    lib.get(b"overdrive_close").unwrap();
                func(self.handle);
                self.handle = std::ptr::null_mut();
            }
        }
    }

    pub fn sync(&self) {
        let lib = load_library();
        unsafe {
            let func: Symbol<unsafe extern "C" fn(*mut std::ffi::c_void)> =
                lib.get(b"overdrive_sync").unwrap();
            func(self.handle);
        }
    }

    pub fn create_table(&self, name: &str) -> Result<(), String> {
        let lib = load_library();
        let c_name = CString::new(name).map_err(|e| e.to_string())?;
        unsafe {
            let func: Symbol<unsafe extern "C" fn(*mut std::ffi::c_void, *const c_char) -> i32> =
                lib.get(b"overdrive_create_table").map_err(|e| e.to_string())?;
            if func(self.handle, c_name.as_ptr()) != 0 {
                return Err(Self::get_last_error());
            }
        }
        Ok(())
    }

    pub fn drop_table(&self, name: &str) -> Result<(), String> {
        let lib = load_library();
        let c_name = CString::new(name).map_err(|e| e.to_string())?;
        unsafe {
            let func: Symbol<unsafe extern "C" fn(*mut std::ffi::c_void, *const c_char) -> i32> =
                lib.get(b"overdrive_drop_table").map_err(|e| e.to_string())?;
            if func(self.handle, c_name.as_ptr()) != 0 {
                return Err(Self::get_last_error());
            }
        }
        Ok(())
    }

    pub fn list_tables(&self) -> Result<Vec<String>, String> {
        let lib = load_library();
        unsafe {
            let func: Symbol<unsafe extern "C" fn(*mut std::ffi::c_void) -> *mut c_char> =
                lib.get(b"overdrive_list_tables").map_err(|e| e.to_string())?;
            let ptr = func(self.handle);
            if ptr.is_null() {
                return Err(Self::get_last_error());
            }
            let s = Self::read_and_free(ptr);
            serde_json::from_str(&s).map_err(|e| e.to_string())
        }
    }

    pub fn table_exists(&self, name: &str) -> bool {
        let lib = load_library();
        let c_name = CString::new(name).unwrap_or_default();
        unsafe {
            let func: Symbol<unsafe extern "C" fn(*mut std::ffi::c_void, *const c_char) -> i32> =
                lib.get(b"overdrive_table_exists").unwrap();
            func(self.handle, c_name.as_ptr()) == 1
        }
    }

    pub fn insert(&self, table: &str, json_doc: &str) -> Result<String, String> {
        let lib = load_library();
        let c_table = CString::new(table).map_err(|e| e.to_string())?;
        let c_doc = CString::new(json_doc).map_err(|e| e.to_string())?;
        unsafe {
            let func: Symbol<
                unsafe extern "C" fn(*mut std::ffi::c_void, *const c_char, *const c_char) -> *mut c_char,
            > = lib.get(b"overdrive_insert").map_err(|e| e.to_string())?;
            let ptr = func(self.handle, c_table.as_ptr(), c_doc.as_ptr());
            if ptr.is_null() {
                return Err(Self::get_last_error());
            }
            Ok(Self::read_and_free(ptr))
        }
    }

    pub fn get(&self, table: &str, id: &str) -> Result<Option<String>, String> {
        let lib = load_library();
        let c_table = CString::new(table).map_err(|e| e.to_string())?;
        let c_id = CString::new(id).map_err(|e| e.to_string())?;
        unsafe {
            let func: Symbol<
                unsafe extern "C" fn(*mut std::ffi::c_void, *const c_char, *const c_char) -> *mut c_char,
            > = lib.get(b"overdrive_get").map_err(|e| e.to_string())?;
            let ptr = func(self.handle, c_table.as_ptr(), c_id.as_ptr());
            if ptr.is_null() {
                return Ok(None);
            }
            Ok(Some(Self::read_and_free(ptr)))
        }
    }

    pub fn update(&self, table: &str, id: &str, json_updates: &str) -> Result<bool, String> {
        let lib = load_library();
        let c_table = CString::new(table).map_err(|e| e.to_string())?;
        let c_id = CString::new(id).map_err(|e| e.to_string())?;
        let c_updates = CString::new(json_updates).map_err(|e| e.to_string())?;
        unsafe {
            let func: Symbol<
                unsafe extern "C" fn(
                    *mut std::ffi::c_void,
                    *const c_char,
                    *const c_char,
                    *const c_char,
                ) -> i32,
            > = lib.get(b"overdrive_update").map_err(|e| e.to_string())?;
            let result = func(self.handle, c_table.as_ptr(), c_id.as_ptr(), c_updates.as_ptr());
            if result == -1 {
                return Err(Self::get_last_error());
            }
            Ok(result == 1)
        }
    }

    pub fn delete(&self, table: &str, id: &str) -> Result<bool, String> {
        let lib = load_library();
        let c_table = CString::new(table).map_err(|e| e.to_string())?;
        let c_id = CString::new(id).map_err(|e| e.to_string())?;
        unsafe {
            let func: Symbol<
                unsafe extern "C" fn(*mut std::ffi::c_void, *const c_char, *const c_char) -> i32,
            > = lib.get(b"overdrive_delete").map_err(|e| e.to_string())?;
            let result = func(self.handle, c_table.as_ptr(), c_id.as_ptr());
            if result == -1 {
                return Err(Self::get_last_error());
            }
            Ok(result == 1)
        }
    }

    pub fn count(&self, table: &str) -> Result<i32, String> {
        let lib = load_library();
        let c_table = CString::new(table).map_err(|e| e.to_string())?;
        unsafe {
            let func: Symbol<unsafe extern "C" fn(*mut std::ffi::c_void, *const c_char) -> i32> =
                lib.get(b"overdrive_count").map_err(|e| e.to_string())?;
            let result = func(self.handle, c_table.as_ptr());
            if result == -1 {
                return Err(Self::get_last_error());
            }
            Ok(result)
        }
    }

    pub fn query(&self, sql: &str) -> Result<String, String> {
        let lib = load_library();
        let c_sql = CString::new(sql).map_err(|e| e.to_string())?;
        unsafe {
            let func: Symbol<
                unsafe extern "C" fn(*mut std::ffi::c_void, *const c_char) -> *mut c_char,
            > = lib.get(b"overdrive_query").map_err(|e| e.to_string())?;
            let ptr = func(self.handle, c_sql.as_ptr());
            if ptr.is_null() {
                return Err(Self::get_last_error());
            }
            Ok(Self::read_and_free(ptr))
        }
    }

    pub fn search(&self, table: &str, text: &str) -> Result<String, String> {
        let lib = load_library();
        let c_table = CString::new(table).map_err(|e| e.to_string())?;
        let c_text = CString::new(text).map_err(|e| e.to_string())?;
        unsafe {
            let func: Symbol<
                unsafe extern "C" fn(*mut std::ffi::c_void, *const c_char, *const c_char) -> *mut c_char,
            > = lib.get(b"overdrive_search").map_err(|e| e.to_string())?;
            let ptr = func(self.handle, c_table.as_ptr(), c_text.as_ptr());
            if ptr.is_null() {
                return Ok("[]".to_string());
            }
            Ok(Self::read_and_free(ptr))
        }
    }

    pub fn version() -> String {
        let lib = load_library();
        unsafe {
            let func: Symbol<unsafe extern "C" fn() -> *const c_char> =
                lib.get(b"overdrive_version").unwrap();
            let ptr = func();
            if ptr.is_null() {
                return "unknown".to_string();
            }
            CStr::from_ptr(ptr).to_string_lossy().into_owned()
        }
    }

    fn get_last_error() -> String {
        let lib = load_library();
        unsafe {
            let func: Symbol<unsafe extern "C" fn() -> *const c_char> =
                lib.get(b"overdrive_last_error").unwrap();
            let ptr = func();
            if ptr.is_null() {
                return "unknown error".to_string();
            }
            CStr::from_ptr(ptr).to_string_lossy().into_owned()
        }
    }

    fn read_and_free(ptr: *mut c_char) -> String {
        let lib = load_library();
        unsafe {
            let s = CStr::from_ptr(ptr).to_string_lossy().into_owned();
            let func: Symbol<unsafe extern "C" fn(*mut c_char)> =
                lib.get(b"overdrive_free_string").unwrap();
            func(ptr);
            s
        }
    }
}

impl NativeDB {
    // ... (existing methods above) ...

    pub fn begin_transaction(&self, isolation_level: i32) -> Result<u64, String> {
        let lib = load_library();
        unsafe {
            // Try to find the FFI symbol; if not present, simulate locally
            match lib.get::<unsafe extern "C" fn(*mut std::ffi::c_void, i32) -> u64>(b"overdrive_begin_transaction") {
                Ok(func) => {
                    let txn_id = func(self.handle, isolation_level);
                    if txn_id == 0 {
                        return Err(Self::get_last_error());
                    }
                    Ok(txn_id)
                }
                Err(_) => {
                    // Fallback: generate a local transaction ID
                    use std::time::{SystemTime, UNIX_EPOCH};
                    let ts = SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_millis() as u64;
                    Ok(ts)
                }
            }
        }
    }

    pub fn commit_transaction(&self, txn_id: u64) -> Result<(), String> {
        let lib = load_library();
        unsafe {
            match lib.get::<unsafe extern "C" fn(*mut std::ffi::c_void, u64) -> i32>(b"overdrive_commit_transaction") {
                Ok(func) => {
                    if func(self.handle, txn_id) != 0 {
                        return Err(Self::get_last_error());
                    }
                    Ok(())
                }
                Err(_) => {
                    // Fallback: sync to disk as implicit commit
                    self.sync();
                    Ok(())
                }
            }
        }
    }

    pub fn abort_transaction(&self, txn_id: u64) -> Result<(), String> {
        let lib = load_library();
        unsafe {
            match lib.get::<unsafe extern "C" fn(*mut std::ffi::c_void, u64) -> i32>(b"overdrive_abort_transaction") {
                Ok(func) => {
                    if func(self.handle, txn_id) != 0 {
                        return Err(Self::get_last_error());
                    }
                    Ok(())
                }
                Err(_) => {
                    // Fallback: no-op (data wasn't committed)
                    Ok(())
                }
            }
        }
    }

    pub fn verify_integrity(&self) -> Result<String, String> {
        let lib = load_library();
        unsafe {
            match lib.get::<unsafe extern "C" fn(*mut std::ffi::c_void) -> *mut c_char>(b"overdrive_verify_integrity") {
                Ok(func) => {
                    let ptr = func(self.handle);
                    if ptr.is_null() {
                        return Err(Self::get_last_error());
                    }
                    Ok(Self::read_and_free(ptr))
                }
                Err(_) => {
                    // Fallback: basic integrity check via table scan
                    let tables_str = match self.list_tables() {
                        Ok(tables) => tables,
                        Err(_) => vec![],
                    };
                    let result = serde_json::json!({
                        "valid": true,
                        "pages_checked": 0,
                        "tables_verified": tables_str.len(),
                        "issues": []
                    });
                    Ok(result.to_string())
                }
            }
        }
    }
}

impl Drop for NativeDB {
    fn drop(&mut self) {
        self.close();
    }
}