Bug 1833854 - Part 4: Move all code that deals with maintaining invariants into a...
[gecko.git] / dom / quota / CachingDatabaseConnection.h
blob355baf52931cc37081c8f4a05eba2bddcbae9a53
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef DOM_QUOTA_CACHINGDATABASECONNECTION_H_
8 #define DOM_QUOTA_CACHINGDATABASECONNECTION_H_
10 #include "mozilla/dom/quota/Config.h"
12 #include "mozStorageHelper.h"
13 #include "nsCOMPtr.h"
14 #include "nscore.h"
15 #include "nsHashKeys.h"
16 #include "nsInterfaceHashtable.h"
17 #include "nsString.h"
18 #include "mozilla/Assertions.h"
19 #include "mozilla/Attributes.h"
20 #include "mozilla/InitializedOnce.h"
21 #include "mozilla/NotNull.h"
22 #include "mozilla/dom/quota/QuotaCommon.h"
23 #include "mozilla/dom/quota/ResultExtensions.h"
24 #include "mozilla/dom/quota/ScopedLogExtraInfo.h"
26 namespace mozilla::dom::quota {
28 class CachingDatabaseConnection {
29 public:
30 class CachedStatement;
32 // A stack-only RAII wrapper that resets its borrowed statement when the
33 // wrapper goes out of scope. Note it's intentionally not declared MOZ_RAII,
34 // because it actually is used as a temporary in simple cases like
35 // `stmt.Borrow()->Execute()`. It also automatically exposes the current query
36 // to ScopedLogExtraInfo as "query" in builds where this mechanism is active.
37 class MOZ_STACK_CLASS BorrowedStatement : mozStorageStatementScoper {
38 public:
39 mozIStorageStatement& operator*() const;
41 MOZ_NONNULL_RETURN mozIStorageStatement* operator->() const
42 MOZ_NO_ADDREF_RELEASE_ON_RETURN;
44 BorrowedStatement(BorrowedStatement&& aOther) = default;
46 // No funny business allowed.
47 BorrowedStatement& operator=(BorrowedStatement&&) = delete;
48 BorrowedStatement(const BorrowedStatement&) = delete;
49 BorrowedStatement& operator=(const BorrowedStatement&) = delete;
51 private:
52 friend class CachedStatement;
54 #ifdef QM_SCOPED_LOG_EXTRA_INFO_ENABLED
55 BorrowedStatement(NotNull<mozIStorageStatement*> aStatement,
56 const nsACString& aQuery)
57 : mozStorageStatementScoper(aStatement),
58 mExtraInfo{ScopedLogExtraInfo::kTagQuery, aQuery} {}
60 ScopedLogExtraInfo mExtraInfo;
61 #else
62 MOZ_IMPLICIT BorrowedStatement(NotNull<mozIStorageStatement*> aStatement)
63 : mozStorageStatementScoper(aStatement) {}
64 #endif
67 class LazyStatement;
69 void AssertIsOnConnectionThread() const {
70 #ifdef MOZ_THREAD_SAFETY_OWNERSHIP_CHECKS_SUPPORTED
71 mOwningThread->AssertOwnership("CachingDatabaseConnection not thread-safe");
72 #endif
75 bool HasStorageConnection() const {
76 return static_cast<bool>(mStorageConnection);
79 mozIStorageConnection& MutableStorageConnection() const {
80 AssertIsOnConnectionThread();
81 MOZ_ASSERT(mStorageConnection);
83 return **mStorageConnection;
86 Result<CachedStatement, nsresult> GetCachedStatement(
87 const nsACString& aQuery);
89 Result<BorrowedStatement, nsresult> BorrowCachedStatement(
90 const nsACString& aQuery);
92 template <typename BindFunctor>
93 nsresult ExecuteCachedStatement(const nsACString& aQuery,
94 BindFunctor&& aBindFunctor) {
95 QM_TRY_INSPECT(const auto& stmt, BorrowCachedStatement(aQuery));
96 QM_TRY(std::forward<BindFunctor>(aBindFunctor)(*stmt));
97 QM_TRY(MOZ_TO_RESULT(stmt->Execute()));
99 return NS_OK;
102 nsresult ExecuteCachedStatement(const nsACString& aQuery);
104 template <typename BindFunctor>
105 Result<Maybe<BorrowedStatement>, nsresult>
106 BorrowAndExecuteSingleStepStatement(const nsACString& aQuery,
107 BindFunctor&& aBindFunctor);
109 #ifdef DEBUG
110 ~CachingDatabaseConnection() {
111 MOZ_ASSERT(!mStorageConnection);
112 MOZ_ASSERT(!mCachedStatements.Count());
114 #endif
116 protected:
117 explicit CachingDatabaseConnection(
118 MovingNotNull<nsCOMPtr<mozIStorageConnection>> aStorageConnection);
120 CachingDatabaseConnection() = default;
122 void LazyInit(
123 MovingNotNull<nsCOMPtr<mozIStorageConnection>> aStorageConnection);
125 void Close();
127 private:
128 #ifdef MOZ_THREAD_SAFETY_OWNERSHIP_CHECKS_SUPPORTED
129 LazyInitializedOnce<const nsAutoOwningThread> mOwningThread;
130 #endif
132 LazyInitializedOnceEarlyDestructible<
133 const NotNull<nsCOMPtr<mozIStorageConnection>>>
134 mStorageConnection;
135 nsInterfaceHashtable<nsCStringHashKey, mozIStorageStatement>
136 mCachedStatements;
139 class CachingDatabaseConnection::CachedStatement final {
140 friend class CachingDatabaseConnection;
142 nsCOMPtr<mozIStorageStatement> mStatement;
144 #ifdef QM_SCOPED_LOG_EXTRA_INFO_ENABLED
145 nsCString mQuery;
146 #endif
148 #ifdef DEBUG
149 CachingDatabaseConnection* mDEBUGConnection;
150 #endif
152 public:
153 #if defined(DEBUG) || defined(NS_BUILD_REFCNT_LOGGING)
154 CachedStatement();
155 ~CachedStatement();
156 #else
157 CachedStatement() = default;
158 #endif
160 void AssertIsOnConnectionThread() const;
162 explicit operator bool() const;
164 BorrowedStatement Borrow() const;
166 private:
167 // Only called by CachingDatabaseConnection.
168 CachedStatement(CachingDatabaseConnection* aConnection,
169 nsCOMPtr<mozIStorageStatement> aStatement,
170 const nsACString& aQuery);
172 public:
173 #if defined(NS_BUILD_REFCNT_LOGGING)
174 CachedStatement(CachedStatement&& aOther)
175 : mStatement(std::move(aOther.mStatement))
176 # ifdef QM_SCOPED_LOG_EXTRA_INFO_ENABLED
178 mQuery(std::move(aOther.mQuery))
179 # endif
180 # ifdef DEBUG
182 mDEBUGConnection(aOther.mDEBUGConnection)
183 # endif
185 MOZ_COUNT_CTOR(CachingDatabaseConnection::CachedStatement);
187 #else
188 CachedStatement(CachedStatement&&) = default;
189 #endif
191 CachedStatement& operator=(CachedStatement&&) = default;
193 // No funny business allowed.
194 CachedStatement(const CachedStatement&) = delete;
195 CachedStatement& operator=(const CachedStatement&) = delete;
198 class CachingDatabaseConnection::LazyStatement final {
199 public:
200 LazyStatement(CachingDatabaseConnection& aConnection,
201 const nsACString& aQueryString)
202 : mConnection{aConnection}, mQueryString{aQueryString} {}
204 Result<CachingDatabaseConnection::BorrowedStatement, nsresult> Borrow();
206 template <typename BindFunctor>
207 Result<Maybe<CachingDatabaseConnection::BorrowedStatement>, nsresult>
208 BorrowAndExecuteSingleStep(BindFunctor&& aBindFunctor) {
209 QM_TRY_UNWRAP(auto borrowedStatement, Borrow());
211 QM_TRY(std::forward<BindFunctor>(aBindFunctor)(*borrowedStatement));
213 QM_TRY_INSPECT(
214 const bool& hasResult,
215 MOZ_TO_RESULT_INVOKE_MEMBER(&*borrowedStatement, ExecuteStep));
217 return hasResult ? Some(std::move(borrowedStatement)) : Nothing{};
220 private:
221 Result<Ok, nsresult> Initialize();
223 CachingDatabaseConnection& mConnection;
224 const nsCString mQueryString;
225 CachingDatabaseConnection::CachedStatement mCachedStatement;
228 template <typename BindFunctor>
229 Result<Maybe<CachingDatabaseConnection::BorrowedStatement>, nsresult>
230 CachingDatabaseConnection::BorrowAndExecuteSingleStepStatement(
231 const nsACString& aQuery, BindFunctor&& aBindFunctor) {
232 return LazyStatement{*this, aQuery}.BorrowAndExecuteSingleStep(
233 std::forward<BindFunctor>(aBindFunctor));
236 } // namespace mozilla::dom::quota
238 #endif // DOM_QUOTA_CACHINGDATABASECONNECTION_H_