Skip to content

Remove reference changes are lost #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 28, 2025
Merged

Conversation

myieye
Copy link
Contributor

@myieye myieye commented Mar 19, 2025

Resolves #30

Instead of dropping snapshots, this PR refactors the SnapshotWorker so that new snapshots triggered by deletes are handled by the same code that handles "normal" snapshots triggered by changes.

In theory we could take it even farther with something like:
private async Task UpdateEntity(Id, Action<Entity, ChangeContext, ...> updater) which would find the current entity, let the caller do there thing and then generate a new snapshot afterwards, checking whether it was deleted etc.

But, it's not quite that simple, so I didn't go for it in this PR.

Summary by CodeRabbit

  • Tests

    • Expanded test coverage to validate robust handling of simultaneous updates and deletions, ensuring improved data integrity.
  • Refactor

    • Streamlined internal change tracking and snapshot generation processes, enhancing overall system stability and reliability.

Copy link

coderabbitai bot commented Mar 19, 2025

Walkthrough

The pull request introduces two new test methods in the DataModelReferenceTests class to validate add/delete and update/delete behaviors in a single commit. Additionally, the ChangeContext class has been refactored to include new parameters and properties for improved internal state management, and the SnapshotWorker has been updated to use the enhanced ChangeContext for snapshot generation and deletion logic during commit processing.

Changes

Files Change Summary
src/SIL.Harmony.Tests/…/DataModelReferenceTests.cs Added two new test methods: one verifies that adding and deleting an entity in the same commit marks the definition as deleted, and the other verifies that an update followed by deletion correctly retains updates in the definition.
src/SIL.Harmony/…/ChangeContext.cs
src/SIL.Harmony/…/SnapshotWorker.cs
Modified ChangeContext by changing its access modifier, updating its constructor to accept commitIndex and intermediateSnapshots, and adding corresponding public properties.
Updated SnapshotWorker to replace direct commit usage with the new ChangeContext, including a new method for snapshot generation and updated deletion logic.

Sequence Diagram(s)

sequenceDiagram
    participant T as Test Method
    participant W as WriteNextChange
    participant CTX as ChangeContext
    participant SW as SnapshotWorker

    T->>W: Execute add/update/delete operations
    W->>CTX: Initialize ChangeContext with commitIndex & intermediateSnapshots
    CTX->>SW: Pass change context for commit processing
    SW->>SW: Generate snapshot for updated entity (GenerateSnapshotForEntity)
    SW->>CTX: Mark entity as deleted using updated ChangeContext
    T->>W: Validate entity state (reference, updated part-of-speech, and deletion timestamp)
Loading

Assessment against linked issues

Objective Addressed Explanation
Ensure proper snapshot retention when entities are updated or created in the same commit (#30)

Possibly related PRs

Suggested reviewers

  • hahn-kev

Poem

In fields of code, I hop with glee,
New tests and snapshots, a joy to see.
Each commit a nibble on crunchy byte,
With deleted marks glowing in the night.
Hop along through loops and checks,
As I dance with changes and avoid all wrecks.
🐰 Code hops free, forever in flight!

✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@myieye myieye force-pushed the remove-reference-changes-are-lost branch from 63db780 to 27fc53f Compare March 19, 2025 12:14
@myieye myieye changed the base branch from main to bug/snapshots-referencing-misses-current-commit March 19, 2025 15:27
@myieye myieye marked this pull request as ready for review March 19, 2025 15:27
Copy link
Collaborator

@hahn-kev hahn-kev left a comment

Choose a reason for hiding this comment

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

looks good, the slight refactoring of intermediate snapshots has made me even more uncomfortable with it than I already was... not sure what to do about that though.

Base automatically changed from bug/snapshots-referencing-misses-current-commit to main March 24, 2025 12:51
@myieye myieye mentioned this pull request Mar 24, 2025
@myieye myieye force-pushed the remove-reference-changes-are-lost branch from 5105ce7 to 4009ed7 Compare March 24, 2025 16:24
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 55f4ac3 and 4009ed7.

📒 Files selected for processing (3)
  • src/SIL.Harmony.Tests/DataModelReferenceTests.cs (1 hunks)
  • src/SIL.Harmony/Changes/ChangeContext.cs (1 hunks)
  • src/SIL.Harmony/SnapshotWorker.cs (4 hunks)
🧰 Additional context used
🧬 Code Definitions (3)
src/SIL.Harmony.Tests/DataModelReferenceTests.cs (2)
src/SIL.Harmony.Sample/Models/Word.cs (2)
  • Guid (16-25)
  • Word (5-52)
src/SIL.Harmony/DataModel.cs (2)
  • DataModel (14-350)
  • DataModel (31-43)
src/SIL.Harmony/SnapshotWorker.cs (2)
src/SIL.Harmony/Changes/ChangeContext.cs (4)
  • ChangeContext (5-29)
  • ChangeContext (10-17)
  • ValueTask (23-23)
  • IObjectBase (28-28)
src/SIL.Harmony/Db/ObjectSnapshot.cs (2)
  • ObjectSnapshot (32-80)
  • ObjectSnapshot (34-48)
src/SIL.Harmony/Changes/ChangeContext.cs (4)
src/SIL.Harmony/SnapshotWorker.cs (3)
  • SnapshotWorker (11-257)
  • SnapshotWorker (20-29)
  • SnapshotWorker (46-50)
src/SIL.Harmony/CrdtConfig.cs (2)
  • CrdtConfig (12-94)
  • CrdtConfig (31-37)
src/SIL.Harmony/Commit.cs (3)
  • Commit (7-41)
  • Commit (17-21)
  • Commit (28-31)
src/SIL.Harmony/Db/ObjectSnapshot.cs (2)
  • ObjectSnapshot (32-80)
  • ObjectSnapshot (34-48)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: build
  • GitHub Check: build
  • GitHub Check: Analyze (csharp)
🔇 Additional comments (9)
src/SIL.Harmony.Tests/DataModelReferenceTests.cs (2)

88-104: Well-structured test for reference deletion in a single commit

This test properly validates that when a word and its definition are added and the word is immediately deleted in the same commit, the definition should still exist but be marked as deleted. This directly addresses the PR objective of ensuring reference changes aren't lost during delete operations.


106-128: Comprehensive test for update and delete operations in a single commit

This test effectively builds on the first test by adding complexity - it confirms that when a definition's part of speech is updated and its parent word is deleted within the same commit, both operations are properly processed. The definition retains its updated part of speech while also being marked as deleted.

src/SIL.Harmony/SnapshotWorker.cs (5)

83-83: Proper context enrichment for commit processing

The enhanced ChangeContext now receives additional parameters, allowing for better state management during commit processing.


101-101: Improved deletion handling and snapshot generation

By using the expanded context object and the new GenerateSnapshotForEntity method, the code now ensures that snapshots created as a result of delete operations are processed with the same logic as regular snapshots.

Also applies to: 103-104


116-117: Updated method signature for better context preservation

Changed the MarkDeleted method to accept a ChangeContext instead of just a Commit, allowing it to access the enhanced context information. This enables consistent handling of snapshots during deletion operations.

Also applies to: 123-123


132-133: Consistent snapshot generation and deletion handling

The consistent use of GenerateSnapshotForEntity and passing the full context to MarkDeleted ensures that recursive deletion operations maintain the same contextual awareness.

Also applies to: 137-138


199-226: Well-encapsulated snapshot generation logic

The extraction of snapshot generation into a dedicated method greatly improves code organization and maintainability. The detailed comments explain the snapshot lifecycle well, and the logic for determining when to create root snapshots and when to preserve intermediate snapshots is clear.

The conditional saving of intermediate snapshots based on commit index parity is an interesting optimization for controlling snapshot frequency.

src/SIL.Harmony/Changes/ChangeContext.cs (2)

5-5: Appropriate access restriction for internal implementation details

Changing the class from public to internal is appropriate as it prevents external code from depending on these implementation details.


10-17: Enhanced context construction for better state management

The constructor now accepts additional parameters to support the refactored snapshot generation logic, properly initializing the new properties.

@myieye myieye merged commit b092b25 into main Mar 28, 2025
5 checks passed
@myieye myieye deleted the remove-reference-changes-are-lost branch March 28, 2025 10:05
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.

Changes caused by RemoveReference are lost if entity was created or updated in same commit
2 participants