Skip to content

Reduce redundant DB writes for users and channel configs#6509

Merged
gpunto merged 2 commits into
developfrom
reduce-db-writes-v7
Jun 22, 2026
Merged

Reduce redundant DB writes for users and channel configs#6509
gpunto merged 2 commits into
developfrom
reduce-db-writes-v7

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 configuration and user data being saved to local storage. The system now properly deduplicates entries before storing, retaining the most recent version.
  • Tests

    • Added test coverage to verify deduplication behavior for channel configurations and user data.

@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 the PR is bot-authored.
  • An issue is linked (Linear ticket or GitHub issue), or the PR is bot-authored.

🎉 Great job! This PR is ready for review.

@github-actions

github-actions Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

SDK Size Comparison 📏

SDK Before After Difference Status
stream-chat-android-client 5.90 MB 5.90 MB 0.00 MB 🟢
stream-chat-android-ui-components 11.14 MB 11.14 MB 0.00 MB 🟢
stream-chat-android-compose 12.61 MB 12.61 MB 0.00 MB 🟢

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

coderabbitai Bot commented Jun 19, 2026

Copy link
Copy Markdown

Review Change Stack

Walkthrough

Two repository insert methods now deduplicate incoming collections before writing to the in-memory cache and Room database. DatabaseChannelConfigRepository.insertChannelConfigs deduplicates by ChannelConfig.type, and DatabaseUserRepository.insertUsers deduplicates by User::id, both keeping the last occurrence. Tests are added for each.

Changes

Repository insert deduplication

Layer / File(s) Summary
Deduplication logic in both repositories
...channelconfig/internal/DatabaseChannelConfigRepository.kt, ...user/internal/DatabaseUserRepository.kt
insertChannelConfigs builds a configsByType map to deduplicate by ChannelConfig.type before updating the cache and DAO insert. insertUsers uses associateBy(User::id).values to keep the last occurrence of each user ID before inserting.
Tests for deduplication behavior
...repository/ChannelConfigRepositoryTest.kt, ...repository/UserRepositoryTests.kt
New test in ChannelConfigRepositoryTest inserts three configs with two duplicate types and asserts the DAO receives two deduplicated entities with the last messaging config retained. New test in UserRepositoryTests inserts two copies of the same user ID plus another user and asserts the DAO receives the last copy for the duplicate ID.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐇 Hop, hop, no doubles today!
Two repos kept duplicates at bay,
The last one wins, the map decides,
Clean entities where each type resides.
One key, one value — the rabbit's way! 🗝️

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: deduplicating user and channel config data before DB writes to reduce redundant SQLite operations.
Description check ✅ Passed The PR description covers Goal, Implementation, and Testing sections as required by the template. Goal explains the issue, Implementation details the deduplication strategy, and Testing documents unit tests and manual validation.
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-v7

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@stream-chat-android-client/src/test/java/io/getstream/chat/android/client/internal/offline/repository/UserRepositoryTests.kt`:
- Around line 146-147: The test assertion on line 146 in UserRepositoryTests.kt
is over-constraining by verifying a specific insertion order using
shouldBeEqualTo with listOf(id, other.id), but the actual contract only requires
dedup-by-id and last-wins semantics. Replace the order-dependent assertion with
one that verifies both user entities are present regardless of order (such as
checking that the map contains both ids without asserting list equality), while
keeping the existing assertion on line 147 that validates the entity with the
matching id has the correct "last" name value.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 2185fa27-889e-4a18-94a0-33dc4c8fbf3e

📥 Commits

Reviewing files that changed from the base of the PR and between dcb8199 and d8419f8.

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

@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

@gpunto gpunto force-pushed the reduce-db-writes-v7 branch from d8419f8 to 24c6e42 Compare June 22, 2026 08:45
@gpunto gpunto enabled auto-merge (squash) June 22, 2026 08:46
@sonarqubecloud

Copy link
Copy Markdown

@gpunto gpunto merged commit e8e287b into develop Jun 22, 2026
19 checks passed
@gpunto gpunto deleted the reduce-db-writes-v7 branch June 22, 2026 10:33
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