Skip to content

Commit

Permalink
Revert "server/sqlite: don't use content-less fts table"
Browse files Browse the repository at this point in the history
This reverts commit 427bd50.
  • Loading branch information
jasonish committed Aug 8, 2023
1 parent 427bd50 commit 894ea47
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
5 changes: 3 additions & 2 deletions src/commands/sqlite/fts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ fn reindex_fts(tx: &Transaction) -> Result<usize> {
let mut count = 0;
while let Some(row) = rows.next()? {
let rowid: u64 = row.get(0)?;
let timestamp: u64 = row.get(1)?;
let source: String = row.get(2)?;
let source: Value = serde_json::from_str(&source)?;
let flat = extract_values(&source);
Expand All @@ -98,8 +99,8 @@ fn reindex_fts(tx: &Transaction) -> Result<usize> {
params![&flat, rowid],
)?;
tx.execute(
"insert into fts (rowid, source_values) values (?, ?)",
params![rowid, &flat],
"insert into fts (rowid, timestamp, source_values) values (?, ?, ?)",
params![rowid, timestamp, &flat],
)?;

count += 1;
Expand Down
10 changes: 0 additions & 10 deletions src/sqlite/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,6 @@ pub fn init_event_db(db: &mut Connection) -> Result<(), rusqlite::Error> {
tx.commit()?;
} else if !db.has_table("fts")? {
info!("FTS not enabled, consider enabling for query performance improvements");
} else {
check_0_17_broken_fts(db);
}

Ok(())
Expand Down Expand Up @@ -176,14 +174,6 @@ pub(crate) fn get_pragma<T: FromSql>(db: &Connection, name: &str) -> Result<T, r
db.query_row_and_then(&sql, [], |row| row.get(0))
}

fn check_0_17_broken_fts(db: &Connection) {
if let Ok(mut s) = db.prepare("SELECT timestamp FROM fts LIMIT 1") {
if s.query([]).is_ok() {
warn!("Using pre-0.18.0 broken FTS, please disable and re-enable FTS: See https://github.com/jasonish/evebox/issues/279");
}
}
}

mod embedded {
use refinery::embed_migrations;
embed_migrations!("./resources/sqlite/migrations");
Expand Down
8 changes: 6 additions & 2 deletions src/sqlite/importer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,12 @@ pub(crate) fn prepare_sql(
});

if fts {
let statement = "INSERT INTO fts (rowid, source_values) VALUES (last_insert_rowid(), ?)";
let params = vec![SqliteValue::String(source_values)];
let statement =
"INSERT INTO fts (rowid, timestamp, source_values) VALUES (last_insert_rowid(), ?, ?)";
let params = vec![
SqliteValue::I64(ts.unix_timestamp_nanos() as i64),
SqliteValue::String(source_values),
];
statements.push(QueuedStatement {
statement: statement.to_string(),
params,
Expand Down
9 changes: 7 additions & 2 deletions src/sqlite/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
use rusqlite::Transaction;

pub fn fts_create(tx: &Transaction) -> Result<(), rusqlite::Error> {
tx.execute("create virtual table fts using fts5(source_values)", [])?;
tx.execute(
"create virtual table fts
using fts5(timestamp unindexed, source_values, content=events, content_rowid=rowid)",
[],
)?;
tx.execute(
"
create trigger events_ad_trigger after delete on events begin
delete from fts where rowid = old.rowid;
insert into fts(fts, rowid, timestamp, source_values)
values ('delete', old.rowid, old.timestamp, old.source_values);
end",
[],
)?;
Expand Down

0 comments on commit 894ea47

Please sign in to comment.