Commit d42fadc0 authored by Marco Mariani's avatar Marco Mariani

Merge remote-tracking branch 'origin/master' into keys-jiodate-async

parents 98cf075d cadb268e
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
} }
window.complex_queries = {}; window.complex_queries = {};
module(window.complex_queries, RSVP); module(window.complex_queries, RSVP);
}(['exports'], function (to_export, RSVP) { }(['exports', 'rsvp'], function (to_export, RSVP) {
"use strict"; "use strict";
/** /**
......
...@@ -15,12 +15,12 @@ ...@@ -15,12 +15,12 @@
<body> <body>
<table> <table>
<tr> <tr>
<td>Query (String):<br /><textarea id="str">1:abc AND 2:def</textarea></td> <td>Query (String):<br /><textarea id="str">title:abc AND format:def</textarea></td>
<td>Query (Object):<br /><textarea id="obj">{&quot;type&quot;:&quot;complex&quot;,&quot;operator&quot;:&quot;AND&quot;,&quot;query_list&quot;:[{&quot;type&quot;:&quot;simple&quot;,&quot;operator&quot;:&quot;=&quot;,&quot;key&quot;:&quot;1&quot;,&quot;value&quot;:&quot;abc&quot;},{&quot;type&quot;:&quot;simple&quot;,&quot;operator&quot;:&quot;=&quot;,&quot;key&quot;:&quot;2&quot;,&quot;value&quot;:&quot;def&quot;}]}</textarea></td> <td>Query (Object):<br /><textarea id="obj">{&quot;type&quot;:&quot;complex&quot;,&quot;operator&quot;:&quot;AND&quot;,&quot;query_list&quot;:[{&quot;type&quot;:&quot;simple&quot;,&quot;operator&quot;:&quot;=&quot;,&quot;key&quot;:&quot;title&quot;,&quot;value&quot;:&quot;abc&quot;},{&quot;type&quot;:&quot;simple&quot;,&quot;operator&quot;:&quot;=&quot;,&quot;key&quot;:&quot;format&quot;,&quot;value&quot;:&quot;def&quot;}]}</textarea></td>
</tr> </tr>
<tr> <tr>
<td>Item list (to filter, from 'Query (Object)'):<br /><textarea id="list">[{&quot;1&quot;:&quot;abc&quot;,&quot;2&quot;:&quot;def&quot;},{&quot;1&quot;:&quot;def&quot;,&quot;2&quot;:&quot;abc&quot;}]</textarea></td> <td>Item list (to filter, from 'Query (Object)'):<br /><textarea id="list">[{&quot;title&quot;:&quot;abc&quot;,&quot;format&quot;:&quot;def&quot;},{&quot;title&quot;:&quot;def&quot;,&quot;format&quot;:&quot;abc&quot;}]</textarea></td>
<td>Result list:<br /><textarea id="result">[{&quot;1&quot;:&quot;abc&quot;,&quot;2&quot;:&quot;def&quot;}]</textarea></td> <td>Result list:<br /><textarea id="result"></textarea></td>
</tr> </tr>
<tr> <tr>
<td><label for="wildcard">Wildcard char: </label></td> <td><label for="wildcard">Wildcard char: </label></td>
...@@ -28,11 +28,11 @@ ...@@ -28,11 +28,11 @@
</tr> </tr>
<tr> <tr>
<td><label for="sort_on">Sort on: </label></td> <td><label for="sort_on">Sort on: </label></td>
<td><input type="text" id="sort_on" name="sort_on" value="[[&quot;1&quot;,&quot;ascending&quot;],[&quot;2&quot;,&quot;descending&quot;]]" /></td> <td><input type="text" id="sort_on" name="sort_on" value="[[&quot;title&quot;,&quot;ascending&quot;],[&quot;format&quot;,&quot;descending&quot;]]" /></td>
</tr> </tr>
<tr> <tr>
<td><label for="select_list">Select_list: </label></td> <td><label for="select_list">Select_list: </label></td>
<td><input type="text" id="select_list" name="select_list" value="[&quot;1&quot;,&quot;2&quot;]" /></td> <td><input type="text" id="select_list" name="select_list" value="[&quot;title&quot;,&quot;format&quot;]" /></td>
</tr> </tr>
<tr> <tr>
<td><label for="limit">Limit: </label></td> <td><label for="limit">Limit: </label></td>
...@@ -42,24 +42,39 @@ ...@@ -42,24 +42,39 @@
<button onclick="searchTextToJson()">Search text to JSON</button> <button onclick="searchTextToJson()">Search text to JSON</button>
<button onclick="jsonToSearchText()">JSON to Search text</button> <button onclick="jsonToSearchText()">JSON to Search text</button>
<button onclick="query()">Query</button> <button onclick="query()">Query</button>
<script type="text/javascript" src="../lib/md5/md5.js"></script> <script type="text/javascript" src="../lib/rsvp/rsvp-custom.js"></script>
<script type="text/javascript" src="../lib/jsSha2/sha2.js"></script> <script type="text/javascript" src="../complex_queries.js"></script>
<script type="text/javascript" src="../lib/sjcl/sjcl.min.js"></script>
<script type="text/javascript" src="../jio.min.js"></script>
<script type="text/javascript" src="../complex_queries.min.js"></script>
<script type="text/javascript" <script type="text/javascript"
src="http://code.jquery.com/jquery-1.8.2.min.js"></script> src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script type="text/javascript"> <script type="text/javascript">
<!-- <!--
var searchTextToJson = function () { function searchTextToJson() {
$("#obj").attr("value", JSON.stringify(complex_queries.Query.parseStringToObject($("#str").attr("value")))); $("#obj").attr(
}; "value",
var jsonToSearchText = function () { JSON.stringify(
$("#str").attr("value", complex_queries.QueryFactory.create(JSON.parse($("#obj").attr("value"))).toString()); complex_queries.QueryFactory.create(
}; $("#str").attr("value")
var query = function () { ).serialized()
)
);
}
function jsonToSearchText() {
$("#str").attr(
"value",
complex_queries.QueryFactory.create(
JSON.parse(
$("#obj").attr("value")
)
).toString()
);
}
function query() {
var list = JSON.parse($("#list").attr("value")); var list = JSON.parse($("#list").attr("value"));
$("#str").attr("value", complex_queries.QueryFactory.create(JSON.parse($("#obj").attr("value"))).exec( complex_queries.QueryFactory.create(
JSON.parse(
$("#obj").attr("value")
)
).exec(
list, list,
{ {
"wildcard_character": $('#wildcard').attr('value'), "wildcard_character": $('#wildcard').attr('value'),
...@@ -67,9 +82,10 @@ var query = function () { ...@@ -67,9 +82,10 @@ var query = function () {
"limit": JSON.parse($("#limit").attr("value")), "limit": JSON.parse($("#limit").attr("value")),
"select_list": JSON.parse($("#select_list").attr("value")) "select_list": JSON.parse($("#select_list").attr("value"))
} }
)); ).then(function (list) {
$("#result").attr("value", JSON.stringify(list)); $("#result").attr("value", JSON.stringify(list));
}; });
}
// --> // -->
</script> </script>
</body> </body>
......
...@@ -17,6 +17,7 @@ var log = function (o) { ...@@ -17,6 +17,7 @@ var log = function (o) {
<div id="log"> <div id="log">
</div> </div>
<script src="../src/sha256.amd.js"></script> <script src="../src/sha256.amd.js"></script>
<script src="../lib/rsvp/rsvp-custom.js"></script>
<script src="../jio.js"></script> <script src="../jio.js"></script>
<script src="../complex_queries.js"></script> <script src="../complex_queries.js"></script>
<script src="../src/jio.storage/localstorage.js"></script> <script src="../src/jio.storage/localstorage.js"></script>
...@@ -34,6 +35,11 @@ jio_instance = jIO.createJIO({ ...@@ -34,6 +35,11 @@ jio_instance = jIO.createJIO({
}); });
// Careful! These are asynchronous methods! // Careful! These are asynchronous methods!
// And they use promises with a custom version of RSVP.js
// For more information, see
// RSVP https://github.com/tildeio/rsvp.js#rsvpjs--
// Promises A+ http://promisesaplus.com/
// CommonJS http://wiki.commonjs.org/wiki/Promises
log('-> post "video" document metadata to localStorage'); log('-> post "video" document metadata to localStorage');
jio_instance.post({ jio_instance.post({
...@@ -86,11 +92,11 @@ jio_instance.post({ ...@@ -86,11 +92,11 @@ jio_instance.post({
}).then(function (response) { }).then(function (response) {
return jIO.util.blobAsBinaryString(response.data); return jIO.util.readBlobAsBinaryString(response.data);
}).then(function (binary_string) { }).then(function (event) {
log('Video content is: ' + binary_string); log('Video content is: ' + event.target.result);
log('-> remove "video" document from localStorage'); log('-> remove "video" document from localStorage');
return jio_instance.remove({"_id":'video'}); return jio_instance.remove({"_id":'video'});
......
...@@ -197,6 +197,24 @@ ...@@ -197,6 +197,24 @@
*/ */
that._database = spec.database || []; that._database = spec.database || [];
/**
* True if it has been modified
*
* @property modified
* @type Boolean
* @default false
*/
that.modified = false;
/**
* Updates the modified date
*
* @method touch
*/
that.touch = function () {
that.modified = true;
};
/** /**
* Adds a metadata object in the database, replace if already exist * Adds a metadata object in the database, replace if already exist
* *
...@@ -232,10 +250,11 @@ ...@@ -232,10 +250,11 @@
that._database.push(needed_meta); that._database.push(needed_meta);
that._location[meta._id] = that._database.length - 1; that._location[meta._id] = that._database.length - 1;
} }
that.modified = true;
return true; return true;
} }
if (typeof that._location[meta._id] === "number") { if (typeof that._location[meta._id] === "number") {
that.remove(meta); return that.remove(meta);
} }
return false; return false;
}; };
...@@ -245,6 +264,7 @@ ...@@ -245,6 +264,7 @@
* *
* @method remove * @method remove
* @param {Object} meta The metadata to remove * @param {Object} meta The metadata to remove
* @return {Boolean} true if removed else false
*/ */
that.remove = function (meta) { that.remove = function (meta) {
if (typeof meta._id !== "string") { if (typeof meta._id !== "string") {
...@@ -252,11 +272,13 @@ ...@@ -252,11 +272,13 @@
} }
if (typeof that._location[meta._id] !== "number") { if (typeof that._location[meta._id] !== "number") {
// throw new ReferenceError("Not Found"); // throw new ReferenceError("Not Found");
return; return false;
} }
that._database[that._location[meta._id]] = null; that._database[that._location[meta._id]] = null;
that._free.push(that._location[meta._id]); that._free.push(that._location[meta._id]);
delete that._location[meta._id]; delete that._location[meta._id];
that.modified = true;
return true;
}; };
/** /**
...@@ -354,6 +376,8 @@ ...@@ -354,6 +376,8 @@
that._database.splice(i, 1); that._database.splice(i, 1);
} }
} }
that.modified = true;
return true;
}; };
/** /**
...@@ -454,11 +478,13 @@ ...@@ -454,11 +478,13 @@
IndexStorage.prototype.getIndexDatabase = function (command, index) { IndexStorage.prototype.getIndexDatabase = function (command, index) {
index = this._indices[index]; index = this._indices[index];
function makeNewIndex() { function makeNewIndex() {
return new JSONIndex({ var json_index = new JSONIndex({
"_id": index.id, "_id": index.id,
"_attachment": index.attachment || "body", "_attachment": index.attachment || "body",
"indexing": index.index "indexing": index.index
}); });
json_index.touch();
return json_index;
} }
return command.storage( return command.storage(
index.sub_storage || this._sub_storage index.sub_storage || this._sub_storage
...@@ -497,6 +523,9 @@ ...@@ -497,6 +523,9 @@
IndexStorage.prototype.storeIndexDatabase = function (command, database, IndexStorage.prototype.storeIndexDatabase = function (command, database,
index) { index) {
var that = this; var that = this;
if (!database.modified) {
return RSVP.resolve({"result": "success"});
}
index = this._indices[index]; index = this._indices[index];
function putAttachment() { function putAttachment() {
return command.storage( return command.storage(
......
...@@ -364,7 +364,7 @@ ...@@ -364,7 +364,7 @@
*/ */
LocalStorage.prototype.removeAttachment = function (command, param) { LocalStorage.prototype.removeAttachment = function (command, param) {
var doc = this._storage.getItem(this._localpath + "/" + param._id); var doc = this._storage.getItem(this._localpath + "/" + param._id);
if (typeof doc !== 'object') { if (typeof doc !== 'object' || doc === null) {
return command.error( return command.error(
"not_found", "not_found",
"missing document", "missing document",
...@@ -539,7 +539,7 @@ ...@@ -539,7 +539,7 @@
} }
// check document type // check document type
if (typeof doc !== 'object') { if (typeof doc !== 'object' || doc === null) {
// wrong document // wrong document
if (!repair) { if (!repair) {
return {"error": true, "answers": [ return {"error": true, "answers": [
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
*/ */
/*jslint indent:2, maxlen: 80, nomen: true */ /*jslint indent:2, maxlen: 80, nomen: true */
/*global jIO, define */ /*global jIO, define, Blob */
/** /**
* Provides a split storage for JIO. This storage splits data * Provides a split storage for JIO. This storage splits data
...@@ -329,9 +329,9 @@ ...@@ -329,9 +329,9 @@
if (err) { if (err) {
err.message = "Unable to " + method + " document"; err.message = "Unable to " + method + " document";
delete err.index; delete err.index;
return that.error(err); return command.error(err);
} }
that.success({"id": doc_underscores._id}); command.success({"id": doc_underscores._id});
}); });
}; };
...@@ -368,10 +368,10 @@ ...@@ -368,10 +368,10 @@
var i, attachment_list = [], data = param._blob; var i, attachment_list = [], data = param._blob;
for (i = 0; i < priv.storage_list.length; i += 1) { for (i = 0; i < priv.storage_list.length; i += 1) {
attachment_list[i] = jIO.util.deepClone(param); attachment_list[i] = jIO.util.deepClone(param);
attachment_list[i]._data = data.slice( attachment_list[i]._blob = data.slice(
(data.length / priv.storage_list.length) * i, data.size * i / priv.storage_list.length,
(data.length / priv.storage_list.length) * (i + 1), data.size * (i + 1) / priv.storage_list.length,
"application/octet-stream" data.type
); );
} }
priv.send( priv.send(
...@@ -447,7 +447,7 @@ ...@@ -447,7 +447,7 @@
} }
} }
} }
that.success({"data": doc}); command.success({"data": doc});
}); });
}; };
...@@ -462,17 +462,15 @@ ...@@ -462,17 +462,15 @@
err, err,
response response
) { ) {
var i, doc;
if (err) { if (err) {
err.message = "Unable to get attachment"; err.message = "Unable to get attachment";
delete err.index; delete err.index;
return command.error(err); return command.error(err);
} }
doc = '';
for (i = 0; i < response.length; i += 1) { command.success({"data": new Blob(response.map(function (answer) {
doc += response[i].data; return answer.data;
} }), {"type": response[0].data.type})});
that.success({"data": doc}); // XXX get mimetype
}); });
}; };
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
} }
window.complex_queries = {}; window.complex_queries = {};
module(window.complex_queries, RSVP); module(window.complex_queries, RSVP);
}(['exports'], function (to_export, RSVP) { }(['exports', 'rsvp'], function (to_export, RSVP) {
"use strict"; "use strict";
/** /**
......
...@@ -263,6 +263,43 @@ ...@@ -263,6 +263,43 @@
(/\bPhantomJS\b/i).test(navigator.userAgent)) { (/\bPhantomJS\b/i).test(navigator.userAgent)) {
window.Blob = Blob; window.Blob = Blob;
window.FileReader = FileReader; window.FileReader = FileReader;
//console.warn("Blob and FileReader have been replaced!");
}
if (!Function.prototype.bind) {
//////////////////////////////////////////////////////////////////////
// https://github.com/TristanCavelier/notesntools/blob/master/javascript/\
// bind.js
/**
* Creates a new function that, when called, has its `this` keyword set to
* the provided value, with a given sequence of arguments preceding any
* provided when the new function is called. See Mozilla Developer Network:
* Function.prototype.bind
*
* In PhantomJS, their is a bug with `Function.prototype.bind`. You can
* reproduce this bug by testing this code:
*
* function a(str) { console.log(this, str); }
* var b = a.bind({"a": "b"}, "test");
* b();
*
* @param {Object} thisArg The value to be passed as the `this` parameter to
* the target function when the bound function is called. The value is
* ignored if the bound function is constructed using the `new` operator.
*
* @param {Any} [arg]* Arguments to prepend to arguments provided to the
* bound function when invoking the target function.
*
* @return {Function} The bound function.
*/
Function.prototype.bind = function (thisArg) {
var fun = this, args = [].slice.call(arguments, 1);
return function () {
args.push.apply(args, arguments);
return fun.apply(thisArg, args);
};
};
//console.warn("Function.prototype.bind has been replaced!");
} }
}()); }());
This diff is collapsed.
...@@ -47,5 +47,8 @@ ...@@ -47,5 +47,8 @@
<script src="../src/jio.storage/replicaterevisionstorage.js"></script> <script src="../src/jio.storage/replicaterevisionstorage.js"></script>
<script src="../test/jio.storage/replicaterevisionstorage.tests.js"></script> <script src="../test/jio.storage/replicaterevisionstorage.tests.js"></script>
<script src="../src/jio.storage/splitstorage.js"></script>
<script src="../test/jio.storage/splitstorage.tests.js"></script>
</body> </body>
</html> </html>
...@@ -37,6 +37,9 @@ ...@@ -37,6 +37,9 @@
"replicaterevisionstorage_tests": "replicaterevisionstorage_tests":
"jio.storage/replicaterevisionstorage.tests", "jio.storage/replicaterevisionstorage.tests",
"splitstorage": "../src/jio.storage/splitstorage",
"splitstorage_tests": "jio.storage/splitstorage.tests",
"qunit": "../lib/qunit/qunit", "qunit": "../lib/qunit/qunit",
"sinon": "../lib/sinon/sinon", "sinon": "../lib/sinon/sinon",
"sinon_qunit": "../lib/sinon/sinon-qunit" "sinon_qunit": "../lib/sinon/sinon-qunit"
...@@ -56,6 +59,7 @@ ...@@ -56,6 +59,7 @@
"indexstorage_tests", "indexstorage_tests",
"gidstorage_tests", "gidstorage_tests",
"revisionstorage_tests", "revisionstorage_tests",
"replicaterevisionstorage_tests" "replicaterevisionstorage_tests",
"splitstorage_tests"
]); ]);
}()); }());
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