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

Use precomputed gravity count from database #688

Merged
merged 3 commits into from
Feb 5, 2020
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
19 changes: 15 additions & 4 deletions src/database/gravity-db.c
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,15 @@ int gravityDB_count(const unsigned char list)

char *querystr = NULL;
// Build correct query string to be used depending on list to be read
if(asprintf(&querystr, "SELECT count(DISTINCT domain) FROM %s", tablename[list]) < 18)
if(list != GRAVITY_TABLE && asprintf(&querystr, "SELECT COUNT(DISTINCT domain) FROM %s", tablename[list]) < 18)
{
logg("readGravity(%u) - asprintf() error", list);
return false;
}
// We get the number of unique gravity domains as counted and stored by gravity. Counting the number
// of distinct domains in vw_gravity may take up to several minutes for very large blocking lists on
// very low-end devices such as the Raspierry Pi Zero
else if(list == GRAVITY_TABLE && asprintf(&querystr, "SELECT value FROM info WHERE property = 'gravity_count';") < 18)
{
logg("readGravity(%u) - asprintf() error", list);
return false;
Expand All @@ -445,7 +453,7 @@ int gravityDB_count(const unsigned char list)
int rc = sqlite3_prepare_v2(gravity_db, querystr, -1, &table_stmt, NULL);
if(rc != SQLITE_OK){
logg("gravityDB_count(%s) - SQL error prepare (%i): %s", querystr, rc, sqlite3_errmsg(gravity_db));
sqlite3_finalize(table_stmt);
gravityDB_finalizeTable();
gravityDB_close();
free(querystr);
return DB_FAILED;
Expand All @@ -455,8 +463,11 @@ int gravityDB_count(const unsigned char list)
rc = sqlite3_step(table_stmt);
if(rc != SQLITE_ROW){
logg("gravityDB_count(%s) - SQL error step (%i): %s", querystr, rc, sqlite3_errmsg(gravity_db));
sqlite3_finalize(table_stmt);
gravityDB_close();
if(list == GRAVITY_TABLE)
{
logg("Count of gravity domains not available. Please run pihole -g");
}
gravityDB_finalizeTable();
free(querystr);
return DB_FAILED;
}
Expand Down
1 change: 1 addition & 0 deletions test/gravity.db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ INSERT INTO adlist VALUES(1,'https://hosts-file.net/ad_servers.txt',1,1559928803
INSERT INTO gravity VALUES('whitelisted.test.pi-hole.net',1);
INSERT INTO gravity VALUES('gravity-blocked.test.pi-hole.net',1);
INSERT INTO gravity VALUES('discourse.pi-hole.net',1);
INSERT INTO info VALUES("gravity_count",3);

INSERT INTO "group" VALUES(1,0,'Test group',1559928803,1559928803,'A disabled test group');
INSERT INTO domainlist VALUES(7,1,'blacklisted-group-disabled.com',1,1559928803,1559928803,'Entry disabled by a group');
Expand Down