Commit 8a7f3c76 authored by Romain Courteaud's avatar Romain Courteaud

Test drive to jio mapping.

parent 4428686a
/*jslint nomen: true, maxlen: 200*/ /*jslint nomen: true*/
/*global console, RSVP, Blob*/ /*global RSVP, Blob*/
(function (jIO) { (function (jIO, RSVP, Blob) {
"use strict"; "use strict";
/** /**
...@@ -11,10 +11,11 @@ ...@@ -11,10 +11,11 @@
*/ */
function FileSystemBridgeStorage(spec) { function FileSystemBridgeStorage(spec) {
this._sub_storage = jIO.createJIO(spec.sub_storage); this._sub_storage = jIO.createJIO(spec.sub_storage);
this._document_key = "/.jio_documents/";
this._attachment_key = "/.jio_attachments/";
} }
var DOCUMENT_EXTENSION = ".json", var DOCUMENT_EXTENSION = ".json",
DOCUMENT_REGEXP = new RegExp("^([\\w=]+)" +
DOCUMENT_EXTENSION + "$"),
DOCUMENT_KEY = "/.jio_documents/",
ROOT = "/"; ROOT = "/";
FileSystemBridgeStorage.prototype.get = function (param) { FileSystemBridgeStorage.prototype.get = function (param) {
...@@ -28,21 +29,22 @@ ...@@ -28,21 +29,22 @@
.push(function () { .push(function () {
// First get the document itself if it exists // First get the document itself if it exists
return context._sub_storage.getAttachment({ return context._sub_storage.getAttachment({
"_id": context._document_key, "_id": DOCUMENT_KEY,
"_attachment": param._id + DOCUMENT_EXTENSION "_attachment": param._id + DOCUMENT_EXTENSION
}); });
}) })
.push(function (blob) { .push(function (blob) {
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
return jIO.util.readBlobAsText(blob); return jIO.util.readBlobAsText(blob.data);
}) })
.push(function (text) { .push(function (text) {
explicit_document = true; explicit_document = true;
return JSON.parse(text.target.result); return JSON.parse(text.target.result);
}); });
}, function (error) { }, function (error) {
if ((error instanceof jIO.util.jIOError) && (error.status_code === 404)) { if ((error instanceof jIO.util.jIOError) &&
(error.status_code === 404)) {
return {}; return {};
} }
throw error; throw error;
...@@ -59,13 +61,15 @@ ...@@ -59,13 +61,15 @@
}) })
.push(function (directory_document) { .push(function (directory_document) {
if (directory_document._attachments.hasOwnProperty(param._id)) { if ((directory_document.hasOwnProperty("_attachments")) &&
(directory_document._attachments.hasOwnProperty(param._id))) {
json_document._attachments = { json_document._attachments = {
enclosure: {} enclosure: {}
}; };
} else { } else {
if (!explicit_document) { if (!explicit_document) {
throw new jIO.util.jIOError("Cannot find document", 404); throw new jIO.util.jIOError("Cannot find document " + param._id,
404);
} }
} }
return json_document; return json_document;
...@@ -73,38 +77,28 @@ ...@@ -73,38 +77,28 @@
}; };
FileSystemBridgeStorage.prototype.post = function (param) {
var doc_id = param._id;
if (doc_id === undefined) {
doc_id = jIO.util.generateUuid();
}
param._id = doc_id;
return this.put(param);
};
FileSystemBridgeStorage.prototype.put = function (param) { FileSystemBridgeStorage.prototype.put = function (param) {
var context = this, var context = this,
doc_id = param._id; doc_id = param._id;
// XXX Handle conflict! // XXX Handle conflict!
// XXX Put empty enclosure directly if json is empty
return context._sub_storage.putAttachment({ return context._sub_storage.putAttachment({
"_id": context._document_key, "_id": DOCUMENT_KEY,
"_attachment": doc_id + DOCUMENT_EXTENSION, "_attachment": doc_id + DOCUMENT_EXTENSION,
"_blob": new Blob([JSON.stringify(param)], {type: "application/json"}) "_blob": new Blob([JSON.stringify(param)], {type: "application/json"})
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
if ((error instanceof jIO.util.jIOError) && (error.status_code === 404)) { if ((error instanceof jIO.util.jIOError) &&
(error.status_code === 404)) {
return context._sub_storage.put({ return context._sub_storage.put({
"_id": context._document_key "_id": DOCUMENT_KEY
}) })
.push(function () { .push(function () {
return context._sub_storage.putAttachment({ return context._sub_storage.putAttachment({
"_id": context._document_key, "_id": DOCUMENT_KEY,
"_attachment": doc_id + DOCUMENT_EXTENSION, "_attachment": doc_id + DOCUMENT_EXTENSION,
"_blob": new Blob([JSON.stringify(param)], {type: "application/json"}) "_blob": new Blob([JSON.stringify(param)],
{type: "application/json"})
}); });
}); });
} }
...@@ -131,7 +125,8 @@ ...@@ -131,7 +125,8 @@
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
if ((error instanceof jIO.util.jIOError) && (error.status_code === 404)) { if ((error instanceof jIO.util.jIOError) &&
(error.status_code === 404)) {
got_error = true; got_error = true;
return; return;
} }
...@@ -141,14 +136,15 @@ ...@@ -141,14 +136,15 @@
// Second, try to remove explicit doc // Second, try to remove explicit doc
.push(function () { .push(function () {
return context._sub_storage.removeAttachment({ return context._sub_storage.removeAttachment({
"_id": context._document_key, "_id": DOCUMENT_KEY,
"_attachment": doc_id + DOCUMENT_EXTENSION "_attachment": doc_id + DOCUMENT_EXTENSION
}); });
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
if ((!got_error) && (error instanceof jIO.util.jIOError) && (error.status_code === 404)) { if ((!got_error) && (error instanceof jIO.util.jIOError) &&
return; (error.status_code === 404)) {
return doc_id;
} }
throw error; throw error;
}); });
...@@ -156,10 +152,7 @@ ...@@ -156,10 +152,7 @@
}; };
FileSystemBridgeStorage.prototype.hasCapacity = function (capacity) { FileSystemBridgeStorage.prototype.hasCapacity = function (capacity) {
if (capacity === "list") { return (capacity === "list");
return true;
}
return false;
}; };
FileSystemBridgeStorage.prototype.buildQuery = function () { FileSystemBridgeStorage.prototype.buildQuery = function () {
...@@ -171,18 +164,21 @@ ...@@ -171,18 +164,21 @@
.push(function () { .push(function () {
return context._sub_storage.get({ return context._sub_storage.get({
"_id": context._document_key "_id": DOCUMENT_KEY
}); });
}) })
.push(function (result) { .push(function (result) {
var key; var key;
for (key in result._attachments) { for (key in result._attachments) {
if (result._attachments.hasOwnProperty(key)) { if (result._attachments.hasOwnProperty(key)) {
result_dict[key.slice(0, key.length - DOCUMENT_EXTENSION.length)] = null; if (DOCUMENT_REGEXP.test(key)) {
result_dict[DOCUMENT_REGEXP.exec(key)[1]] = null;
}
} }
} }
}, function (error) { }, function (error) {
if ((error instanceof jIO.util.jIOError) && (error.status_code === 404)) { if ((error instanceof jIO.util.jIOError) &&
(error.status_code === 404)) {
return; return;
} }
throw error; throw error;
...@@ -224,7 +220,8 @@ ...@@ -224,7 +220,8 @@
FileSystemBridgeStorage.prototype.getAttachment = function (param) { FileSystemBridgeStorage.prototype.getAttachment = function (param) {
if (param._attachment !== "enclosure") { if (param._attachment !== "enclosure") {
throw new Error("Only support 'enclosure' attachment"); throw new jIO.util.jIOError("Only support 'enclosure' attachment",
400);
} }
return this._sub_storage.getAttachment({ return this._sub_storage.getAttachment({
...@@ -235,7 +232,8 @@ ...@@ -235,7 +232,8 @@
FileSystemBridgeStorage.prototype.putAttachment = function (param) { FileSystemBridgeStorage.prototype.putAttachment = function (param) {
if (param._attachment !== "enclosure") { if (param._attachment !== "enclosure") {
throw new Error("Only support 'enclosure' attachment"); throw new jIO.util.jIOError("Only support 'enclosure' attachment",
400);
} }
return this._sub_storage.putAttachment({ return this._sub_storage.putAttachment({
...@@ -247,7 +245,8 @@ ...@@ -247,7 +245,8 @@
FileSystemBridgeStorage.prototype.removeAttachment = function (param) { FileSystemBridgeStorage.prototype.removeAttachment = function (param) {
if (param._attachment !== "enclosure") { if (param._attachment !== "enclosure") {
throw new Error("Only support 'enclosure' attachment"); throw new jIO.util.jIOError("Only support 'enclosure' attachment",
400);
} }
return this._sub_storage.removeAttachment({ return this._sub_storage.removeAttachment({
...@@ -258,4 +257,4 @@ ...@@ -258,4 +257,4 @@
jIO.addStorage('drivetojiomapping', FileSystemBridgeStorage); jIO.addStorage('drivetojiomapping', FileSystemBridgeStorage);
}(jIO)); }(jIO, RSVP, Blob));
This diff is collapsed.
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
<script src="jio.storage/localstorage.tests.js"></script> <script src="jio.storage/localstorage.tests.js"></script>
<script src="jio.storage/documentstorage.tests.js"></script> <script src="jio.storage/documentstorage.tests.js"></script>
<script src="jio.storage/davstorage.tests.js"></script> <script src="jio.storage/davstorage.tests.js"></script>
<script src="jio.storage/drivetojiomapping.tests.js"></script>
<!--script src="jio.storage/unionstorage.tests.js"></script--> <!--script src="jio.storage/unionstorage.tests.js"></script-->
......
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