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

Test drive to jio mapping.

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