Skip to content

Commit 3ab820d

Browse files
authored
Add libsql_open_sync_with_config (#1566)
Bindings: Add libsql_open_sync_with_config Signed-off-by: Piotr Jastrzebski <[email protected]>
1 parent 06bb81b commit 3ab820d

File tree

3 files changed

+58
-29
lines changed

3 files changed

+58
-29
lines changed

bindings/c/include/libsql.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ typedef struct libsql_stmt libsql_stmt;
2727

2828
typedef const libsql_database *libsql_database_t;
2929

30+
typedef struct {
31+
const char *db_path;
32+
const char *primary_url;
33+
const char *auth_token;
34+
char read_your_writes;
35+
const char *encryption_key;
36+
int sync_interval;
37+
char with_webpki;
38+
} libsql_config;
39+
3040
typedef const libsql_connection *libsql_connection_t;
3141

3242
typedef const libsql_stmt *libsql_stmt_t;
@@ -64,6 +74,8 @@ int libsql_open_sync_with_webpki(const char *db_path,
6474
libsql_database_t *out_db,
6575
const char **out_err_msg);
6676

77+
int libsql_open_sync_with_config(libsql_config config, libsql_database_t *out_db, const char **out_err_msg);
78+
6779
int libsql_open_ext(const char *url, libsql_database_t *out_db, const char **out_err_msg);
6880

6981
int libsql_open_file(const char *url, libsql_database_t *out_db, const char **out_err_msg);

bindings/c/src/lib.rs

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ extern crate lazy_static;
55

66
mod types;
77

8+
use crate::types::libsql_config;
89
use tokio::runtime::Runtime;
910
use types::{
1011
blob, libsql_connection, libsql_connection_t, libsql_database, libsql_database_t, libsql_row,
@@ -54,16 +55,16 @@ pub unsafe extern "C" fn libsql_open_sync(
5455
out_db: *mut libsql_database_t,
5556
out_err_msg: *mut *const std::ffi::c_char,
5657
) -> std::ffi::c_int {
57-
libsql_open_sync_internal(
58+
let config = libsql_config {
5859
db_path,
5960
primary_url,
6061
auth_token,
6162
read_your_writes,
6263
encryption_key,
63-
false,
64-
out_db,
65-
out_err_msg,
66-
)
64+
sync_interval: 0,
65+
with_webpki: 0,
66+
};
67+
libsql_open_sync_with_config(config, out_db, out_err_msg)
6768
}
6869

6970
#[no_mangle]
@@ -76,45 +77,41 @@ pub unsafe extern "C" fn libsql_open_sync_with_webpki(
7677
out_db: *mut libsql_database_t,
7778
out_err_msg: *mut *const std::ffi::c_char,
7879
) -> std::ffi::c_int {
79-
libsql_open_sync_internal(
80+
let config = libsql_config {
8081
db_path,
8182
primary_url,
8283
auth_token,
8384
read_your_writes,
8485
encryption_key,
85-
true,
86-
out_db,
87-
out_err_msg,
88-
)
86+
sync_interval: 0,
87+
with_webpki: 1,
88+
};
89+
libsql_open_sync_with_config(config, out_db, out_err_msg)
8990
}
9091

91-
unsafe fn libsql_open_sync_internal(
92-
db_path: *const std::ffi::c_char,
93-
primary_url: *const std::ffi::c_char,
94-
auth_token: *const std::ffi::c_char,
95-
read_your_writes: std::ffi::c_char,
96-
encryption_key: *const std::ffi::c_char,
97-
with_webpki: bool,
92+
#[no_mangle]
93+
pub unsafe extern "C" fn libsql_open_sync_with_config(
94+
config: libsql_config,
9895
out_db: *mut libsql_database_t,
9996
out_err_msg: *mut *const std::ffi::c_char,
10097
) -> std::ffi::c_int {
101-
let db_path = unsafe { std::ffi::CStr::from_ptr(db_path) };
98+
let db_path = unsafe { std::ffi::CStr::from_ptr(config.db_path) };
10299
let db_path = match db_path.to_str() {
103100
Ok(url) => url,
104101
Err(e) => {
105102
set_err_msg(format!("Wrong URL: {e}"), out_err_msg);
106103
return 1;
107104
}
108105
};
109-
let primary_url = unsafe { std::ffi::CStr::from_ptr(primary_url) };
106+
let primary_url = unsafe { std::ffi::CStr::from_ptr(config.primary_url) };
110107
let primary_url = match primary_url.to_str() {
111108
Ok(url) => url,
112109
Err(e) => {
113110
set_err_msg(format!("Wrong URL: {e}"), out_err_msg);
114111
return 2;
115112
}
116113
};
117-
let auth_token = unsafe { std::ffi::CStr::from_ptr(auth_token) };
114+
let auth_token = unsafe { std::ffi::CStr::from_ptr(config.auth_token) };
118115
let auth_token = match auth_token.to_str() {
119116
Ok(token) => token,
120117
Err(e) => {
@@ -127,29 +124,37 @@ unsafe fn libsql_open_sync_internal(
127124
primary_url.to_string(),
128125
auth_token.to_string(),
129126
);
130-
if with_webpki {
127+
if config.with_webpki != 0 {
131128
let https = hyper_rustls::HttpsConnectorBuilder::new()
132129
.with_webpki_roots()
133130
.https_or_http()
134131
.enable_http1()
135132
.build();
136133
builder = builder.connector(https);
137134
}
138-
let builder = builder.read_your_writes(read_your_writes != 0);
139-
let builder = if encryption_key.is_null() {
140-
builder
141-
} else {
142-
let key = unsafe { std::ffi::CStr::from_ptr(encryption_key) };
135+
if config.sync_interval > 0 {
136+
let interval = match config.sync_interval.try_into() {
137+
Ok(d) => d,
138+
Err(e) => {
139+
set_err_msg(format!("Wrong periodic sync interval: {e}"), out_err_msg);
140+
return 4;
141+
}
142+
};
143+
builder = builder.sync_interval(std::time::Duration::from_secs(interval));
144+
}
145+
builder = builder.read_your_writes(config.read_your_writes != 0);
146+
if !config.encryption_key.is_null() {
147+
let key = unsafe { std::ffi::CStr::from_ptr(config.encryption_key) };
143148
let key = match key.to_str() {
144149
Ok(k) => k,
145150
Err(e) => {
146151
set_err_msg(format!("Wrong encryption key: {e}"), out_err_msg);
147-
return 4;
152+
return 5;
148153
}
149154
};
150155
let key = bytes::Bytes::copy_from_slice(key.as_bytes());
151156
let config = libsql::EncryptionConfig::new(libsql::Cipher::Aes256Cbc, key);
152-
builder.encryption_config(config)
157+
builder = builder.encryption_config(config)
153158
};
154159
match RT.block_on(builder.build()) {
155160
Ok(db) => {
@@ -162,7 +167,7 @@ unsafe fn libsql_open_sync_internal(
162167
format!("Error opening db path {db_path}, primary url {primary_url}: {e}"),
163168
out_err_msg,
164169
);
165-
5
170+
6
166171
}
167172
}
168173
}

bindings/c/src/types.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ pub const LIBSQL_TEXT: i8 = 3;
44
pub const LIBSQL_BLOB: i8 = 4;
55
pub const LIBSQL_NULL: i8 = 5;
66

7+
#[derive(Clone, Debug)]
8+
#[repr(C)]
9+
pub struct libsql_config {
10+
pub db_path: *const std::ffi::c_char,
11+
pub primary_url: *const std::ffi::c_char,
12+
pub auth_token: *const std::ffi::c_char,
13+
pub read_your_writes: std::ffi::c_char,
14+
pub encryption_key: *const std::ffi::c_char,
15+
pub sync_interval: std::ffi::c_int,
16+
pub with_webpki: std::ffi::c_char,
17+
}
18+
719
#[derive(Clone, Debug)]
820
#[repr(C)]
921
pub struct blob {

0 commit comments

Comments
 (0)