DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (d1ed7de67f5a)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
/* -*- js-indent-level: 2; indent-tabs-mode: nil -*- */
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
/* 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/. */

'use strict'

this.EXPORTED_SYMBOLS = ['DataStoreCursor'];

function debug(s) {
  //dump('DEBUG DataStoreCursor: ' + s + '\n');
}

const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;

const STATE_INIT = 0;
const STATE_REVISION_INIT = 1;
const STATE_REVISION_CHECK = 2;
const STATE_SEND_ALL = 3;
const STATE_REVISION_SEND = 4;
const STATE_DONE = 5;

const REVISION_ADDED = 'added';
const REVISION_UPDATED = 'updated';
const REVISION_REMOVED = 'removed';
const REVISION_VOID = 'void';
const REVISION_SKIP = 'skip'

Cu.import('resource://gre/modules/Services.jsm');
Cu.import('resource://gre/modules/XPCOMUtils.jsm');

/**
 * legend:
 * - RID = revision ID
 * - R = revision object (with the internalRevisionId that is a number)
 * - X = current object ID.
 * - L = the list of revisions that we have to send
 *
 * State: init: do you have RID ?
 *   YES: state->initRevision; loop
 *   NO: get R; X=0; state->sendAll; send a 'clear'
 *
 * State: initRevision. Get R from RID. Done?
 *   YES: state->revisionCheck; loop
 *   NO: RID = null; state->init; loop
 *
 * State: revisionCheck: get all the revisions between R and NOW. Done?
 *   YES and R == NOW: state->done; loop
 *   YES and R != NOW: Store this revisions in L; state->revisionSend; loop
 *   NO: R = NOW; X=0; state->sendAll; send a 'clear'
 *
 * State: sendAll: is R still the last revision?
 *   YES get the first object with id > X. Done?
 *     YES: X = object.id; send 'add'
 *     NO: state->revisionCheck; loop
 *   NO: R = NOW; X=0; send a 'clear'
 *
 * State: revisionSend: do you have something from L to send?
 *   YES and L[0] == 'removed': R=L[0]; send 'remove' with ID
 *   YES and L[0] == 'added': R=L[0]; get the object; found?
 *     NO: loop
 *     YES: send 'add' with ID and object
 *   YES and L[0] == 'updated': R=L[0]; get the object; found?
 *     NO: loop
 *     YES and object.R > R: continue
 *     YES and object.R <= R: send 'update' with ID and object
 *   YES L[0] == 'void': R=L[0]; state->init; loop
 *   NO: state->revisionCheck; loop
 *
 * State: done: send a 'done' with R
 */

/* Helper functions */
function createDOMError(aWindow, aEvent) {
  return new aWindow.DOMError(aEvent.target.error.name);
}

/* DataStoreCursor object */
this.DataStoreCursor = function(aWindow, aDataStore, aRevisionId) {
  debug("DataStoreCursor created");
  this.init(aWindow, aDataStore, aRevisionId);
}

this.DataStoreCursor.prototype = {
  classDescription: 'DataStoreCursor XPCOM Component',
  classID: Components.ID('{b6d14349-1eab-46b8-8513-584a7328a26b}'),
  contractID: '@mozilla.org/dom/datastore-cursor-impl;1',
  QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsISupports]),

  _shuttingdown: false,

  _window: null,
  _dataStore: null,
  _revisionId: null,
  _revision: null,
  _revisionsList: null,
  _objectId: 0,

  _state: STATE_INIT,

  init: function(aWindow, aDataStore, aRevisionId) {
    debug('DataStoreCursor init');

    this._window = aWindow;
    this._dataStore = aDataStore;
    this._revisionId = aRevisionId;

    Services.obs.addObserver(this, "inner-window-destroyed", false);

    let util = aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
                      .getInterface(Ci.nsIDOMWindowUtils);
    this._innerWindowID = util.currentInnerWindowID;
  },

  observe: function(aSubject, aTopic, aData) {
    let wId = aSubject.QueryInterface(Ci.nsISupportsPRUint64).data;
    if (wId == this._innerWindowID) {
      Services.obs.removeObserver(this, "inner-window-destroyed");
      this._shuttingdown = true;
    }
  },

  // This is the implementation of the state machine.
  // Read the comments at the top of this file in order to follow what it does.
  stateMachine: function(aStore, aRevisionStore, aResolve, aReject) {
    debug('StateMachine: ' + this._state);

    // If the window has been destroyed we cannot create the Promise object.
    if (this._shuttingdown) {
      return;
    }

    switch (this._state) {
      case STATE_INIT:
        this.stateMachineInit(aStore, aRevisionStore, aResolve, aReject);
        break;

      case STATE_REVISION_INIT:
        this.stateMachineRevisionInit(aStore, aRevisionStore, aResolve, aReject);
        break;

      case STATE_REVISION_CHECK:
        this.stateMachineRevisionCheck(aStore, aRevisionStore, aResolve, aReject);
        break;

      case STATE_SEND_ALL:
        this.stateMachineSendAll(aStore, aRevisionStore, aResolve, aReject);
        break;

      case STATE_REVISION_SEND:
        this.stateMachineRevisionSend(aStore, aRevisionStore, aResolve, aReject);
        break;

      case STATE_DONE:
        this.stateMachineDone(aStore, aRevisionStore, aResolve, aReject);
        break;
    }
  },

  stateMachineInit: function(aStore, aRevisionStore, aResolve, aReject) {
    debug('StateMachineInit');

    if (this._revisionId) {
      this._state = STATE_REVISION_INIT;
      this.stateMachine(aStore, aRevisionStore, aResolve, aReject);
      return;
    }

    let self = this;
    let request = aRevisionStore.openCursor(null, 'prev');
    request.onsuccess = function(aEvent) {
      if (aEvent.target.result === undefined) {
        aReject(self._window.DOMError("InvalidRevision",
                                      "The DataStore is corrupted"));
        return;
      }

      self._revision = aEvent.target.result.value;
      self._objectId = 0;
      self._state = STATE_SEND_ALL;
      aResolve(self.createTask('clear', null, '', null));
    }
  },

  stateMachineRevisionInit: function(aStore, aRevisionStore, aResolve, aReject) {
    debug('StateMachineRevisionInit');

    let self = this;
    let request = this._dataStore._db.getInternalRevisionId(
      self._revisionId,
      aRevisionStore,
      function(aInternalRevisionId) {
        // This revision doesn't exist.
        if (aInternalRevisionId == undefined) {
          self._revisionId = null;
          self._objectId = 0;
          self._state = STATE_INIT;
          self.stateMachine(aStore, aRevisionStore, aResolve, aReject);
          return;
        }

        self._revision = { revisionId: self._revisionId,
                           internalRevisionId: aInternalRevisionId };
        self._state = STATE_REVISION_CHECK;
        self.stateMachine(aStore, aRevisionStore, aResolve, aReject);
      }
    );
  },

  stateMachineRevisionCheck: function(aStore, aRevisionStore, aResolve, aReject) {
    debug('StateMachineRevisionCheck');

    let changes = {
      addedIds: {},
      updatedIds: {},
      removedIds: {}
    };

    let self = this;
    let request = aRevisionStore.mozGetAll(
      self._window.IDBKeyRange.lowerBound(this._revision.internalRevisionId, true));
    request.onsuccess = function(aEvent) {

      // Optimize the operations.
      for (let i = 0; i < aEvent.target.result.length; ++i) {
        let data = aEvent.target.result[i];

        switch (data.operation) {
          case REVISION_ADDED:
            changes.addedIds[data.objectId] = data.internalRevisionId;
            break;

          case REVISION_UPDATED:
            // We don't consider an update if this object has been added
            // or if it has been already modified by a previous
            // operation.
            if (!(data.objectId in changes.addedIds) &&
                !(data.objectId in changes.updatedIds)) {
              changes.updatedIds[data.objectId] = data.internalRevisionId;
            }
            break;

          case REVISION_REMOVED:
            let id = data.objectId;

            // If the object has been added in this range of revisions
            // we can ignore it and remove it from the list.
            if (id in changes.addedIds) {
              delete changes.addedIds[id];
            } else {
              changes.removedIds[id] = data.internalRevisionId;
            }

            if (id in changes.updatedIds) {
              delete changes.updatedIds[id];
            }
            break;

          case REVISION_VOID:
            if (i != 0) {
              dump('Internal error: Revision "' + REVISION_VOID + '" should not be found!!!\n');
              return;
            }

            self._revisionId = null;
            self._objectId = 0;
            self._state = STATE_INIT;
            self.stateMachine(aStore, aRevisionStore, aResolve, aReject);
            return;
        }
      }

      // From changes to a map of internalRevisionId.
      let revisions = {};
      function addRevisions(obj) {
        for (let key in obj) {
          revisions[obj[key]] = true;
        }
      }

      addRevisions(changes.addedIds);
      addRevisions(changes.updatedIds);
      addRevisions(changes.removedIds);

      // Create the list of revisions.
      let list = [];
      for (let i = 0; i < aEvent.target.result.length; ++i) {
        let data = aEvent.target.result[i];

        // If this revision doesn't contain useful data, we still need to keep
        // it in the list because we need to update the internal revision ID.
        if (!(data.internalRevisionId in revisions)) {
          data.operation = REVISION_SKIP;
        }

        list.push(data);
      }

      if (list.length == 0) {
        self._state = STATE_DONE;
        self.stateMachine(aStore, aRevisionStore, aResolve, aReject);
        return;
      }

      // Some revision has to be sent.
      self._revisionsList = list;
      self._state = STATE_REVISION_SEND;
      self.stateMachine(aStore, aRevisionStore, aResolve, aReject);
    };
  },

  stateMachineSendAll: function(aStore, aRevisionStore, aResolve, aReject) {
    debug('StateMachineSendAll');

    let self = this;
    let request = aRevisionStore.openCursor(null, 'prev');
    request.onsuccess = function(aEvent) {
      if (self._revision.revisionId != aEvent.target.result.value.revisionId) {
        self._revision = aEvent.target.result.value;
        self._objectId = 0;
        aResolve(self.createTask('clear', null, '', null));
        return;
      }

      let request = aStore.openCursor(self._window.IDBKeyRange.lowerBound(self._objectId, true));
      request.onsuccess = function(aEvent) {
        let cursor = aEvent.target.result;
        if (!cursor) {
          self._state = STATE_REVISION_CHECK;
          self.stateMachine(aStore, aRevisionStore, aResolve, aReject);
          return;
        }

        self._objectId = cursor.key;
        aResolve(self.createTask('add', self._objectId, '', cursor.value));
      };
    };
  },

  stateMachineRevisionSend: function(aStore, aRevisionStore, aResolve, aReject) {
    debug('StateMachineRevisionSend');

    if (!this._revisionsList.length) {
      this._state = STATE_REVISION_CHECK;
      this.stateMachine(aStore, aRevisionStore, aResolve, aReject);
      return;
    }

    this._revision = this._revisionsList.shift();

    switch (this._revision.operation) {
      case REVISION_REMOVED:
        aResolve(this.createTask('remove', this._revision.objectId, '', null));
        break;

      case REVISION_ADDED: {
        let request = aStore.get(this._revision.objectId);
        let self = this;
        request.onsuccess = function(aEvent) {
          if (aEvent.target.result == undefined) {
            self.stateMachine(aStore, aRevisionStore, aResolve, aReject);
            return;
          }

          aResolve(self.createTask('add', self._revision.objectId, '',
                                   aEvent.target.result));
        }
        break;
      }

      case REVISION_UPDATED: {
        let request = aStore.get(this._revision.objectId);
        let self = this;
        request.onsuccess = function(aEvent) {
          if (aEvent.target.result == undefined) {
            self.stateMachine(aStore, aRevisionStore, aResolve, aReject);
            return;
          }

          if (aEvent.target.result.revisionId >  self._revision.internalRevisionId) {
            self.stateMachine(aStore, aRevisionStore, aResolve, aReject);
            return;
          }

          aResolve(self.createTask('update', self._revision.objectId, '',
                                   aEvent.target.result));
        }
        break;
      }

      case REVISION_VOID:
        // Internal error!
        dump('Internal error: Revision "' + REVISION_VOID + '" should not be found!!!\n');
        break;

      case REVISION_SKIP:
        // This revision contains data that has already been sent by another one.
        this.stateMachine(aStore, aRevisionStore, aResolve, aReject);
        break;
    }
  },

  stateMachineDone: function(aStore, aRevisionStore, aResolve, aReject) {
    this.close();
    aResolve(this.createTask('done', null, this._revision.revisionId, null));
  },

  // public interface

  get store() {
    return this._dataStore.exposedObject;
  },

  next: function() {
    debug('Next');

    // If the window has been destroyed we cannot create the Promise object.
    if (this._shuttingdown) {
      throw Cr.NS_ERROR_FAILURE;
    }

    let self = this;
    return new this._window.Promise(function(aResolve, aReject) {
      self._dataStore._db.cursorTxn(
        function(aTxn, aStore, aRevisionStore) {
          self.stateMachine(aStore, aRevisionStore, aResolve, aReject);
        },
        function(aEvent) {
          aReject(createDOMError(self._window, aEvent));
        }
      );
    });
  },

  close: function() {
    this._dataStore.syncTerminated(this);
  },

  createTask: function(aOperation, aId, aRevisionId, aData) {
    return Cu.cloneInto({ operation: aOperation, id: aId,
                          revisionId: aRevisionId, data: aData }, this._window);
  }
};