dom/indexedDB/DatabaseInfo.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/indexedDB/DatabaseInfo.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,269 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=2 et sw=2 tw=80: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "DatabaseInfo.h"
    1.11 +
    1.12 +#include "nsDataHashtable.h"
    1.13 +#include "nsThreadUtils.h"
    1.14 +
    1.15 +USING_INDEXEDDB_NAMESPACE
    1.16 +
    1.17 +namespace {
    1.18 +
    1.19 +typedef nsDataHashtable<nsCStringHashKey, DatabaseInfo*>
    1.20 +        DatabaseHash;
    1.21 +
    1.22 +DatabaseHash* gDatabaseHash = nullptr;
    1.23 +
    1.24 +PLDHashOperator
    1.25 +EnumerateObjectStoreNames(const nsAString& aKey,
    1.26 +                          ObjectStoreInfo* aData,
    1.27 +                          void* aUserArg)
    1.28 +{
    1.29 +  nsTArray<nsString>* array = static_cast<nsTArray<nsString>*>(aUserArg);
    1.30 +  if (!array->InsertElementSorted(aData->name)) {
    1.31 +    NS_ERROR("Out of memory?");
    1.32 +    return PL_DHASH_STOP;
    1.33 +  }
    1.34 +  return PL_DHASH_NEXT;
    1.35 +}
    1.36 +
    1.37 +PLDHashOperator
    1.38 +CloneObjectStoreInfo(const nsAString& aKey,
    1.39 +                     ObjectStoreInfo* aData,
    1.40 +                     void* aUserArg)
    1.41 +{
    1.42 +  ObjectStoreInfoHash* hash = static_cast<ObjectStoreInfoHash*>(aUserArg);
    1.43 +
    1.44 +  nsRefPtr<ObjectStoreInfo> newInfo(new ObjectStoreInfo(*aData));
    1.45 +
    1.46 +  hash->Put(aKey, newInfo);
    1.47 +
    1.48 +  return PL_DHASH_NEXT;
    1.49 +}
    1.50 +
    1.51 +}
    1.52 +
    1.53 +DatabaseInfo::~DatabaseInfo()
    1.54 +{
    1.55 +  // Clones are never in the hash.
    1.56 +  if (!cloned) {
    1.57 +    DatabaseInfo::Remove(id);
    1.58 +  }
    1.59 +}
    1.60 +
    1.61 +ObjectStoreInfo::ObjectStoreInfo(ObjectStoreInfo& aOther)
    1.62 +: nextAutoIncrementId(aOther.nextAutoIncrementId),
    1.63 +  comittedAutoIncrementId(aOther.comittedAutoIncrementId)
    1.64 +{
    1.65 +  *static_cast<ObjectStoreInfoGuts*>(this) =
    1.66 +    static_cast<ObjectStoreInfoGuts&>(aOther);
    1.67 +
    1.68 +  // Doesn't copy the refcount
    1.69 +  MOZ_COUNT_CTOR(ObjectStoreInfo);
    1.70 +}
    1.71 +
    1.72 +#ifdef NS_BUILD_REFCNT_LOGGING
    1.73 +
    1.74 +IndexInfo::IndexInfo()
    1.75 +: id(INT64_MIN),
    1.76 +  keyPath(0),
    1.77 +  unique(false),
    1.78 +  multiEntry(false)
    1.79 +{
    1.80 +  MOZ_COUNT_CTOR(IndexInfo);
    1.81 +}
    1.82 +
    1.83 +IndexInfo::IndexInfo(const IndexInfo& aOther)
    1.84 +: name(aOther.name),
    1.85 +  id(aOther.id),
    1.86 +  keyPath(aOther.keyPath),
    1.87 +  unique(aOther.unique),
    1.88 +  multiEntry(aOther.multiEntry)
    1.89 +{
    1.90 +  MOZ_COUNT_CTOR(IndexInfo);
    1.91 +}
    1.92 +
    1.93 +IndexInfo::~IndexInfo()
    1.94 +{
    1.95 +  MOZ_COUNT_DTOR(IndexInfo);
    1.96 +}
    1.97 +
    1.98 +ObjectStoreInfo::ObjectStoreInfo()
    1.99 +: nextAutoIncrementId(0),
   1.100 +  comittedAutoIncrementId(0)
   1.101 +{
   1.102 +  MOZ_COUNT_CTOR(ObjectStoreInfo);
   1.103 +}
   1.104 +
   1.105 +ObjectStoreInfo::~ObjectStoreInfo()
   1.106 +{
   1.107 +  MOZ_COUNT_DTOR(ObjectStoreInfo);
   1.108 +}
   1.109 +
   1.110 +IndexUpdateInfo::IndexUpdateInfo()
   1.111 +: indexId(0),
   1.112 +  indexUnique(false)
   1.113 +{
   1.114 +  MOZ_COUNT_CTOR(IndexUpdateInfo);
   1.115 +}
   1.116 +
   1.117 +IndexUpdateInfo::IndexUpdateInfo(const IndexUpdateInfo& aOther)
   1.118 +: indexId(aOther.indexId),
   1.119 +  indexUnique(aOther.indexUnique),
   1.120 +  value(aOther.value)
   1.121 +{
   1.122 +  MOZ_COUNT_CTOR(IndexUpdateInfo);
   1.123 +}
   1.124 +
   1.125 +IndexUpdateInfo::~IndexUpdateInfo()
   1.126 +{
   1.127 +  MOZ_COUNT_DTOR(IndexUpdateInfo);
   1.128 +}
   1.129 +
   1.130 +#endif /* NS_BUILD_REFCNT_LOGGING */
   1.131 +
   1.132 +// static
   1.133 +bool
   1.134 +DatabaseInfo::Get(const nsACString& aId,
   1.135 +                  DatabaseInfo** aInfo)
   1.136 +{
   1.137 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
   1.138 +  NS_ASSERTION(!aId.IsEmpty(), "Bad id!");
   1.139 +
   1.140 +  if (gDatabaseHash &&
   1.141 +      gDatabaseHash->Get(aId, aInfo)) {
   1.142 +    NS_IF_ADDREF(*aInfo);
   1.143 +    return true;
   1.144 +  }
   1.145 +  return false;
   1.146 +}
   1.147 +
   1.148 +// static
   1.149 +bool
   1.150 +DatabaseInfo::Put(DatabaseInfo* aInfo)
   1.151 +{
   1.152 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
   1.153 +  NS_ASSERTION(aInfo, "Null pointer!");
   1.154 +
   1.155 +  if (!gDatabaseHash) {
   1.156 +    nsAutoPtr<DatabaseHash> databaseHash(new DatabaseHash());
   1.157 +    gDatabaseHash = databaseHash.forget();
   1.158 +  }
   1.159 +
   1.160 +  if (gDatabaseHash->Get(aInfo->id, nullptr)) {
   1.161 +    NS_ERROR("Already know about this database!");
   1.162 +    return false;
   1.163 +  }
   1.164 +
   1.165 +  gDatabaseHash->Put(aInfo->id, aInfo);
   1.166 +
   1.167 +  return true;
   1.168 +}
   1.169 +
   1.170 +// static
   1.171 +void
   1.172 +DatabaseInfo::Remove(const nsACString& aId)
   1.173 +{
   1.174 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
   1.175 +
   1.176 +  if (gDatabaseHash) {
   1.177 +    gDatabaseHash->Remove(aId);
   1.178 +
   1.179 +    if (!gDatabaseHash->Count()) {
   1.180 +      delete gDatabaseHash;
   1.181 +      gDatabaseHash = nullptr;
   1.182 +    }
   1.183 +  }
   1.184 +}
   1.185 +
   1.186 +bool
   1.187 +DatabaseInfo::GetObjectStoreNames(nsTArray<nsString>& aNames)
   1.188 +{
   1.189 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
   1.190 +
   1.191 +  aNames.Clear();
   1.192 +  if (objectStoreHash) {
   1.193 +    objectStoreHash->EnumerateRead(EnumerateObjectStoreNames, &aNames);
   1.194 +  }
   1.195 +  return true;
   1.196 +}
   1.197 +
   1.198 +bool
   1.199 +DatabaseInfo::ContainsStoreName(const nsAString& aName)
   1.200 +{
   1.201 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
   1.202 +
   1.203 +  return objectStoreHash && objectStoreHash->Get(aName, nullptr);
   1.204 +}
   1.205 +
   1.206 +ObjectStoreInfo*
   1.207 +DatabaseInfo::GetObjectStore(const nsAString& aName)
   1.208 +{
   1.209 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
   1.210 +
   1.211 +  if (objectStoreHash) {
   1.212 +    return objectStoreHash->GetWeak(aName);
   1.213 +  }
   1.214 +
   1.215 +  return nullptr;
   1.216 +}
   1.217 +
   1.218 +bool
   1.219 +DatabaseInfo::PutObjectStore(ObjectStoreInfo* aInfo)
   1.220 +{
   1.221 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
   1.222 +  NS_ASSERTION(aInfo, "Null pointer!");
   1.223 +
   1.224 +  if (!objectStoreHash) {
   1.225 +    nsAutoPtr<ObjectStoreInfoHash> hash(new ObjectStoreInfoHash());
   1.226 +    objectStoreHash = hash.forget();
   1.227 +  }
   1.228 +
   1.229 +  if (objectStoreHash->Get(aInfo->name, nullptr)) {
   1.230 +    NS_ERROR("Already have an entry for this objectstore!");
   1.231 +    return false;
   1.232 +  }
   1.233 +
   1.234 +  objectStoreHash->Put(aInfo->name, aInfo);
   1.235 +  return true;
   1.236 +}
   1.237 +
   1.238 +void
   1.239 +DatabaseInfo::RemoveObjectStore(const nsAString& aName)
   1.240 +{
   1.241 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
   1.242 +  NS_ASSERTION(GetObjectStore(aName), "Don't know about this one!");
   1.243 +
   1.244 +  if (objectStoreHash) {
   1.245 +    objectStoreHash->Remove(aName);
   1.246 +  }
   1.247 +}
   1.248 +
   1.249 +already_AddRefed<DatabaseInfo>
   1.250 +DatabaseInfo::Clone()
   1.251 +{
   1.252 +  nsRefPtr<DatabaseInfo> dbInfo(new DatabaseInfo());
   1.253 +
   1.254 +  dbInfo->cloned = true;
   1.255 +  dbInfo->name = name;
   1.256 +  dbInfo->group = group;
   1.257 +  dbInfo->origin = origin;
   1.258 +  dbInfo->version = version;
   1.259 +  dbInfo->persistenceType = persistenceType;
   1.260 +  dbInfo->id = id;
   1.261 +  dbInfo->filePath = filePath;
   1.262 +  dbInfo->nextObjectStoreId = nextObjectStoreId;
   1.263 +  dbInfo->nextIndexId = nextIndexId;
   1.264 +
   1.265 +  if (objectStoreHash) {
   1.266 +    dbInfo->objectStoreHash = new ObjectStoreInfoHash();
   1.267 +    objectStoreHash->EnumerateRead(CloneObjectStoreInfo,
   1.268 +                                   dbInfo->objectStoreHash);
   1.269 +  }
   1.270 +
   1.271 +  return dbInfo.forget();
   1.272 +}

mercurial