saferepairstorage.js 2.98 KB
Newer Older
1 2 3
/*jslint indent:2,maxlen:80,nomen:true*/
/*global  jIO, RSVP*/
(function (jIO, RSVP) {
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
  "use strict";

  /**
   * The jIO SafeRepairStorage extension
   *
   * @class SafeRepairStorage
   * @constructor
   */


  function SafeRepairStorage(spec) {
    this._sub_storage = jIO.createJIO(spec.sub_storage);
    this._id_dict = {};
  }

  SafeRepairStorage.prototype.get = function () {
    return this._sub_storage.get.apply(this._sub_storage, arguments);
  };
  SafeRepairStorage.prototype.allAttachments = function () {
    return this._sub_storage.allAttachments.apply(this._sub_storage, arguments);
  };
  SafeRepairStorage.prototype.post = function () {
    return this._sub_storage.post.apply(this._sub_storage, arguments);
  };
  SafeRepairStorage.prototype.put = function (id, doc) {
    var storage = this;
    return this._sub_storage.put.apply(this._sub_storage, arguments)
      .push(undefined, function (error) {
        if (error instanceof jIO.util.jIOError &&
            error.status_code === 403) {
          if (storage._id_dict[id]) {
            return storage._sub_storage.put(storage._id_dict[id], doc);
          }
          return storage._sub_storage.post(doc)
            .push(function (sub_id) {
              storage._id_dict[id] = sub_id;
              return sub_id;
            });
        }
      });
  };
  SafeRepairStorage.prototype.remove = function () {
    return;
  };
  SafeRepairStorage.prototype.getAttachment = function () {
    return this._sub_storage.getAttachment.apply(this._sub_storage, arguments);
  };
  SafeRepairStorage.prototype.putAttachment = function (id, attachment_id,
      attachment) {
    var storage = this;
    return this._sub_storage.putAttachment.apply(this._sub_storage, arguments)
      .push(undefined, function (error) {
        if (error instanceof jIO.util.jIOError &&
            error.status_code === 403) {
          return new RSVP.Queue()
            .push(function () {
              if (storage._id_dict[id]) {
                return storage._id_dict[id];
              }
              return storage._sub_storage.get(id)
                .push(function (doc) {
                  return storage._sub_storage.post(doc);
                });
            })
            .push(function (sub_id) {
              storage._id_dict[id] = sub_id;
              return storage._sub_storage.putAttachment(sub_id, attachment_id,
                  attachment);
            });
        }
      });
  };
  SafeRepairStorage.prototype.removeAttachment = function () {
    return;
  };
  SafeRepairStorage.prototype.repair = function () {
    return this._sub_storage.repair.apply(this._sub_storage, arguments);
  };
  SafeRepairStorage.prototype.hasCapacity = function (name) {
    return this._sub_storage.hasCapacity(name);
  };
  SafeRepairStorage.prototype.buildQuery = function () {
    return this._sub_storage.buildQuery.apply(this._sub_storage,
                                              arguments);
  };

  jIO.addStorage('saferepair', SafeRepairStorage);

92
}(jIO, RSVP));