Wed, 31 Dec 2014 06:55:50 +0100
Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "DatabaseInfo.h" |
michael@0 | 8 | |
michael@0 | 9 | #include "nsDataHashtable.h" |
michael@0 | 10 | #include "nsThreadUtils.h" |
michael@0 | 11 | |
michael@0 | 12 | USING_INDEXEDDB_NAMESPACE |
michael@0 | 13 | |
michael@0 | 14 | namespace { |
michael@0 | 15 | |
michael@0 | 16 | typedef nsDataHashtable<nsCStringHashKey, DatabaseInfo*> |
michael@0 | 17 | DatabaseHash; |
michael@0 | 18 | |
michael@0 | 19 | DatabaseHash* gDatabaseHash = nullptr; |
michael@0 | 20 | |
michael@0 | 21 | PLDHashOperator |
michael@0 | 22 | EnumerateObjectStoreNames(const nsAString& aKey, |
michael@0 | 23 | ObjectStoreInfo* aData, |
michael@0 | 24 | void* aUserArg) |
michael@0 | 25 | { |
michael@0 | 26 | nsTArray<nsString>* array = static_cast<nsTArray<nsString>*>(aUserArg); |
michael@0 | 27 | if (!array->InsertElementSorted(aData->name)) { |
michael@0 | 28 | NS_ERROR("Out of memory?"); |
michael@0 | 29 | return PL_DHASH_STOP; |
michael@0 | 30 | } |
michael@0 | 31 | return PL_DHASH_NEXT; |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | PLDHashOperator |
michael@0 | 35 | CloneObjectStoreInfo(const nsAString& aKey, |
michael@0 | 36 | ObjectStoreInfo* aData, |
michael@0 | 37 | void* aUserArg) |
michael@0 | 38 | { |
michael@0 | 39 | ObjectStoreInfoHash* hash = static_cast<ObjectStoreInfoHash*>(aUserArg); |
michael@0 | 40 | |
michael@0 | 41 | nsRefPtr<ObjectStoreInfo> newInfo(new ObjectStoreInfo(*aData)); |
michael@0 | 42 | |
michael@0 | 43 | hash->Put(aKey, newInfo); |
michael@0 | 44 | |
michael@0 | 45 | return PL_DHASH_NEXT; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | DatabaseInfo::~DatabaseInfo() |
michael@0 | 51 | { |
michael@0 | 52 | // Clones are never in the hash. |
michael@0 | 53 | if (!cloned) { |
michael@0 | 54 | DatabaseInfo::Remove(id); |
michael@0 | 55 | } |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | ObjectStoreInfo::ObjectStoreInfo(ObjectStoreInfo& aOther) |
michael@0 | 59 | : nextAutoIncrementId(aOther.nextAutoIncrementId), |
michael@0 | 60 | comittedAutoIncrementId(aOther.comittedAutoIncrementId) |
michael@0 | 61 | { |
michael@0 | 62 | *static_cast<ObjectStoreInfoGuts*>(this) = |
michael@0 | 63 | static_cast<ObjectStoreInfoGuts&>(aOther); |
michael@0 | 64 | |
michael@0 | 65 | // Doesn't copy the refcount |
michael@0 | 66 | MOZ_COUNT_CTOR(ObjectStoreInfo); |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | #ifdef NS_BUILD_REFCNT_LOGGING |
michael@0 | 70 | |
michael@0 | 71 | IndexInfo::IndexInfo() |
michael@0 | 72 | : id(INT64_MIN), |
michael@0 | 73 | keyPath(0), |
michael@0 | 74 | unique(false), |
michael@0 | 75 | multiEntry(false) |
michael@0 | 76 | { |
michael@0 | 77 | MOZ_COUNT_CTOR(IndexInfo); |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | IndexInfo::IndexInfo(const IndexInfo& aOther) |
michael@0 | 81 | : name(aOther.name), |
michael@0 | 82 | id(aOther.id), |
michael@0 | 83 | keyPath(aOther.keyPath), |
michael@0 | 84 | unique(aOther.unique), |
michael@0 | 85 | multiEntry(aOther.multiEntry) |
michael@0 | 86 | { |
michael@0 | 87 | MOZ_COUNT_CTOR(IndexInfo); |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | IndexInfo::~IndexInfo() |
michael@0 | 91 | { |
michael@0 | 92 | MOZ_COUNT_DTOR(IndexInfo); |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | ObjectStoreInfo::ObjectStoreInfo() |
michael@0 | 96 | : nextAutoIncrementId(0), |
michael@0 | 97 | comittedAutoIncrementId(0) |
michael@0 | 98 | { |
michael@0 | 99 | MOZ_COUNT_CTOR(ObjectStoreInfo); |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | ObjectStoreInfo::~ObjectStoreInfo() |
michael@0 | 103 | { |
michael@0 | 104 | MOZ_COUNT_DTOR(ObjectStoreInfo); |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | IndexUpdateInfo::IndexUpdateInfo() |
michael@0 | 108 | : indexId(0), |
michael@0 | 109 | indexUnique(false) |
michael@0 | 110 | { |
michael@0 | 111 | MOZ_COUNT_CTOR(IndexUpdateInfo); |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | IndexUpdateInfo::IndexUpdateInfo(const IndexUpdateInfo& aOther) |
michael@0 | 115 | : indexId(aOther.indexId), |
michael@0 | 116 | indexUnique(aOther.indexUnique), |
michael@0 | 117 | value(aOther.value) |
michael@0 | 118 | { |
michael@0 | 119 | MOZ_COUNT_CTOR(IndexUpdateInfo); |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | IndexUpdateInfo::~IndexUpdateInfo() |
michael@0 | 123 | { |
michael@0 | 124 | MOZ_COUNT_DTOR(IndexUpdateInfo); |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | #endif /* NS_BUILD_REFCNT_LOGGING */ |
michael@0 | 128 | |
michael@0 | 129 | // static |
michael@0 | 130 | bool |
michael@0 | 131 | DatabaseInfo::Get(const nsACString& aId, |
michael@0 | 132 | DatabaseInfo** aInfo) |
michael@0 | 133 | { |
michael@0 | 134 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 135 | NS_ASSERTION(!aId.IsEmpty(), "Bad id!"); |
michael@0 | 136 | |
michael@0 | 137 | if (gDatabaseHash && |
michael@0 | 138 | gDatabaseHash->Get(aId, aInfo)) { |
michael@0 | 139 | NS_IF_ADDREF(*aInfo); |
michael@0 | 140 | return true; |
michael@0 | 141 | } |
michael@0 | 142 | return false; |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | // static |
michael@0 | 146 | bool |
michael@0 | 147 | DatabaseInfo::Put(DatabaseInfo* aInfo) |
michael@0 | 148 | { |
michael@0 | 149 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 150 | NS_ASSERTION(aInfo, "Null pointer!"); |
michael@0 | 151 | |
michael@0 | 152 | if (!gDatabaseHash) { |
michael@0 | 153 | nsAutoPtr<DatabaseHash> databaseHash(new DatabaseHash()); |
michael@0 | 154 | gDatabaseHash = databaseHash.forget(); |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | if (gDatabaseHash->Get(aInfo->id, nullptr)) { |
michael@0 | 158 | NS_ERROR("Already know about this database!"); |
michael@0 | 159 | return false; |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | gDatabaseHash->Put(aInfo->id, aInfo); |
michael@0 | 163 | |
michael@0 | 164 | return true; |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | // static |
michael@0 | 168 | void |
michael@0 | 169 | DatabaseInfo::Remove(const nsACString& aId) |
michael@0 | 170 | { |
michael@0 | 171 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 172 | |
michael@0 | 173 | if (gDatabaseHash) { |
michael@0 | 174 | gDatabaseHash->Remove(aId); |
michael@0 | 175 | |
michael@0 | 176 | if (!gDatabaseHash->Count()) { |
michael@0 | 177 | delete gDatabaseHash; |
michael@0 | 178 | gDatabaseHash = nullptr; |
michael@0 | 179 | } |
michael@0 | 180 | } |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | bool |
michael@0 | 184 | DatabaseInfo::GetObjectStoreNames(nsTArray<nsString>& aNames) |
michael@0 | 185 | { |
michael@0 | 186 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 187 | |
michael@0 | 188 | aNames.Clear(); |
michael@0 | 189 | if (objectStoreHash) { |
michael@0 | 190 | objectStoreHash->EnumerateRead(EnumerateObjectStoreNames, &aNames); |
michael@0 | 191 | } |
michael@0 | 192 | return true; |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | bool |
michael@0 | 196 | DatabaseInfo::ContainsStoreName(const nsAString& aName) |
michael@0 | 197 | { |
michael@0 | 198 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 199 | |
michael@0 | 200 | return objectStoreHash && objectStoreHash->Get(aName, nullptr); |
michael@0 | 201 | } |
michael@0 | 202 | |
michael@0 | 203 | ObjectStoreInfo* |
michael@0 | 204 | DatabaseInfo::GetObjectStore(const nsAString& aName) |
michael@0 | 205 | { |
michael@0 | 206 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 207 | |
michael@0 | 208 | if (objectStoreHash) { |
michael@0 | 209 | return objectStoreHash->GetWeak(aName); |
michael@0 | 210 | } |
michael@0 | 211 | |
michael@0 | 212 | return nullptr; |
michael@0 | 213 | } |
michael@0 | 214 | |
michael@0 | 215 | bool |
michael@0 | 216 | DatabaseInfo::PutObjectStore(ObjectStoreInfo* aInfo) |
michael@0 | 217 | { |
michael@0 | 218 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 219 | NS_ASSERTION(aInfo, "Null pointer!"); |
michael@0 | 220 | |
michael@0 | 221 | if (!objectStoreHash) { |
michael@0 | 222 | nsAutoPtr<ObjectStoreInfoHash> hash(new ObjectStoreInfoHash()); |
michael@0 | 223 | objectStoreHash = hash.forget(); |
michael@0 | 224 | } |
michael@0 | 225 | |
michael@0 | 226 | if (objectStoreHash->Get(aInfo->name, nullptr)) { |
michael@0 | 227 | NS_ERROR("Already have an entry for this objectstore!"); |
michael@0 | 228 | return false; |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | objectStoreHash->Put(aInfo->name, aInfo); |
michael@0 | 232 | return true; |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | void |
michael@0 | 236 | DatabaseInfo::RemoveObjectStore(const nsAString& aName) |
michael@0 | 237 | { |
michael@0 | 238 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 239 | NS_ASSERTION(GetObjectStore(aName), "Don't know about this one!"); |
michael@0 | 240 | |
michael@0 | 241 | if (objectStoreHash) { |
michael@0 | 242 | objectStoreHash->Remove(aName); |
michael@0 | 243 | } |
michael@0 | 244 | } |
michael@0 | 245 | |
michael@0 | 246 | already_AddRefed<DatabaseInfo> |
michael@0 | 247 | DatabaseInfo::Clone() |
michael@0 | 248 | { |
michael@0 | 249 | nsRefPtr<DatabaseInfo> dbInfo(new DatabaseInfo()); |
michael@0 | 250 | |
michael@0 | 251 | dbInfo->cloned = true; |
michael@0 | 252 | dbInfo->name = name; |
michael@0 | 253 | dbInfo->group = group; |
michael@0 | 254 | dbInfo->origin = origin; |
michael@0 | 255 | dbInfo->version = version; |
michael@0 | 256 | dbInfo->persistenceType = persistenceType; |
michael@0 | 257 | dbInfo->id = id; |
michael@0 | 258 | dbInfo->filePath = filePath; |
michael@0 | 259 | dbInfo->nextObjectStoreId = nextObjectStoreId; |
michael@0 | 260 | dbInfo->nextIndexId = nextIndexId; |
michael@0 | 261 | |
michael@0 | 262 | if (objectStoreHash) { |
michael@0 | 263 | dbInfo->objectStoreHash = new ObjectStoreInfoHash(); |
michael@0 | 264 | objectStoreHash->EnumerateRead(CloneObjectStoreInfo, |
michael@0 | 265 | dbInfo->objectStoreHash); |
michael@0 | 266 | } |
michael@0 | 267 | |
michael@0 | 268 | return dbInfo.forget(); |
michael@0 | 269 | } |