2010-10-19 17:46:49 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
|
2012-05-21 15:12:37 +04: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-10-19 17:46:49 +04:00
|
|
|
|
2012-12-15 03:58:45 +04:00
|
|
|
#include "mozilla/DebugOnly.h"
|
|
|
|
|
2010-10-19 17:46:49 +04:00
|
|
|
#include "VacuumManager.h"
|
|
|
|
|
|
|
|
#include "mozilla/Services.h"
|
2011-06-02 05:05:12 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2010-10-19 17:46:49 +04:00
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2013-09-23 21:29:27 +04:00
|
|
|
#include "prtime.h"
|
Bug 1650201 - Fix mozStorage prefs read before profile and fallback to a non-exclusive VFS when it can't get an exclusive lock. r=asuth,geckoview-reviewers,agi
mozStorage used to read prefs on service init, because they could only be read
on the main-thread. When service init was moved earlier, it started trying
to read prefs too early, before the profile was set up, thus it ended up always
reading the default value.
This patch moves the only relevant pref to mirrored StaticPrefs that can be accessed
from different threads, and removes two preferences that apparently are not necessary
(they have been broken from a long time) for now.
In particular, providing a global synchronous setting is a footgun, each consumer should
decide about their synchronous needs, rather than abusing a dangerous "go fast" setting.
The page size is something we don't change from quite some time, and it's unlikely to be
used to run experiments in the wild before doing local measurements first, for which Try
builds are enough.
The remaining exclusiveLock pref is a bit controversial, because in general exclusive lock
is better for various reasons, and mostly it is necessary to use WAL on network shares.
Though developers may find it useful for debugging, and some third parties are doing
dangerous things (like copying over databases) to work around it, for which it's safer to
provide a less dangerous alternative.
Note exclusive lock only works on Unix-derived systems for now (no Windows implementation).
Finally, this introduces a fallback to exclusive lock, so that if a third party is using our
databases, so that we can't get an exclusive lock, we'll fallback to normal locking.
Differential Revision: https://phabricator.services.mozilla.com/D82717
2020-07-11 00:45:53 +03:00
|
|
|
#include "mozilla/StaticPrefs_storage.h"
|
2010-10-19 17:46:49 +04:00
|
|
|
|
|
|
|
#include "mozStorageConnection.h"
|
|
|
|
#include "mozIStorageStatement.h"
|
|
|
|
#include "mozIStorageAsyncStatement.h"
|
|
|
|
#include "mozIStoragePendingStatement.h"
|
|
|
|
#include "mozIStorageError.h"
|
2012-01-06 03:46:35 +04:00
|
|
|
#include "mozStorageHelper.h"
|
2013-08-29 20:10:35 +04:00
|
|
|
#include "nsXULAppAPI.h"
|
2010-10-19 17:46:49 +04:00
|
|
|
|
|
|
|
#define OBSERVER_TOPIC_IDLE_DAILY "idle-daily"
|
|
|
|
#define OBSERVER_TOPIC_XPCOM_SHUTDOWN "xpcom-shutdown"
|
|
|
|
|
|
|
|
// Used to notify begin and end of a heavy IO task.
|
|
|
|
#define OBSERVER_TOPIC_HEAVY_IO "heavy-io-task"
|
2017-03-14 05:26:27 +03:00
|
|
|
#define OBSERVER_DATA_VACUUM_BEGIN u"vacuum-begin"
|
|
|
|
#define OBSERVER_DATA_VACUUM_END u"vacuum-end"
|
2010-10-19 17:46:49 +04:00
|
|
|
|
|
|
|
// This preferences root will contain last vacuum timestamps (in seconds) for
|
|
|
|
// each database. The database filename is used as a key.
|
|
|
|
#define PREF_VACUUM_BRANCH "storage.vacuum.last."
|
|
|
|
|
|
|
|
// Time between subsequent vacuum calls for a certain database.
|
|
|
|
#define VACUUM_INTERVAL_SECONDS 30 * 86400 // 30 days.
|
|
|
|
|
2016-01-28 21:35:00 +03:00
|
|
|
extern mozilla::LazyLogModule gStorageLog;
|
2010-10-19 17:46:49 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace storage {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// BaseCallback
|
|
|
|
|
|
|
|
class BaseCallback : public mozIStorageStatementCallback {
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_MOZISTORAGESTATEMENTCALLBACK
|
|
|
|
BaseCallback() {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-10-19 17:46:49 +04:00
|
|
|
protected:
|
|
|
|
virtual ~BaseCallback() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BaseCallback::HandleError(mozIStorageError* aError) {
|
|
|
|
#ifdef DEBUG
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t result;
|
2010-10-19 17:46:49 +04:00
|
|
|
nsresult rv = aError->GetResult(&result);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString message;
|
2010-10-19 17:46:49 +04:00
|
|
|
rv = aError->GetMessage(message);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString warnMsg;
|
2010-10-19 17:46:49 +04:00
|
|
|
warnMsg.AppendLiteral("An error occured during async execution: ");
|
|
|
|
warnMsg.AppendInt(result);
|
2014-05-22 07:48:51 +04:00
|
|
|
warnMsg.Append(' ');
|
2010-10-19 17:46:49 +04:00
|
|
|
warnMsg.Append(message);
|
|
|
|
NS_WARNING(warnMsg.get());
|
|
|
|
#endif
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BaseCallback::HandleResult(mozIStorageResultSet* aResultSet) {
|
|
|
|
// We could get results from PRAGMA statements, but we don't mind them.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
BaseCallback::HandleCompletion(uint16_t aReason) {
|
2010-10-19 17:46:49 +04:00
|
|
|
// By default BaseCallback will just be silent on completion.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(BaseCallback, mozIStorageStatementCallback)
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// Vacuumer declaration.
|
|
|
|
|
|
|
|
class Vacuumer : public BaseCallback {
|
|
|
|
public:
|
|
|
|
NS_DECL_MOZISTORAGESTATEMENTCALLBACK
|
|
|
|
|
2014-08-08 03:47:28 +04:00
|
|
|
explicit Vacuumer(mozIStorageVacuumParticipant* aParticipant);
|
2010-10-19 17:46:49 +04:00
|
|
|
|
|
|
|
bool execute();
|
|
|
|
nsresult notifyCompletion(bool aSucceeded);
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<mozIStorageVacuumParticipant> mParticipant;
|
|
|
|
nsCString mDBFilename;
|
|
|
|
nsCOMPtr<mozIStorageConnection> mDBConn;
|
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// Vacuumer implementation.
|
|
|
|
|
2011-06-02 05:05:12 +04:00
|
|
|
Vacuumer::Vacuumer(mozIStorageVacuumParticipant* aParticipant)
|
2010-10-19 17:46:49 +04:00
|
|
|
: mParticipant(aParticipant) {}
|
|
|
|
|
|
|
|
bool Vacuumer::execute() {
|
2012-02-22 15:23:05 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Must be running on the main thread!");
|
2010-10-19 17:46:49 +04:00
|
|
|
|
|
|
|
// Get the connection and check its validity.
|
|
|
|
nsresult rv = mParticipant->GetDatabaseConnection(getter_AddRefs(mDBConn));
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
2012-02-22 15:23:05 +04:00
|
|
|
bool ready = false;
|
2010-10-19 17:46:49 +04:00
|
|
|
if (!mDBConn || NS_FAILED(mDBConn->GetConnectionReady(&ready)) || !ready) {
|
2012-02-22 15:23:05 +04:00
|
|
|
NS_WARNING("Unable to get a connection to vacuum database");
|
2010-10-19 17:46:49 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-02-22 15:23:05 +04:00
|
|
|
// Ask for the expected page size. Vacuum can change the page size, unless
|
|
|
|
// the database is using WAL journaling.
|
|
|
|
// TODO Bug 634374: figure out a strategy to fix page size with WAL.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t expectedPageSize = 0;
|
2010-10-19 17:46:49 +04:00
|
|
|
rv = mParticipant->GetExpectedDatabasePageSize(&expectedPageSize);
|
2013-04-05 04:14:46 +04:00
|
|
|
if (NS_FAILED(rv) || !Service::pageSizeIsValid(expectedPageSize)) {
|
2010-10-19 17:46:49 +04:00
|
|
|
NS_WARNING("Invalid page size requested for database, will use default ");
|
|
|
|
NS_WARNING(mDBFilename.get());
|
Bug 1650201 - Fix mozStorage prefs read before profile and fallback to a non-exclusive VFS when it can't get an exclusive lock. r=asuth,geckoview-reviewers,agi
mozStorage used to read prefs on service init, because they could only be read
on the main-thread. When service init was moved earlier, it started trying
to read prefs too early, before the profile was set up, thus it ended up always
reading the default value.
This patch moves the only relevant pref to mirrored StaticPrefs that can be accessed
from different threads, and removes two preferences that apparently are not necessary
(they have been broken from a long time) for now.
In particular, providing a global synchronous setting is a footgun, each consumer should
decide about their synchronous needs, rather than abusing a dangerous "go fast" setting.
The page size is something we don't change from quite some time, and it's unlikely to be
used to run experiments in the wild before doing local measurements first, for which Try
builds are enough.
The remaining exclusiveLock pref is a bit controversial, because in general exclusive lock
is better for various reasons, and mostly it is necessary to use WAL on network shares.
Though developers may find it useful for debugging, and some third parties are doing
dangerous things (like copying over databases) to work around it, for which it's safer to
provide a less dangerous alternative.
Note exclusive lock only works on Unix-derived systems for now (no Windows implementation).
Finally, this introduces a fallback to exclusive lock, so that if a third party is using our
databases, so that we can't get an exclusive lock, we'll fallback to normal locking.
Differential Revision: https://phabricator.services.mozilla.com/D82717
2020-07-11 00:45:53 +03:00
|
|
|
expectedPageSize = Service::kDefaultPageSize;
|
2010-10-19 17:46:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the database filename. Last vacuum time is stored under this name
|
|
|
|
// in PREF_VACUUM_BRANCH.
|
|
|
|
nsCOMPtr<nsIFile> databaseFile;
|
|
|
|
mDBConn->GetDatabaseFile(getter_AddRefs(databaseFile));
|
|
|
|
if (!databaseFile) {
|
|
|
|
NS_WARNING("Trying to vacuum a in-memory database!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
nsAutoString databaseFilename;
|
|
|
|
rv = databaseFile->GetLeafName(databaseFilename);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
2020-09-02 12:54:37 +03:00
|
|
|
CopyUTF16toUTF8(databaseFilename, mDBFilename);
|
2012-02-22 15:23:05 +04:00
|
|
|
MOZ_ASSERT(!mDBFilename.IsEmpty(), "Database filename cannot be empty");
|
2010-10-19 17:46:49 +04:00
|
|
|
|
|
|
|
// Check interval from last vacuum.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t now = static_cast<int32_t>(PR_Now() / PR_USEC_PER_SEC);
|
|
|
|
int32_t lastVacuum;
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString prefName(PREF_VACUUM_BRANCH);
|
2011-06-02 05:05:12 +04:00
|
|
|
prefName += mDBFilename;
|
|
|
|
rv = Preferences::GetInt(prefName.get(), &lastVacuum);
|
2012-02-22 15:23:05 +04:00
|
|
|
if (NS_SUCCEEDED(rv) && (now - lastVacuum) < VACUUM_INTERVAL_SECONDS) {
|
|
|
|
// This database was vacuumed recently, skip it.
|
2010-10-19 17:46:49 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify that we are about to start vacuuming. The participant can opt-out
|
|
|
|
// if it cannot handle a vacuum at this time, and then we'll move to the next
|
|
|
|
// one.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool vacuumGranted = false;
|
2010-10-19 17:46:49 +04:00
|
|
|
rv = mParticipant->OnBeginVacuum(&vacuumGranted);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
if (!vacuumGranted) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify a heavy IO task is about to start.
|
|
|
|
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
|
|
|
|
if (os) {
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = os->NotifyObservers(nullptr, OBSERVER_TOPIC_HEAVY_IO,
|
2017-03-14 05:26:27 +03:00
|
|
|
OBSERVER_DATA_VACUUM_BEGIN);
|
2012-02-22 15:23:05 +04:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv), "Should be able to notify");
|
2010-10-19 17:46:49 +04:00
|
|
|
}
|
|
|
|
|
2012-02-22 15:23:05 +04:00
|
|
|
// Execute the statements separately, since the pragma may conflict with the
|
|
|
|
// vacuum, if they are executed in the same transaction.
|
|
|
|
nsCOMPtr<mozIStorageAsyncStatement> pageSizeStmt;
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString pageSizeQuery(MOZ_STORAGE_UNIQUIFY_QUERY_STR
|
2012-04-09 17:56:41 +04:00
|
|
|
"PRAGMA page_size = ");
|
|
|
|
pageSizeQuery.AppendInt(expectedPageSize);
|
|
|
|
rv = mDBConn->CreateAsyncStatement(pageSizeQuery,
|
|
|
|
getter_AddRefs(pageSizeStmt));
|
2012-02-22 15:23:05 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<BaseCallback> callback = new BaseCallback();
|
2012-02-22 15:23:05 +04:00
|
|
|
nsCOMPtr<mozIStoragePendingStatement> ps;
|
|
|
|
rv = pageSizeStmt->ExecuteAsync(callback, getter_AddRefs(ps));
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
2010-10-19 17:46:49 +04:00
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageAsyncStatement> stmt;
|
2020-07-01 11:29:29 +03:00
|
|
|
rv = mDBConn->CreateAsyncStatement("VACUUM"_ns, getter_AddRefs(stmt));
|
2010-10-19 17:46:49 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
rv = stmt->ExecuteAsync(this, getter_AddRefs(ps));
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// mozIStorageStatementCallback
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Vacuumer::HandleError(mozIStorageError* aError) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t result;
|
2016-06-08 08:08:41 +03:00
|
|
|
nsresult rv;
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString message;
|
2016-06-08 08:08:41 +03:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
rv = aError->GetResult(&result);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-10-19 17:46:49 +04:00
|
|
|
rv = aError->GetMessage(message);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString warnMsg;
|
2010-10-19 17:46:49 +04:00
|
|
|
warnMsg.AppendLiteral("Unable to vacuum database: ");
|
|
|
|
warnMsg.Append(mDBFilename);
|
|
|
|
warnMsg.AppendLiteral(" - ");
|
|
|
|
warnMsg.AppendInt(result);
|
2014-05-22 07:48:51 +04:00
|
|
|
warnMsg.Append(' ');
|
2010-10-19 17:46:49 +04:00
|
|
|
warnMsg.Append(message);
|
|
|
|
NS_WARNING(warnMsg.get());
|
|
|
|
#endif
|
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
if (MOZ_LOG_TEST(gStorageLog, LogLevel::Error)) {
|
2016-06-08 08:08:41 +03:00
|
|
|
rv = aError->GetResult(&result);
|
2010-10-19 17:46:49 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = aError->GetMessage(message);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gStorageLog, LogLevel::Error,
|
2010-10-19 17:46:49 +04:00
|
|
|
("Vacuum failed with error: %d '%s'. Database was: '%s'", result,
|
|
|
|
message.get(), mDBFilename.get()));
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Vacuumer::HandleResult(mozIStorageResultSet* aResultSet) {
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Got a resultset from a vacuum?");
|
2010-10-19 17:46:49 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
Vacuumer::HandleCompletion(uint16_t aReason) {
|
2010-10-19 17:46:49 +04:00
|
|
|
if (aReason == REASON_FINISHED) {
|
|
|
|
// Update last vacuum time.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t now = static_cast<int32_t>(PR_Now() / PR_USEC_PER_SEC);
|
2012-02-22 15:23:05 +04:00
|
|
|
MOZ_ASSERT(!mDBFilename.IsEmpty(), "Database filename cannot be empty");
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString prefName(PREF_VACUUM_BRANCH);
|
2011-06-02 05:05:12 +04:00
|
|
|
prefName += mDBFilename;
|
2012-02-22 15:23:05 +04:00
|
|
|
DebugOnly<nsresult> rv = Preferences::SetInt(prefName.get(), now);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv), "Should be able to set a preference");
|
2010-10-19 17:46:49 +04:00
|
|
|
}
|
|
|
|
|
2011-02-16 15:51:26 +03:00
|
|
|
notifyCompletion(aReason == REASON_FINISHED);
|
2010-10-19 17:46:49 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult Vacuumer::notifyCompletion(bool aSucceeded) {
|
|
|
|
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
|
|
|
|
if (os) {
|
2012-07-30 18:20:58 +04:00
|
|
|
os->NotifyObservers(nullptr, OBSERVER_TOPIC_HEAVY_IO,
|
2017-03-14 05:26:27 +03:00
|
|
|
OBSERVER_DATA_VACUUM_END);
|
2010-10-19 17:46:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = mParticipant->OnEndVacuum(aSucceeded);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2010-10-19 17:46:49 +04:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// VacuumManager
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(VacuumManager, nsIObserver)
|
2010-10-19 17:46:49 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
VacuumManager* VacuumManager::gVacuumManager = nullptr;
|
2010-10-19 17:46:49 +04:00
|
|
|
|
2017-10-17 07:08:42 +03:00
|
|
|
already_AddRefed<VacuumManager> VacuumManager::getSingleton() {
|
2013-08-29 20:10:35 +04:00
|
|
|
// Don't allocate it in the child Process.
|
2015-07-04 04:29:00 +03:00
|
|
|
if (!XRE_IsParentProcess()) {
|
2013-08-29 20:10:35 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-10-17 07:08:42 +03:00
|
|
|
if (!gVacuumManager) {
|
2017-10-30 02:02:40 +03:00
|
|
|
auto manager = MakeRefPtr<VacuumManager>();
|
|
|
|
MOZ_ASSERT(gVacuumManager == manager.get());
|
|
|
|
return manager.forget();
|
2010-10-19 17:46:49 +04:00
|
|
|
}
|
2017-10-17 07:08:42 +03:00
|
|
|
return do_AddRef(gVacuumManager);
|
2010-10-19 17:46:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
VacuumManager::VacuumManager() : mParticipants("vacuum-participant") {
|
2012-02-22 15:23:05 +04:00
|
|
|
MOZ_ASSERT(!gVacuumManager,
|
|
|
|
"Attempting to create two instances of the service!");
|
2010-10-19 17:46:49 +04:00
|
|
|
gVacuumManager = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
VacuumManager::~VacuumManager() {
|
|
|
|
// Remove the static reference to the service. Check to make sure its us
|
|
|
|
// in case somebody creates an extra instance of the service.
|
2012-02-22 15:23:05 +04:00
|
|
|
MOZ_ASSERT(gVacuumManager == this,
|
|
|
|
"Deleting a non-singleton instance of the service");
|
2010-10-19 17:46:49 +04:00
|
|
|
if (gVacuumManager == this) {
|
2012-07-30 18:20:58 +04:00
|
|
|
gVacuumManager = nullptr;
|
2010-10-19 17:46:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// nsIObserver
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
VacuumManager::Observe(nsISupports* aSubject, const char* aTopic,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* aData) {
|
2010-10-19 17:46:49 +04:00
|
|
|
if (strcmp(aTopic, OBSERVER_TOPIC_IDLE_DAILY) == 0) {
|
|
|
|
// Try to run vacuum on all registered entries. Will stop at the first
|
|
|
|
// successful one.
|
2013-10-10 16:48:03 +04:00
|
|
|
nsCOMArray<mozIStorageVacuumParticipant> entries;
|
|
|
|
mParticipants.GetEntries(entries);
|
2010-10-19 17:46:49 +04:00
|
|
|
// If there are more entries than what a month can contain, we could end up
|
|
|
|
// skipping some, since we run daily. So we use a starting index.
|
2011-06-02 05:05:12 +04:00
|
|
|
static const char* kPrefName = PREF_VACUUM_BRANCH "index";
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t startIndex = Preferences::GetInt(kPrefName, 0);
|
2010-10-19 17:46:49 +04:00
|
|
|
if (startIndex >= entries.Count()) {
|
|
|
|
startIndex = 0;
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t index;
|
2010-10-19 17:46:49 +04:00
|
|
|
for (index = startIndex; index < entries.Count(); ++index) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Vacuumer> vacuum = new Vacuumer(entries[index]);
|
2010-10-19 17:46:49 +04:00
|
|
|
// Only vacuum one database per day.
|
|
|
|
if (vacuum->execute()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-02-22 15:23:05 +04:00
|
|
|
DebugOnly<nsresult> rv = Preferences::SetInt(kPrefName, index);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv), "Should be able to set a preference");
|
2010-10-19 17:46:49 +04:00
|
|
|
}
|
2010-10-19 17:46:51 +04:00
|
|
|
|
2010-10-19 17:46:49 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace storage
|
|
|
|
} // namespace mozilla
|