2009-04-21 19:27:03 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2009-04-21 19:27:03 +04:00
|
|
|
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
|
|
|
|
#include "sqlite3.h"
|
|
|
|
|
|
|
|
#include "mozIStorageStatementCallback.h"
|
2009-06-17 23:12:51 +04:00
|
|
|
#include "mozStorageBindingParams.h"
|
2009-04-21 19:27:03 +04:00
|
|
|
#include "mozStorageHelper.h"
|
|
|
|
#include "mozStorageResultSet.h"
|
|
|
|
#include "mozStorageRow.h"
|
|
|
|
#include "mozStorageConnection.h"
|
|
|
|
#include "mozStorageError.h"
|
|
|
|
#include "mozStoragePrivateHelpers.h"
|
2009-06-17 23:12:51 +04:00
|
|
|
#include "mozStorageStatementData.h"
|
2009-04-21 19:27:03 +04:00
|
|
|
#include "mozStorageAsyncStatementExecution.h"
|
|
|
|
|
2014-05-01 02:41:05 +04:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2011-10-13 12:48:40 +04:00
|
|
|
#include "mozilla/Telemetry.h"
|
|
|
|
|
2009-04-21 19:27:03 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace storage {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The following constants help batch rows into result sets.
|
|
|
|
* MAX_MILLISECONDS_BETWEEN_RESULTS was chosen because any user-based task that
|
|
|
|
* takes less than 200 milliseconds is considered to feel instantaneous to end
|
|
|
|
* users. MAX_ROWS_PER_RESULT was arbitrarily chosen to reduce the number of
|
|
|
|
* dispatches to calling thread, while also providing reasonably-sized sets of
|
|
|
|
* data for consumers. Both of these constants are used because we assume that
|
|
|
|
* consumers are trying to avoid blocking their execution thread for long
|
|
|
|
* periods of time, and dispatching many small events to the calling thread will
|
|
|
|
* end up blocking it.
|
|
|
|
*/
|
2009-07-29 00:18:10 +04:00
|
|
|
#define MAX_MILLISECONDS_BETWEEN_RESULTS 75
|
2009-04-21 19:27:03 +04:00
|
|
|
#define MAX_ROWS_PER_RESULT 15
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// AsyncExecuteStatements
|
|
|
|
|
|
|
|
/* static */
|
2009-06-17 23:12:51 +04:00
|
|
|
nsresult AsyncExecuteStatements::execute(
|
2019-05-01 11:47:10 +03:00
|
|
|
StatementDataArray& aStatements, Connection* aConnection,
|
|
|
|
sqlite3* aNativeConnection, mozIStorageStatementCallback* aCallback,
|
|
|
|
mozIStoragePendingStatement** _stmt) {
|
2009-04-21 19:27:03 +04:00
|
|
|
// Create our event to run in the background
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AsyncExecuteStatements> event = new AsyncExecuteStatements(
|
2014-04-24 13:54:09 +04:00
|
|
|
aStatements, aConnection, aNativeConnection, aCallback);
|
2009-04-21 19:27:03 +04:00
|
|
|
NS_ENSURE_TRUE(event, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
// Dispatch it to the background
|
2019-05-01 11:47:10 +03:00
|
|
|
nsIEventTarget* target = aConnection->getAsyncExecutionTarget();
|
2012-03-09 18:32:28 +04:00
|
|
|
|
|
|
|
// If we don't have a valid target, this is a bug somewhere else. In the past,
|
|
|
|
// this assert found cases where a Run method would schedule a new statement
|
|
|
|
// without checking if asyncClose had been called. The caller must prevent
|
|
|
|
// that from happening or, if the work is not critical, just avoid creating
|
|
|
|
// the new statement during shutdown. See bug 718449 for an example.
|
|
|
|
MOZ_ASSERT(target);
|
|
|
|
if (!target) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2009-06-17 23:12:43 +04:00
|
|
|
nsresult rv = target->Dispatch(event, NS_DISPATCH_NORMAL);
|
2009-04-21 19:27:03 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2009-11-09 20:58:34 +03:00
|
|
|
// Return it as the pending statement object and track it.
|
2015-07-01 20:11:11 +03:00
|
|
|
event.forget(_stmt);
|
2009-04-21 19:27:03 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-06-17 23:12:51 +04:00
|
|
|
AsyncExecuteStatements::AsyncExecuteStatements(
|
2019-05-01 11:47:10 +03:00
|
|
|
StatementDataArray& aStatements, Connection* aConnection,
|
|
|
|
sqlite3* aNativeConnection, mozIStorageStatementCallback* aCallback)
|
2009-04-21 19:27:03 +04:00
|
|
|
: mConnection(aConnection),
|
2014-04-24 13:54:09 +04:00
|
|
|
mNativeConnection(aNativeConnection),
|
2014-04-24 13:54:15 +04:00
|
|
|
mHasTransaction(false),
|
2009-04-21 19:27:03 +04:00
|
|
|
mCallback(aCallback),
|
|
|
|
mCallingThread(::do_GetCurrentThread()),
|
2009-07-29 00:18:10 +04:00
|
|
|
mMaxWait(
|
|
|
|
TimeDuration::FromMilliseconds(MAX_MILLISECONDS_BETWEEN_RESULTS)),
|
|
|
|
mIntervalStart(TimeStamp::Now()),
|
2009-04-21 19:27:03 +04:00
|
|
|
mState(PENDING),
|
2009-09-03 00:30:24 +04:00
|
|
|
mCancelRequested(false),
|
|
|
|
mMutex(aConnection->sharedAsyncExecutionMutex),
|
2010-03-24 10:32:40 +03:00
|
|
|
mDBMutex(aConnection->sharedDBMutex),
|
2017-03-28 13:54:02 +03:00
|
|
|
mRequestStartDate(TimeStamp::Now()) {
|
2009-04-21 19:27:03 +04:00
|
|
|
(void)mStatements.SwapElements(aStatements);
|
|
|
|
NS_ASSERTION(mStatements.Length(), "We weren't given any statements!");
|
|
|
|
}
|
|
|
|
|
2014-04-24 13:54:15 +04:00
|
|
|
AsyncExecuteStatements::~AsyncExecuteStatements() {
|
2017-03-28 13:54:02 +03:00
|
|
|
MOZ_ASSERT(!mCallback, "Never called the Completion callback!");
|
2014-04-24 13:54:15 +04:00
|
|
|
MOZ_ASSERT(!mHasTransaction, "There should be no transaction at this point");
|
2017-06-21 19:47:26 +03:00
|
|
|
if (mCallback) {
|
|
|
|
NS_ProxyRelease("AsyncExecuteStatements::mCallback", mCallingThread,
|
|
|
|
mCallback.forget());
|
|
|
|
}
|
2014-04-24 13:54:15 +04:00
|
|
|
}
|
|
|
|
|
2009-04-21 19:27:03 +04:00
|
|
|
bool AsyncExecuteStatements::shouldNotify() {
|
|
|
|
#ifdef DEBUG
|
2009-09-03 00:30:24 +04:00
|
|
|
mMutex.AssertNotCurrentThreadOwns();
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool onCallingThread = false;
|
2009-04-21 19:27:03 +04:00
|
|
|
(void)mCallingThread->IsOnCurrentThread(&onCallingThread);
|
|
|
|
NS_ASSERTION(onCallingThread, "runEvent not running on the calling thread!");
|
|
|
|
#endif
|
|
|
|
|
2009-09-03 00:30:24 +04:00
|
|
|
// We do not need to acquire mMutex here because it can only ever be written
|
|
|
|
// to on the calling thread, and the only thread that can call us is the
|
|
|
|
// calling thread, so we know that our access is serialized.
|
2009-04-21 19:27:03 +04:00
|
|
|
return !mCancelRequested;
|
|
|
|
}
|
|
|
|
|
2009-06-17 23:12:51 +04:00
|
|
|
bool AsyncExecuteStatements::bindExecuteAndProcessStatement(
|
2019-05-01 11:47:10 +03:00
|
|
|
StatementData& aData, bool aLastStatement) {
|
2009-09-03 00:30:24 +04:00
|
|
|
mMutex.AssertNotCurrentThreadOwns();
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
sqlite3_stmt* aStatement = nullptr;
|
2010-03-24 10:32:40 +03:00
|
|
|
// This cannot fail; we are only called if it's available.
|
|
|
|
(void)aData.getSqliteStatement(&aStatement);
|
|
|
|
NS_ASSERTION(aStatement, "You broke the code; do not call here like that!");
|
2019-05-01 11:47:10 +03:00
|
|
|
BindingParamsArray* paramsArray(aData);
|
2009-06-17 23:12:51 +04:00
|
|
|
|
|
|
|
// Iterate through all of our parameters, bind them, and execute.
|
|
|
|
bool continueProcessing = true;
|
|
|
|
BindingParamsArray::iterator itr = paramsArray->begin();
|
|
|
|
BindingParamsArray::iterator end = paramsArray->end();
|
|
|
|
while (itr != end && continueProcessing) {
|
|
|
|
// Bind the data to our statement.
|
2017-06-21 19:47:26 +03:00
|
|
|
nsCOMPtr<IStorageBindingParamsInternal> bindingInternal =
|
2010-03-24 10:32:40 +03:00
|
|
|
do_QueryInterface(*itr);
|
|
|
|
nsCOMPtr<mozIStorageError> error = bindingInternal->bind(aStatement);
|
2009-06-17 23:12:51 +04:00
|
|
|
if (error) {
|
|
|
|
// Set our error state.
|
2009-09-03 00:30:24 +04:00
|
|
|
mState = ERROR;
|
2009-06-17 23:12:51 +04:00
|
|
|
|
|
|
|
// And notify.
|
|
|
|
(void)notifyError(error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advance our iterator, execute, and then process the statement.
|
|
|
|
itr++;
|
|
|
|
bool lastStatement = aLastStatement && itr == end;
|
2010-03-24 10:32:40 +03:00
|
|
|
continueProcessing = executeAndProcessStatement(aStatement, lastStatement);
|
2009-06-17 23:12:51 +04:00
|
|
|
|
|
|
|
// Always reset our statement.
|
2010-03-24 10:32:40 +03:00
|
|
|
(void)::sqlite3_reset(aStatement);
|
2009-06-17 23:12:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return continueProcessing;
|
|
|
|
}
|
|
|
|
|
2009-04-21 19:27:03 +04:00
|
|
|
bool AsyncExecuteStatements::executeAndProcessStatement(
|
2019-05-01 11:47:10 +03:00
|
|
|
sqlite3_stmt* aStatement, bool aLastStatement) {
|
2009-09-03 00:30:24 +04:00
|
|
|
mMutex.AssertNotCurrentThreadOwns();
|
|
|
|
|
2009-06-17 23:12:43 +04:00
|
|
|
// Execute our statement
|
|
|
|
bool hasResults;
|
|
|
|
do {
|
|
|
|
hasResults = executeStatement(aStatement);
|
2009-04-21 19:27:03 +04:00
|
|
|
|
2009-06-17 23:12:43 +04:00
|
|
|
// If we had an error, bail.
|
2017-07-31 23:27:23 +03:00
|
|
|
if (mState == ERROR || mState == CANCELED) return false;
|
2009-06-17 23:12:43 +04:00
|
|
|
|
2009-06-19 23:21:44 +04:00
|
|
|
// If we have been canceled, there is no point in going on...
|
2009-09-03 00:30:24 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lockedScope(mMutex);
|
|
|
|
if (mCancelRequested) {
|
|
|
|
mState = CANCELED;
|
|
|
|
return false;
|
|
|
|
}
|
2009-06-17 23:12:43 +04:00
|
|
|
}
|
2009-04-21 19:27:03 +04:00
|
|
|
|
2009-06-17 23:12:43 +04:00
|
|
|
// Build our result set and notify if we got anything back and have a
|
|
|
|
// callback to notify.
|
|
|
|
if (mCallback && hasResults &&
|
|
|
|
NS_FAILED(buildAndNotifyResults(aStatement))) {
|
|
|
|
// We had an error notifying, so we notify on error and stop processing.
|
2009-09-03 00:30:24 +04:00
|
|
|
mState = ERROR;
|
2009-06-19 23:21:44 +04:00
|
|
|
|
2009-07-30 04:24:50 +04:00
|
|
|
// Notify, and stop processing statements.
|
|
|
|
(void)notifyError(mozIStorageError::ERROR,
|
|
|
|
"An error occurred while notifying about results");
|
2009-06-19 23:21:44 +04:00
|
|
|
|
2009-06-17 23:12:43 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} while (hasResults);
|
2009-04-21 19:27:03 +04:00
|
|
|
|
2015-09-26 02:33:22 +03:00
|
|
|
#ifndef MOZ_STORAGE_SORTWARNING_SQL_DUMP
|
|
|
|
if (MOZ_LOG_TEST(gStorageLog, LogLevel::Warning))
|
2009-04-21 19:27:03 +04:00
|
|
|
#endif
|
2015-09-26 02:33:22 +03:00
|
|
|
{
|
|
|
|
// Check to make sure that this statement was smart about what it did.
|
|
|
|
checkAndLogStatementPerformance(aStatement);
|
|
|
|
}
|
2009-04-21 19:27:03 +04:00
|
|
|
|
2009-09-03 00:30:24 +04:00
|
|
|
// If we are done, we need to set our state accordingly while we still hold
|
|
|
|
// our mutex. We would have already returned if we were canceled or had
|
|
|
|
// an error at this point.
|
|
|
|
if (aLastStatement) mState = COMPLETED;
|
2009-04-21 19:27:03 +04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
bool AsyncExecuteStatements::executeStatement(sqlite3_stmt* aStatement) {
|
2009-09-03 00:30:24 +04:00
|
|
|
mMutex.AssertNotCurrentThreadOwns();
|
2011-10-13 12:48:40 +04:00
|
|
|
Telemetry::AutoTimer<Telemetry::MOZ_STORAGE_ASYNC_REQUESTS_MS>
|
|
|
|
finallySendExecutionDuration(mRequestStartDate);
|
2009-06-17 23:12:43 +04:00
|
|
|
while (true) {
|
2010-03-24 10:32:40 +03:00
|
|
|
// lock the sqlite mutex so sqlite3_errmsg cannot change
|
|
|
|
SQLiteMutexAutoLock lockedScope(mDBMutex);
|
|
|
|
|
2014-04-24 13:54:12 +04:00
|
|
|
int rc = mConnection->stepStatement(mNativeConnection, aStatement);
|
2009-06-17 23:12:43 +04:00
|
|
|
// Stop if we have no more results.
|
|
|
|
if (rc == SQLITE_DONE) {
|
2011-10-13 12:48:40 +04:00
|
|
|
Telemetry::Accumulate(Telemetry::MOZ_STORAGE_ASYNC_REQUESTS_SUCCESS,
|
|
|
|
true);
|
2009-06-17 23:12:43 +04:00
|
|
|
return false;
|
2011-10-13 12:48:40 +04:00
|
|
|
}
|
2009-06-17 23:12:43 +04:00
|
|
|
|
|
|
|
// If we got results, we can return now.
|
|
|
|
if (rc == SQLITE_ROW) {
|
2011-10-13 12:48:40 +04:00
|
|
|
Telemetry::Accumulate(Telemetry::MOZ_STORAGE_ASYNC_REQUESTS_SUCCESS,
|
|
|
|
true);
|
2009-06-17 23:12:43 +04:00
|
|
|
return true;
|
2011-10-13 12:48:40 +04:00
|
|
|
}
|
2009-06-17 23:12:43 +04:00
|
|
|
|
|
|
|
// Some errors are not fatal, and we can handle them and continue.
|
|
|
|
if (rc == SQLITE_BUSY) {
|
2018-02-24 06:50:38 +03:00
|
|
|
{
|
|
|
|
// Don't hold the lock while we call outside our module.
|
|
|
|
SQLiteMutexAutoUnlock unlockedScope(mDBMutex);
|
|
|
|
// Yield, and try again
|
|
|
|
(void)::PR_Sleep(PR_INTERVAL_NO_WAIT);
|
|
|
|
}
|
|
|
|
::sqlite3_reset(aStatement);
|
2009-06-17 23:12:43 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-07-31 23:27:23 +03:00
|
|
|
if (rc == SQLITE_INTERRUPT) {
|
|
|
|
mState = CANCELED;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-06-17 23:12:43 +04:00
|
|
|
// Set an error state.
|
2009-09-03 00:30:24 +04:00
|
|
|
mState = ERROR;
|
2011-10-13 12:48:40 +04:00
|
|
|
Telemetry::Accumulate(Telemetry::MOZ_STORAGE_ASYNC_REQUESTS_SUCCESS, false);
|
2009-06-17 23:12:43 +04:00
|
|
|
|
2010-03-24 10:32:40 +03:00
|
|
|
// Construct the error message before giving up the mutex (which we cannot
|
|
|
|
// hold during the call to notifyError).
|
2014-04-24 13:54:09 +04:00
|
|
|
nsCOMPtr<mozIStorageError> errorObj(
|
|
|
|
new Error(rc, ::sqlite3_errmsg(mNativeConnection)));
|
2010-03-24 10:32:40 +03:00
|
|
|
// We cannot hold the DB mutex while calling notifyError.
|
|
|
|
SQLiteMutexAutoUnlock unlockedScope(mDBMutex);
|
|
|
|
(void)notifyError(errorObj);
|
2009-06-17 23:12:43 +04:00
|
|
|
|
|
|
|
// Finally, indicate that we should stop processing.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-21 19:27:03 +04:00
|
|
|
nsresult AsyncExecuteStatements::buildAndNotifyResults(
|
2019-05-01 11:47:10 +03:00
|
|
|
sqlite3_stmt* aStatement) {
|
2009-04-21 19:27:03 +04:00
|
|
|
NS_ASSERTION(mCallback, "Trying to dispatch results without a callback!");
|
2009-09-03 00:30:24 +04:00
|
|
|
mMutex.AssertNotCurrentThreadOwns();
|
2009-04-21 19:27:03 +04:00
|
|
|
|
|
|
|
// Build result object if we need it.
|
2009-05-09 04:29:56 +04:00
|
|
|
if (!mResultSet) mResultSet = new ResultSet();
|
2009-04-21 19:27:03 +04:00
|
|
|
NS_ENSURE_TRUE(mResultSet, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Row> row(new Row());
|
2009-04-21 19:27:03 +04:00
|
|
|
NS_ENSURE_TRUE(row, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
nsresult rv = row->initialize(aStatement);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = mResultSet->add(row);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// If we have hit our maximum number of allowed results, or if we have hit
|
|
|
|
// the maximum amount of time we want to wait for results, notify the
|
|
|
|
// calling thread about it.
|
2009-07-29 00:18:10 +04:00
|
|
|
TimeStamp now = TimeStamp::Now();
|
|
|
|
TimeDuration delta = now - mIntervalStart;
|
|
|
|
if (mResultSet->rows() >= MAX_ROWS_PER_RESULT || delta > mMaxWait) {
|
2009-04-21 19:27:03 +04:00
|
|
|
// Notify the caller
|
|
|
|
rv = notifyResults();
|
|
|
|
if (NS_FAILED(rv)) return NS_OK; // we'll try again with the next result
|
|
|
|
|
|
|
|
// Reset our start time
|
|
|
|
mIntervalStart = now;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult AsyncExecuteStatements::notifyComplete() {
|
2009-09-03 00:30:24 +04:00
|
|
|
mMutex.AssertNotCurrentThreadOwns();
|
2009-04-21 19:27:03 +04:00
|
|
|
NS_ASSERTION(mState != PENDING,
|
|
|
|
"Still in a pending state when calling Complete!");
|
|
|
|
|
2013-08-28 01:07:04 +04:00
|
|
|
// Reset our statements before we try to commit or rollback. If we are
|
2009-04-21 19:27:03 +04:00
|
|
|
// canceling and have statements that think they have pending work, the
|
|
|
|
// rollback will fail.
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < mStatements.Length(); i++) mStatements[i].reset();
|
2013-08-28 01:07:04 +04:00
|
|
|
|
|
|
|
// Release references to the statement data as soon as possible. If this
|
|
|
|
// is the last reference, statements will be finalized immediately on the
|
|
|
|
// async thread, hence avoiding several bounces between threads and possible
|
|
|
|
// race conditions with AsyncClose().
|
|
|
|
mStatements.Clear();
|
2009-04-21 19:27:03 +04:00
|
|
|
|
|
|
|
// Handle our transaction, if we have one
|
2014-04-24 13:54:15 +04:00
|
|
|
if (mHasTransaction) {
|
2009-04-21 19:27:03 +04:00
|
|
|
if (mState == COMPLETED) {
|
2014-04-24 13:54:15 +04:00
|
|
|
nsresult rv = mConnection->commitTransactionInternal(mNativeConnection);
|
2009-04-21 19:27:03 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2009-09-03 00:30:24 +04:00
|
|
|
mState = ERROR;
|
2009-04-21 19:27:03 +04:00
|
|
|
(void)notifyError(mozIStorageError::ERROR,
|
|
|
|
"Transaction failed to commit");
|
|
|
|
}
|
|
|
|
} else {
|
2014-05-01 02:41:05 +04:00
|
|
|
DebugOnly<nsresult> rv =
|
|
|
|
mConnection->rollbackTransactionInternal(mNativeConnection);
|
2016-09-01 08:01:16 +03:00
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Transaction failed to rollback");
|
2009-04-21 19:27:03 +04:00
|
|
|
}
|
2014-04-24 13:54:15 +04:00
|
|
|
mHasTransaction = false;
|
2009-04-21 19:27:03 +04:00
|
|
|
}
|
|
|
|
|
2017-06-21 19:47:26 +03:00
|
|
|
// This will take ownership of mCallback and make sure its destruction will
|
|
|
|
// happen on the owner thread.
|
|
|
|
Unused << mCallingThread->Dispatch(
|
2017-06-12 22:34:10 +03:00
|
|
|
NewRunnableMethod("AsyncExecuteStatements::notifyCompleteOnCallingThread",
|
|
|
|
this,
|
|
|
|
&AsyncExecuteStatements::notifyCompleteOnCallingThread),
|
2017-06-21 19:47:26 +03:00
|
|
|
NS_DISPATCH_NORMAL);
|
2009-04-21 19:27:03 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-06-21 19:47:26 +03:00
|
|
|
nsresult AsyncExecuteStatements::notifyCompleteOnCallingThread() {
|
|
|
|
MOZ_ASSERT(mCallingThread->IsOnCurrentThread());
|
|
|
|
// Take ownership of mCallback and responsibility for freeing it when we
|
|
|
|
// release it. Any notifyResultsOnCallingThread and
|
|
|
|
// notifyErrorOnCallingThread calls on the stack spinning the event loop have
|
|
|
|
// guaranteed their safety by creating their own strong reference before
|
|
|
|
// invoking the callback.
|
2020-02-12 21:13:29 +03:00
|
|
|
nsCOMPtr<mozIStorageStatementCallback> callback = mCallback.forget();
|
2017-06-21 19:47:26 +03:00
|
|
|
if (callback) {
|
|
|
|
Unused << callback->HandleCompletion(mState);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult AsyncExecuteStatements::notifyError(int32_t aErrorCode,
|
2019-05-01 11:47:10 +03:00
|
|
|
const char* aMessage) {
|
2009-09-03 00:30:24 +04:00
|
|
|
mMutex.AssertNotCurrentThreadOwns();
|
2010-03-24 10:32:40 +03:00
|
|
|
mDBMutex.assertNotCurrentThreadOwns();
|
2009-09-03 00:30:24 +04:00
|
|
|
|
2009-04-21 19:27:03 +04:00
|
|
|
if (!mCallback) return NS_OK;
|
|
|
|
|
2009-05-09 04:29:55 +04:00
|
|
|
nsCOMPtr<mozIStorageError> errorObj(new Error(aErrorCode, aMessage));
|
2009-04-21 19:27:03 +04:00
|
|
|
NS_ENSURE_TRUE(errorObj, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
2009-06-17 23:12:51 +04:00
|
|
|
return notifyError(errorObj);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
nsresult AsyncExecuteStatements::notifyError(mozIStorageError* aError) {
|
2009-09-03 00:30:24 +04:00
|
|
|
mMutex.AssertNotCurrentThreadOwns();
|
2010-03-24 10:32:40 +03:00
|
|
|
mDBMutex.assertNotCurrentThreadOwns();
|
2009-09-03 00:30:24 +04:00
|
|
|
|
2009-06-17 23:12:51 +04:00
|
|
|
if (!mCallback) return NS_OK;
|
|
|
|
|
2017-06-21 19:47:26 +03:00
|
|
|
Unused << mCallingThread->Dispatch(
|
2017-06-12 22:34:10 +03:00
|
|
|
NewRunnableMethod<nsCOMPtr<mozIStorageError>>(
|
|
|
|
"AsyncExecuteStatements::notifyErrorOnCallingThread", this,
|
|
|
|
&AsyncExecuteStatements::notifyErrorOnCallingThread, aError),
|
2017-06-21 19:47:26 +03:00
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2009-04-21 19:27:03 +04:00
|
|
|
|
2017-06-21 19:47:26 +03:00
|
|
|
nsresult AsyncExecuteStatements::notifyErrorOnCallingThread(
|
2019-05-01 11:47:10 +03:00
|
|
|
mozIStorageError* aError) {
|
2017-06-21 19:47:26 +03:00
|
|
|
MOZ_ASSERT(mCallingThread->IsOnCurrentThread());
|
|
|
|
// Acquire our own strong reference so that if the callback spins a nested
|
|
|
|
// event loop and notifyCompleteOnCallingThread is executed, forgetting
|
|
|
|
// mCallback, we still have a valid/strong reference that won't be freed until
|
|
|
|
// we exit.
|
|
|
|
nsCOMPtr<mozIStorageStatementCallback> callback = mCallback;
|
|
|
|
if (shouldNotify() && callback) {
|
|
|
|
Unused << callback->HandleError(aError);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2009-04-21 19:27:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult AsyncExecuteStatements::notifyResults() {
|
2009-09-03 00:30:24 +04:00
|
|
|
mMutex.AssertNotCurrentThreadOwns();
|
2017-06-21 19:47:26 +03:00
|
|
|
MOZ_ASSERT(mCallback, "notifyResults called without a callback!");
|
|
|
|
|
|
|
|
// This takes ownership of mResultSet, a new one will be generated in
|
|
|
|
// buildAndNotifyResults() when further results will arrive.
|
|
|
|
Unused << mCallingThread->Dispatch(
|
2017-06-12 22:34:10 +03:00
|
|
|
NewRunnableMethod<RefPtr<ResultSet>>(
|
|
|
|
"AsyncExecuteStatements::notifyResultsOnCallingThread", this,
|
|
|
|
&AsyncExecuteStatements::notifyResultsOnCallingThread,
|
|
|
|
mResultSet.forget()),
|
2017-06-21 19:47:26 +03:00
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult AsyncExecuteStatements::notifyResultsOnCallingThread(
|
2019-05-01 11:47:10 +03:00
|
|
|
ResultSet* aResultSet) {
|
2017-06-21 19:47:26 +03:00
|
|
|
MOZ_ASSERT(mCallingThread->IsOnCurrentThread());
|
|
|
|
// Acquire our own strong reference so that if the callback spins a nested
|
|
|
|
// event loop and notifyCompleteOnCallingThread is executed, forgetting
|
|
|
|
// mCallback, we still have a valid/strong reference that won't be freed until
|
|
|
|
// we exit.
|
|
|
|
nsCOMPtr<mozIStorageStatementCallback> callback = mCallback;
|
|
|
|
if (shouldNotify() && callback) {
|
|
|
|
Unused << callback->HandleResult(aResultSet);
|
2017-03-28 13:54:02 +03:00
|
|
|
}
|
2017-06-21 19:47:26 +03:00
|
|
|
return NS_OK;
|
2009-04-21 19:27:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(AsyncExecuteStatements, nsIRunnable,
|
|
|
|
mozIStoragePendingStatement)
|
|
|
|
|
2011-05-24 02:15:01 +04:00
|
|
|
bool AsyncExecuteStatements::statementsNeedTransaction() {
|
|
|
|
// If there is more than one write statement, run in a transaction.
|
|
|
|
// Additionally, if we have only one statement but it needs a transaction, due
|
|
|
|
// to multiple BindingParams, we will wrap it in one.
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0, transactionsCount = 0; i < mStatements.Length(); ++i) {
|
2011-05-24 02:15:01 +04:00
|
|
|
transactionsCount += mStatements[i].needsTransaction();
|
|
|
|
if (transactionsCount > 1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-04-21 19:27:03 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// mozIStoragePendingStatement
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2009-07-30 04:24:50 +04:00
|
|
|
AsyncExecuteStatements::Cancel() {
|
2009-04-21 19:27:03 +04:00
|
|
|
#ifdef DEBUG
|
2011-09-29 10:19:26 +04:00
|
|
|
bool onCallingThread = false;
|
2009-04-21 19:27:03 +04:00
|
|
|
(void)mCallingThread->IsOnCurrentThread(&onCallingThread);
|
|
|
|
NS_ASSERTION(onCallingThread, "Not canceling from the calling thread!");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// If we have already canceled, we have an error, but always indicate that
|
|
|
|
// we are trying to cancel.
|
|
|
|
NS_ENSURE_FALSE(mCancelRequested, NS_ERROR_UNEXPECTED);
|
|
|
|
|
2009-09-03 00:30:24 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lockedScope(mMutex);
|
2009-04-21 19:27:03 +04:00
|
|
|
|
2009-09-03 00:30:24 +04:00
|
|
|
// We need to indicate that we want to try and cancel now.
|
|
|
|
mCancelRequested = true;
|
|
|
|
}
|
2009-04-21 19:27:03 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// nsIRunnable
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AsyncExecuteStatements::Run() {
|
2017-06-16 18:43:23 +03:00
|
|
|
MOZ_ASSERT(mConnection->isConnectionReadyOnThisThread());
|
2014-04-24 13:54:12 +04:00
|
|
|
|
2009-06-17 23:12:43 +04:00
|
|
|
// Do not run if we have been canceled.
|
2009-09-03 00:30:24 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lockedScope(mMutex);
|
|
|
|
if (mCancelRequested) mState = CANCELED;
|
2009-07-30 04:24:50 +04:00
|
|
|
}
|
2009-09-03 00:30:24 +04:00
|
|
|
if (mState == CANCELED) return notifyComplete();
|
2009-04-21 19:27:03 +04:00
|
|
|
|
2015-04-10 14:16:00 +03:00
|
|
|
if (statementsNeedTransaction() && mConnection->getAutocommit()) {
|
2014-04-24 13:54:15 +04:00
|
|
|
if (NS_SUCCEEDED(mConnection->beginTransactionInternal(
|
|
|
|
mNativeConnection, mozIStorageConnection::TRANSACTION_IMMEDIATE))) {
|
|
|
|
mHasTransaction = true;
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
else {
|
|
|
|
NS_WARNING("Unable to create a transaction for async execution.");
|
|
|
|
}
|
|
|
|
#endif
|
2009-04-21 19:27:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute each statement, giving the callback results if it returns any.
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < mStatements.Length(); i++) {
|
2009-06-17 23:12:51 +04:00
|
|
|
bool finished = (i == (mStatements.Length() - 1));
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
sqlite3_stmt* stmt;
|
2010-03-24 10:32:40 +03:00
|
|
|
{ // lock the sqlite mutex so sqlite3_errmsg cannot change
|
|
|
|
SQLiteMutexAutoLock lockedScope(mDBMutex);
|
|
|
|
|
|
|
|
int rc = mStatements[i].getSqliteStatement(&stmt);
|
|
|
|
if (rc != SQLITE_OK) {
|
|
|
|
// Set our error state.
|
|
|
|
mState = ERROR;
|
|
|
|
|
|
|
|
// Build the error object; can't call notifyError with the lock held
|
|
|
|
nsCOMPtr<mozIStorageError> errorObj(
|
2014-04-24 13:54:09 +04:00
|
|
|
new Error(rc, ::sqlite3_errmsg(mNativeConnection)));
|
2010-03-24 10:32:40 +03:00
|
|
|
{
|
|
|
|
// We cannot hold the DB mutex and call notifyError.
|
|
|
|
SQLiteMutexAutoUnlock unlockedScope(mDBMutex);
|
|
|
|
(void)notifyError(errorObj);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-17 23:12:51 +04:00
|
|
|
// If we have parameters to bind, bind them, execute, and process.
|
|
|
|
if (mStatements[i].hasParametersToBeBound()) {
|
|
|
|
if (!bindExecuteAndProcessStatement(mStatements[i], finished)) break;
|
|
|
|
}
|
|
|
|
// Otherwise, just execute and process the statement.
|
2010-03-24 10:32:40 +03:00
|
|
|
else if (!executeAndProcessStatement(stmt, finished)) {
|
2009-04-21 19:27:03 +04:00
|
|
|
break;
|
2009-06-17 23:12:51 +04:00
|
|
|
}
|
2009-04-21 19:27:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we still have results that we haven't notified about, take care of
|
|
|
|
// them now.
|
|
|
|
if (mResultSet) (void)notifyResults();
|
|
|
|
|
|
|
|
// Notify about completion
|
|
|
|
return notifyComplete();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace storage
|
|
|
|
} // namespace mozilla
|