summaryrefslogtreecommitdiffstats
path: root/storage/mozStorageHelper.h
blob: 35ee54faa3779dd8e20cf34d7f90ad227ecb2363 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */

#ifndef MOZSTORAGEHELPER_H
#define MOZSTORAGEHELPER_H

#include "nsCOMPtr.h"
#include "nsString.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/ScopeExit.h"

#include "mozilla/storage/SQLiteMutex.h"
#include "mozIStorageConnection.h"
#include "mozIStorageStatement.h"
#include "mozIStoragePendingStatement.h"
#include "mozilla/DebugOnly.h"
#include "nsCOMPtr.h"
#include "nsError.h"

/**
 * This class wraps a transaction inside a given C++ scope, guaranteeing that
 * the transaction will be completed even if you have an exception or
 * return early.
 *
 * A common use is to create an instance with aCommitOnComplete = false
 * (rollback), then call Commit() on this object manually when your function
 * completes successfully.
 *
 * @note nested transactions are not supported by Sqlite, only nested
 * savepoints, so if a transaction is already in progress, this object creates
 * a nested savepoint to the existing transaction which is considered as
 * anonymous savepoint itself. However, aType and aAsyncCommit are ignored
 * in the case of nested savepoints.
 *
 * @param aConnection
 *        The connection to create the transaction on.
 * @param aCommitOnComplete
 *        Controls whether the transaction is committed or rolled back when
 *        this object goes out of scope.
 * @param aType [optional]
 *        The transaction type, as defined in mozIStorageConnection. Uses the
 *        default transaction behavior for the connection if unspecified.
 * @param aAsyncCommit [optional]
 *        Whether commit should be executed asynchronously on the helper thread.
 *        This is a special option introduced as an interim solution to reduce
 *        main-thread fsyncs in Places.  Can only be used on main-thread.
 *
 *        WARNING: YOU SHOULD _NOT_ WRITE NEW MAIN-THREAD CODE USING THIS!
 *
 *        Notice that async commit might cause synchronous statements to fail
 *        with SQLITE_BUSY.  A possible mitigation strategy is to use
 *        PRAGMA busy_timeout, but notice that might cause main-thread jank.
 *        Finally, if the database is using WAL journaling mode, other
 *        connections won't see the changes done in async committed transactions
 *        until commit is complete.
 *
 *        For all of the above reasons, this should only be used as an interim
 *        solution and avoided completely if possible.
 */
class mozStorageTransaction {
  using SQLiteMutexAutoLock = mozilla::storage::SQLiteMutexAutoLock;

 public:
  mozStorageTransaction(
      mozIStorageConnection* aConnection, bool aCommitOnComplete,
      int32_t aType = mozIStorageConnection::TRANSACTION_DEFAULT,
      bool aAsyncCommit = false)
      : mConnection(aConnection),
        mType(aType),
        mNestingLevel(0),
        mHasTransaction(false),
        mCommitOnComplete(aCommitOnComplete),
        mCompleted(false),
        mAsyncCommit(aAsyncCommit) {}

  ~mozStorageTransaction() {
    if (mConnection && mHasTransaction && !mCompleted) {
      if (mCommitOnComplete) {
        mozilla::DebugOnly<nsresult> rv = Commit();
        NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
                             "A transaction didn't commit correctly");
      } else {
        mozilla::DebugOnly<nsresult> rv = Rollback();
        NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
                             "A transaction didn't rollback correctly");
      }
    }
  }

  /**
   * Starts the transaction.
   */
  nsresult Start() {
    // XXX We should probably get rid of mHasTransaction and use mConnection
    // for checking if a transaction has been started. However, we need to
    // first stop supporting null mConnection and also move aConnection from
    // the constructor to Start.
    MOZ_DIAGNOSTIC_ASSERT(!mHasTransaction);

    // XXX We should probably stop supporting null mConnection.

    // XXX We should probably get rid of mCompleted and allow to start the
    // transaction again if it was already committed or rolled back.
    if (!mConnection || mCompleted) {
      return NS_OK;
    }

    SQLiteMutexAutoLock lock(mConnection->GetSharedDBMutex());

    // We nee to speculatively set the nesting level to be able to decide
    // if this is a top level transaction and to be able to generate the
    // savepoint name.
    TransactionStarted(lock);

    // If there's a failure we need to revert the speculatively set nesting
    // level on the connection.
    auto autoFinishTransaction =
        mozilla::MakeScopeExit([&] { TransactionFinished(lock); });

    nsAutoCString query;

    if (TopLevelTransaction(lock)) {
      query.Assign("BEGIN");
      int32_t type = mType;
      if (type == mozIStorageConnection::TRANSACTION_DEFAULT) {
        MOZ_ALWAYS_SUCCEEDS(mConnection->GetDefaultTransactionType(&type));
      }
      switch (type) {
        case mozIStorageConnection::TRANSACTION_IMMEDIATE:
          query.AppendLiteral(" IMMEDIATE");
          break;
        case mozIStorageConnection::TRANSACTION_EXCLUSIVE:
          query.AppendLiteral(" EXCLUSIVE");
          break;
        case mozIStorageConnection::TRANSACTION_DEFERRED:
          query.AppendLiteral(" DEFERRED");
          break;
        default:
          MOZ_ASSERT(false, "Unknown transaction type");
      }
    } else {
      query.Assign("SAVEPOINT sp"_ns + IntToCString(mNestingLevel));
    }

    nsresult rv = mConnection->ExecuteSimpleSQL(query);
    NS_ENSURE_SUCCESS(rv, rv);

    autoFinishTransaction.release();

    return NS_OK;
  }

  /**
   * Commits the transaction if one is in progress. If one is not in progress,
   * this is a NOP since the actual owner of the transaction outside of our
   * scope is in charge of finally committing or rolling back the transaction.
   */
  nsresult Commit() {
    // XXX Assert instead of returning NS_OK if the transaction hasn't been
    // started.
    if (!mConnection || mCompleted || !mHasTransaction) return NS_OK;

    SQLiteMutexAutoLock lock(mConnection->GetSharedDBMutex());

#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
    MOZ_DIAGNOSTIC_ASSERT(CurrentTransaction(lock));
#else
    if (!CurrentTransaction(lock)) {
      return NS_ERROR_NOT_AVAILABLE;
    }
#endif

    mCompleted = true;

    nsresult rv;

    if (TopLevelTransaction(lock)) {
      // TODO (bug 559659): this might fail with SQLITE_BUSY, but we don't
      // handle it, thus the transaction might stay open until the next COMMIT.
      if (mAsyncCommit) {
        nsCOMPtr<mozIStoragePendingStatement> ps;
        rv = mConnection->ExecuteSimpleSQLAsync("COMMIT"_ns, nullptr,
                                                getter_AddRefs(ps));
      } else {
        rv = mConnection->ExecuteSimpleSQL("COMMIT"_ns);
      }
    } else {
      rv = mConnection->ExecuteSimpleSQL("RELEASE sp"_ns +
                                         IntToCString(mNestingLevel));
    }

    NS_ENSURE_SUCCESS(rv, rv);

    TransactionFinished(lock);

    return NS_OK;
  }

  /**
   * Rolls back the transaction if one is in progress. If one is not in
   * progress, this is a NOP since the actual owner of the transaction outside
   * of our scope is in charge of finally rolling back the transaction.
   */
  nsresult Rollback() {
    // XXX Assert instead of returning NS_OK if the transaction hasn't been
    // started.
    if (!mConnection || mCompleted || !mHasTransaction) return NS_OK;

    SQLiteMutexAutoLock lock(mConnection->GetSharedDBMutex());

#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
    MOZ_DIAGNOSTIC_ASSERT(CurrentTransaction(lock));
#else
    if (!CurrentTransaction(lock)) {
      return NS_ERROR_NOT_AVAILABLE;
    }
#endif

    mCompleted = true;

    nsresult rv;

    if (TopLevelTransaction(lock)) {
      // TODO (bug 1062823): from Sqlite 3.7.11 on, rollback won't ever return
      // a busy error, so this handling can be removed.
      do {
        rv = mConnection->ExecuteSimpleSQL("ROLLBACK"_ns);
        if (rv == NS_ERROR_STORAGE_BUSY) (void)PR_Sleep(PR_INTERVAL_NO_WAIT);
      } while (rv == NS_ERROR_STORAGE_BUSY);
    } else {
      const auto nestingLevelCString = IntToCString(mNestingLevel);
      rv = mConnection->ExecuteSimpleSQL(
          "ROLLBACK TO sp"_ns + nestingLevelCString + "; RELEASE sp"_ns +
          nestingLevelCString);
    }

    NS_ENSURE_SUCCESS(rv, rv);

    TransactionFinished(lock);

    return NS_OK;
  }

 protected:
  void TransactionStarted(const SQLiteMutexAutoLock& aProofOfLock) {
    MOZ_ASSERT(mConnection);
    MOZ_ASSERT(!mHasTransaction);
    MOZ_ASSERT(mNestingLevel == 0);
    mHasTransaction = true;
    mNestingLevel = mConnection->IncreaseTransactionNestingLevel(aProofOfLock);
  }

  bool CurrentTransaction(const SQLiteMutexAutoLock& aProofOfLock) const {
    MOZ_ASSERT(mConnection);
    MOZ_ASSERT(mHasTransaction);
    MOZ_ASSERT(mNestingLevel > 0);
    return mNestingLevel ==
           mConnection->GetTransactionNestingLevel(aProofOfLock);
  }

  bool TopLevelTransaction(const SQLiteMutexAutoLock& aProofOfLock) const {
    MOZ_ASSERT(mConnection);
    MOZ_ASSERT(mHasTransaction);
    MOZ_ASSERT(mNestingLevel > 0);
    MOZ_ASSERT(CurrentTransaction(aProofOfLock));
    return mNestingLevel == 1;
  }

  void TransactionFinished(const SQLiteMutexAutoLock& aProofOfLock) {
    MOZ_ASSERT(mConnection);
    MOZ_ASSERT(mHasTransaction);
    MOZ_ASSERT(mNestingLevel > 0);
    MOZ_ASSERT(CurrentTransaction(aProofOfLock));
    mConnection->DecreaseTransactionNestingLevel(aProofOfLock);
    mNestingLevel = 0;
    mHasTransaction = false;
  }

  nsCOMPtr<mozIStorageConnection> mConnection;
  int32_t mType;
  uint32_t mNestingLevel;
  bool mHasTransaction;
  bool mCommitOnComplete;
  bool mCompleted;
  bool mAsyncCommit;
};

/**
 * This class wraps a statement so that it is guaraneed to be reset when
 * this object goes out of scope.
 *
 * Note that this always just resets the statement. If the statement doesn't
 * need resetting, the reset operation is inexpensive.
 */
class MOZ_STACK_CLASS mozStorageStatementScoper {
 public:
  explicit mozStorageStatementScoper(mozIStorageStatement* aStatement)
      : mStatement(aStatement) {}
  ~mozStorageStatementScoper() {
    if (mStatement) mStatement->Reset();
  }

  mozStorageStatementScoper(mozStorageStatementScoper&&) = default;
  mozStorageStatementScoper& operator=(mozStorageStatementScoper&&) = default;
  mozStorageStatementScoper(const mozStorageStatementScoper&) = delete;
  mozStorageStatementScoper& operator=(const mozStorageStatementScoper&) =
      delete;

  /**
   * Call this to make the statement not reset. You might do this if you know
   * that the statement has been reset.
   */
  void Abandon() { mStatement = nullptr; }

 protected:
  nsCOMPtr<mozIStorageStatement> mStatement;
};

// Use this to make queries uniquely identifiable in telemetry
// statistics, especially PRAGMAs.  We don't include __LINE__ so that
// queries are stable in the face of source code changes.
#define MOZ_STORAGE_UNIQUIFY_QUERY_STR "/* " __FILE__ " */ "

#endif /* MOZSTORAGEHELPER_H */