Skip to content

Commit 5fce832

Browse files
committed
Bug 1972112 - Add run_maintenance for autofill / logins
Adding `run_maintenance` for autofill/logins. I didn't split this up or add metrics like we did for places because I highly doubt we're going to have performances issues with these functions. The places metrics are fine and needs to do more work then these components (the prune step and places DBs are larger in general).
1 parent a913e00 commit 5fce832

File tree

6 files changed

+66
-1
lines changed

6 files changed

+66
-1
lines changed

components/autofill/src/autofill.udl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ interface Store {
127127
[Throws=AutofillApiError, Self=ByArc]
128128
void scrub_encrypted_data();
129129

130+
/// Run maintenance on the DB
131+
///
132+
/// This is intended to be run during idle time and will take steps / to clean up / shrink the
133+
/// database.
134+
[Throws=AutofillApiError]
135+
void run_maintenance();
136+
130137
[Self=ByArc]
131138
void register_with_sync_manager();
132139
};

components/autofill/src/db/store.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rusqlite::{
1111
types::{FromSql, ToSql},
1212
Connection,
1313
};
14-
use sql_support::{self, ConnExt};
14+
use sql_support::{self, run_maintenance, ConnExt};
1515
use std::path::Path;
1616
use std::sync::{Arc, Mutex, Weak};
1717
use sync15::engine::{SyncEngine, SyncEngineId};
@@ -162,6 +162,13 @@ impl Store {
162162
Ok(())
163163
}
164164

165+
#[handle_error(Error)]
166+
pub fn run_maintenance(&self) -> ApiResult<()> {
167+
let conn = self.db.lock().unwrap();
168+
run_maintenance(&conn)?;
169+
Ok(())
170+
}
171+
165172
// This allows the embedding app to say "make this instance available to
166173
// the sync manager". The implementation is more like "offer to sync mgr"
167174
// (thereby avoiding us needing to link with the sync manager) but

components/logins/src/logins.udl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,13 @@ interface LoginStore {
244244
[Throws=LoginsApiError]
245245
string? get_checkpoint();
246246

247+
/// Run maintenance on the DB
248+
///
249+
/// This is intended to be run during idle time and will take steps / to clean up / shrink the
250+
/// database.
251+
[Throws=LoginsApiError]
252+
void run_maintenance();
253+
247254
[Self=ByArc]
248255
void register_with_sync_manager();
249256
};

components/logins/src/store.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::login::{BulkResultEntry, EncryptedLogin, Login, LoginEntry, LoginEntr
88
use crate::schema;
99
use crate::LoginsSyncEngine;
1010
use parking_lot::Mutex;
11+
use sql_support::run_maintenance;
1112
use std::path::Path;
1213
use std::sync::{Arc, Weak};
1314
use sync15::{
@@ -276,6 +277,13 @@ impl LoginStore {
276277
self.db.lock().get_meta(schema::CHECKPOINT_KEY)
277278
}
278279

280+
#[handle_error(Error)]
281+
pub fn run_maintenance(&self) -> ApiResult<()> {
282+
let conn = self.db.lock();
283+
run_maintenance(&conn)?;
284+
Ok(())
285+
}
286+
279287
// This allows the embedding app to say "make this instance available to
280288
// the sync manager". The implementation is more like "offer to sync mgr"
281289
// (thereby avoiding us needing to link with the sync manager) but

components/support/sql/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ pub mod debug_tools {
2020

2121
mod each_chunk;
2222
mod lazy;
23+
mod maintenance;
2324
mod maybe_cached;
2425
pub mod open_database;
2526
mod repeat;
2627

2728
pub use conn_ext::*;
2829
pub use each_chunk::*;
2930
pub use lazy::*;
31+
pub use maintenance::run_maintenance;
3032
pub use maybe_cached::*;
3133
pub use repeat::*;
3234

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
use crate::ConnExt;
6+
use rusqlite::{Connection, Result};
7+
8+
/// Run maintenance on the DB
9+
///
10+
/// `run_maintenance()` is intended to be run during idle time and will take steps to clean up /
11+
/// shrink the database.
12+
pub fn run_maintenance(conn: &Connection) -> Result<()> {
13+
vacuum(conn)?;
14+
conn.execute_one("PRAGMA optimize")?;
15+
conn.execute_one("PRAGMA wal_checkpoint(PASSIVE)")?;
16+
Ok(())
17+
}
18+
19+
/// Run vacuum on the DB
20+
fn vacuum(conn: &Connection) -> Result<()> {
21+
let auto_vacuum_setting: u32 = conn.query_one("PRAGMA auto_vacuum")?;
22+
if auto_vacuum_setting == 2 {
23+
// Ideally, we run an incremental vacuum to delete 2 pages
24+
conn.execute_one("PRAGMA incremental_vacuum(2)")?;
25+
} else {
26+
// If auto_vacuum=incremental isn't set, configure it and run a full vacuum.
27+
error_support::warn!(
28+
"run_maintenance_vacuum: Need to run a full vacuum to set auto_vacuum=incremental"
29+
);
30+
conn.execute_one("PRAGMA auto_vacuum=incremental")?;
31+
conn.execute_one("VACUUM")?;
32+
}
33+
Ok(())
34+
}

0 commit comments

Comments
 (0)