Commit 0f5db310 authored by Tristan Cavelier's avatar Tristan Cavelier

localstorage memory mode added

parent 02686913
...@@ -5,8 +5,7 @@ ...@@ -5,8 +5,7 @@
*/ */
/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */ /*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */
/*global jIO: true, localStorage: true, setTimeout: true, /*global jIO, localStorage, setTimeout, complex_queries, define */
complex_queries, define */
/** /**
* JIO Local Storage. Type = 'local'. * JIO Local Storage. Type = 'local'.
...@@ -16,6 +15,9 @@ ...@@ -16,6 +15,9 @@
* *
* { * {
* "type": "local", * "type": "local",
* "mode": <string>,
* // - "localStorage" // default
* // - "memory"
* "username": <non empty string>, // to define user space * "username": <non empty string>, // to define user space
* "application_name": <string> // default 'untitled' * "application_name": <string> // default 'untitled'
* } * }
...@@ -94,11 +96,13 @@ ...@@ -94,11 +96,13 @@
return true; return true;
} }
var ram = {}, memorystorage, localstorage;
/* /*
* Wrapper for the localStorage used to simplify instion of any kind of * Wrapper for the localStorage used to simplify instion of any kind of
* values * values
*/ */
var localstorage = { localstorage = {
getItem: function (item) { getItem: function (item) {
var value = localStorage.getItem(item); var value = localStorage.getItem(item);
return value === null ? null : JSON.parse(value); return value === null ? null : JSON.parse(value);
...@@ -111,6 +115,23 @@ ...@@ -111,6 +115,23 @@
} }
}; };
/*
* Wrapper for the localStorage used to simplify instion of any kind of
* values
*/
memorystorage = {
getItem: function (item) {
var value = ram[item];
return value === undefined ? null : JSON.parse(value);
},
setItem: function (item, value) {
ram[item] = JSON.stringify(value);
},
removeItem: function (item) {
delete ram[item];
}
};
jIO.addStorageType('local', function (spec, my) { jIO.addStorageType('local', function (spec, my) {
spec = spec || {}; spec = spec || {};
...@@ -125,11 +146,26 @@ ...@@ -125,11 +146,26 @@
priv.localpath = 'jio/localstorage/' + priv.username + '/' + priv.localpath = 'jio/localstorage/' + priv.username + '/' +
priv.application_name; priv.application_name;
switch (spec.mode) {
case "memory":
priv.database = ram;
priv.storage = memorystorage;
priv.mode = "memory";
break;
default:
priv.database = localStorage;
priv.storage = localstorage;
priv.mode = "localStorage";
break;
}
// ===================== overrides ====================== // ===================== overrides ======================
that.specToStore = function () { that.specToStore = function () {
return { return {
"application_name": priv.application_name, "application_name": priv.application_name,
"username": priv.username "username": priv.username,
"mode": priv.mode
}; };
}; };
...@@ -152,13 +188,13 @@ ...@@ -152,13 +188,13 @@
if (!doc_id) { if (!doc_id) {
doc_id = generateUuid(); doc_id = generateUuid();
} }
doc = localstorage.getItem(priv.localpath + "/" + doc_id); doc = priv.storage.getItem(priv.localpath + "/" + doc_id);
if (doc === null) { if (doc === null) {
// the document does not exist // the document does not exist
doc = command.cloneDoc(); doc = command.cloneDoc();
doc._id = doc_id; doc._id = doc_id;
delete doc._attachments; delete doc._attachments;
localstorage.setItem(priv.localpath + "/" + doc_id, doc); priv.storage.setItem(priv.localpath + "/" + doc_id, doc);
that.success({ that.success({
"ok": true, "ok": true,
"id": doc_id "id": doc_id
...@@ -184,7 +220,7 @@ ...@@ -184,7 +220,7 @@
that.put = function (command) { that.put = function (command) {
setTimeout(function () { setTimeout(function () {
var doc, tmp; var doc, tmp;
doc = localstorage.getItem(priv.localpath + "/" + command.getDocId()); doc = priv.storage.getItem(priv.localpath + "/" + command.getDocId());
if (doc === null) { if (doc === null) {
// the document does not exist // the document does not exist
doc = command.cloneDoc(); doc = command.cloneDoc();
...@@ -196,7 +232,7 @@ ...@@ -196,7 +232,7 @@
doc = tmp; doc = tmp;
} }
// write // write
localstorage.setItem(priv.localpath + "/" + command.getDocId(), doc); priv.storage.setItem(priv.localpath + "/" + command.getDocId(), doc);
that.success({ that.success({
"ok": true, "ok": true,
"id": command.getDocId() "id": command.getDocId()
...@@ -212,7 +248,7 @@ ...@@ -212,7 +248,7 @@
that.putAttachment = function (command) { that.putAttachment = function (command) {
setTimeout(function () { setTimeout(function () {
var doc; var doc;
doc = localstorage.getItem(priv.localpath + "/" + command.getDocId()); doc = priv.storage.getItem(priv.localpath + "/" + command.getDocId());
if (doc === null) { if (doc === null) {
// the document does not exist // the document does not exist
that.error({ that.error({
...@@ -234,11 +270,11 @@ ...@@ -234,11 +270,11 @@
}; };
// upload data // upload data
localstorage.setItem(priv.localpath + "/" + command.getDocId() + "/" + priv.storage.setItem(priv.localpath + "/" + command.getDocId() + "/" +
command.getAttachmentId(), command.getAttachmentId(),
command.getAttachmentData()); command.getAttachmentData());
// write document // write document
localstorage.setItem(priv.localpath + "/" + command.getDocId(), doc); priv.storage.setItem(priv.localpath + "/" + command.getDocId(), doc);
that.success({ that.success({
"ok": true, "ok": true,
"id": command.getDocId(), "id": command.getDocId(),
...@@ -254,7 +290,7 @@ ...@@ -254,7 +290,7 @@
*/ */
that.get = function (command) { that.get = function (command) {
setTimeout(function () { setTimeout(function () {
var doc = localstorage.getItem( var doc = priv.storage.getItem(
priv.localpath + "/" + command.getDocId() priv.localpath + "/" + command.getDocId()
); );
if (doc !== null) { if (doc !== null) {
...@@ -278,7 +314,7 @@ ...@@ -278,7 +314,7 @@
*/ */
that.getAttachment = function (command) { that.getAttachment = function (command) {
setTimeout(function () { setTimeout(function () {
var doc = localstorage.getItem( var doc = priv.storage.getItem(
priv.localpath + "/" + command.getDocId() + priv.localpath + "/" + command.getDocId() +
"/" + command.getAttachmentId() "/" + command.getAttachmentId()
); );
...@@ -304,7 +340,7 @@ ...@@ -304,7 +340,7 @@
that.remove = function (command) { that.remove = function (command) {
setTimeout(function () { setTimeout(function () {
var doc, i, attachment_list; var doc, i, attachment_list;
doc = localstorage.getItem(priv.localpath + "/" + command.getDocId()); doc = priv.storage.getItem(priv.localpath + "/" + command.getDocId());
attachment_list = []; attachment_list = [];
if (doc !== null && typeof doc === "object") { if (doc !== null && typeof doc === "object") {
if (typeof doc._attachments === "object") { if (typeof doc._attachments === "object") {
...@@ -324,10 +360,10 @@ ...@@ -324,10 +360,10 @@
"reason": "missing" "reason": "missing"
}); });
} }
localstorage.removeItem(priv.localpath + "/" + command.getDocId()); priv.storage.removeItem(priv.localpath + "/" + command.getDocId());
// delete all attachments // delete all attachments
for (i = 0; i < attachment_list.length; i += 1) { for (i = 0; i < attachment_list.length; i += 1) {
localstorage.removeItem(priv.localpath + "/" + command.getDocId() + priv.storage.removeItem(priv.localpath + "/" + command.getDocId() +
"/" + attachment_list[i]); "/" + attachment_list[i]);
} }
that.success({ that.success({
...@@ -354,7 +390,7 @@ ...@@ -354,7 +390,7 @@
"reason": "missing" "reason": "missing"
}); });
}; };
doc = localstorage.getItem(priv.localpath + "/" + command.getDocId()); doc = priv.storage.getItem(priv.localpath + "/" + command.getDocId());
// remove attachment from document // remove attachment from document
if (doc !== null && typeof doc === "object" && if (doc !== null && typeof doc === "object" &&
typeof doc._attachments === "object") { typeof doc._attachments === "object") {
...@@ -364,9 +400,9 @@ ...@@ -364,9 +400,9 @@
if (objectIsEmpty(doc._attachments)) { if (objectIsEmpty(doc._attachments)) {
delete doc._attachments; delete doc._attachments;
} }
localstorage.setItem(priv.localpath + "/" + command.getDocId(), priv.storage.setItem(priv.localpath + "/" + command.getDocId(),
doc); doc);
localstorage.removeItem(priv.localpath + "/" + command.getDocId() + priv.storage.removeItem(priv.localpath + "/" + command.getDocId() +
"/" + command.getAttachmentId()); "/" + command.getAttachmentId());
that.success({ that.success({
"ok": true, "ok": true,
...@@ -401,15 +437,15 @@ ...@@ -401,15 +437,15 @@
option.select_list === undefined && option.select_list === undefined &&
option.include_docs === undefined)) { option.include_docs === undefined)) {
rows = []; rows = [];
for (i in localStorage) { for (i in priv.database) {
if (localStorage.hasOwnProperty(i)) { if (priv.database.hasOwnProperty(i)) {
// filter non-documents // filter non-documents
if (path_re.test(i)) { if (path_re.test(i)) {
row = { value: {} }; row = { value: {} };
row.id = i.split('/').slice(-1)[0]; row.id = i.split('/').slice(-1)[0];
row.key = row.id; row.key = row.id;
if (command.getOption('include_docs')) { if (command.getOption('include_docs')) {
row.doc = JSON.parse(localStorage.getItem(i)); row.doc = JSON.parse(priv.storage.getItem(i));
} }
rows.push(row); rows.push(row);
} }
...@@ -418,10 +454,10 @@ ...@@ -418,10 +454,10 @@
that.success({"rows": rows, "total_rows": rows.length}); that.success({"rows": rows, "total_rows": rows.length});
} else { } else {
// create complex query object from returned results // create complex query object from returned results
for (i in localStorage) { for (i in priv.database) {
if (localStorage.hasOwnProperty(i)) { if (priv.database.hasOwnProperty(i)) {
if (path_re.test(i)) { if (path_re.test(i)) {
document_list.push(localstorage.getItem(i)); document_list.push(priv.storage.getItem(i));
} }
} }
} }
......
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