Skip to content

perf(share): replace per-row DB loops with batch operations in shareCap#1738

Open
MinitJain wants to merge 1 commit intoCapSoftware:mainfrom
MinitJain:perf/share-cap-batch-db
Open

perf(share): replace per-row DB loops with batch operations in shareCap#1738
MinitJain wants to merge 1 commit intoCapSoftware:mainfrom
MinitJain:perf/share-cap-batch-db

Conversation

@MinitJain
Copy link
Copy Markdown
Contributor

@MinitJain MinitJain commented Apr 17, 2026

Summary

shareCap was issuing one DELETE and one INSERT per organization/space in sequential for loops — N DB round-trips per call.

Replaced with:

  • Single batch DELETE ... WHERE organizationId IN (...) for orgs to remove
  • Single batch INSERT with all new org entries collected upfront
  • Same pattern for the space block

Before / After

Before: O(n) DB calls where n = number of orgs/spaces being updated

After: Max 4 DB calls total (1 delete + 1 insert for orgs, 1 delete + 1 insert for spaces), regardless of n

Test plan

  • Share a cap with multiple organizations — verify shares created/removed correctly
  • Share a cap with multiple spaces — verify space memberships update correctly
  • Share a cap with no new orgs/spaces — verify no-op path works

🤖 Generated with Claude Code

Greptile Summary

This PR replaces sequential per-row DELETE/INSERT loops in shareCap with batch Drizzle ORM operations (inArray-based delete + bulk insert), reducing DB round-trips from O(n) to a constant max of 4 calls. The refactoring preserves identical semantics — the set-reconciliation logic (delete current minus desired, insert desired minus current) is unchanged for both org shares and space video associations.

Confidence Score: 5/5

Safe to merge — batch refactor is semantically equivalent to the original loops with no logic regressions.

All changed code is a clean set-reconciliation batch optimization. The empty-array guard (length > 0) prevents invalid inArray calls, organizationId is NOT NULL in the schema so no null-safety issues, and the filter logic (delete current minus desired, insert desired minus current) is identical to the original loops. No P0 or P1 findings.

No files require special attention.

Important Files Changed

Filename Overview
apps/web/actions/caps/share.ts Batch optimization replacing per-row loops with inArray deletes and bulk inserts; logic is semantically equivalent and correctly guarded against empty-array inArray calls.

Sequence Diagram

sequenceDiagram
    participant Client
    participant shareCap
    participant DB

    Client->>shareCap: shareCap({ capId, spaceIds, public })
    shareCap->>DB: SELECT user, cap, orgMemberships
    shareCap->>DB: SELECT directOrgIds (inArray orgs)
    shareCap->>DB: SELECT spacesData (inArray spaces)
    shareCap->>DB: SELECT currentSharedOrganizations WHERE videoId=capId
    alt orgIdsToRemove.length > 0
        shareCap->>DB: DELETE sharedVideos WHERE videoId=capId AND orgId IN (...)
    end
    alt newOrgEntries.length > 0
        shareCap->>DB: INSERT INTO sharedVideos (bulk)
    end
    shareCap->>DB: SELECT currentSpaceVideos WHERE videoId=capId
    alt spaceIdsToRemove.length > 0
        shareCap->>DB: DELETE spaceVideos WHERE videoId=capId AND spaceId IN (...)
    end
    alt newSpaceEntries.length > 0
        shareCap->>DB: INSERT INTO spaceVideos (bulk)
    end
    opt isPublic is boolean
        shareCap->>DB: UPDATE videos SET public=isPublic WHERE id=capId
    end
    shareCap-->>Client: { success: true }
Loading

Reviews (1): Last reviewed commit: "perf(share): replace per-row DB loops wi..." | Re-trigger Greptile

Sequential await db().delete/insert inside for-loops caused N round-trips
per sharing update. Replaced with single batch delete using inArray and
single batch insert with all entries collected upfront.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant