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: */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Indexed Database.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* The Mozilla Foundation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2010
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Ben Turner <bent.mozilla@gmail.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#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;
|
|
|
|
|
|
|
|
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-07 16:15:45 -08:00
|
|
|
PLDHashOperator
|
|
|
|
CloneObjectStoreInfo(const nsAString& aKey,
|
|
|
|
ObjectStoreInfo* aData,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
|
|
|
ObjectStoreInfoHash* hash = static_cast<ObjectStoreInfoHash*>(aUserArg);
|
|
|
|
|
2011-12-16 13:09:42 -08:00
|
|
|
nsAutoPtr<ObjectStoreInfo> newInfo(new ObjectStoreInfo(*aData));
|
2011-11-07 16:15:45 -08:00
|
|
|
|
|
|
|
if (!hash->Put(aKey, newInfo)) {
|
|
|
|
NS_WARNING("Out of memory?");
|
|
|
|
return PL_DHASH_STOP;
|
|
|
|
}
|
|
|
|
|
2011-12-16 13:09:42 -08:00
|
|
|
newInfo.forget();
|
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-11-07 16:06:13 -08:00
|
|
|
#ifdef NS_BUILD_REFCNT_LOGGING
|
|
|
|
|
2010-10-19 10:58:42 -07:00
|
|
|
IndexInfo::IndexInfo()
|
|
|
|
: id(LL_MININT),
|
|
|
|
unique(false),
|
2011-12-16 13:09:42 -08:00
|
|
|
autoIncrement(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)
|
|
|
|
: id(aOther.id),
|
|
|
|
name(aOther.name),
|
|
|
|
keyPath(aOther.keyPath),
|
|
|
|
unique(aOther.unique),
|
2011-12-16 13:09:42 -08:00
|
|
|
autoIncrement(aOther.autoIncrement),
|
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()
|
|
|
|
: id(0),
|
2011-12-16 13:09:42 -08:00
|
|
|
autoIncrement(false),
|
|
|
|
databaseId(0)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(ObjectStoreInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectStoreInfo::ObjectStoreInfo(ObjectStoreInfo& aOther)
|
|
|
|
: name(aOther.name),
|
|
|
|
id(aOther.id),
|
|
|
|
keyPath(aOther.keyPath),
|
|
|
|
autoIncrement(aOther.autoIncrement),
|
|
|
|
databaseId(aOther.databaseId),
|
|
|
|
indexes(aOther.indexes)
|
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()
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
#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());
|
|
|
|
if (!databaseHash->Init()) {
|
|
|
|
NS_ERROR("Failed to initialize hashtable!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
gDatabaseHash = databaseHash.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gDatabaseHash->Get(aInfo->id, nsnull)) {
|
|
|
|
NS_ERROR("Already know about this database!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-11-07 15:20:20 -08:00
|
|
|
if (!gDatabaseHash->Put(aInfo->id, aInfo)) {
|
2010-06-23 12:46:08 -07:00
|
|
|
NS_ERROR("Put failed!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2011-12-16 13:09:42 -08:00
|
|
|
DatabaseInfo* info = nsnull;
|
|
|
|
|
|
|
|
DebugOnly<bool> got = Get(aId, &info);
|
|
|
|
NS_ASSERTION(got && info, "Don't know anything about this one!");
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
if (gDatabaseHash) {
|
|
|
|
gDatabaseHash->Remove(aId);
|
|
|
|
|
|
|
|
if (!gDatabaseHash->Count()) {
|
|
|
|
delete gDatabaseHash;
|
|
|
|
gDatabaseHash = nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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!");
|
|
|
|
|
2011-11-07 16:15:45 -08:00
|
|
|
return objectStoreHash && objectStoreHash->Get(aName, nsnull);
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2011-12-16 13:09:42 -08:00
|
|
|
bool
|
|
|
|
DatabaseInfo::GetObjectStore(const nsAString& aName,
|
|
|
|
ObjectStoreInfo** aInfo)
|
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 13:09:42 -08:00
|
|
|
return objectStoreHash->Get(aName, aInfo);
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2011-12-16 13:09:42 -08:00
|
|
|
return false;
|
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());
|
|
|
|
if (!hash->Init()) {
|
2010-06-23 12:46:08 -07:00
|
|
|
NS_ERROR("Failed to initialize hashtable!");
|
|
|
|
return false;
|
|
|
|
}
|
2011-11-07 16:15:43 -08:00
|
|
|
objectStoreHash = hash.forget();
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2011-11-07 16:15:43 -08:00
|
|
|
if (objectStoreHash->Get(aInfo->name, nsnull)) {
|
2010-06-23 12:46:08 -07:00
|
|
|
NS_ERROR("Already have an entry for this objectstore!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-11-07 16:15:43 -08:00
|
|
|
return objectStoreHash->Put(aInfo->name, aInfo);
|
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 13:09:42 -08:00
|
|
|
NS_ASSERTION(GetObjectStore(aName, nsnull), "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()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!cloned, "Should never clone a clone!");
|
|
|
|
|
|
|
|
nsRefPtr<DatabaseInfo> dbInfo(new DatabaseInfo());
|
|
|
|
|
|
|
|
dbInfo->cloned = true;
|
|
|
|
dbInfo->name = name;
|
|
|
|
dbInfo->version = version;
|
|
|
|
dbInfo->id = id;
|
|
|
|
dbInfo->filePath = filePath;
|
|
|
|
dbInfo->nextObjectStoreId = nextObjectStoreId;
|
|
|
|
dbInfo->nextIndexId = nextIndexId;
|
|
|
|
|
|
|
|
if (objectStoreHash) {
|
|
|
|
dbInfo->objectStoreHash = new ObjectStoreInfoHash();
|
|
|
|
if (!dbInfo->objectStoreHash->Init()) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
objectStoreHash->EnumerateRead(CloneObjectStoreInfo,
|
|
|
|
dbInfo->objectStoreHash);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dbInfo.forget();
|
|
|
|
}
|