2010-06-23 19:46:08 +00: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 11:12:37 +00: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 19:46:08 +00:00
|
|
|
|
|
|
|
#include "DatabaseInfo.h"
|
|
|
|
|
2011-11-07 23:20:20 +00:00
|
|
|
#include "nsDataHashtable.h"
|
2010-06-23 19:46:08 +00:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
|
|
|
USING_INDEXEDDB_NAMESPACE
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2011-11-07 23:20:20 +00:00
|
|
|
typedef nsDataHashtable<nsISupportsHashKey, DatabaseInfo*>
|
2010-06-23 19:46:08 +00:00
|
|
|
DatabaseHash;
|
|
|
|
|
|
|
|
DatabaseHash* gDatabaseHash = nsnull;
|
|
|
|
|
|
|
|
PLDHashOperator
|
|
|
|
EnumerateObjectStoreNames(const nsAString& aKey,
|
|
|
|
ObjectStoreInfo* aData,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
|
|
|
nsTArray<nsString>* array = static_cast<nsTArray<nsString>*>(aUserArg);
|
|
|
|
if (!array->AppendElement(aData->name)) {
|
|
|
|
NS_ERROR("Out of memory?");
|
|
|
|
return PL_DHASH_STOP;
|
|
|
|
}
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2011-11-08 00:15:45 +00:00
|
|
|
PLDHashOperator
|
|
|
|
CloneObjectStoreInfo(const nsAString& aKey,
|
|
|
|
ObjectStoreInfo* aData,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
|
|
|
ObjectStoreInfoHash* hash = static_cast<ObjectStoreInfoHash*>(aUserArg);
|
|
|
|
|
2011-12-17 00:40:47 +00:00
|
|
|
nsRefPtr<ObjectStoreInfo> newInfo(new ObjectStoreInfo(*aData));
|
2011-11-08 00:15:45 +00:00
|
|
|
|
2012-05-18 17:30:49 +00:00
|
|
|
hash->Put(aKey, newInfo);
|
2011-11-08 00:15:45 +00:00
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
2010-10-19 17:58:42 +00:00
|
|
|
DatabaseInfo::~DatabaseInfo()
|
|
|
|
{
|
2011-11-08 00:15:45 +00:00
|
|
|
// Clones are never in the hash.
|
|
|
|
if (!cloned) {
|
|
|
|
DatabaseInfo::Remove(id);
|
|
|
|
}
|
2010-10-19 17:58:42 +00:00
|
|
|
}
|
|
|
|
|
2011-12-17 00:40:47 +00:00
|
|
|
ObjectStoreInfo::ObjectStoreInfo(ObjectStoreInfo& aOther)
|
2012-06-01 17:21:12 +00:00
|
|
|
: nextAutoIncrementId(aOther.nextAutoIncrementId),
|
2011-12-17 00:40:47 +00:00
|
|
|
comittedAutoIncrementId(aOther.comittedAutoIncrementId)
|
|
|
|
{
|
2012-06-01 17:21:12 +00:00
|
|
|
*static_cast<ObjectStoreInfoGuts*>(this) =
|
|
|
|
static_cast<ObjectStoreInfoGuts&>(aOther);
|
|
|
|
|
2011-12-17 00:40:47 +00:00
|
|
|
// Doesn't copy the refcount
|
|
|
|
MOZ_COUNT_CTOR(ObjectStoreInfo);
|
|
|
|
}
|
|
|
|
|
2011-11-08 00:06:13 +00:00
|
|
|
#ifdef NS_BUILD_REFCNT_LOGGING
|
|
|
|
|
2010-10-19 17:58:42 +00:00
|
|
|
IndexInfo::IndexInfo()
|
|
|
|
: id(LL_MININT),
|
2012-06-21 22:27:13 +00:00
|
|
|
keyPath(0),
|
2010-10-19 17:58:42 +00:00
|
|
|
unique(false),
|
2011-12-04 17:39:01 +00:00
|
|
|
multiEntry(false)
|
2010-10-19 17:58:42 +00:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(IndexInfo);
|
|
|
|
}
|
|
|
|
|
2011-11-08 00:15:45 +00:00
|
|
|
IndexInfo::IndexInfo(const IndexInfo& aOther)
|
2012-06-01 17:21:12 +00:00
|
|
|
: name(aOther.name),
|
|
|
|
id(aOther.id),
|
2011-11-08 00:15:45 +00:00
|
|
|
keyPath(aOther.keyPath),
|
|
|
|
unique(aOther.unique),
|
2011-12-04 17:39:01 +00:00
|
|
|
multiEntry(aOther.multiEntry)
|
2011-11-08 00:15:45 +00:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(IndexInfo);
|
|
|
|
}
|
|
|
|
|
2010-10-19 17:58:42 +00:00
|
|
|
IndexInfo::~IndexInfo()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(IndexInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectStoreInfo::ObjectStoreInfo()
|
2012-06-01 17:21:12 +00:00
|
|
|
: nextAutoIncrementId(0),
|
2011-12-17 00:40:47 +00:00
|
|
|
comittedAutoIncrementId(0)
|
2011-11-08 00:15:45 +00:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(ObjectStoreInfo);
|
|
|
|
}
|
|
|
|
|
2010-10-19 17:58:42 +00:00
|
|
|
ObjectStoreInfo::~ObjectStoreInfo()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(ObjectStoreInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
IndexUpdateInfo::IndexUpdateInfo()
|
2012-06-20 01:50:39 +00: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 17:58:42 +00:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(IndexUpdateInfo);
|
|
|
|
}
|
2010-10-19 17:58:52 +00:00
|
|
|
|
2010-10-19 17:58:42 +00:00
|
|
|
IndexUpdateInfo::~IndexUpdateInfo()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(IndexUpdateInfo);
|
|
|
|
}
|
2012-06-20 01:50:39 +00:00
|
|
|
|
2010-10-19 17:58:42 +00:00
|
|
|
#endif /* NS_BUILD_REFCNT_LOGGING */
|
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
// static
|
|
|
|
bool
|
2011-11-02 12:53:12 +00:00
|
|
|
DatabaseInfo::Get(nsIAtom* aId,
|
2010-06-23 19:46:08 +00:00
|
|
|
DatabaseInfo** aInfo)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aId, "Bad id!");
|
|
|
|
|
2011-11-07 23:20:20 +00:00
|
|
|
if (gDatabaseHash &&
|
|
|
|
gDatabaseHash->Get(aId, aInfo)) {
|
2011-11-08 00:15:45 +00:00
|
|
|
NS_IF_ADDREF(*aInfo);
|
2011-11-07 23:20:20 +00:00
|
|
|
return true;
|
2010-06-23 19:46:08 +00: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());
|
2012-05-18 17:30:49 +00:00
|
|
|
databaseHash->Init();
|
2010-06-23 19:46:08 +00:00
|
|
|
gDatabaseHash = databaseHash.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gDatabaseHash->Get(aInfo->id, nsnull)) {
|
|
|
|
NS_ERROR("Already know about this database!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-05-18 17:30:49 +00:00
|
|
|
gDatabaseHash->Put(aInfo->id, aInfo);
|
2010-06-23 19:46:08 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void
|
2011-11-02 12:53:12 +00:00
|
|
|
DatabaseInfo::Remove(nsIAtom* aId)
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2011-11-07 23:20:20 +00:00
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
if (gDatabaseHash) {
|
|
|
|
gDatabaseHash->Remove(aId);
|
|
|
|
|
|
|
|
if (!gDatabaseHash->Count()) {
|
|
|
|
delete gDatabaseHash;
|
|
|
|
gDatabaseHash = nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-17 00:40:47 +00:00
|
|
|
PLDHashOperator
|
|
|
|
EnumerateDatabasesRemoveOrigin(nsISupports* aId,
|
|
|
|
DatabaseInfo*& aDatabaseInfo,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
const nsACString* origin = static_cast<const nsACString*>(aUserArg);
|
|
|
|
return aDatabaseInfo->origin.Equals(*origin) ?
|
|
|
|
PL_DHASH_REMOVE :
|
|
|
|
PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void
|
|
|
|
DatabaseInfo::RemoveAllForOrigin(const nsACString& aOrigin)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
if (gDatabaseHash) {
|
|
|
|
gDatabaseHash->Enumerate(EnumerateDatabasesRemoveOrigin,
|
|
|
|
const_cast<nsACString*>(&aOrigin));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
bool
|
|
|
|
DatabaseInfo::GetObjectStoreNames(nsTArray<nsString>& aNames)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
aNames.Clear();
|
2011-11-08 00:15:45 +00:00
|
|
|
if (objectStoreHash) {
|
|
|
|
objectStoreHash->EnumerateRead(EnumerateObjectStoreNames, &aNames);
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DatabaseInfo::ContainsStoreName(const nsAString& aName)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2011-11-08 00:15:45 +00:00
|
|
|
return objectStoreHash && objectStoreHash->Get(aName, nsnull);
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
2011-12-17 00:40:47 +00:00
|
|
|
ObjectStoreInfo*
|
|
|
|
DatabaseInfo::GetObjectStore(const nsAString& aName)
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2011-11-08 00:15:43 +00:00
|
|
|
if (objectStoreHash) {
|
2011-12-17 00:40:47 +00:00
|
|
|
return objectStoreHash->GetWeak(aName);
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
2011-12-17 00:40:47 +00:00
|
|
|
return nsnull;
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2011-11-08 00:15:43 +00:00
|
|
|
DatabaseInfo::PutObjectStore(ObjectStoreInfo* aInfo)
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aInfo, "Null pointer!");
|
|
|
|
|
2011-11-08 00:15:43 +00:00
|
|
|
if (!objectStoreHash) {
|
|
|
|
nsAutoPtr<ObjectStoreInfoHash> hash(new ObjectStoreInfoHash());
|
2012-05-18 17:30:49 +00:00
|
|
|
hash->Init();
|
2011-11-08 00:15:43 +00:00
|
|
|
objectStoreHash = hash.forget();
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
2011-11-08 00:15:43 +00:00
|
|
|
if (objectStoreHash->Get(aInfo->name, nsnull)) {
|
2010-06-23 19:46:08 +00:00
|
|
|
NS_ERROR("Already have an entry for this objectstore!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-05-18 17:30:49 +00:00
|
|
|
objectStoreHash->Put(aInfo->name, aInfo);
|
|
|
|
return true;
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-11-08 00:15:43 +00:00
|
|
|
DatabaseInfo::RemoveObjectStore(const nsAString& aName)
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2011-12-17 00:40:47 +00:00
|
|
|
NS_ASSERTION(GetObjectStore(aName), "Don't know about this one!");
|
2010-06-23 19:46:08 +00:00
|
|
|
|
2011-11-08 00:15:43 +00:00
|
|
|
if (objectStoreHash) {
|
|
|
|
objectStoreHash->Remove(aName);
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-08 00:15:45 +00:00
|
|
|
|
|
|
|
already_AddRefed<DatabaseInfo>
|
|
|
|
DatabaseInfo::Clone()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!cloned, "Should never clone a clone!");
|
|
|
|
|
|
|
|
nsRefPtr<DatabaseInfo> dbInfo(new DatabaseInfo());
|
|
|
|
|
|
|
|
dbInfo->cloned = true;
|
|
|
|
dbInfo->name = name;
|
2012-06-01 17:21:12 +00:00
|
|
|
dbInfo->origin = origin;
|
2011-11-08 00:15:45 +00:00
|
|
|
dbInfo->version = version;
|
|
|
|
dbInfo->id = id;
|
|
|
|
dbInfo->filePath = filePath;
|
|
|
|
dbInfo->nextObjectStoreId = nextObjectStoreId;
|
|
|
|
dbInfo->nextIndexId = nextIndexId;
|
|
|
|
|
|
|
|
if (objectStoreHash) {
|
|
|
|
dbInfo->objectStoreHash = new ObjectStoreInfoHash();
|
2012-05-18 17:30:49 +00:00
|
|
|
dbInfo->objectStoreHash->Init();
|
2011-11-08 00:15:45 +00:00
|
|
|
objectStoreHash->EnumerateRead(CloneObjectStoreInfo,
|
|
|
|
dbInfo->objectStoreHash);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dbInfo.forget();
|
|
|
|
}
|