Skip to content
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

Allow returning result for write queries #1990

Merged
merged 5 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions sqlitecluster/SQLite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,18 +566,25 @@ bool SQLite::write(const string& query) {
}

// This is literally identical to the idempotent version except for the check for _noopUpdateMode.
return _writeIdempotent(query);
SQResult ignore;
return _writeIdempotent(query, ignore);
MonilBhavsar marked this conversation as resolved.
Show resolved Hide resolved
}

bool SQLite::writeIdempotent(const string& query) {
return _writeIdempotent(query);
SQResult ignore;
return _writeIdempotent(query, ignore);
}

bool SQLite::writeIdempotent(const string& query, SQResult& result) {
return _writeIdempotent(query, result);
}

bool SQLite::writeUnmodified(const string& query) {
return _writeIdempotent(query, true);
SQResult ignore;
return _writeIdempotent(query, ignore, true);
}

bool SQLite::_writeIdempotent(const string& query, bool alwaysKeepQueries) {
bool SQLite::_writeIdempotent(const string& query, SQResult& result, bool alwaysKeepQueries) {
SASSERT(_insideTransaction);
_queryCache.clear();
_queryCount++;
Expand All @@ -600,7 +607,7 @@ bool SQLite::_writeIdempotent(const string& query, bool alwaysKeepQueries) {
{
shared_lock<shared_mutex> lock(_sharedData.writeLock);
if (_enableRewrite) {
resultCode = SQuery(_db, "read/write transaction", query, 2'000'000, true);
resultCode = SQuery(_db, "read/write transaction", query, result, 2'000'000, true);
if (resultCode == SQLITE_AUTH) {
// Run re-written query.
_currentlyRunningRewritten = true;
Expand All @@ -610,7 +617,7 @@ bool SQLite::_writeIdempotent(const string& query, bool alwaysKeepQueries) {
_currentlyRunningRewritten = false;
}
} else {
resultCode = SQuery(_db, "read/write transaction", query);
resultCode = SQuery(_db, "read/write transaction", query, result);
}
}

Expand Down
7 changes: 6 additions & 1 deletion sqlitecluster/SQLite.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include <libstuff/sqlite3.h>
#include <libstuff/SQResult.h>
#include <libstuff/SPerformanceTimer.h>

class SQLite {
Expand Down Expand Up @@ -109,6 +110,10 @@ class SQLite {
// known to be repeatable. What counts as repeatable is up to the individual command.
bool writeIdempotent(const string& query);

// Executes a write query and retrieves the result.
// Designed for use with queries that include a RETURNING clause
bool writeIdempotent(const string& query, SQResult& result);

// This runs a query completely unchanged, always adding it to the uncommitted query, such that it will be recorded
// in the journal even if it had no effect on the database. This lets replicated or synchronized queries be added
// to the journal *even if they have no effect* on the rest of the database.
Expand Down Expand Up @@ -400,7 +405,7 @@ class SQLite {
static thread_local int64_t _conflictPage;
static thread_local string _conflictTable;

bool _writeIdempotent(const string& query, bool alwaysKeepQueries = false);
bool _writeIdempotent(const string& query, SQResult& result, bool alwaysKeepQueries = false);

// Constructs a UNION query from a list of 'query parts' over each of our journal tables.
// Fore each table, queryParts will be joined with that table's name as a separator. I.e., if you have a tables
Expand Down