Commit 7afa86e8 authored by Romain Courteaud's avatar Romain Courteaud

Drop "command" parameter from the previous jio version.

parent 71d52ce6
...@@ -29,22 +29,7 @@ ...@@ -29,22 +29,7 @@
*/ */
/*jslint indent: 2, maxlen: 120, nomen: true */ /*jslint indent: 2, maxlen: 120, nomen: true */
/*global define, module, require, indexedDB, jIO, RSVP, Blob, Math, alert*/ /*global indexedDB, jIO, RSVP, Blob, Math, alert, console*/
// (function (dependencies, factory) {
// "use strict";
// if (typeof define === "function" && define.amd) {
// return define(dependencies, factory);
// }
// if (typeof module === "object" && module !== null &&
// typeof module.exports === "object" && module.exports !== null &&
// typeof require === "function") {
// module.exports = factory.apply(null, dependencies.map(require));
// return;
// }
// factory(jIO, RSVP);
// }(["jio", "rsvp"], function (jIO, RSVP) {
// "use strict";
(function (jIO) { (function (jIO) {
"use strict"; "use strict";
...@@ -262,11 +247,10 @@ ...@@ -262,11 +247,10 @@
* @param {function} research The function to reserach * @param {function} research The function to reserach
* @param {function} ongoing The function to process * @param {function} ongoing The function to process
* @param {function} end The completed function * @param {function} end The completed function
* @param {Object} command The JIO command
* @param {Object} metadata The data to put * @param {Object} metadata The data to put
*/ */
IndexedDBStorage.prototype._putOrPost = IndexedDBStorage.prototype._putOrPost =
function (open, research, ongoing, end, command, metadata) { function (open, research, ongoing, end, metadata) {
var jio_storage = this, var jio_storage = this,
transaction, transaction,
global_db, global_db,
...@@ -309,8 +293,7 @@ ...@@ -309,8 +293,7 @@
global_db.close(); global_db.close();
} }
throw error; throw error;
}) });
.push(command.success, command.error, command.notify);
}; };
...@@ -319,7 +302,6 @@ ...@@ -319,7 +302,6 @@
/** /**
* Retrieve data * Retrieve data
* *
*@param {Object} command The JIO command
*@param {Object} param The command parameters *@param {Object} param The command parameters
*/ */
IndexedDBStorage.prototype.get = function (param) { IndexedDBStorage.prototype.get = function (param) {
...@@ -333,7 +315,7 @@ ...@@ -333,7 +315,7 @@
}) })
.push(function (db) { .push(function (db) {
global_db = db; global_db = db;
transaction = db.transaction(["metadata", "attachment"], "readwrite"); transaction = db.transaction(["metadata", "attachment"], "readonly");
var store = transaction.objectStore("metadata"); var store = transaction.objectStore("metadata");
return getIndexedDB(store, param._id); return getIndexedDB(store, param._id);
}) })
...@@ -354,7 +336,7 @@ ...@@ -354,7 +336,7 @@
return transactionEnd(transaction); return transactionEnd(transaction);
}) })
.push(function () { .push(function () {
return ({"data": meta}); return meta;
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
if (global_db !== undefined) { if (global_db !== undefined) {
...@@ -368,10 +350,9 @@ ...@@ -368,10 +350,9 @@
/** /**
* Remove a document * Remove a document
* *
* @param {Object} command The JIO command
* @param {Object} param The command parameters * @param {Object} param The command parameters
*/ */
IndexedDBStorage.prototype.remove = function (command, param) { IndexedDBStorage.prototype.remove = function (param) {
var jio_storage = this, var jio_storage = this,
transaction, transaction,
global_db, global_db,
...@@ -405,8 +386,7 @@ ...@@ -405,8 +386,7 @@
}) })
.push(function (resultResearch) { .push(function (resultResearch) {
if (resultResearch.result === undefined) { if (resultResearch.result === undefined) {
throw ({"status": 404, "reason": "Not Found", throw new jIO.util.jIOError("IndexeddbStorage, unable to get metadata.", 500);
"message": "IndexeddbStorage, unable to get metadata."});
} }
//delete metadata //delete metadata
return removeIndexedDB(resultResearch.store, param._id); return removeIndexedDB(resultResearch.store, param._id);
...@@ -439,18 +419,16 @@ ...@@ -439,18 +419,16 @@
global_db.close(); global_db.close();
} }
throw error; throw error;
}) });
.push(command.success, command.error, command.notify);
}; };
/** /**
* Creates a new document if not already existes * Creates a new document if not already existes
* @param {Object} command The JIO command
* @param {Object} metadata The metadata to put * @param {Object} metadata The metadata to put
*/ */
IndexedDBStorage.prototype.post = function (command, metadata) { IndexedDBStorage.prototype.post = function (metadata) {
var that = this; var that = this;
if (!metadata._id) { if (!metadata._id) {
metadata._id = generateUuid(); metadata._id = generateUuid();
...@@ -464,22 +442,20 @@ ...@@ -464,22 +442,20 @@
} }
function promiseEndPost(metadata) { function promiseEndPost(metadata) {
return ({"id": metadata._id}); return metadata._id;
} }
return that._putOrPost(openIndexedDB, promiseResearch, return that._putOrPost(openIndexedDB, promiseResearch,
promiseOngoingPost, promiseEndPost, promiseOngoingPost, promiseEndPost,
command, metadata); metadata);
}; };
/** /**
* Creates or updates a document * Creates or updates a document
* @param {Object} command The JIO command
* @param {Object} metadata The metadata to post * @param {Object} metadata The metadata to post
*/ */
IndexedDBStorage.prototype.put = function (command, metadata) { IndexedDBStorage.prototype.put = function (metadata) {
var that = this, var that = this;
found;
function promiseOngoingPut(researchResult) { function promiseOngoingPut(researchResult) {
var key; var key;
for (key in metadata) { for (key in metadata) {
...@@ -488,19 +464,15 @@ ...@@ -488,19 +464,15 @@
} }
} }
delete metadata._attachment; delete metadata._attachment;
if (researchResult.result !== undefined) {
found = true;
}
return putIndexedDB(researchResult.store, metadata); return putIndexedDB(researchResult.store, metadata);
} }
function promiseEndPut() { function promiseEndPut() {
return {"status": (found ? 204 : 201) }; return metadata._id;
} }
return that._putOrPost(openIndexedDB, promiseResearch, return that._putOrPost(openIndexedDB, promiseResearch,
promiseOngoingPut, promiseEndPut, promiseOngoingPut, promiseEndPut,
command, metadata); metadata);
}; };
...@@ -510,7 +482,6 @@ ...@@ -510,7 +482,6 @@
// * Retrieve a list of present document // * Retrieve a list of present document
// * // *
// * @method allDocs // * @method allDocs
// * @param {Object} command The JIO command
// * @param {Object} param The command parameters // * @param {Object} param The command parameters
// * @param {Object} options The command options // * @param {Object} options The command options
// * @param {Boolean} [options.include_docs=false] // * @param {Boolean} [options.include_docs=false]
...@@ -633,262 +604,242 @@ ...@@ -633,262 +604,242 @@
// }; // };
// /** /**
// * Add an attachment to a document * Add an attachment to a document
// * *
// * @param {Object} command The JIO command * @param {Object} metadata The data
// * @param {Object} metadata The data *
// * */
// */ IndexedDBStorage.prototype.putAttachment = function (metadata) {
// IndexedDBStorage.prototype.putAttachment = function (command, metadata) { var jio_storage = this,
// var jio_storage = this, transaction,
// transaction, global_db,
// global_db, BlobInfo,
// BlobInfo, readResult;
// readResult; function putAllPart(store, metadata, readResult, count, part) {
// function putAllPart(store, metadata, readResult, count, part) { var blob,
// var blob, readPart,
// readPart, end;
// end; if (count >= metadata._blob.size) {
// if (count >= metadata._blob.size) { return;
// return; }
// } end = count + jio_storage._unite;
// end = count + jio_storage._unite; blob = metadata._blob.slice(count, end);
// blob = metadata._blob.slice(count, end); readPart = readResult.slice(count, end);
// readPart = readResult.slice(count, end); return putIndexedDB(store, {"_id": metadata._id,
// return putIndexedDB(store, {"_id": metadata._id, "_attachment" : metadata._attachment,
// "_attachment" : metadata._attachment, "_part" : part,
// "_part" : part, "blob": blob}, readPart)
// "blob": blob}, readPart) .then(function () {
// .then(function () { return putAllPart(store, metadata, readResult, end, part + 1);
// return putAllPart(store, metadata, readResult, end, part + 1); });
// }); }
// } return jIO.util.readBlobAsArrayBuffer(metadata._blob)
// return jIO.util.readBlobAsArrayBuffer(metadata._blob) .then(function (event) {
// .then(function (event) { readResult = event.target.result;
// readResult = event.target.result; BlobInfo = {
// BlobInfo = { "content_type": metadata._blob.type,
// "content_type": metadata._blob.type, "length": metadata._blob.size
// "length": metadata._blob.size };
// }; return new RSVP.Queue()
// return new RSVP.Queue() .push(function () {
// .push(function () { return openIndexedDB(jio_storage._database_name);
// return openIndexedDB(jio_storage._database_name); })
// }) .push(function (db) {
// .push(function (db) { global_db = db;
// global_db = db; transaction = db.transaction(["attachment",
// transaction = db.transaction(["attachment", "blob"], "readwrite");
// "blob"], "readwrite"); return promiseResearch(transaction,
// return promiseResearch(transaction, metadata._id, "attachment", "_id");
// metadata._id, "attachment", "_id"); })
// }) .push(function (researchResult) {
// .push(function (researchResult) { if (researchResult.result === undefined) {
// if (researchResult.result === undefined) { throw new jIO.util.jIOError("IndexeddbStorage unable to put attachment.", 500);
// throw ({"status": 404, "reason": "Not Found", }
// "message": "indexeddbStorage unable to put attachment"}); //update attachment
// } researchResult.result._attachment = researchResult.
// //update attachment result._attachment || {};
// researchResult.result._attachment = researchResult. researchResult.result._attachment[metadata._attachment] =
// result._attachment || {}; (BlobInfo === undefined) ? "BlobInfo" : BlobInfo;
// researchResult.result._attachment[metadata._attachment] = return putIndexedDB(researchResult.store, researchResult.result);
// (BlobInfo === undefined) ? "BlobInfo" : BlobInfo; })
// return putIndexedDB(researchResult.store, researchResult.result); .push(function () {
// }) //put in blob
// .push(function () { var store = transaction.objectStore("blob");
// //put in blob return putAllPart(store, metadata, readResult, 0, 0);
// var store = transaction.objectStore("blob"); })
// return putAllPart(store, metadata, readResult, 0, 0); .push(function () {
// }) return transactionEnd(transaction);
// .push(function () { })
// return transactionEnd(transaction);
// })
// .push(function () { // .push(function () {
// return {"status": 204}; // return {"status": 204};
// }) // })
// .push(undefined, function (error) { .push(undefined, function (error) {
// if (global_db !== undefined) { if (global_db !== undefined) {
// global_db.close(); global_db.close();
// } }
// throw error; throw error;
// }) });
// .push(command.success, command.error, command.notify); });
// }); };
// };
// /** /**
// * Retriev a document attachment * Retriev a document attachment
// * *
// * @param {Object} command The JIO command * @param {Object} param The command parameter
// * @param {Object} param The command parameter */
// */ IndexedDBStorage.prototype.getAttachment = function (param) {
// IndexedDBStorage.prototype.getAttachment = function (command, param) { var jio_storage = this,
// var jio_storage = this, transaction,
// transaction, global_db,
// global_db, blob,
// blob, totalLength;
// totalLength; function getDesirePart(store, start, end) {
// function getDesirePart(store, start, end) { if (start > end) {
// if (start > end) { return;
// return; }
// } return getIndexedDB(store, [param._id, param._attachment, start])
// return getIndexedDB(store, [param._id, param._attachment, start]) .then(function (result) {
// .then(function (result) { var blobPart = result.blob;
// var blobPart = result.blob; if (result.blob.byteLength !== undefined) {
// if (result.blob.byteLength !== undefined) { blobPart = new Blob([result.blob]);
// blobPart = new Blob([result.blob]); }
// } if (blob) {
// if (blob) { blob = new Blob([blob, blobPart]);
// blob = new Blob([blob, blobPart]); } else {
// } else { blob = blobPart;
// blob = blobPart; }
// } return getDesirePart(store, start + 1, end);
// return getDesirePart(store, start + 1, end); });
// }); }
// } return new RSVP.Queue()
// return new RSVP.Queue() .push(function () {
// .push(function () { return openIndexedDB(jio_storage._database_name);
// return openIndexedDB(jio_storage._database_name); })
// }) .push(function (db) {
// .push(function (db) { global_db = db;
// global_db = db; transaction = db.transaction(["attachment", "blob"], "readwrite");
// transaction = db.transaction(["attachment", "blob"], "readwrite"); //check if the attachment exists
// //check if the attachment exists return promiseResearch(transaction,
// return promiseResearch(transaction, param._id, "attachment", "_id");
// param._id, "attachment", "_id"); })
// }) .push(function (researchResult) {
// .push(function (researchResult) { var result = researchResult.result,
// var result = researchResult.result, start,
// start, end;
// end; if (result === undefined ||
// if (result === undefined || result._attachment[param._attachment] === undefined) {
// result._attachment[param._attachment] === undefined) { throw new jIO.util.jIOError("IndexeddbStorage unable to get attachment.", 404);
// throw ({"status": 404, "reason": "missing attachment", }
// "message": "IndexeddbStorage, unable to get attachment."}); totalLength = result._attachment[param._attachment].length;
// } param._start = param._start === undefined ? 0 : param._start;
// totalLength = result._attachment[param._attachment].length; param._end = param._end === undefined ? totalLength
// param._start = param._start === undefined ? 0 : param._start; : param._end;
// param._end = param._end === undefined ? totalLength if (param._end > totalLength) {
// : param._end; param._end = totalLength;
// if (param._end > totalLength) { }
// param._end = totalLength; if (param._start < 0 || param._end < 0) {
// } throw ({"status": 404, "reason": "invalide _start, _end",
// if (param._start < 0 || param._end < 0) { "message": "_start and _end must be positive"});
// throw ({"status": 404, "reason": "invalide _start, _end", }
// "message": "_start and _end must be positive"}); if (param._start > param._end) {
// } throw ({"status": 404, "reason": "invalide offset",
// if (param._start > param._end) { "message": "start is great then end"});
// throw ({"status": 404, "reason": "invalide offset", }
// "message": "start is great then end"}); start = Math.floor(param._start / jio_storage._unite);
// } end = Math.floor(param._end / jio_storage._unite);
// start = Math.floor(param._start / jio_storage._unite); if (param._end % jio_storage._unite === 0) {
// end = Math.floor(param._end / jio_storage._unite); end -= 1;
// if (param._end % jio_storage._unite === 0) { }
// end -= 1; return getDesirePart(transaction.objectStore("blob"),
// } start,
// return getDesirePart(transaction.objectStore("blob"), end);
// start, })
// end); .push(function () {
// }) var start = param._start % jio_storage._unite,
// .push(function () { end = start + param._end - param._start;
// var start = param._start % jio_storage._unite, blob = blob.slice(start, end);
// end = start + param._end - param._start; return new Blob([blob], {type: "text/plain"});
// blob = blob.slice(start, end); })
// return ({ "data": new Blob([blob], {type: "text/plain"})}); .push(undefined, function (error) {
// }) // Check if transaction is ongoing, if so, abort it
// .push(undefined, function (error) { if (transaction !== undefined) {
// // Check if transaction is ongoing, if so, abort it transaction.abort();
// if (transaction !== undefined) { }
// transaction.abort(); if (global_db !== undefined) {
// } global_db.close();
// if (global_db !== undefined) { }
// global_db.close(); throw error;
// } });
// throw error; };
// })
// .push(command.success, command.error, command.notify);
// };
// /** /**
// * Remove an attachment * Remove an attachment
// * *
// * @method removeAttachment * @method removeAttachment
// * @param {Object} command The JIO command * @param {Object} param The command parameters
// * @param {Object} param The command parameters */
// */ IndexedDBStorage.prototype.removeAttachment = function (param) {
// IndexedDBStorage.prototype.removeAttachment = function (command, param) { var jio_storage = this,
// var jio_storage = this, transaction,
// transaction, global_db,
// global_db, totalLength;
// totalLength; function removePart(store, part) {
// function removePart(store, part) { if (part * jio_storage._unite >= totalLength) {
// if (part * jio_storage._unite >= totalLength) { return;
// return; }
// } return removeIndexedDB(store, [param._id, param._attachment, part])
// return removeIndexedDB(store, [param._id, param._attachment, part]) .then(function () {
// .then(function () { return removePart(store, part + 1);
// return removePart(store, part + 1); });
// }); }
// } return new RSVP.Queue()
// return new RSVP.Queue() .push(function () {
// .push(function () { return openIndexedDB(jio_storage._database_name);
// return openIndexedDB(jio_storage._database_name); })
// }) .push(function (db) {
// .push(function (db) { global_db = db;
// global_db = db; transaction = db.transaction(["attachment", "blob"], "readwrite");
// transaction = db.transaction(["attachment", "blob"], "readwrite"); //check if the attachment exists
// //check if the attachment exists return promiseResearch(transaction, param._id,
// return promiseResearch(transaction, param._id, "attachment", "_id");
// "attachment", "_id"); })
// }) .push(function (researchResult) {
// .push(function (researchResult) { var result = researchResult.result;
// var result = researchResult.result; if (result === undefined ||
// if (result === undefined || result._attachment[param._attachment] === undefined) {
// result._attachment[param._attachment] === undefined) { throw ({"status": 404, "reason": "missing attachment",
// throw ({"status": 404, "reason": "missing attachment", "message":
// "message": "IndexeddbStorage, document attachment not found."});
// "IndexeddbStorage, document attachment not found."}); }
// } totalLength = result._attachment[param._attachment].length;
// totalLength = result._attachment[param._attachment].length; //updata attachment
// //updata attachment delete result._attachment[param._attachment];
// delete result._attachment[param._attachment]; return putIndexedDB(researchResult.store, result);
// return putIndexedDB(researchResult.store, result); })
// }) .push(function () {
// .push(function () { var store = transaction.objectStore("blob");
// var store = transaction.objectStore("blob"); return removePart(store, 0);
// return removePart(store, 0); })
// }) .push(function () {
// .push(function () { return transactionEnd(transaction);
// return transactionEnd(transaction); })
// }) .push(undefined, function (error) {
// .push(function () { if (global_db !== undefined) {
// return ({ "status": 204 }); global_db.close();
// }) }
// .push(undefined, function (error) { throw error;
// if (global_db !== undefined) { });
// global_db.close(); };
// }
// throw error;
// })
// .push(command.success, command.error, command.notify);
// };
// IndexedDBStorage.prototype.allDocs = function (command, param, option) { // IndexedDBStorage.prototype.allDocs = function (param, option) {
// /*jslint unparam: true */ // /*jslint unparam: true */
// this.createDBIfNecessary(). // this.createDBIfNecessary().
// then(this.getListMetadata.bind(this, option)). // then(this.getListMetadata.bind(this, option));
// then(command.success, command.error, command.notify);
// };
//
// IndexedDBStorage.prototype.check = function (command) {
// command.success();
// };
//
// IndexedDBStorage.prototype.repair = function (command) {
// command.success();
// }; // };
jIO.addStorage("indexeddb", IndexedDBStorage); jIO.addStorage("indexeddb", IndexedDBStorage);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment