Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vacuum the sqlite rpmdb if necessary #3452

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions lib/backend/sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <sqlite3.h>
#include <fcntl.h>
#include <inttypes.h>

#include <rpm/rpmlog.h>
#include <rpm/rpmfileutil.h>
Expand Down Expand Up @@ -185,6 +186,21 @@ static int sqlite_init(rpmdb rdb, const char * dbhome)
return rc;
}

static int64_t sqlite_free_space_kb(sqlite3 * db)
{
int64_t result = 0;
sqlite3_stmt *s = NULL;
const char *cmd = "SELECT (freelist_count*page_size) as FreeSizeEstimate"
" FROM pragma_freelist_count, pragma_page_size";
if (sqlite3_prepare_v2(db, cmd, -1, &s, NULL) == SQLITE_OK) {
while (sqlite3_step(s) == SQLITE_ROW) {
result = sqlite3_column_int64(s, 0);
}
sqlite3_finalize(s);
}
return result / 1024;
}

static int sqlite_fini(rpmdb rdb)
{
int rc = 0;
Expand All @@ -194,8 +210,21 @@ static int sqlite_fini(rpmdb rdb)
rdb->db_opens--;
} else {
if (sqlite3_db_readonly(sdb, NULL) == 0) {

sqlexec(sdb, "PRAGMA optimize");
sqlexec(sdb, "PRAGMA wal_checkpoint = TRUNCATE");

int max_size = rpmExpandNumeric("%{?_sqlite_vacuum_kb}");
if (max_size <= 0)
max_size = 20*1024;
int64_t free_space = sqlite_free_space_kb(sdb);

if (free_space > max_size) {
sqlexec(sdb, "VACUUM");
rpmlog(RPMLOG_DEBUG, "rpmdb sqlite backend VACUUM maxfree:"
" %ikB, free: %" PRIu64 "kB -> %" PRIu64 "kB\n",
max_size, free_space, sqlite_free_space_kb(sdb));
}
}
rdb->db_dbenv = NULL;
int xx = sqlite3_close(sdb);
Expand Down
17 changes: 17 additions & 0 deletions tests/rpmdb.at
Original file line number Diff line number Diff line change
Expand Up @@ -683,3 +683,20 @@ runroot rpm -q 'versiontest-1.0~2-1'
[])

RPMTEST_CLEANUP

# ------------------------------
AT_SETUP([rpmdb vacuum])
AT_KEYWORDS([install rpmdb sqlite])
RPMDB_INIT
RPMTEST_CHECK([
runroot rpm -U --noscripts --nodeps --ignorearch --noverify \
/data/RPMS/hello-1.0-1.i386.rpm
runroot rpm -D "_sqlite_vacuum_kb 1" -vv -U --noscripts --nodeps --ignorearch \
/data/RPMS/hello-2.0-1.i686.rpm 2>&1 | grep VACUUM
],
[0],
[D: VACUUM: 0
D: rpmdb sqlite backend VACUUM maxfree: 1kB, free: 8kB -> 0kB
],
[])
RPMTEST_CLEANUP
Loading