-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/transactional commands #48
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c37d288
Added INCR Command
unikdahal 8059bcc
Added Transactional Command Discard Exec and Multi Command
unikdahal 44a4e22
Updated ReadmE.md to include transaction
unikdahal b333332
📝 CodeRabbit Chat: Generate unit tests for PR changes
coderabbitai[bot] 5b029f6
Refactored as per PR comments
unikdahal 101c034
Added Integration Tests to the project abd enforced pipeline to pass …
unikdahal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| # DISCARD | ||
|
|
||
| ## Syntax | ||
|
|
||
| ``` | ||
| DISCARD | ||
| ``` | ||
|
|
||
| ## Description | ||
|
|
||
| Flushes all previously queued commands in a transaction and restores the connection state to normal. | ||
|
|
||
| If MULTI was called, DISCARD will abort the transaction. All queued commands are discarded and the client can issue regular commands again. | ||
|
|
||
| ## Return Value | ||
|
|
||
| **Simple string reply:** always `OK`. | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Abort a Transaction | ||
|
|
||
| ``` | ||
| redis> SET key "original" | ||
| OK | ||
| redis> MULTI | ||
| OK | ||
| redis> SET key "modified" | ||
| QUEUED | ||
| redis> GET key | ||
| QUEUED | ||
| redis> DISCARD | ||
| OK | ||
| redis> GET key | ||
| "original" | ||
| ``` | ||
|
|
||
| ### Start New Transaction After DISCARD | ||
|
|
||
| ``` | ||
| redis> MULTI | ||
| OK | ||
| redis> SET foo bar | ||
| QUEUED | ||
| redis> DISCARD | ||
| OK | ||
| redis> MULTI | ||
| OK | ||
| redis> SET foo baz | ||
| QUEUED | ||
| redis> EXEC | ||
| 1) OK | ||
| redis> GET foo | ||
| "baz" | ||
| ``` | ||
|
|
||
| ## Error Handling | ||
|
|
||
| ### DISCARD Without MULTI | ||
|
|
||
| ``` | ||
| redis> DISCARD | ||
| (error) ERR DISCARD without MULTI | ||
| ``` | ||
|
|
||
| ## Implementation Notes | ||
|
|
||
| ### Memory Efficiency | ||
|
|
||
| The queued commands list is cleared but not deallocated when DISCARD is called. This allows the memory to be reused if another transaction starts on the same connection, reducing allocation overhead and GC pressure. | ||
|
|
||
| ## Related Commands | ||
|
|
||
| - [MULTI](MULTI.md) - Start a transaction | ||
| - [EXEC](EXEC.md) - Execute all queued commands |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| # EXEC | ||
|
|
||
| ## Syntax | ||
|
|
||
| ``` | ||
| EXEC | ||
| ``` | ||
|
|
||
| ## Description | ||
|
|
||
| Executes all previously queued commands in a MULTI/EXEC block and restores the connection state to normal. | ||
|
|
||
| When EXEC is called, all commands queued since MULTI are executed atomically. This means that either all commands are processed, or none are (in case of errors during queueing). | ||
|
|
||
| ## Return Value | ||
|
|
||
| **Array reply:** Each element is the reply from each command in the transaction, in the order they were queued. | ||
|
|
||
| **Nil reply:** If EXEC is called without a prior MULTI or if the transaction was aborted due to errors. | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Basic Transaction | ||
|
|
||
| ``` | ||
| redis> MULTI | ||
| OK | ||
| redis> SET key1 "Hello" | ||
| QUEUED | ||
| redis> SET key2 "World" | ||
| QUEUED | ||
| redis> GET key1 | ||
| QUEUED | ||
| redis> GET key2 | ||
| QUEUED | ||
| redis> EXEC | ||
| 1) OK | ||
| 2) OK | ||
| 3) "Hello" | ||
| 4) "World" | ||
| ``` | ||
|
|
||
| ### Counter Transaction | ||
|
|
||
| ``` | ||
| redis> SET counter 0 | ||
| OK | ||
| redis> MULTI | ||
| OK | ||
| redis> INCR counter | ||
| QUEUED | ||
| redis> INCR counter | ||
| QUEUED | ||
| redis> INCR counter | ||
| QUEUED | ||
| redis> GET counter | ||
| QUEUED | ||
| redis> EXEC | ||
| 1) (integer) 1 | ||
| 2) (integer) 2 | ||
| 3) (integer) 3 | ||
| 4) "3" | ||
| ``` | ||
|
|
||
| ## Error Handling | ||
|
|
||
| ### EXEC Without MULTI | ||
|
|
||
| ``` | ||
| redis> EXEC | ||
| (error) ERR EXEC without MULTI | ||
| ``` | ||
|
|
||
| ### Transaction with Queueing Error | ||
|
|
||
| If an error occurs while queueing commands (e.g., syntax error, unknown command), the transaction is aborted: | ||
|
|
||
| ``` | ||
| redis> MULTI | ||
| OK | ||
| redis> SET key value | ||
| QUEUED | ||
| redis> UNKNOWNCOMMAND | ||
| (error) ERR unknown command 'UNKNOWNCOMMAND' | ||
| redis> EXEC | ||
| (error) EXECABORT Transaction discarded because of previous errors. | ||
| ``` | ||
|
|
||
| ### Empty Transaction | ||
|
|
||
| ``` | ||
| redis> MULTI | ||
| OK | ||
| redis> EXEC | ||
| (empty array) | ||
| ``` | ||
|
|
||
| ## Implementation Notes | ||
|
|
||
| ### Optimizations Over Standard Redis | ||
|
|
||
| 1. **Pre-allocated Response Buffer:** The response StringBuilder is pre-sized based on the number of queued commands, minimizing reallocations during execution. | ||
|
|
||
| 2. **Zero Command Lookups:** Commands are resolved and stored at queue time, not during EXEC. This eliminates registry lookups during the critical execution phase. | ||
|
|
||
| 3. **Cache-Friendly Iteration:** Commands are stored in a contiguous ArrayList, providing excellent CPU cache utilization during batch execution. | ||
|
|
||
| 4. **Batch Execution:** All commands execute in a tight loop without intermediate I/O operations, reducing context switches. | ||
|
|
||
| ## Related Commands | ||
|
|
||
| - [MULTI](MULTI.md) - Start a transaction | ||
| - [DISCARD](DISCARD.md) - Abort the transaction |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix markdownlint table spacing for the Transactions section.
markdownlint flags missing blank lines around the table (MD058) and compact table pipe spacing (MD060). Adjust spacing to satisfy the style rules.
📝 Proposed fix
📝 Committable suggestion
🧰 Tools
🪛 markdownlint-cli2 (0.20.0)
[warning] 45-45: Tables should be surrounded by blank lines
(MD058, blanks-around-tables)
[warning] 46-46: Table column style
Table pipe is missing space to the right for style "compact"
(MD060, table-column-style)
[warning] 46-46: Table column style
Table pipe is missing space to the left for style "compact"
(MD060, table-column-style)
[warning] 46-46: Table column style
Table pipe is missing space to the right for style "compact"
(MD060, table-column-style)
[warning] 46-46: Table column style
Table pipe is missing space to the left for style "compact"
(MD060, table-column-style)
[warning] 46-46: Table column style
Table pipe is missing space to the right for style "compact"
(MD060, table-column-style)
[warning] 46-46: Table column style
Table pipe is missing space to the left for style "compact"
(MD060, table-column-style)
🤖 Prompt for AI Agents