2010-06-23 12:46:08 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
2012-05-21 04:12:37 -07: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/. */
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
#include "DatabaseInfo.h"
|
|
|
|
|
2011-11-07 15:20:20 -08:00
|
|
|
#include "nsDataHashtable.h"
|
2010-06-23 12:46:08 -07:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
|
|
|
USING_INDEXEDDB_NAMESPACE
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2011-11-07 15:20:20 -08:00
|
|
|
typedef nsDataHashtable<nsISupportsHashKey, DatabaseInfo*>
|
2010-06-23 12:46:08 -07:00
|
|
|
DatabaseHash;
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
DatabaseHash* gDatabaseHash = nullptr;
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
PLDHashOperator
|
|
|
|
EnumerateObjectStoreNames(const nsAString& aKey,
|
|
|
|
ObjectStoreInfo* aData,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
|
|
|
nsTArray<nsString>* array = static_cast<nsTArray<nsString>*>(aUserArg);
|
2012-06-29 09:48:34 -07:00
|
|
|
if (!array->InsertElementSorted(aData->name)) {
|
2010-06-23 12:46:08 -07:00
|
|
|
NS_ERROR("Out of memory?");
|
|
|
|
return PL_DHASH_STOP;
|
|
|
|
}
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2011-11-07 16:15:45 -08:00
|
|
|
PLDHashOperator
|
|
|
|
CloneObjectStoreInfo(const nsAString& aKey,
|
|
|
|
ObjectStoreInfo* aData,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
|
|
|
ObjectStoreInfoHash* hash = static_cast<ObjectStoreInfoHash*>(aUserArg);
|
|
|
|
|
2011-12-16 16:40:47 -08:00
|
|
|
nsRefPtr<ObjectStoreInfo> newInfo(new ObjectStoreInfo(*aData));
|
2011-11-07 16:15:45 -08:00
|
|
|
|
2012-05-18 10:30:49 -07:00
|
|
|
hash->Put(aKey, newInfo);
|
2011-11-07 16:15:45 -08:00
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-10-19 10:58:42 -07:00
|
|
|
DatabaseInfo::~DatabaseInfo()
|
|
|
|
{
|
2011-11-07 16:15:45 -08:00
|
|
|
// Clones are never in the hash.
|
|
|
|
if (!cloned) {
|
|
|
|
DatabaseInfo::Remove(id);
|
|
|
|
}
|
2010-10-19 10:58:42 -07:00
|
|
|
}
|
|
|
|
|
2011-12-16 16:40:47 -08:00
|
|
|
ObjectStoreInfo::ObjectStoreInfo(ObjectStoreInfo& aOther)
|
2012-06-01 10:21:12 -07:00
|
|
|
: nextAutoIncrementId(aOther.nextAutoIncrementId),
|
2011-12-16 16:40:47 -08:00
|
|
|
comittedAutoIncrementId(aOther.comittedAutoIncrementId)
|
|
|
|
{
|
2012-06-01 10:21:12 -07:00
|
|
|
*static_cast<ObjectStoreInfoGuts*>(this) =
|
|
|
|
static_cast<ObjectStoreInfoGuts&>(aOther);
|
|
|
|
|
2011-12-16 16:40:47 -08:00
|
|
|
// Doesn't copy the refcount
|
|
|
|
MOZ_COUNT_CTOR(ObjectStoreInfo);
|
|
|
|
}
|
|
|
|
|
2011-11-07 16:06:13 -08:00
|
|
|
#ifdef NS_BUILD_REFCNT_LOGGING
|
|
|
|
|
2010-10-19 10:58:42 -07:00
|
|
|
IndexInfo::IndexInfo()
|
2012-09-28 12:55:23 -07:00
|
|
|
: id(INT64_MIN),
|
2012-06-21 15:27:13 -07:00
|
|
|
keyPath(0),
|
2010-10-19 10:58:42 -07:00
|
|
|
unique(false),
|
2011-12-04 09:39:01 -08:00
|
|
|
multiEntry(false)
|
2010-10-19 10:58:42 -07:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(IndexInfo);
|
|
|
|
}
|
|
|
|
|
2011-11-07 16:15:45 -08:00
|
|
|
IndexInfo::IndexInfo(const IndexInfo& aOther)
|
2012-06-01 10:21:12 -07:00
|
|
|
: name(aOther.name),
|
|
|
|
id(aOther.id),
|
2011-11-07 16:15:45 -08:00
|
|
|
keyPath(aOther.keyPath),
|
|
|
|
unique(aOther.unique),
|
2011-12-04 09:39:01 -08:00
|
|
|
multiEntry(aOther.multiEntry)
|
2011-11-07 16:15:45 -08:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(IndexInfo);
|
|
|
|
}
|
|
|
|
|
2010-10-19 10:58:42 -07:00
|
|
|
IndexInfo::~IndexInfo()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(IndexInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectStoreInfo::ObjectStoreInfo()
|
2012-06-01 10:21:12 -07:00
|
|
|
: nextAutoIncrementId(0),
|
2011-12-16 16:40:47 -08:00
|
|
|
comittedAutoIncrementId(0)
|
2011-11-07 16:15:45 -08:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(ObjectStoreInfo);
|
|
|
|
}
|
|
|
|
|
2010-10-19 10:58:42 -07:00
|
|
|
ObjectStoreInfo::~ObjectStoreInfo()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(ObjectStoreInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
IndexUpdateInfo::IndexUpdateInfo()
|
2012-06-19 18:50:39 -07:00
|
|
|
: indexId(0),
|
|
|
|
indexUnique(false)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(IndexUpdateInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
IndexUpdateInfo::IndexUpdateInfo(const IndexUpdateInfo& aOther)
|
|
|
|
: indexId(aOther.indexId),
|
|
|
|
indexUnique(aOther.indexUnique),
|
|
|
|
value(aOther.value)
|
2010-10-19 10:58:42 -07:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(IndexUpdateInfo);
|
|
|
|
}
|
2010-10-19 10:58:52 -07:00
|
|
|
|
2010-10-19 10:58:42 -07:00
|
|
|
IndexUpdateInfo::~IndexUpdateInfo()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(IndexUpdateInfo);
|
|
|
|
}
|
2012-06-19 18:50:39 -07:00
|
|
|
|
2010-10-19 10:58:42 -07:00
|
|
|
#endif /* NS_BUILD_REFCNT_LOGGING */
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
// static
|
|
|
|
bool
|
2011-11-02 05:53:12 -07:00
|
|
|
DatabaseInfo::Get(nsIAtom* aId,
|
2010-06-23 12:46:08 -07:00
|
|
|
DatabaseInfo** aInfo)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aId, "Bad id!");
|
|
|
|
|
2011-11-07 15:20:20 -08:00
|
|
|
if (gDatabaseHash &&
|
|
|
|
gDatabaseHash->Get(aId, aInfo)) {
|
2011-11-07 16:15:45 -08:00
|
|
|
NS_IF_ADDREF(*aInfo);
|
2011-11-07 15:20:20 -08:00
|
|
|
return true;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
bool
|
|
|
|
DatabaseInfo::Put(DatabaseInfo* aInfo)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aInfo, "Null pointer!");
|
|
|
|
|
|
|
|
if (!gDatabaseHash) {
|
|
|
|
nsAutoPtr<DatabaseHash> databaseHash(new DatabaseHash());
|
|
|
|
gDatabaseHash = databaseHash.forget();
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
if (gDatabaseHash->Get(aInfo->id, nullptr)) {
|
2010-06-23 12:46:08 -07:00
|
|
|
NS_ERROR("Already know about this database!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-05-18 10:30:49 -07:00
|
|
|
gDatabaseHash->Put(aInfo->id, aInfo);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void
|
2011-11-02 05:53:12 -07:00
|
|
|
DatabaseInfo::Remove(nsIAtom* aId)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2011-11-07 15:20:20 -08:00
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
if (gDatabaseHash) {
|
|
|
|
gDatabaseHash->Remove(aId);
|
|
|
|
|
|
|
|
if (!gDatabaseHash->Count()) {
|
|
|
|
delete gDatabaseHash;
|
2012-07-30 07:20:58 -07:00
|
|
|
gDatabaseHash = nullptr;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
}
|
2011-12-16 16:40:47 -08:00
|
|
|
}
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
bool
|
|
|
|
DatabaseInfo::GetObjectStoreNames(nsTArray<nsString>& aNames)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
aNames.Clear();
|
2011-11-07 16:15:45 -08:00
|
|
|
if (objectStoreHash) {
|
|
|
|
objectStoreHash->EnumerateRead(EnumerateObjectStoreNames, &aNames);
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DatabaseInfo::ContainsStoreName(const nsAString& aName)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
return objectStoreHash && objectStoreHash->Get(aName, nullptr);
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2011-12-16 16:40:47 -08:00
|
|
|
ObjectStoreInfo*
|
|
|
|
DatabaseInfo::GetObjectStore(const nsAString& aName)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2011-11-07 16:15:43 -08:00
|
|
|
if (objectStoreHash) {
|
2011-12-16 16:40:47 -08:00
|
|
|
return objectStoreHash->GetWeak(aName);
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2011-11-07 16:15:43 -08:00
|
|
|
DatabaseInfo::PutObjectStore(ObjectStoreInfo* aInfo)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aInfo, "Null pointer!");
|
|
|
|
|
2011-11-07 16:15:43 -08:00
|
|
|
if (!objectStoreHash) {
|
|
|
|
nsAutoPtr<ObjectStoreInfoHash> hash(new ObjectStoreInfoHash());
|
|
|
|
objectStoreHash = hash.forget();
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
if (objectStoreHash->Get(aInfo->name, nullptr)) {
|
2010-06-23 12:46:08 -07:00
|
|
|
NS_ERROR("Already have an entry for this objectstore!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-05-18 10:30:49 -07:00
|
|
|
objectStoreHash->Put(aInfo->name, aInfo);
|
|
|
|
return true;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-11-07 16:15:43 -08:00
|
|
|
DatabaseInfo::RemoveObjectStore(const nsAString& aName)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2011-12-16 16:40:47 -08:00
|
|
|
NS_ASSERTION(GetObjectStore(aName), "Don't know about this one!");
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-11-07 16:15:43 -08:00
|
|
|
if (objectStoreHash) {
|
|
|
|
objectStoreHash->Remove(aName);
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
}
|
2011-11-07 16:15:45 -08:00
|
|
|
|
|
|
|
already_AddRefed<DatabaseInfo>
|
|
|
|
DatabaseInfo::Clone()
|
|
|
|
{
|
|
|
|
nsRefPtr<DatabaseInfo> dbInfo(new DatabaseInfo());
|
|
|
|
|
|
|
|
dbInfo->cloned = true;
|
|
|
|
dbInfo->name = name;
|
2013-09-10 21:18:36 -07:00
|
|
|
dbInfo->group = group;
|
2012-06-01 10:21:12 -07:00
|
|
|
dbInfo->origin = origin;
|
2011-11-07 16:15:45 -08:00
|
|
|
dbInfo->version = version;
|
2013-09-10 21:18:36 -07:00
|
|
|
dbInfo->persistenceType = persistenceType;
|
2011-11-07 16:15:45 -08:00
|
|
|
dbInfo->id = id;
|
|
|
|
dbInfo->filePath = filePath;
|
|
|
|
dbInfo->nextObjectStoreId = nextObjectStoreId;
|
|
|
|
dbInfo->nextIndexId = nextIndexId;
|
|
|
|
|
|
|
|
if (objectStoreHash) {
|
|
|
|
dbInfo->objectStoreHash = new ObjectStoreInfoHash();
|
|
|
|
objectStoreHash->EnumerateRead(CloneObjectStoreInfo,
|
|
|
|
dbInfo->objectStoreHash);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dbInfo.forget();
|
|
|
|
}
|