Skip to content

Conversation

@strawgate
Copy link
Owner

@strawgate strawgate commented Oct 28, 2025

Adds a TTL index on the expires_at field in MongoDB collections to enable automatic cleanup of expired entries. MongoDB will delete expired documents approximately every 60 seconds using its built-in background process.

This brings MongoDB's TTL handling in line with other stores like DynamoDB (which has native TTL) and Elasticsearch (which has manual cleanup).

Benefits

  • Automatic cleanup of expired entries without manual intervention
  • Reduced storage usage for expired data
  • Leverages MongoDB's native TTL index feature

Implementation

The TTL index is created during collection setup with expireAfterSeconds=0, meaning documents will be deleted when the current time reaches the value in the expires_at field.

Related to #148


🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Automatic entry expiration: Key-value entries now automatically expire and are removed from the database when their expiration time is reached, improving data freshness and reducing manual cleanup requirements
    • Enhanced database performance: Optimized indexing strategies significantly improve the efficiency and speed of key lookup operations in the database

Adds a TTL index on the `expires_at` field in MongoDB collections to enable
automatic cleanup of expired entries. MongoDB will delete expired documents
approximately every 60 seconds using its built-in background process.

This brings MongoDB's TTL handling in line with other stores like DynamoDB
(which has native TTL) and Elasticsearch (which has manual cleanup).

Benefits:
- Automatic cleanup of expired entries without manual intervention
- Reduced storage usage for expired data
- Leverages MongoDB's native TTL index feature

Related to #148

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: William Easton <[email protected]>
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 28, 2025

📝 Walkthrough

Walkthrough

Two MongoDB store implementations (async and sync variants) now create indexes during collection setup: a standard index on the "key" field for lookups and a TTL index on "expires_at" with expireAfterSeconds set to 0 for automatic document expiration. No control flow modifications.

Changes

Cohort / File(s) Summary
MongoDB Collection Indexing
key-value/key-value-aio/src/key_value/aio/stores/mongodb/store.py, key-value/key-value-sync/src/key_value/sync/code_gen/stores/mongodb/store.py
Added two indexes during collection setup: a standard index on "key" field for efficient key lookups, and a TTL index on "expires_at" field with expireAfterSeconds=0 to automatically expire documents when expiration time is reached

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The PR title "feat: add TTL index to MongoDB store for automatic expiration" clearly and accurately summarizes the main change in the pull request. The title specifically identifies the feature being added (TTL index), the component affected (MongoDB store), and the intended benefit (automatic expiration). It is concise, specific, and non-generic—a developer scanning the commit history would understand that this PR introduces MongoDB TTL indexing functionality to enable automatic document expiration. The title correctly prioritizes the primary objective over secondary implementation details like maintaining the existing key field index.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch claude/issue-148-20251027-1750

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e2d9143 and 320f575.

📒 Files selected for processing (2)
  • key-value/key-value-aio/src/key_value/aio/stores/mongodb/store.py (1 hunks)
  • key-value/key-value-sync/src/key_value/sync/code_gen/stores/mongodb/store.py (1 hunks)
🧰 Additional context used
📓 Path-based instructions (6)
**/*.py

📄 CodeRabbit inference engine (AGENTS.md)

**/*.py: Python code must adhere to Ruff formatting and linting with a maximum line length of 140 characters
Use Basedpyright in strict mode for type checking
Use Beartype for runtime type checking (can be disabled via PY_KEY_VALUE_DISABLE_BEARTYPE=true)
Code must be compatible with Python 3.10+ (codegen targets 3.10)

Files:

  • key-value/key-value-sync/src/key_value/sync/code_gen/stores/mongodb/store.py
  • key-value/key-value-aio/src/key_value/aio/stores/mongodb/store.py
key-value/key-value-sync/**

📄 CodeRabbit inference engine (AGENTS.md)

Never edit files in key-value/key-value-sync; the sync package is generated and changes will be overwritten

Files:

  • key-value/key-value-sync/src/key_value/sync/code_gen/stores/mongodb/store.py
{key-value/key-value-aio/src/key_value/aio/stores/**,key-value/key-value-sync/src/key_value/sync/code_gen/stores/**}

📄 CodeRabbit inference engine (AGENTS.md)

Store implementations must store ManagedEntry wrappers, not raw values

Files:

  • key-value/key-value-sync/src/key_value/sync/code_gen/stores/mongodb/store.py
  • key-value/key-value-aio/src/key_value/aio/stores/mongodb/store.py
key-value/key-value-sync/src/key_value/sync/code_gen/stores/**

📄 CodeRabbit inference engine (AGENTS.md)

Sync store implementations must conform to the KeyValue protocol (generated mirror) from key_value/sync/code_gen/protocols/key_value.py

Files:

  • key-value/key-value-sync/src/key_value/sync/code_gen/stores/mongodb/store.py
key-value/key-value-aio/**

📄 CodeRabbit inference engine (AGENTS.md)

Async-first: Make all code changes in the async package (key-value/key-value-aio) and regenerate sync

Files:

  • key-value/key-value-aio/src/key_value/aio/stores/mongodb/store.py
key-value/key-value-aio/src/key_value/aio/stores/**

📄 CodeRabbit inference engine (AGENTS.md)

Async store implementations must conform to the AsyncKeyValue protocol from key_value/aio/protocols/key_value.py

Files:

  • key-value/key-value-aio/src/key_value/aio/stores/mongodb/store.py
🧬 Code graph analysis (1)
key-value/key-value-sync/src/key_value/sync/code_gen/stores/mongodb/store.py (1)
key-value/key-value-sync/src/key_value/sync/code_gen/stores/base.py (1)
  • keys (355-361)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (10)
  • GitHub Check: test_quick (3.10, windows-2022, key-value/key-value-sync)
  • GitHub Check: test_quick (3.10, windows-latest, key-value/key-value-aio)
  • GitHub Check: test_quick (3.10, windows-latest, key-value/key-value-sync)
  • GitHub Check: test_quick (3.10, windows-2022, key-value/key-value-aio)
  • GitHub Check: test_quick (3.10, macos-latest, key-value/key-value-aio)
  • GitHub Check: test_quick (3.10, ubuntu-latest, key-value/key-value-aio)
  • GitHub Check: test_quick (3.10, ubuntu-22.04, key-value/key-value-aio)
  • GitHub Check: test_quick (3.10, macos-14, key-value/key-value-aio)
  • GitHub Check: test_quick (3.10, ubuntu-latest, key-value/key-value-sync)
  • GitHub Check: test_quick (3.10, ubuntu-22.04, key-value/key-value-sync)
🔇 Additional comments (2)
key-value/key-value-aio/src/key_value/aio/stores/mongodb/store.py (2)

177-178: LGTM: Key index added for efficient lookups.

The standard index on the "key" field will optimize key-based queries. This is correctly placed after collection creation and only for new collections.


180-181: LGTM: TTL index correctly configured.

The TTL index with expireAfterSeconds=0 will cause MongoDB to automatically delete documents when the current time reaches the expires_at value. This aligns with the PR objective to leverage MongoDB's native TTL feature for automatic cleanup.


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

@sonarqubecloud
Copy link

@strawgate strawgate merged commit 681aa5e into main Oct 28, 2025
79 checks passed
@strawgate strawgate deleted the claude/issue-148-20251027-1750 branch October 28, 2025 19:16
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.

2 participants