From 750cba83513488775989f80df4c1fcf9c2642bcb Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Thu, 19 Mar 2026 08:40:09 -0400 Subject: [PATCH] [db] Fix database missing indexes --- build-aux/com.ranfdev.Notify.Devel.json | 77 ++++++++++--------- build-aux/com.ranfdev.Notify.Devel.json~ | 68 ++++++++++++++++ .../src/message_repo/migrations/01.sql | 1 + .../src/message_repo/migrations/02.sql | 3 + ntfy-daemon/src/message_repo/mod.rs | 42 +++++++--- ntfy-daemon/src/subscription.rs | 6 +- 6 files changed, 146 insertions(+), 51 deletions(-) create mode 100644 build-aux/com.ranfdev.Notify.Devel.json~ create mode 100644 ntfy-daemon/src/message_repo/migrations/01.sql create mode 100644 ntfy-daemon/src/message_repo/migrations/02.sql diff --git a/build-aux/com.ranfdev.Notify.Devel.json b/build-aux/com.ranfdev.Notify.Devel.json index 5a85fd9..8e7a027 100644 --- a/build-aux/com.ranfdev.Notify.Devel.json +++ b/build-aux/com.ranfdev.Notify.Devel.json @@ -1,14 +1,14 @@ { - "id": "com.ranfdev.Notify.Devel", - "runtime": "org.gnome.Platform", - "runtime-version": "47", - "sdk": "org.gnome.Sdk", - "sdk-extensions": [ + "id" : "com.ranfdev.Notify.Devel", + "runtime" : "org.gnome.Platform", + "runtime-version" : "47", + "sdk" : "org.gnome.Sdk", + "sdk-extensions" : [ "org.freedesktop.Sdk.Extension.rust-stable", "org.freedesktop.Sdk.Extension.llvm18" ], - "command": "notify", - "finish-args": [ + "command" : "notify", + "finish-args" : [ "--share=ipc", "--share=network", "--socket=fallback-x11", @@ -19,48 +19,51 @@ "--env=RUST_BACKTRACE=1", "--talk-name=org.freedesktop.Notifications" ], - "build-options": { - "append-path": "/usr/lib/sdk/rust-stable/bin:/usr/lib/sdk/llvm18/bin", - "build-args": [ + "build-options" : { + "append-path" : "/usr/lib/sdk/rust-stable/bin:/usr/lib/sdk/llvm18/bin", + "build-args" : [ "--share=network" ], - "env": { - "CARGO_REGISTRIES_CRATES_IO_PROTOCOL": "sparse", - "CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER": "clang", - "CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUSTFLAGS": "-C link-arg=-fuse-ld=/usr/lib/sdk/rust-stable/bin/mold", - "CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER": "clang", - "CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_RUSTFLAGS": "-C link-arg=-fuse-ld=/usr/lib/sdk/rust-stable/bin/mold" + "env" : { + "CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER" : "clang", + "CARGO_REGISTRIES_CRATES_IO_PROTOCOL" : "sparse", + "CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUSTFLAGS" : "-C link-arg=-fuse-ld=/usr/lib/sdk/rust-stable/bin/mold", + "CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER" : "clang", + "CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_RUSTFLAGS" : "-C link-arg=-fuse-ld=/usr/lib/sdk/rust-stable/bin/mold" }, - "test-args": [ + "test-args" : [ "--socket=x11", "--share=network" ] }, - "modules": [ + "modules" : [ { - "name": "blueprint-compiler", - "buildsystem": "meson", - "cleanup": ["*"], - "sources": [ - { - "type": "git", - "url": "https://gitlab.gnome.org/jwestman/blueprint-compiler", - "tag": "v0.14.0", - "commit": "8e10fcf8692108b9d4ab78f41086c5d7773ef864" - } - ] + "name" : "blueprint-compiler", + "buildsystem" : "meson", + "cleanup" : [ + "*" + ], + "sources" : [ + { + "type" : "git", + "url" : "https://gitlab.gnome.org/jwestman/blueprint-compiler", + "tag" : "v0.14.0", + "commit" : "8e10fcf8692108b9d4ab78f41086c5d7773ef864" + } + ] }, { - "name": "notify", - "buildsystem": "meson", - "run-tests": true, - "config-opts": [ - "-Dprofile=development" + "name" : "notify", + "buildsystem" : "meson", + "run-tests" : true, + "config-opts" : [ + "-Dprofile=development", + "--libdir=lib" ], - "sources": [ + "sources" : [ { - "type": "dir", - "path": "../" + "type" : "dir", + "path" : "../" } ] } diff --git a/build-aux/com.ranfdev.Notify.Devel.json~ b/build-aux/com.ranfdev.Notify.Devel.json~ new file mode 100644 index 0000000..5a85fd9 --- /dev/null +++ b/build-aux/com.ranfdev.Notify.Devel.json~ @@ -0,0 +1,68 @@ +{ + "id": "com.ranfdev.Notify.Devel", + "runtime": "org.gnome.Platform", + "runtime-version": "47", + "sdk": "org.gnome.Sdk", + "sdk-extensions": [ + "org.freedesktop.Sdk.Extension.rust-stable", + "org.freedesktop.Sdk.Extension.llvm18" + ], + "command": "notify", + "finish-args": [ + "--share=ipc", + "--share=network", + "--socket=fallback-x11", + "--socket=wayland", + "--device=dri", + "--env=RUST_LOG=notify=debug,ntfy_daemon=debug", + "--env=G_MESSAGES_DEBUG=none", + "--env=RUST_BACKTRACE=1", + "--talk-name=org.freedesktop.Notifications" + ], + "build-options": { + "append-path": "/usr/lib/sdk/rust-stable/bin:/usr/lib/sdk/llvm18/bin", + "build-args": [ + "--share=network" + ], + "env": { + "CARGO_REGISTRIES_CRATES_IO_PROTOCOL": "sparse", + "CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER": "clang", + "CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUSTFLAGS": "-C link-arg=-fuse-ld=/usr/lib/sdk/rust-stable/bin/mold", + "CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER": "clang", + "CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_RUSTFLAGS": "-C link-arg=-fuse-ld=/usr/lib/sdk/rust-stable/bin/mold" + }, + "test-args": [ + "--socket=x11", + "--share=network" + ] + }, + "modules": [ + { + "name": "blueprint-compiler", + "buildsystem": "meson", + "cleanup": ["*"], + "sources": [ + { + "type": "git", + "url": "https://gitlab.gnome.org/jwestman/blueprint-compiler", + "tag": "v0.14.0", + "commit": "8e10fcf8692108b9d4ab78f41086c5d7773ef864" + } + ] + }, + { + "name": "notify", + "buildsystem": "meson", + "run-tests": true, + "config-opts": [ + "-Dprofile=development" + ], + "sources": [ + { + "type": "dir", + "path": "../" + } + ] + } + ] +} diff --git a/ntfy-daemon/src/message_repo/migrations/01.sql b/ntfy-daemon/src/message_repo/migrations/01.sql new file mode 100644 index 0000000..706b78d --- /dev/null +++ b/ntfy-daemon/src/message_repo/migrations/01.sql @@ -0,0 +1 @@ +CREATE INDEX IF NOT EXISTS message_server_topic_time ON message (server, topic, data ->> '$.time'); diff --git a/ntfy-daemon/src/message_repo/migrations/02.sql b/ntfy-daemon/src/message_repo/migrations/02.sql new file mode 100644 index 0000000..de049ec --- /dev/null +++ b/ntfy-daemon/src/message_repo/migrations/02.sql @@ -0,0 +1,3 @@ +ALTER TABLE message ADD COLUMN timestamp INTEGER AS (data ->> '$.time') STORED; + +CREATE INDEX IF NOT EXISTS message_server_topic_timestamp ON message (server, topic, timestamp); diff --git a/ntfy-daemon/src/message_repo/mod.rs b/ntfy-daemon/src/message_repo/mod.rs index 866689a..169ba08 100644 --- a/ntfy-daemon/src/message_repo/mod.rs +++ b/ntfy-daemon/src/message_repo/mod.rs @@ -27,10 +27,10 @@ impl Db { Ok(this) } fn migrate(&mut self) -> Result<()> { - self.conn - .read() - .unwrap() - .execute_batch(include_str!("./migrations/00.sql"))?; + let conn = self.conn.read().unwrap(); + conn.execute_batch(include_str!("./migrations/00.sql"))?; + conn.execute_batch(include_str!("./migrations/01.sql"))?; + conn.execute_batch(include_str!("./migrations/02.sql"))?; Ok(()) } fn get_or_insert_server(&mut self, server: &str) -> Result { @@ -77,21 +77,41 @@ impl Db { server: &str, topic: &str, since: u64, + limit: Option, + offset: Option, ) -> Result, rusqlite::Error> { let conn = self.conn.read().unwrap(); - let mut stmt = conn.prepare( + let mut query = String::from( " SELECT data FROM subscription sub JOIN server s ON sub.server = s.id JOIN message m ON m.server = sub.server AND m.topic = sub.topic - WHERE s.endpoint = ?1 AND m.topic = ?2 AND m.data ->> 'time' >= ?3 - ORDER BY m.data ->> 'time' + WHERE s.endpoint = ?1 AND m.topic = ?2 AND m.timestamp >= ?3 + ORDER BY m.timestamp ", - )?; - let msgs: Result, _> = stmt - .query_map(params![server, topic, since], |row| row.get(0))? - .collect(); + ); + if limit.is_some() { + query.push_str(" LIMIT ?4"); + if offset.is_some() { + query.push_str(" OFFSET ?5"); + } + } + let mut stmt = conn.prepare(&query)?; + + let msgs: Result, _> = if let (Some(limit), Some(offset)) = (limit, offset) { + stmt.query_map(params![server, topic, since, limit, offset], |row| { + row.get(0) + })? + .collect() + } else if let Some(limit) = limit { + stmt.query_map(params![server, topic, since, limit], |row| row.get(0))? + .collect() + } else { + stmt.query_map(params![server, topic, since], |row| row.get(0))? + .collect() + }; + msgs } pub fn insert_subscription(&mut self, sub: models::Subscription) -> Result<(), Error> { diff --git a/ntfy-daemon/src/subscription.rs b/ntfy-daemon/src/subscription.rs index 9643443..7d45ab9 100644 --- a/ntfy-daemon/src/subscription.rs +++ b/ntfy-daemon/src/subscription.rs @@ -177,9 +177,9 @@ impl SubscriptionActor { debug!(topic=?self.model.topic, "attaching new listener"); let messages = self .env - .db - .list_messages(&self.model.server, &self.model.topic, 0) - .unwrap_or_default(); + .db + .list_messages(&self.model.server, &self.model.topic, 0, None, None) + .unwrap_or_default(); let mut previous_events: Vec = messages .into_iter() .filter_map(|msg| {