changeset 287:113312a23f53 snowl-release-0.2pre1

use julian dates for the lastRefreshed values, push date conversion up the stack (away from the core database access routines), and make the datastore code migrate from a version 4 to a version 5 schema (storing julian instead of JS dates)
author Myk Melez <myk@mozilla.org>
date Tue, 02 Sep 2008 03:41:40 -0700
parents 727bfe9d5fb7
children 2292290943ea
files modules/datastore.js modules/feed.js modules/service.js modules/source.js modules/twitter.js
diffstat 5 files changed, 44 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/modules/datastore.js	Tue Sep 02 03:38:53 2008 -0700
+++ b/modules/datastore.js	Tue Sep 02 03:41:40 2008 -0700
@@ -41,8 +41,6 @@
 const Cr = Components.results;
 const Cu = Components.utils;
 
-Cu.import("resource://snowl/modules/utils.js");
-
 const TABLE_TYPE_NORMAL = 0;
 const TABLE_TYPE_FULLTEXT = 1;
 
@@ -60,7 +58,7 @@
   //**************************************************************************//
   // Database Creation & Access
 
-  _dbVersion: 4,
+  _dbVersion: 5,
 
   _dbSchema: {
     // Note: datetime values like messages:timestamp are stored as Julian dates.
@@ -86,7 +84,7 @@
           // locations, not names (and thus never URNs)?
           "machineURI TEXT NOT NULL",
           "humanURI TEXT",
-          "lastRefreshed INTEGER",
+          "lastRefreshed REAL",
           "importance INTEGER"
         ]
       },
@@ -391,16 +389,20 @@
    * Thus migrating the database is as simple as constructing the schema as if
    * from scratch.
    */
-  _dbMigrate0To2: function(aDBConnection) {
+  _dbMigrate0To5: function(aDBConnection) {
     this._dbCreateTables(aDBConnection);
   },
 
-  _dbMigrate2To3: function(aDBConnection) {
-    aDBConnection.executeSimpleSQL("ALTER TABLE messages ADD COLUMN current BOOLEAN");
-  },
-
-  _dbMigrate3To4: function(aDBConnection) {
-    aDBConnection.executeSimpleSQL("ALTER TABLE messages ADD COLUMN read BOOLEAN");
+  _dbMigrate4To5: function(aDBConnection) {
+    aDBConnection.executeSimpleSQL(
+      "UPDATE sources SET lastRefreshed = lastRefreshed / 1000 / 86400 + 2440587.5"
+    );
+    aDBConnection.executeSimpleSQL(
+      "UPDATE messages SET timestamp = timestamp / 1000 / 86400 + 2440587.5"
+    );
+    aDBConnection.executeSimpleSQL(
+      "ALTER TABLE messages ADD COLUMN received REAL"
+    );
   },
 
   get _selectHasMessageStatement() {
@@ -463,9 +465,9 @@
    * @param aExternalID  {string}  the external ID of the message
    * @param aSubject     {string}  the title of the message
    * @param aAuthorID    {string}  the author of the message
-   * @param aTimestamp   {Date}    the date/time when the message was sent
-   * @param aReceived    {Date}    the date/time when the message was received
-   * @param aLink        {nsIURI}  a link to the content of the message,
+   * @param aTimestamp   {real}    the Julian date when the message was sent
+   * @param aReceived    {real}    the Julian date when the message was received
+   * @param aLink        {string}  a link to the content of the message,
    *                               if the content is hosted on a server
    *
    * @returns {integer} the ID of the newly-created record
@@ -475,12 +477,8 @@
     this._insertMessageStatement.params.externalID = aExternalID;
     this._insertMessageStatement.params.subject = aSubject;
     this._insertMessageStatement.params.authorID = aAuthorID;
-    // FIXME: this method is too low-level to be in charge of massaging data;
-    // make its callers convert the date values to Julian Dates or null values.
-    let timestamp = aTimestamp ? SnowlUtils.jsToJulianDate(aTimestamp) : null;
-    this._insertMessageStatement.params.timestamp = timestamp;
-    let received = aReceived ? SnowlUtils.jsToJulianDate(aReceived) : null;
-    this._insertMessageStatement.params.received = received;
+    this._insertMessageStatement.params.timestamp = aTimestamp;
+    this._insertMessageStatement.params.received = aReceived;
     this._insertMessageStatement.params.link = aLink;
     this._insertMessageStatement.execute();
 
--- a/modules/feed.js	Tue Sep 02 03:38:53 2008 -0700
+++ b/modules/feed.js	Tue Sep 02 03:41:40 2008 -0700
@@ -55,6 +55,7 @@
 Cu.import("resource://snowl/modules/source.js");
 Cu.import("resource://snowl/modules/identity.js");
 Cu.import("resource://snowl/modules/message.js");
+Cu.import("resource://snowl/modules/utils.js");
 
 // FIXME: factor this out into a common file.
 const PART_TYPE_CONTENT = 1;
@@ -330,9 +331,10 @@
     // XXX Should we separately record when we added the entry so that the user
     // can sort in the "order received" and view "when received" separately from
     // "when published/updated"?
-    let timestamp = aEntry.updated ? new Date(aEntry.updated) :
-                    aEntry.published ? new Date(aEntry.published) :
-                    ISO8601DateUtils.parse(aEntry.get("dc:date"));
+    let timestamp =   aEntry.updated        ? new Date(aEntry.updated)
+                    : aEntry.published      ? new Date(aEntry.published)
+                    : aEntry.get("dc:date") ? ISO8601DateUtils.parse(aEntry.get("dc:date"))
+                    : null;
 
     // FIXME: handle titles that contain markup or are missing.
     let messageID = this.addSimpleMessage(this.id, aExternalID,
@@ -461,13 +463,14 @@
    */
   addSimpleMessage: function(aSourceID, aExternalID, aSubject, aAuthorID,
                              aTimestamp, aReceived, aLink) {
-    // Convert the link to its string spec, which is how we store it
-    // in the datastore.
-    let link = aLink ? aLink.spec : null;
-
     let messageID =
-      SnowlDatastore.insertMessage(aSourceID, aExternalID, aSubject, aAuthorID,
-                                   aTimestamp, aReceived, link);
+      SnowlDatastore.insertMessage(aSourceID,
+                                   aExternalID,
+                                   aSubject,
+                                   aAuthorID,
+                                   aTimestamp ? SnowlUtils.jsToJulianDate(aTimestamp) : null,
+                                   SnowlUtils.jsToJulianDate(aReceived),
+                                   aLink ? aLink.spec : null);
 
     return messageID;
   },
--- a/modules/service.js	Tue Sep 02 03:38:53 2008 -0700
+++ b/modules/service.js	Tue Sep 02 03:41:40 2008 -0700
@@ -48,6 +48,7 @@
 Cu.import("resource://snowl/modules/twitter.js");
 Cu.import("resource://snowl/modules/source.js");
 Cu.import("resource://snowl/modules/URI.js");
+Cu.import("resource://snowl/modules/utils.js");
 
 const PERMS_FILE      = 0644;
 const PERMS_DIRECTORY = 0755;
@@ -231,7 +232,7 @@
                                      row.name,
                                      URI.get(row.machineURI),
                                      URI.get(row.humanURI),
-                                     new Date(row.lastRefreshed),
+                                     SnowlUtils.julianToJSDate(row.lastRefreshed),
                                      row.importance));
       }
     }
--- a/modules/source.js	Tue Sep 02 03:38:53 2008 -0700
+++ b/modules/source.js	Tue Sep 02 03:41:40 2008 -0700
@@ -43,6 +43,7 @@
 
 Cu.import("resource://snowl/modules/datastore.js");
 Cu.import("resource://snowl/modules/URI.js");
+Cu.import("resource://snowl/modules/utils.js");
 
 function SnowlSource(aID, aName, aMachineURI, aHumanURI, aLastRefreshed, aImportance) {
   this.id = aID;
@@ -77,7 +78,7 @@
                              this._getStatement.row.name,
                              URI.get(this._getStatement.row.machineURI),
                              URI.get(this._getStatement.row.humanURI),
-                             new Date(this._getStatement.row.lastRefreshed),
+                             SnowlUtils.julianToJSDate(this._getStatement.row.lastRefreshed),
                              this._getStatement.row.importance);
   }
   finally {
@@ -130,7 +131,7 @@
     let stmt = SnowlDatastore.createStatement("UPDATE sources " +
                                               "SET lastRefreshed = :lastRefreshed " +
                                               "WHERE id = :id");
-    stmt.params.lastRefreshed = this._lastRefreshed.getTime();
+    stmt.params.lastRefreshed = SnowlUtils.jsToJulianDate(this._lastRefreshed);
     stmt.params.id = this.id;
     stmt.execute();
   },
--- a/modules/twitter.js	Tue Sep 02 03:38:53 2008 -0700
+++ b/modules/twitter.js	Tue Sep 02 03:41:40 2008 -0700
@@ -50,11 +50,12 @@
 Cu.import("resource://snowl/modules/Observers.js");
 Cu.import("resource://snowl/modules/URI.js");
 
-// Snowl-specific modules
+// modules that are Snowl-specific
 Cu.import("resource://snowl/modules/datastore.js");
 Cu.import("resource://snowl/modules/source.js");
 Cu.import("resource://snowl/modules/identity.js");
 Cu.import("resource://snowl/modules/message.js");
+Cu.import("resource://snowl/modules/utils.js");
 
 // FIXME: factor this out into a common file.
 const PART_TYPE_CONTENT = 1;
@@ -472,13 +473,14 @@
    */
   addSimpleMessage: function(aSourceID, aExternalID, aSubject, aAuthorID,
                              aTimestamp, aReceived, aLink) {
-    // Convert the link to its string spec, which is how we store it
-    // in the datastore.
-    let link = aLink ? aLink.spec : null;
-
     let messageID =
-      SnowlDatastore.insertMessage(aSourceID, aExternalID, aSubject, aAuthorID,
-                                   aTimestamp, aReceived, link);
+      SnowlDatastore.insertMessage(aSourceID,
+                                   aExternalID,
+                                   aSubject,
+                                   aAuthorID,
+                                   aTimestamp ? SnowlUtils.jsToJulianDate(aTimestamp) : null,
+                                   SnowlUtils.jsToJulianDate(aReceived),
+                                   aLink ? aLink.spec : null);
 
     return messageID;
   },