about:startup - some code style and api use changes, and more importantly wrapped the table creation in a transaction to save an fsync. feedback+=mak
authorDaniel Brooks <db48x@db48x.net>
Fri, 05 Nov 2010 21:46:17 -0500
changeset 58870 9d230d75d8573e8beaead1776de4adaba8353123
parent 58869 852178715a8c130afaa6eead2e881080f1e808bf
child 58871 79ab64089a080e04c8e8c0e916d6357e2e96f205
push id17440
push userdb48x@yahoo.com
push dateWed, 08 Dec 2010 04:15:54 +0000
treeherdermozilla-central@a89f24bf1798 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
milestone2.0b8pre
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
about:startup - some code style and api use changes, and more importantly wrapped the table creation in a transaction to save an fsync. feedback+=mak
toolkit/components/startup/src/nsAppStartup.cpp
--- a/toolkit/components/startup/src/nsAppStartup.cpp
+++ b/toolkit/components/startup/src/nsAppStartup.cpp
@@ -75,16 +75,17 @@
 #include "mozilla/FunctionTimer.h"
 #include "nsAppDirectoryServiceDefs.h"
 #include "nsIXULRuntime.h"
 #include "nsIXULAppInfo.h"
 #include "nsXPCOMCIDInternal.h"
 #include "nsIPropertyBag2.h"
 
 static NS_DEFINE_CID(kAppShellCID, NS_APPSHELL_CID);
+static const PRInt32 STARTUP_DATABASE_CURRENT_SCHEMA = 1;
 
 class nsAppExitEvent : public nsRunnable {
 private:
   nsRefPtr<nsAppStartup> mService;
 
 public:
   nsAppExitEvent(nsAppStartup *service) : mService(service) {}
 
@@ -555,57 +556,67 @@ nsresult nsAppStartup::RecordStartupDura
   appinfo->GetAppBuildID(appBuild);
   appinfo->GetPlatformVersion(platformVersion);
   appinfo->GetPlatformBuildID(platformBuild);
 
   nsCOMPtr<mozIStorageConnection> db;
   rv = OpenStartupDatabase(getter_AddRefs(db));
   NS_ENSURE_SUCCESS(rv, rv);
 
-  NS_NAMED_LITERAL_CSTRING(insert,
-                           "INSERT INTO duration                                       \
-                                        (timestamp, launch, startup, appVersion,       \
-                                         appBuild, platformVersion, platformBuild)     \
-                                   VALUES (:timestamp, :launch, :startup, :appVersion, \
-                                           :appBuild, :platformVersion, :platformBuild)");
   nsCOMPtr<mozIStorageAsyncStatement> statement;
-  rv = db->CreateAsyncStatement(insert, getter_AddRefs(statement));
+  rv = db->CreateAsyncStatement(NS_LITERAL_CSTRING(
+    "INSERT INTO duration (timestamp, launch, startup, appVersion, "
+                          "appBuild, platformVersion, platformBuild)"
+    "VALUES (:timestamp, :launch, :startup, :appVersion, :appBuild, "
+             ":platformVersion, :platformBuild)"),
+                                getter_AddRefs(statement));
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsCOMPtr<mozIStorageBindingParamsArray> parametersArray;
-  statement->NewBindingParamsArray(getter_AddRefs(parametersArray));
+  rv = statement->NewBindingParamsArray(getter_AddRefs(parametersArray));
+  NS_ENSURE_SUCCESS(rv, rv);
   nsCOMPtr<mozIStorageBindingParams> parameters;
-  parametersArray->NewBindingParams(getter_AddRefs(parameters));
+  rv = parametersArray->NewBindingParams(getter_AddRefs(parameters));
+  NS_ENSURE_SUCCESS(rv, rv);
 
-  parameters->BindInt64ByName(NS_LITERAL_CSTRING("timestamp"),
-                              launched);
-  parameters->BindInt64ByName(NS_LITERAL_CSTRING("launch"),
-                              started - launched);
-  parameters->BindInt64ByName(NS_LITERAL_CSTRING("startup"),
-                              mRestoredTimestamp - started);
-  parameters->BindUTF8StringByName(NS_LITERAL_CSTRING("appVersion"),
-                                   appVersion);
-  parameters->BindUTF8StringByName(NS_LITERAL_CSTRING("appBuild"),
-                                   appBuild);
-  parameters->BindUTF8StringByName(NS_LITERAL_CSTRING("platformVersion"),
-                                   platformVersion);
-  parameters->BindUTF8StringByName(NS_LITERAL_CSTRING("platformBuild"),
-                                   platformBuild);
+  rv = parameters->BindInt64ByName(NS_LITERAL_CSTRING("timestamp"),
+                                   launched);
+  NS_ENSURE_SUCCESS(rv, rv);
+  rv = parameters->BindInt64ByName(NS_LITERAL_CSTRING("launch"),
+                                   started - launched);
+  NS_ENSURE_SUCCESS(rv, rv);
+  rv = parameters->BindInt64ByName(NS_LITERAL_CSTRING("startup"),
+                                   mRestoredTimestamp - started);
+  NS_ENSURE_SUCCESS(rv, rv);
+  rv = parameters->BindUTF8StringByName(NS_LITERAL_CSTRING("appVersion"),
+                                        appVersion);
+  NS_ENSURE_SUCCESS(rv, rv);
+  rv = parameters->BindUTF8StringByName(NS_LITERAL_CSTRING("appBuild"),
+                                        appBuild);
+  NS_ENSURE_SUCCESS(rv, rv);
+  rv = parameters->BindUTF8StringByName(NS_LITERAL_CSTRING("platformVersion"),
+                                        platformVersion);
+  NS_ENSURE_SUCCESS(rv, rv);
+  rv = parameters->BindUTF8StringByName(NS_LITERAL_CSTRING("platformBuild"),
+                                        platformBuild);
+  NS_ENSURE_SUCCESS(rv, rv);
 
-  parametersArray->AddParams(parameters);
-  statement->BindParameters(parametersArray);
+
+  rv = parametersArray->AddParams(parameters);
+  NS_ENSURE_SUCCESS(rv, rv);
+  rv = statement->BindParameters(parametersArray);
+  NS_ENSURE_SUCCESS(rv, rv);
 
   nsCOMPtr<mozIStoragePendingStatement> pending;
   rv = statement->ExecuteAsync(nsnull, getter_AddRefs(pending));
   NS_ENSURE_SUCCESS(rv, rv);
 
-  rv = statement->Finalize();
+  rv = db->AsyncClose(nsnull);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  db->Close();
   return NS_OK;
 }
 
 nsresult nsAppStartup::RecordAddonEvent(const PRUnichar *event, nsISupports *details)
 {
   PRTime now = PR_Now();
   nsresult rv;
 
@@ -615,47 +626,57 @@ nsresult nsAppStartup::RecordAddonEvent(
   bag->GetPropertyAsAString(NS_LITERAL_STRING("id"), id);
   bag->GetPropertyAsAString(NS_LITERAL_STRING("name"), name);
   bag->GetPropertyAsAString(NS_LITERAL_STRING("version"), version);
 
   nsCOMPtr<mozIStorageConnection> db;
   rv = OpenStartupDatabase(getter_AddRefs(db));
   NS_ENSURE_SUCCESS(rv, rv);
 
-  NS_NAMED_LITERAL_CSTRING(insert,
-                           "INSERT INTO events (timestamp, extid, name, version, action) \
-                                   VALUES (:timestamp, :extid, :name, :version, :action)");
   nsCOMPtr<mozIStorageAsyncStatement> statement;
-  rv = db->CreateAsyncStatement(insert, getter_AddRefs(statement));
+  rv = db->CreateAsyncStatement(NS_LITERAL_CSTRING(
+    "INSERT INTO events (timestamp, extid, name, version, action) "
+    "VALUES (:timestamp, :extid, :name, :version, :action)"),
+                                getter_AddRefs(statement));
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsCOMPtr<mozIStorageBindingParamsArray> parametersArray;
-  statement->NewBindingParamsArray(getter_AddRefs(parametersArray));
+  rv = statement->NewBindingParamsArray(getter_AddRefs(parametersArray));
+  NS_ENSURE_SUCCESS(rv, rv);
   nsCOMPtr<mozIStorageBindingParams> parameters;
-  parametersArray->NewBindingParams(getter_AddRefs(parameters));
+  rv = parametersArray->NewBindingParams(getter_AddRefs(parameters));
+  NS_ENSURE_SUCCESS(rv, rv);
 
-  parameters->BindInt64ByName(NS_LITERAL_CSTRING("timestamp"),
-                              now);
-  parameters->BindUTF8StringByName(NS_LITERAL_CSTRING("extid"),
-                                   NS_ConvertUTF16toUTF8(id));
-  parameters->BindUTF8StringByName(NS_LITERAL_CSTRING("name"),
-                                   NS_ConvertUTF16toUTF8(name));
-  parameters->BindUTF8StringByName(NS_LITERAL_CSTRING("version"),
-                                   NS_ConvertUTF16toUTF8(version));
-  parameters->BindUTF8StringByName(NS_LITERAL_CSTRING("action"),
-                                   NS_ConvertUTF16toUTF8(nsDependentString(event)));
+  rv = parameters->BindInt64ByName(NS_LITERAL_CSTRING("timestamp"),
+                                   now);
+  NS_ENSURE_SUCCESS(rv, rv);
+  rv = parameters->BindUTF8StringByName(NS_LITERAL_CSTRING("extid"),
+                                        NS_ConvertUTF16toUTF8(id));
+  NS_ENSURE_SUCCESS(rv, rv);
+  rv = parameters->BindUTF8StringByName(NS_LITERAL_CSTRING("name"),
+                                        NS_ConvertUTF16toUTF8(name));
+  NS_ENSURE_SUCCESS(rv, rv);
+  rv = parameters->BindUTF8StringByName(NS_LITERAL_CSTRING("version"),
+                                        NS_ConvertUTF16toUTF8(version));
+  NS_ENSURE_SUCCESS(rv, rv);
+  rv = parameters->BindUTF8StringByName(NS_LITERAL_CSTRING("action"),
+                                        NS_ConvertUTF16toUTF8(nsDependentString(event)));
+  NS_ENSURE_SUCCESS(rv, rv);
 
-  parametersArray->AddParams(parameters);
-  statement->BindParameters(parametersArray);
+  rv = parametersArray->AddParams(parameters);
+  NS_ENSURE_SUCCESS(rv, rv);
+  rv = statement->BindParameters(parametersArray);
+  NS_ENSURE_SUCCESS(rv, rv);
 
   nsCOMPtr<mozIStoragePendingStatement> pending;
   rv = statement->ExecuteAsync(nsnull, getter_AddRefs(pending));
   NS_ENSURE_SUCCESS(rv, rv);
 
-  db->Close();
+  db->AsyncClose(nsnull);
+  NS_ENSURE_SUCCESS(rv, rv);
   return NS_OK;
 }
 
 nsresult OpenStartupDatabase(mozIStorageConnection **db)
 {
   nsresult rv;
   nsCOMPtr<nsIFile> file;
   rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(file));
@@ -663,42 +684,56 @@ nsresult OpenStartupDatabase(mozIStorage
   rv = file->Append(NS_LITERAL_STRING("startup.sqlite"));
   NS_ENSURE_SUCCESS(rv, rv);
   nsCOMPtr<mozIStorageService> svc = do_GetService(MOZ_STORAGE_SERVICE_CONTRACTID, &rv);
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = svc->OpenDatabase(file, db);
   if (NS_ERROR_FILE_CORRUPTED == rv)
   {
-    svc->BackupDatabaseFile(file, NS_LITERAL_STRING("startup.sqlite.backup"),
-                            nsnull, nsnull);
+    rv = file->Remove(PR_FALSE);
+    NS_ENSURE_SUCCESS(rv, rv);
     rv = svc->OpenDatabase(file, db);
   }
   NS_ENSURE_SUCCESS(rv, rv);
-  (*db)->SetSchemaVersion(1);
+
+  mozStorageTransaction transaction(*db, PR_FALSE);
 
-  rv = EnsureTable(*db,
-                   NS_LITERAL_CSTRING("duration"),
-                   NS_LITERAL_CSTRING("timestamp INTEGER,             \
-                                       launch INTEGER,                \
-                                       startup INTEGER,               \
-                                       appVersion TEXT,               \
-                                       appBuild TEXT,                 \
-                                       platformVersion TEXT,          \
-                                       platformBuild TEXT"));
-  NS_ENSURE_SUCCESS(rv, rv);
-  rv = EnsureTable(*db,
-                   NS_LITERAL_CSTRING("events"),
-                   NS_LITERAL_CSTRING("timestamp INTEGER,             \
-                                       extid TEXT,                    \
-                                       name TEXT,                     \
-                                       version TEXT,                  \
-                                       action TEXT"));
+  PRInt32 schema;
+  rv = (*db)->GetSchemaVersion(&schema);
   NS_ENSURE_SUCCESS(rv, rv);
 
+  if (0 == schema)
+  {
+    rv = EnsureTable(*db,
+                     NS_LITERAL_CSTRING("duration"),
+                     NS_LITERAL_CSTRING("timestamp INTEGER,             \
+                                         launch INTEGER,                \
+                                         startup INTEGER,               \
+                                         appVersion TEXT,               \
+                                         appBuild TEXT,                 \
+                                         platformVersion TEXT,          \
+                                         platformBuild TEXT"));
+    NS_ENSURE_SUCCESS(rv, rv);
+
+    rv = EnsureTable(*db,
+                     NS_LITERAL_CSTRING("events"),
+                     NS_LITERAL_CSTRING("timestamp INTEGER,             \
+                                         extid TEXT,                    \
+                                         name TEXT,                     \
+                                         version TEXT,                  \
+                                         action TEXT"));
+    NS_ENSURE_SUCCESS(rv, rv);
+  }
+
+  rv = (*db)->SetSchemaVersion(STARTUP_DATABASE_CURRENT_SCHEMA);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  rv = transaction.Commit();
+  NS_ENSURE_SUCCESS(rv, rv);
   return NS_OK;
 }
 
 nsresult EnsureTable(mozIStorageConnection *db, const nsACString &table,
                      const nsACString &schema)
 {
   nsresult rv;
   PRBool exists = false;