Commit 637cbbc0 authored by Tristan Cavelier's avatar Tristan Cavelier

Merge remote-tracking branch 'origin/master' into gidstorage

parents a97df9e9 3c383627
...@@ -741,6 +741,33 @@ function stringEscapeRegexpCharacters(string) { ...@@ -741,6 +741,33 @@ function stringEscapeRegexpCharacters(string) {
_export("stringEscapeRegexpCharacters", stringEscapeRegexpCharacters); _export("stringEscapeRegexpCharacters", stringEscapeRegexpCharacters);
/**
* Convert metadata values to array of strings. ex:
*
* "a" -> ["a"],
* {"content": "a"} -> ["a"]
*
* @param {Any} value The metadata value
* @return {Array} The value in string array format
*/
function metadataValueToStringArray(value) {
var i, new_value = [];
if (value === undefined) {
return undefined;
}
if (!Array.isArray(value)) {
value = [value];
}
for (i = 0; i < value.length; i += 1) {
if (typeof value[i] === 'object') {
new_value[i] = value[i].content;
} else {
new_value[i] = value[i];
}
}
return new_value;
}
/** /**
* A sort function to sort items by key * A sort function to sort items by key
* *
...@@ -751,12 +778,50 @@ _export("stringEscapeRegexpCharacters", stringEscapeRegexpCharacters); ...@@ -751,12 +778,50 @@ _export("stringEscapeRegexpCharacters", stringEscapeRegexpCharacters);
function sortFunction(key, way) { function sortFunction(key, way) {
if (way === 'descending') { if (way === 'descending') {
return function (a, b) { return function (a, b) {
return a[key] < b[key] ? 1 : a[key] > b[key] ? -1 : 0; // this comparison is 5 times faster than json comparison
var i, l;
a = metadataValueToStringArray(a[key]) || [];
b = metadataValueToStringArray(b[key]) || [];
l = a.length > b.length ? a.length : b.length;
for (i = 0; i < l; i += 1) {
if (a[i] === undefined) {
return 1;
}
if (b[i] === undefined) {
return -1;
}
if (a[i] > b[i]) {
return -1;
}
if (a[i] < b[i]) {
return 1;
}
}
return 0;
}; };
} }
if (way === 'ascending') { if (way === 'ascending') {
return function (a, b) { return function (a, b) {
return a[key] > b[key] ? 1 : a[key] < b[key] ? -1 : 0; // this comparison is 5 times faster than json comparison
var i, l;
a = metadataValueToStringArray(a[key]) || [];
b = metadataValueToStringArray(b[key]) || [];
l = a.length > b.length ? a.length : b.length;
for (i = 0; i < l; i += 1) {
if (a[i] === undefined) {
return -1;
}
if (b[i] === undefined) {
return 1;
}
if (a[i] > b[i]) {
return 1;
}
if (a[i] < b[i]) {
return -1;
}
}
return 0;
}; };
} }
throw new TypeError("complex_queries.sortFunction(): " + throw new TypeError("complex_queries.sortFunction(): " +
...@@ -1250,17 +1315,18 @@ SimpleQuery.prototype["="] = function (object_value, comparison_value, ...@@ -1250,17 +1315,18 @@ SimpleQuery.prototype["="] = function (object_value, comparison_value,
if (comparison_value === undefined) { if (comparison_value === undefined) {
if (value === undefined) { if (value === undefined) {
return true; return true;
} else {
return false;
} }
return false;
} }
if (value === undefined) { if (value === undefined) {
return false; return false;
} }
if (convertStringToRegExp( if (
convertStringToRegExp(
comparison_value.toString(), comparison_value.toString(),
wildcard_character wildcard_character
).test(value.toString())) { ).test(value.toString())
) {
return true; return true;
} }
} }
...@@ -1290,17 +1356,18 @@ SimpleQuery.prototype["!="] = function (object_value, comparison_value, ...@@ -1290,17 +1356,18 @@ SimpleQuery.prototype["!="] = function (object_value, comparison_value,
if (comparison_value === undefined) { if (comparison_value === undefined) {
if (value === undefined) { if (value === undefined) {
return false; return false;
} else {
return true;
} }
return true;
} }
if (value === undefined) { if (value === undefined) {
return true; return true;
} }
if (convertStringToRegExp( if (
convertStringToRegExp(
comparison_value.toString(), comparison_value.toString(),
wildcard_character wildcard_character
).test(value.toString())) { ).test(value.toString())
) {
return false; return false;
} }
} }
......
...@@ -94,17 +94,18 @@ SimpleQuery.prototype["="] = function (object_value, comparison_value, ...@@ -94,17 +94,18 @@ SimpleQuery.prototype["="] = function (object_value, comparison_value,
if (comparison_value === undefined) { if (comparison_value === undefined) {
if (value === undefined) { if (value === undefined) {
return true; return true;
} else {
return false;
} }
return false;
} }
if (value === undefined) { if (value === undefined) {
return false; return false;
} }
if (convertStringToRegExp( if (
convertStringToRegExp(
comparison_value.toString(), comparison_value.toString(),
wildcard_character wildcard_character
).test(value.toString())) { ).test(value.toString())
) {
return true; return true;
} }
} }
...@@ -134,17 +135,18 @@ SimpleQuery.prototype["!="] = function (object_value, comparison_value, ...@@ -134,17 +135,18 @@ SimpleQuery.prototype["!="] = function (object_value, comparison_value,
if (comparison_value === undefined) { if (comparison_value === undefined) {
if (value === undefined) { if (value === undefined) {
return false; return false;
} else {
return true;
} }
return true;
} }
if (value === undefined) { if (value === undefined) {
return true; return true;
} }
if (convertStringToRegExp( if (
convertStringToRegExp(
comparison_value.toString(), comparison_value.toString(),
wildcard_character wildcard_character
).test(value.toString())) { ).test(value.toString())
) {
return false; return false;
} }
} }
......
...@@ -17,6 +17,33 @@ function stringEscapeRegexpCharacters(string) { ...@@ -17,6 +17,33 @@ function stringEscapeRegexpCharacters(string) {
_export("stringEscapeRegexpCharacters", stringEscapeRegexpCharacters); _export("stringEscapeRegexpCharacters", stringEscapeRegexpCharacters);
/**
* Convert metadata values to array of strings. ex:
*
* "a" -> ["a"],
* {"content": "a"} -> ["a"]
*
* @param {Any} value The metadata value
* @return {Array} The value in string array format
*/
function metadataValueToStringArray(value) {
var i, new_value = [];
if (value === undefined) {
return undefined;
}
if (!Array.isArray(value)) {
value = [value];
}
for (i = 0; i < value.length; i += 1) {
if (typeof value[i] === 'object') {
new_value[i] = value[i].content;
} else {
new_value[i] = value[i];
}
}
return new_value;
}
/** /**
* A sort function to sort items by key * A sort function to sort items by key
* *
...@@ -27,12 +54,50 @@ _export("stringEscapeRegexpCharacters", stringEscapeRegexpCharacters); ...@@ -27,12 +54,50 @@ _export("stringEscapeRegexpCharacters", stringEscapeRegexpCharacters);
function sortFunction(key, way) { function sortFunction(key, way) {
if (way === 'descending') { if (way === 'descending') {
return function (a, b) { return function (a, b) {
return a[key] < b[key] ? 1 : a[key] > b[key] ? -1 : 0; // this comparison is 5 times faster than json comparison
var i, l;
a = metadataValueToStringArray(a[key]) || [];
b = metadataValueToStringArray(b[key]) || [];
l = a.length > b.length ? a.length : b.length;
for (i = 0; i < l; i += 1) {
if (a[i] === undefined) {
return 1;
}
if (b[i] === undefined) {
return -1;
}
if (a[i] > b[i]) {
return -1;
}
if (a[i] < b[i]) {
return 1;
}
}
return 0;
}; };
} }
if (way === 'ascending') { if (way === 'ascending') {
return function (a, b) { return function (a, b) {
return a[key] > b[key] ? 1 : a[key] < b[key] ? -1 : 0; // this comparison is 5 times faster than json comparison
var i, l;
a = metadataValueToStringArray(a[key]) || [];
b = metadataValueToStringArray(b[key]) || [];
l = a.length > b.length ? a.length : b.length;
for (i = 0; i < l; i += 1) {
if (a[i] === undefined) {
return -1;
}
if (b[i] === undefined) {
return 1;
}
if (a[i] > b[i]) {
return 1;
}
if (a[i] < b[i]) {
return -1;
}
}
return 0;
}; };
} }
throw new TypeError("complex_queries.sortFunction(): " + throw new TypeError("complex_queries.sortFunction(): " +
......
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