Skip to content

Commit

Permalink
improved performance of transaction handles
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaWise committed Apr 8, 2017
1 parent 561b96d commit 404c739
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 54 deletions.
12 changes: 4 additions & 8 deletions src/objects/database/database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Nan::Persistent<v8::Function> NullFactory;

Database::Database() : Nan::ObjectWrap(),
db_handle(NULL),
t_handles(NULL),
t_handles(),
open(true),
in_each(false),
safe_ints(false) {}
Expand Down Expand Up @@ -70,9 +70,8 @@ void Database::Init(v8::Local<v8::Object> exports, v8::Local<v8::Object> module)

// Returns an SQLite3 result code.
int Database::CloseHandles() {
delete t_handles;
t_handles.close();
int status = sqlite3_close(db_handle);
t_handles = NULL;
db_handle = NULL;
return status;
}
Expand All @@ -85,9 +84,7 @@ void Database::CloseChildHandles() {
}

int Database::OpenHandles(const char* filename) {
int status;

status = sqlite3_open(filename, &db_handle);
int status = sqlite3_open(filename, &db_handle);
if (status != SQLITE_OK) {
return status;
}
Expand All @@ -100,6 +97,5 @@ int Database::OpenHandles(const char* filename) {
sqlite3_limit(db_handle, SQLITE_LIMIT_COMPOUND_SELECT, 0x7fffffff);
sqlite3_limit(db_handle, SQLITE_LIMIT_VARIABLE_NUMBER, 0x7fffffff);

t_handles = new TransactionHandles(db_handle, &status);
return status;
return t_handles.open(db_handle);
}
2 changes: 1 addition & 1 deletion src/objects/database/database.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Database : public Nan::ObjectWrap {

// Sqlite3 interfacing
sqlite3* db_handle;
TransactionHandles* t_handles;
TransactionHandles t_handles;

// State
bool open;
Expand Down
1 change: 0 additions & 1 deletion src/objects/database/exec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ NAN_METHOD(Database::Exec) {
sqlite3_free(err);
return Nan::ThrowError(message.c_str());
}
sqlite3_free(err);

info.GetReturnValue().Set(info.This());
}
1 change: 0 additions & 1 deletion src/objects/database/pragma.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ NAN_METHOD(Database::Pragma) {
sqlite3_free(err);
return Nan::ThrowError(message.c_str());
}
sqlite3_free(err);

if (simple_result && !pragma_info.after_first) {
info.GetReturnValue().Set(Nan::Undefined());
Expand Down
2 changes: 1 addition & 1 deletion src/objects/statement/each.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// .each(...any boundValues, Function callback) -> undefined
// .each(...any boundValues, function callback) -> undefined

NAN_METHOD(Statement::Each) {
Statement* stmt = Nan::ObjectWrap::Unwrap<Statement>(info.This());
Expand Down
18 changes: 9 additions & 9 deletions src/objects/transaction/run.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ NAN_METHOD(Transaction::Run) {
QUERY_START(trans, transaction, TRANSACTION_BIND, SQLITE_STATIC, info, info.Length());

sqlite3* db_handle = trans->db->db_handle;
TransactionHandles* t_handles = trans->db->t_handles;
TransactionHandles t_handles = trans->db->t_handles;

// Begin Transaction
sqlite3_step(t_handles->begin);
if (sqlite3_reset(t_handles->begin) != SQLITE_OK) {
sqlite3_step(t_handles.begin);
if (sqlite3_reset(t_handles.begin) != SQLITE_OK) {
QUERY_THROW(trans, TRANSACTION_CLEAR_BINDINGS, sqlite3_errmsg(db_handle));
}

Expand All @@ -22,8 +22,8 @@ NAN_METHOD(Transaction::Run) {
sqlite3_step(trans->handles[i]);
if (sqlite3_reset(trans->handles[i]) != SQLITE_OK) {
QUERY_THROW_STAY(trans, TRANSACTION_CLEAR_BINDINGS, sqlite3_errmsg(db_handle));
sqlite3_step(t_handles->rollback);
sqlite3_reset(t_handles->rollback);
sqlite3_step(t_handles.rollback);
sqlite3_reset(t_handles.rollback);
return;
}

Expand All @@ -33,11 +33,11 @@ NAN_METHOD(Transaction::Run) {
}

// Commit Transaction
sqlite3_step(t_handles->commit);
if (sqlite3_reset(t_handles->commit) != SQLITE_OK) {
sqlite3_step(t_handles.commit);
if (sqlite3_reset(t_handles.commit) != SQLITE_OK) {
QUERY_THROW_STAY(trans, TRANSACTION_CLEAR_BINDINGS, sqlite3_errmsg(db_handle));
sqlite3_step(t_handles->rollback);
sqlite3_reset(t_handles->rollback);
sqlite3_step(t_handles.rollback);
sqlite3_reset(t_handles.rollback);
return;
}

Expand Down
3 changes: 1 addition & 2 deletions src/util/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ inline bool IS_32BIT_INT(double num) {
// is thrown and the caller returns.
#define INVOKE_METHOD(result, obj, methodName, argc, argv) \
GET_METHOD(_method, obj, methodName); \
Nan::MaybeLocal<v8::Value> _maybeValue = \
Nan::Call(_method, obj, argc, argv); \
Nan::MaybeLocal<v8::Value> _maybeValue = _method->Call(obj, argc, argv); \
if (_maybeValue.IsEmpty()) {return;} \
v8::Local<v8::Value> result = _maybeValue.ToLocalChecked();

Expand Down
63 changes: 32 additions & 31 deletions src/util/transaction-handles.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,41 @@
#include <sqlite3.h>

// A simple construct for holding three sqlite3_stmt pointers.
// After construction, if statusOut is SQLITE_OK, the three pointers
// can be used to start, commit, and rollback transactions.
class TransactionHandles {
public:
explicit TransactionHandles(sqlite3* db_handle, int* statusOut)
: begin(NULL)
, commit(NULL)
, rollback(NULL) {
int status;

status = sqlite3_prepare_v2(db_handle, "BEGIN TRANSACTION;", -1, &begin, NULL);
if (status != SQLITE_OK) {
*statusOut = status;
return;
}

status = sqlite3_prepare_v2(db_handle, "COMMIT TRANSACTION;", -1, &commit, NULL);
if (status != SQLITE_OK) {
*statusOut = status;
return;
}

*statusOut = sqlite3_prepare_v2(db_handle, "ROLLBACK TRANSACTION;", -1, &rollback, NULL);
// After invoking open(), if SQLITE_OK is returned, the three pointers can be
// used to start, commit, and rollback transactions.
// This object does not own its own data. The owner of this object must
// manually invoke close() to free the memory held by the three pointers.
class TransactionHandles { public:
explicit TransactionHandles() : begin(NULL), commit(NULL), rollback(NULL) {}

int open(sqlite3* db_handle) {
int status;

status = sqlite3_prepare_v2(db_handle, "BEGIN TRANSACTION;", -1, &begin, NULL);
if (status != SQLITE_OK) {
return status;
}
~TransactionHandles() {
sqlite3_finalize(begin);
sqlite3_finalize(commit);
sqlite3_finalize(rollback);

status = sqlite3_prepare_v2(db_handle, "COMMIT TRANSACTION;", -1, &commit, NULL);
if (status != SQLITE_OK) {
return status;
}

public:
sqlite3_stmt* begin;
sqlite3_stmt* commit;
sqlite3_stmt* rollback;
return sqlite3_prepare_v2(db_handle, "ROLLBACK TRANSACTION;", -1, &rollback, NULL);
}

void close() {
sqlite3_finalize(begin);
sqlite3_finalize(commit);
sqlite3_finalize(rollback);
begin = NULL;
commit = NULL;
rollback = NULL;
}

sqlite3_stmt* begin;
sqlite3_stmt* commit;
sqlite3_stmt* rollback;
};

#endif

0 comments on commit 404c739

Please sign in to comment.