Skip to content

Commit

Permalink
Improve open options
Browse files Browse the repository at this point in the history
  • Loading branch information
Sytten committed Sep 10, 2024
1 parent e710cb8 commit 07ed328
Showing 1 changed file with 35 additions and 21 deletions.
56 changes: 35 additions & 21 deletions src/modules/sqlite/open.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::{
path::PathBuf,
sync::atomic::{AtomicUsize, Ordering},
time::Duration,
};

use rquickjs::{Ctx, FromJs, Object, Result, Value};
use either::Either;
use rquickjs::{Ctx, FromJs, Null, Object, Result, Value};
use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions};

use super::Database;
Expand All @@ -15,7 +17,7 @@ pub async fn open(options: OpenOptions) -> Result<Database> {
connect_options = connect_options
.foreign_keys(options.foreign_keys)
.page_size(options.page_size)
.busy_timeout(Duration::from_millis(options.busy_timeout))
.busy_timeout(options.busy_timeout)
.thread_name(|id| format!("quickjs-sqlite-worker-{id}"));
if let Some(filename) = options.filename {
connect_options = connect_options.filename(filename).create_if_missing(true);
Expand All @@ -33,8 +35,8 @@ pub async fn open(options: OpenOptions) -> Result<Database> {

let mut pool_options = SqlitePoolOptions::new();
pool_options = pool_options
.idle_timeout(options.idle_timeout.map(Duration::from_secs))
.max_lifetime(Some(Duration::from_secs(options.max_lifetime)))
.idle_timeout(options.idle_timeout)
.max_lifetime(options.max_lifetime)
.max_connections(options.max_connections)
.min_connections(options.min_connections);

Expand All @@ -44,16 +46,16 @@ pub async fn open(options: OpenOptions) -> Result<Database> {

#[derive(Debug, Clone)]
pub struct OpenOptions {
filename: Option<String>,
filename: Option<PathBuf>,
in_memory: bool,
wal: bool,
page_size: u32,
foreign_keys: bool,
max_connections: u32,
min_connections: u32,
idle_timeout: Option<u64>,
max_lifetime: u64,
busy_timeout: u64,
idle_timeout: Option<Duration>,
max_lifetime: Option<Duration>,
busy_timeout: Duration,
}

impl Default for OpenOptions {
Expand All @@ -67,8 +69,8 @@ impl Default for OpenOptions {
max_connections: 5,
min_connections: 0,
idle_timeout: None,
max_lifetime: 60 * 60,
busy_timeout: 5 * 1000,
max_lifetime: Some(Duration::from_secs(60 * 60)),
busy_timeout: Duration::from_millis(5 * 1000),
}
}
}
Expand All @@ -77,19 +79,31 @@ impl<'js> FromJs<'js> for OpenOptions {
fn from_js(_ctx: &Ctx<'js>, value: Value<'js>) -> Result<Self> {
let default = OpenOptions::default();
let obj = value.get::<Object<'js>>()?;
let filename = obj.get("filename")?;
let in_memory = obj.get("inMemory").unwrap_or(default.in_memory);
let wal = obj.get("wal").unwrap_or(default.wal);
let page_size = obj.get("pageSize").unwrap_or(default.page_size);
let foreign_keys = obj.get("foreignKeys").unwrap_or(default.foreign_keys);
let max_connections = obj.get("maxConnections").unwrap_or(default.max_connections);
let min_connections = obj.get("minConnections").unwrap_or(default.min_connections);
let idle_timeout = obj.get::<_, u64>("idleTimeout").ok();
let max_lifetime = obj
.get::<_, u64>("maxLifetime")
.unwrap_or(default.max_lifetime);
let filename = obj.get::<_, String>("filename").map(PathBuf::from).ok();
let in_memory = obj.get::<_, bool>("inMemory").unwrap_or(default.in_memory);
let wal = obj.get::<_, bool>("wal").unwrap_or(default.wal);
let page_size = obj.get::<_, u32>("pageSize").unwrap_or(default.page_size);
let foreign_keys = obj
.get::<_, bool>("foreignKeys")
.unwrap_or(default.foreign_keys);
let max_connections = obj
.get::<_, u32>("maxConnections")
.unwrap_or(default.max_connections);
let min_connections = obj
.get::<_, u32>("minConnections")
.unwrap_or(default.min_connections);
let idle_timeout = obj
.get::<_, Option<u64>>("idleTimeout")?
.map(Duration::from_secs);
let max_lifetime =
obj.get::<_, Either<Option<u64>, Null>>("maxLifetime")
.map(|e| match e {
Either::Left(s) => s.map(Duration::from_secs).or(default.max_lifetime),
Either::Right(_) => None,
})?;
let busy_timeout = obj
.get::<_, u64>("busyTimeout")
.map(Duration::from_secs)
.unwrap_or(default.busy_timeout);
Ok(Self {
filename,
Expand Down

0 comments on commit 07ed328

Please sign in to comment.