Skip to content

Reduce redundant DB writes for users and channel configs#6508

Merged
aleksandar-apostolov merged 1 commit into
v6from
reduce-db-writes
Jun 22, 2026
Merged

Reduce redundant DB writes for users and channel configs#6508
aleksandar-apostolov merged 1 commit into
v6from
reduce-db-writes

Conversation

@gpunto

@gpunto gpunto commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Goal

Persisting a channel response fans out into redundant SQLite writes: the same user gets inserted once per reference in the payload, and the channel list rewrites each channel's config once per channel. A customer flagged the volume. This dedups before the DB write, with no behaviour change.

Closes AND-1247

Implementation

  • insertUsers: dedup users by id (Channel.users() lists the same user once per member/author/watcher/etc.).
  • insertChannelConfigs: dedup configs by type (one row per type, not per channel).

Both keep the last occurrence, matching the existing INSERT OR REPLACE behaviour.

Testing

  • New unit tests in UserRepositoryTests and ChannelConfigRepositoryTest covering the dedup.
  • Manual: Compose sample with SQLite logging, confirmed write counts drop to the distinct counts on chat open and channel list load.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed duplicate channel configurations and user records being stored in the database. The system now properly deduplicates these entries, ensuring data consistency.

@gpunto gpunto added the pr:internal Internal changes / housekeeping label Jun 19, 2026
@github-actions

github-actions Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

PR checklist ✅

All required conditions are satisfied:

  • Title length is OK (or ignored by label).
  • At least one pr: label exists.
  • Sections ### Goal, ### Implementation, and ### Testing are filled (or ignored for dependabot PRs).

🎉 Great job! This PR is ready for review.

@github-actions

Copy link
Copy Markdown
Contributor

SDK Size Comparison 📏

SDK Before After Difference Status
stream-chat-android-client 5.26 MB 5.31 MB 0.05 MB 🟢
stream-chat-android-offline 5.49 MB 5.53 MB 0.04 MB 🟢
stream-chat-android-ui-components 10.64 MB 10.74 MB 0.10 MB 🟢
stream-chat-android-compose 12.87 MB 12.95 MB 0.08 MB 🟢

@sonarqubecloud

Copy link
Copy Markdown

@gpunto

gpunto commented Jun 19, 2026

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jun 19, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai

coderabbitai Bot commented Jun 19, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 64cebef9-9abd-4e16-b541-1b5ae022c5b9

📥 Commits

Reviewing files that changed from the base of the PR and between 4e3f0a6 and 70dd558.

📒 Files selected for processing (4)
  • stream-chat-android-offline/src/main/java/io/getstream/chat/android/offline/repository/domain/channelconfig/internal/DatabaseChannelConfigRepository.kt
  • stream-chat-android-offline/src/main/java/io/getstream/chat/android/offline/repository/domain/user/internal/DatabaseUserRepository.kt
  • stream-chat-android-offline/src/test/java/io/getstream/chat/android/offline/repository/ChannelConfigRepositoryTest.kt
  • stream-chat-android-offline/src/test/java/io/getstream/chat/android/offline/repository/UserRepositoryTests.kt

Walkthrough

Two offline repository classes (DatabaseChannelConfigRepository and DatabaseUserRepository) are updated to deduplicate their insert inputs by key (ChannelConfig.type and User.id) before writing to Room, keeping the last occurrence. Corresponding unit tests are added to verify this last-wins deduplication behavior.

Changes

Repository insert deduplication

Layer / File(s) Summary
Deduplication logic in both repository inserts
...channelconfig/internal/DatabaseChannelConfigRepository.kt, ...user/internal/DatabaseUserRepository.kt
insertChannelConfigs now builds a configsByType map and passes only its .values to the in-memory cache and Room DAO. insertUsers builds usersToInsert via associateBy(User::id).values, explicitly retaining the last occurrence per id.
Tests for deduplication behavior
...repository/ChannelConfigRepositoryTest.kt, ...repository/UserRepositoryTests.kt
New @Test cases insert collections with duplicate keys and use argument captors to assert that the DAO receives exactly one entity per key, with the last-seen value preserved.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 Hoppity-hop through the cache we go,
No duplicates left from the data flow!
Last one in wins — that's the rule of the day,
Type by type, id by id, swept away.
The DAO gets clean rows, the tests agree,
One entry per key — as it ought to be! 🎉

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely summarizes the main change: reducing redundant database writes for users and channel configs through deduplication.
Description check ✅ Passed The description covers the Goal, Implementation, and Testing sections with sufficient detail, though some optional template sections like UI Changes and the full Contributor Checklist are not included.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch reduce-db-writes

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gpunto gpunto marked this pull request as ready for review June 19, 2026 13:00
@gpunto gpunto requested a review from a team as a code owner June 19, 2026 13:00

@aleksandar-apostolov aleksandar-apostolov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@aleksandar-apostolov aleksandar-apostolov merged commit f87cac6 into v6 Jun 22, 2026
28 of 31 checks passed
@aleksandar-apostolov aleksandar-apostolov deleted the reduce-db-writes branch June 22, 2026 08:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr:internal Internal changes / housekeeping

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants