Skip to content

Commit 658072e

Browse files
authored
Merge pull request tursodatabase#105 from psarna/vwal_default
Use WAL journaling mode by default if custom WAL methods are specified
2 parents 2838baa + 4562eaa commit 658072e

3 files changed

Lines changed: 32 additions & 23 deletions

File tree

src/pager.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3280,13 +3280,11 @@ static int pagerPagecount(Pager *pPager, Pgno *pnPage){
32803280
#ifndef SQLITE_OMIT_WAL
32813281
/*
32823282
** Check if the *-wal file that corresponds to the database opened by pPager
3283-
** exists if the database is not empy, or verify that the *-wal file does
3283+
** exists if the database is not empty, or verify that the *-wal file does
32843284
** not exist (by deleting it) if the database file is empty.
32853285
**
32863286
** If the database is not empty and the *-wal file exists, open the pager
3287-
** in WAL mode. If the database is empty or if no *-wal file exists and
3288-
** if no error occurs, make sure Pager.journalMode is not set to
3289-
** PAGER_JOURNALMODE_WAL.
3287+
** in WAL mode. If the journaling mode is already set to wal, open it.
32903288
**
32913289
** Return SQLITE_OK or an error code.
32923290
**
@@ -3319,7 +3317,7 @@ static int pagerOpenWalIfPresent(Pager *pPager){
33193317
rc = sqlite3PagerOpenWal(pPager, 0);
33203318
}
33213319
}else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
3322-
pPager->journalMode = PAGER_JOURNALMODE_DELETE;
3320+
rc = sqlite3PagerOpenWal(pPager, 0);
33233321
}
33243322
}
33253323
}
@@ -4893,16 +4891,19 @@ int sqlite3PagerOpen(
48934891
sqlite3FileSuffix3(zFilename, pPager->zWal);
48944892
pPtr = (u8*)(pPager->zWal + sqlite3Strlen30(pPager->zWal)+1);
48954893
#endif
4894+
}else{
4895+
pPager->zWal = 0;
4896+
}
48964897

48974898
if (pWalMethods->xPreMainDbOpen) {
48984899
int rc = pWalMethods->xPreMainDbOpen(pWalMethods, zPathname);
48994900
if (rc != SQLITE_OK) {
49004901
return rc;
49014902
}
49024903
}
4903-
4904-
}else{
4905-
pPager->zWal = 0;
4904+
if (strcmp(pWalMethods->zName, "default") != 0) {
4905+
// use WAL journaling by default if custom WAL methods are set
4906+
sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_WAL);
49064907
}
49074908
#endif
49084909
(void)pPtr; /* Suppress warning about unused pPtr value */

test/rust_suite/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@ mod tests {
5151
let also_steven = person_iter.next().unwrap().unwrap();
5252
println!("Read {:#?}", also_steven);
5353
assert!(also_steven == steven);
54-
assert!(person_iter.next() == None)
54+
assert!(person_iter.next().is_none())
5555
}
5656
}

test/rust_suite/src/virtual_wal.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![allow(improper_ctypes)]
12
#[cfg(test)]
23
mod tests {
34
use rusqlite::Connection;
@@ -121,8 +122,7 @@ mod tests {
121122
db: extern "C" fn(wal: *mut Wal, db: *const c_void),
122123
pathname_len: extern "C" fn(orig_len: i32) -> i32,
123124
get_pathname: extern "C" fn(buf: *mut u8, orig: *const u8, orig_len: i32),
124-
pre_main_db_open:
125-
extern "C" fn(methods: *mut libsql_wal_methods, name: *const i8) -> i32,
125+
pre_main_db_open: extern "C" fn(methods: *mut libsql_wal_methods, name: *const i8) -> i32,
126126
b_uses_shm: i32,
127127
name: *const u8,
128128
p_next: *const c_void,
@@ -165,7 +165,7 @@ mod tests {
165165
wal: *mut *const Wal,
166166
) -> i32 {
167167
let new_wal = Box::new(Wal {
168-
vfs: vfs,
168+
vfs,
169169
db_fd: std::ptr::null(),
170170
wal_fd: std::ptr::null(),
171171
callback_value: 0,
@@ -199,7 +199,7 @@ mod tests {
199199
},
200200
min_frame: 0,
201201
recalculate_checksums: 0,
202-
wal_name: wal_name,
202+
wal_name,
203203
n_checkpoints: 0,
204204
lock_error: 0,
205205
p_snapshot: std::ptr::null(),
@@ -253,12 +253,14 @@ mod tests {
253253
return ERR_MISUSE;
254254
}
255255
let out_buffer = unsafe { std::slice::from_raw_parts_mut(p_out, n_out) };
256-
out_buffer.copy_from_slice(&data);
256+
out_buffer.copy_from_slice(data);
257257
println!("\t\tread {} bytes", data.len());
258258
0
259259
}
260-
extern "C" fn db_size(_wal: *mut Wal) -> i32 {
261-
ERR_MISUSE
260+
extern "C" fn db_size(wal: *mut Wal) -> i32 {
261+
println!("Db size called");
262+
let methods = unsafe { &*(*wal).wal_methods };
263+
methods.pages.len() as i32
262264
}
263265
extern "C" fn begin_write(_wal: *mut Wal) -> i32 {
264266
println!("Write started");
@@ -300,7 +302,7 @@ mod tests {
300302
}
301303
.to_vec();
302304
methods.pages.insert(current.pgno, data);
303-
if current.dirty == std::ptr::null() {
305+
if current.dirty.is_null() {
304306
break;
305307
}
306308
current_ptr = current.dirty
@@ -334,12 +336,14 @@ mod tests {
334336
panic!("Should never be called")
335337
}
336338
extern "C" fn db(_wal: *mut Wal, _db: *const c_void) {}
337-
extern "C" fn pathname_len(_orig_len: i32) -> i32 {
338-
println!("Returning length 0");
339-
0
339+
extern "C" fn pathname_len(orig_len: i32) -> i32 {
340+
orig_len + 4
340341
}
341-
extern "C" fn get_pathname(_buf: *mut u8, _orig: *const u8, _orig_len: i32) {
342-
panic!("Should never be called")
342+
extern "C" fn get_pathname(buf: *mut u8, orig: *const u8, orig_len: i32) {
343+
unsafe {
344+
std::ptr::copy_nonoverlapping(orig, buf, orig_len as usize);
345+
std::ptr::copy_nonoverlapping(".wal".as_ptr(), buf.offset(orig_len as isize), 4);
346+
}
343347
}
344348
extern "C" fn pre_main_db_open(_methods: *mut libsql_wal_methods, _name: *const i8) -> i32 {
345349
0
@@ -402,7 +406,11 @@ mod tests {
402406
Box::leak(vwal);
403407
Connection::from_handle(pdb).unwrap()
404408
};
405-
conn.pragma_update(None, "journal_mode", "wal").unwrap();
409+
let journal_mode: String = conn
410+
.query_row("PRAGMA journal_mode", [], |r| r.get(0))
411+
.unwrap();
412+
println!("Journaling mode: {}", journal_mode);
413+
assert_eq!(journal_mode, "wal".to_string());
406414
conn.execute("CREATE TABLE t(id)", ()).unwrap();
407415
conn.execute("INSERT INTO t(id) VALUES (42)", ()).unwrap();
408416
conn.execute("INSERT INTO t(id) VALUES (zeroblob(8193))", ())

0 commit comments

Comments
 (0)