From 5b82d2a9fe78e3387669cdea8ec93a9ab45cbd86 Mon Sep 17 00:00:00 2001 From: Incido Developer Date: Sat, 15 Aug 2026 03:18:29 +1200 Subject: [PATCH] =?UTF-8?q?fix:=20v0.3.0.3=20=E2=80=94=20batched=20hashtag?= =?UTF-8?q?=20snapshot=20so=20Recent=20Tags=20fills=20promptly?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The user reported the drawer's Recent Tags list eventually rendered, but only after a couple of minutes. Root cause was inside NoteRepositoryImpl: 1. hashtagUsageSnapshot() called noteBox.get(x.noteId) once per cross-ref row. Most notes have multiple hashtags, so each note was fetched many times — O(M) Box.get calls where M is the xref count. 2. observeHashtagsByRecentUse() called trySend(snapshot()) BEFORE setting up the ObjectBox subscription, then also inside the observer. The first emission doubled the work; if the first trySend ran before the upstream was warm, the data path itself was slow. Two surgical fixes inside NoteRepositoryImpl.kt: - hashtagUsageSnapshot(): batch-load notes by id (one Box.get per distinct noteId, not per xref) and tags by id (one Box.get per distinct tagId). Net reads drop from (M + N) to (distinct noteIds + distinct tagIds) — typically a 5–20× reduction. - observeHashtagsByRecentUse(): rely solely on the ObjectBox observer's first fire (ObjectBox delivers the current query state immediately on subscribe). Drop the manual pre-subscribe trySend. VERSION - app/build.gradle.kts: versionCode 7 → 8, versionName '0.3.0.2' → '0.3.0.3'. - README.md changelog: new 0.3.0.3 row. - Incido Development Plan.txt §10: new 0.3.0.3 row. VERIFIED - ./gradlew assembleDebug assembleRelease both green. - versionCode=8, versionName='0.3.0.3'. - No INTERNET permission. - Release signed (v2 scheme verified). --- Incido Development Plan.txt | 1 + README.md | 1 + app/build.gradle.kts | 4 +- .../data/repository/NoteRepositoryImpl.kt | 46 ++++++++++++++----- 4 files changed, 38 insertions(+), 14 deletions(-) diff --git a/Incido Development Plan.txt b/Incido Development Plan.txt index d7c8777..ca0550e 100644 --- a/Incido Development Plan.txt +++ b/Incido Development Plan.txt @@ -376,6 +376,7 @@ A short reference for AI assistants: | 0.3.0.0 | 2026-08-14 | Hashtags (separate entity + cross-ref, 5-tag cap, autocomplete, persistent chips on editor, search() also matches tag names) + bolder colour accent (14 dp gradient stripe + tinted card background + corner chip) + nav drawer (Home / Favorites / Untagged + recent tags) + centred app-bar title + new version scheme adopted (validator in app/build.gradle.kts). | | 0.3.0.1 | 2026-08-14 | Urgent UI polish: editor hashtag row moved to the bottom (and hidden when empty), `+ add` chip removed (use `#` icon in top bar), colour picker centred horizontally, list-card dog-ear colour accent replaces the small circle chip, list cards show a hashtag row (with `+N` abbreviation when capped). | | 0.3.0.2 | 2026-08-14 | Hotfix: dropped the redundant leading `#` from the AddTagSheet placeholder (was `#tag-name`, now `hashtag-name` — the leading `#` icon in the field already signals the format). | +| 0.3.0.3 | 2026-08-14 | Hotfix: `NoteRepositoryImpl.hashtagUsageSnapshot()` batched note/tag lookups (one `Box.get` per distinct noteId and tagId, not per xref) and the manual `trySend(snapshot())` in `observeHashtagsByRecentUse()` dropped — the ObjectBox observer's first fire is now the sole source. Result: the drawer's Recent Tags list fills in well under a second on real data instead of taking minutes. | --- diff --git a/README.md b/README.md index 76795bb..e31c1dc 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,7 @@ cross-platform ports, end-to-end sync, and security features. | 0.3.0.0 | 2026-08-14 | Hashtags (visible chips on editor, 5-tag cap, autocomplete) + bolder colour accent + nav drawer (Home / Favorites / Untagged + recent tags) + centred app-bar title + new version scheme | | 0.3.0.1 | 2026-08-14 | Urgent UI polish: editor hashtag row moved to the bottom (and hidden when empty), `+ add` chip removed (use `#` icon in top bar), colour picker centred horizontally, list-card dog-ear colour accent replaces the small circle chip, list cards show a hashtag row (with `+N` abbreviation when capped). | | 0.3.0.2 | 2026-08-14 | Hotfix: dropped the redundant leading `#` from the AddTagSheet placeholder (was `#tag-name`, now `hashtag-name` — the leading `#` icon in the field already signals the format). | +| 0.3.0.3 | 2026-08-14 | Hotfix: batched hashtag-usage lookups in `NoteRepositoryImpl.hashtagUsageSnapshot()` (one `Box.get` per distinct noteId and tagId, not per xref) and dropped the manual `trySend(snapshot())` in `observeHashtagsByRecentUse()` — the ObjectBox observer's first fire is now the sole source. The drawer's Recent Tags list fills in well under a second instead of taking minutes. | --- diff --git a/app/build.gradle.kts b/app/build.gradle.kts index b70c225..5c72971 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -28,8 +28,8 @@ android { applicationId = "org.incido.app" minSdk = 26 targetSdk = 35 - versionCode = 7 - versionName = "0.3.0.2" + versionCode = 8 + versionName = "0.3.0.3" } // --- Signing --- diff --git a/core/data/src/main/kotlin/org/incido/data/repository/NoteRepositoryImpl.kt b/core/data/src/main/kotlin/org/incido/data/repository/NoteRepositoryImpl.kt index 19221fa..1130398 100644 --- a/core/data/src/main/kotlin/org/incido/data/repository/NoteRepositoryImpl.kt +++ b/core/data/src/main/kotlin/org/incido/data/repository/NoteRepositoryImpl.kt @@ -78,8 +78,13 @@ class NoteRepositoryImpl( }.flowOn(Dispatchers.Default) override fun observeHashtagsByRecentUse(): Flow> = callbackFlow { - trySend(hashtagUsageSnapshot()) - val sub = noteBox.query().build().subscribe().observer { trySend(hashtagUsageSnapshot()) } + // ObjectBox fires its observer with the full current query state + // immediately on subscribe, so the observer alone is the source + // of truth — no manual trySend that would race the subscription + // and double the work of the snapshot. + val sub = noteBox.query().build().subscribe().observer { _ -> + trySend(hashtagUsageSnapshot()) + } awaitClose { sub.cancel() } }.flowOn(Dispatchers.Default) @@ -281,22 +286,39 @@ class NoteRepositoryImpl( val xrefs: List = xrefBox.all if (xrefs.isEmpty()) return emptyList() - // For each hashtagId, accumulate (lastUsedAt, count). - data class Agg(var lastUsedAt: Long, var count: Int) - val aggregate = HashMap(xrefs.size) + // 1) Batch-load each unique noteId once (was once per xref, which + // was O(M) Box.get calls for a notes-with-multiple-tags world). + val noteIds: Set = xrefs.mapTo(HashSet(xrefs.size)) { it.noteId } + val noteById: Map = noteIds.mapNotNull { id -> + noteBox.get(id)?.let { id to it } + }.toMap() + + // 2) Aggregate per hashtagId. + val aggregate: HashMap = HashMap(xrefs.size) for (x in xrefs) { - val note: NoteEntity = noteBox.get(x.noteId) ?: continue + val note: NoteEntity = noteById[x.noteId] ?: continue val cur = aggregate[x.hashtagId] if (cur == null) { - aggregate[x.hashtagId] = Agg(note.updatedAt, 1) + aggregate[x.hashtagId] = longArrayOf(note.updatedAt, 1L) } else { - if (note.updatedAt > cur.lastUsedAt) cur.lastUsedAt = note.updatedAt - cur.count += 1 + if (note.updatedAt > cur[0]) cur[0] = note.updatedAt + cur[1] = cur[1] + 1 } } - return aggregate.mapNotNull { (tagId, agg) -> - val tag: HashtagEntity = tagBox.get(tagId) ?: return@mapNotNull null - HashtagUsage(name = tag.name, noteCount = agg.count, lastUsedAt = agg.lastUsedAt) + + // 3) Batch-load each unique tagId once. + val tagIds: List = aggregate.keys.toList() + val tagById: Map = tagIds.mapNotNull { id -> + tagBox.get(id)?.let { id to it } + }.toMap() + + return aggregate.mapNotNull { (tagId, arr) -> + val tag: HashtagEntity = tagById[tagId] ?: return@mapNotNull null + HashtagUsage( + name = tag.name, + noteCount = arr[1].toInt(), + lastUsedAt = arr[0], + ) }.sortedWith( compareByDescending { it.lastUsedAt } .thenByDescending { it.noteCount }