From 53d45b9204c119db56c133c8d3fe2d51ef4ef153 Mon Sep 17 00:00:00 2001 From: Tray Torrance Date: Mon, 29 Jul 2024 23:25:41 -0700 Subject: [PATCH] Break out Database constructor flags into type-safe enums --- src/db.rs | 60 +++++++++++++++++++++++++++++++++++++++++++++---- tests/common.rs | 4 ++-- 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/src/db.rs b/src/db.rs index 1b83717..d521114 100644 --- a/src/db.rs +++ b/src/db.rs @@ -3,6 +3,7 @@ use crate::ffi::{self, cxx_bytes, ToCxxString}; use std::{path::Path, pin::Pin}; use autocxx::{cxx, prelude::*}; +use bitflags::bitflags; use bytes::Bytes; /// A read-only Xapian database @@ -10,8 +11,8 @@ pub struct Database(Pin>); impl Database { /// Open a read-only Database at the provided path - pub fn open(path: impl AsRef, flags: impl Into>) -> Self { - let flags = flags.into().unwrap_or(0); + pub fn open(path: impl AsRef, backend: impl Into>) -> Self { + let flags = backend.into().unwrap_or_default(); Self(ffi::Database::new1(&path.as_ref().to_cxx_string(), flags.into()).within_box()) } @@ -56,6 +57,51 @@ impl From for Database { } } +/// A flag indicating how to handle the database already existing (or not) +#[repr(i32)] +#[derive(Default)] +pub enum DbAction { + #[default] + CreateOrOpen = 0x00, + CreateOrOverwrite = 0x01, + Create = 0x02, + Open = 0x03, +} + +impl From for autocxx::c_int { + fn from(value: DbAction) -> Self { + (value as i32).into() + } +} + +/// The type of backend to use for the database +#[repr(i32)] +#[derive(Default)] +pub enum DbBackend { + #[default] + Auto = 0x000, + Glass = 0x100, + Chert = 0x200, + Stub = 0x300, + InMemory = 0x400, +} + +impl From for autocxx::c_int { + fn from(value: DbBackend) -> Self { + (value as i32).into() + } +} + +bitflags! { + pub struct DbFlags: u32 { + const NO_SYNC = 0x04; + const FULL_SYNC = 0x08; + const DANGEROUS = 0x10; + const NO_TERMLIST = 0x20; + const RETRY_LOCK = 0x40; + } +} + /// A Xapian database that can be read or written to pub struct WritableDatabase(Pin>); @@ -72,10 +118,16 @@ impl WritableDatabase { /// Automatically selects the appropriate backend to use pub fn open( path: impl AsRef, - flags: impl Into>, + action: impl Into>, + backend: impl Into>, + flags: impl Into>, block_size: impl Into>, ) -> Self { - let flags = flags.into().unwrap_or(0); + let action = action.into().unwrap_or_default(); + let backend = backend.into().unwrap_or_default(); + let flags = flags.into().map(|f| f.bits()).unwrap_or(0); + let flags = action as i32 | backend as i32 | flags as i32; + let block_size = block_size.into().unwrap_or(0); Self( ffi::WritableDatabase::new1( diff --git a/tests/common.rs b/tests/common.rs index 5819c68..6b232bc 100644 --- a/tests/common.rs +++ b/tests/common.rs @@ -56,7 +56,7 @@ pub fn us_states() -> Vec { pub fn seed_objects(path: impl Into>) -> WritableDatabase { let mut db = path.into().map_or_else(WritableDatabase::inmemory, |path| { - WritableDatabase::open(path, None, None) + WritableDatabase::open(path, None, None, None, None) }); let mut indexer = TermGenerator::default(); @@ -111,7 +111,7 @@ pub fn seed_objects(path: impl Into>) -> WritableDatabase { pub fn seed_states(path: impl Into>) -> WritableDatabase { let mut db = path.into().map_or_else(WritableDatabase::inmemory, |path| { - WritableDatabase::open(path, None, None) + WritableDatabase::open(path, None, None, None, None) }); let mut indexer = TermGenerator::default();