1
0
Fork 0

speedup for KEYS()

This commit is contained in:
Jan Steemann 2013-06-13 16:08:11 +02:00
parent e7975b5273
commit 7d101890c7
1 changed files with 87 additions and 118 deletions

View File

@ -198,47 +198,6 @@ function COLLECTION (name) {
return INTERNAL.db[name]; return INTERNAL.db[name];
} }
////////////////////////////////////////////////////////////////////////////////
/// @brief normalize a value for comparison, sorting etc.
////////////////////////////////////////////////////////////////////////////////
function NORMALIZE (value) {
"use strict";
if (value === null || value === undefined) {
return null;
}
if (typeof(value) !== "object") {
return value;
}
var result;
if (Array.isArray(value)) {
result = [ ];
value.forEach(function (v) {
result.push(NORMALIZE(v));
});
}
else {
var attributes = [ ], a;
for (a in value) {
if (value.hasOwnProperty(a)) {
attributes.push(a);
}
}
attributes.sort();
result = { };
attributes.forEach(function (a) {
result[a] = NORMALIZE(value[a]);
});
}
return result;
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
/// @brief clone an object /// @brief clone an object
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -526,21 +485,18 @@ function EXTRACT_KEYS (args, startArgument, functionName) {
function KEYS (value, doSort) { function KEYS (value, doSort) {
"use strict"; "use strict";
var keys = [ ]; var keys;
if (Array.isArray(value)) { if (Array.isArray(value)) {
var n = value.length, i; var n = value.length, i;
keys = [ ];
for (i = 0; i < n; ++i) { for (i = 0; i < n; ++i) {
keys.push(i); keys.push(i);
} }
} }
else { else {
var a; keys = Object.keys(value);
for (a in value) {
if (value.hasOwnProperty(a)) {
keys.push(a);
}
}
if (doSort) { if (doSort) {
// object keys need to be sorted by names // object keys need to be sorted by names
@ -558,30 +514,13 @@ function KEYS (value, doSort) {
function KEYLIST (lhs, rhs) { function KEYLIST (lhs, rhs) {
"use strict"; "use strict";
var keys = [ ];
if (Array.isArray(lhs)) { if (Array.isArray(lhs)) {
// lhs & rhs are lists // lhs & rhs are lists
var i, n; return KEYS(lhs.length > rhs.length ? lhs : rhs);
if (lhs.length > rhs.length) {
n = lhs.length;
}
else {
n = rhs.length;
}
for (i = 0; i < n; ++i) {
keys.push(i);
}
return keys;
} }
// lhs & rhs are arrays // lhs & rhs are arrays
var a; var a, keys = KEYS(lhs);
for (a in lhs) {
if (lhs.hasOwnProperty(a)) {
keys.push(a);
}
}
for (a in rhs) { for (a in rhs) {
if (rhs.hasOwnProperty(a) && ! lhs.hasOwnProperty(a)) { if (rhs.hasOwnProperty(a) && ! lhs.hasOwnProperty(a)) {
keys.push(a); keys.push(a);
@ -630,6 +569,39 @@ function GET_INDEX (value, index) {
return result; return result;
} }
////////////////////////////////////////////////////////////////////////////////
/// @brief normalize a value for comparison, sorting etc.
////////////////////////////////////////////////////////////////////////////////
function NORMALIZE (value) {
"use strict";
if (value === null || value === undefined) {
return null;
}
if (typeof(value) !== "object") {
return value;
}
var result;
if (Array.isArray(value)) {
result = [ ];
value.forEach(function (v) {
result.push(NORMALIZE(v));
});
}
else {
result = { };
KEYS(value, true).forEach(function (a) {
result[a] = NORMALIZE(value[a]);
});
}
return result;
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
/// @brief get an attribute from a document (e.g. users.name) /// @brief get an attribute from a document (e.g. users.name)
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -836,12 +808,11 @@ function GET_DOCUMENTS_HASH_LIST (collection, idx, attribute, values) {
c = COLLECTION(collection); c = COLLECTION(collection);
values.forEach(function (value) { values.forEach(function (value) {
var example = { }, documents; var example = { };
example[attribute] = value; example[attribute] = value;
documents = c.BY_EXAMPLE_HASH(idx, example).documents; c.BY_EXAMPLE_HASH(idx, example).documents.forEach(function (doc) {
documents.forEach(function (doc) {
result.push(doc); result.push(doc);
}); });
}); });
@ -967,9 +938,7 @@ function GET_DOCUMENTS_SKIPLIST (collection, idx, example) {
function GET_DOCUMENTS_SKIPLIST_LIST (collection, idx, attribute, values) { function GET_DOCUMENTS_SKIPLIST_LIST (collection, idx, attribute, values) {
"use strict"; "use strict";
var result = [ ], c; var result = [ ], c = COLLECTION(collection);
c = COLLECTION(collection);
values.forEach(function (value) { values.forEach(function (value) {
var example = { }, documents; var example = { }, documents;
@ -991,10 +960,13 @@ function GET_DOCUMENTS_SKIPLIST_LIST (collection, idx, attribute, values) {
function COLLECTIONS () { function COLLECTIONS () {
"use strict"; "use strict";
var result = [ ], collections = INTERNAL.db._collections(); var result = [ ];
collections.forEach(function (c) { INTERNAL.db._collections().forEach(function (c) {
result.push({ "_id" : c._id, "name" : c.name() }); result.push({
_id : c._id,
name : c.name()
});
}); });
return result; return result;
@ -1126,9 +1098,9 @@ function RELATIONAL_EQUAL (lhs, rhs) {
if (leftWeight >= TYPEWEIGHT_LIST) { if (leftWeight >= TYPEWEIGHT_LIST) {
// arrays and objects // arrays and objects
var keys = KEYLIST(lhs, rhs), key, i; var keys = KEYLIST(lhs, rhs), i, n = keys.length;
for (i = 0; i < keys.length; ++i) { for (i = 0; i < n; ++i) {
key = keys[i]; var key = keys[i];
if (RELATIONAL_EQUAL(lhs[key], rhs[key]) === false) { if (RELATIONAL_EQUAL(lhs[key], rhs[key]) === false) {
return false; return false;
} }
@ -1171,9 +1143,9 @@ function RELATIONAL_UNEQUAL (lhs, rhs) {
if (leftWeight >= TYPEWEIGHT_LIST) { if (leftWeight >= TYPEWEIGHT_LIST) {
// arrays and objects // arrays and objects
var keys = KEYLIST(lhs, rhs), key, i; var keys = KEYLIST(lhs, rhs), i, n = keys.length;
for (i = 0; i < keys.length; ++i) { for (i = 0; i < n; ++i) {
key = keys[i]; var key = keys[i];
if (RELATIONAL_UNEQUAL(lhs[key], rhs[key]) === true) { if (RELATIONAL_UNEQUAL(lhs[key], rhs[key]) === true) {
return true; return true;
} }
@ -1218,10 +1190,9 @@ function RELATIONAL_GREATER_REC (lhs, rhs) {
if (leftWeight >= TYPEWEIGHT_LIST) { if (leftWeight >= TYPEWEIGHT_LIST) {
// arrays and objects // arrays and objects
var keys = KEYLIST(lhs, rhs), key, i, result; var keys = KEYLIST(lhs, rhs), i, n = keys.length;
for (i = 0; i < keys.length; ++i) { for (i = 0; i < n; ++i) {
key = keys[i]; var key = keys[i], result = RELATIONAL_GREATER_REC(lhs[key], rhs[key]);
result = RELATIONAL_GREATER_REC(lhs[key], rhs[key]);
if (result !== null) { if (result !== null) {
return result; return result;
} }
@ -1288,10 +1259,9 @@ function RELATIONAL_GREATEREQUAL_REC (lhs, rhs) {
if (leftWeight >= TYPEWEIGHT_LIST) { if (leftWeight >= TYPEWEIGHT_LIST) {
// arrays and objects // arrays and objects
var keys = KEYLIST(lhs, rhs), key, i, result; var keys = KEYLIST(lhs, rhs), i, n = keys.length;
for (i = 0; i < keys.length; ++i) { for (i = 0; i < n; ++i) {
key = keys[i]; var key = keys[i], result = RELATIONAL_GREATEREQUAL_REC(lhs[key], rhs[key]);
result = RELATIONAL_GREATEREQUAL_REC(lhs[key], rhs[key]);
if (result !== null) { if (result !== null) {
return result; return result;
} }
@ -1358,10 +1328,9 @@ function RELATIONAL_LESS_REC (lhs, rhs) {
if (leftWeight >= TYPEWEIGHT_LIST) { if (leftWeight >= TYPEWEIGHT_LIST) {
// arrays and objects // arrays and objects
var keys = KEYLIST(lhs, rhs), key, i, result; var keys = KEYLIST(lhs, rhs), i, n = keys.length;
for (i = 0; i < keys.length; ++i) { for (i = 0; i < n; ++i) {
key = keys[i]; var key = keys[i], result = RELATIONAL_LESS_REC(lhs[key], rhs[key]);
result = RELATIONAL_LESS_REC(lhs[key], rhs[key]);
if (result !== null) { if (result !== null) {
return result; return result;
} }
@ -1428,10 +1397,9 @@ function RELATIONAL_LESSEQUAL_REC (lhs, rhs) {
if (leftWeight >= TYPEWEIGHT_LIST) { if (leftWeight >= TYPEWEIGHT_LIST) {
// arrays and objects // arrays and objects
var keys = KEYLIST(lhs, rhs), key, i, result; var keys = KEYLIST(lhs, rhs), i, n = keys.length;
for (i = 0; i < keys.length; ++i) { for (i = 0; i < n; ++i) {
key = keys[i]; var key = keys[i], result = RELATIONAL_LESSEQUAL_REC(lhs[key], rhs[key]);
result = RELATIONAL_LESSEQUAL_REC(lhs[key], rhs[key]);
if (result !== null) { if (result !== null) {
return result; return result;
} }
@ -1501,10 +1469,9 @@ function RELATIONAL_CMP (lhs, rhs) {
if (leftWeight >= TYPEWEIGHT_LIST) { if (leftWeight >= TYPEWEIGHT_LIST) {
// arrays and objects // arrays and objects
var keys = KEYLIST(lhs, rhs), key, i, result; var keys = KEYLIST(lhs, rhs), i, n = keys.length;
for (i = 0; i < keys.length; ++i) { for (i = 0; i < n; ++i) {
key = keys[i]; var key = keys[i], result = RELATIONAL_CMP(lhs[key], rhs[key]);
result = RELATIONAL_CMP(lhs[key], rhs[key]);
if (result !== 0) { if (result !== 0) {
return result; return result;
} }
@ -2998,21 +2965,25 @@ function ATTRIBUTES (element, removeInternal, sort) {
THROW(INTERNAL.errors.ERROR_QUERY_FUNCTION_ARGUMENT_TYPE_MISMATCH, "ATTRIBUTES"); THROW(INTERNAL.errors.ERROR_QUERY_FUNCTION_ARGUMENT_TYPE_MISMATCH, "ATTRIBUTES");
} }
var result = [ ], a; if (removeInternal) {
var a, result = [ ];
for (a in element) {
if (element.hasOwnProperty(a)) { for (a in element) {
if (! removeInternal || a.substring(0, 1) !== '_') { if (element.hasOwnProperty(a)) {
result.push(a); if (a.substring(0, 1) !== '_') {
result.push(a);
}
} }
} }
}
if (sort) {
result.sort();
}
if (sort) { return result;
result.sort();
} }
return result; return KEYS(element, sort);
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -3026,9 +2997,7 @@ function UNSET (value) {
THROW(INTERNAL.errors.ERROR_QUERY_FUNCTION_ARGUMENT_TYPE_MISMATCH, "UNSET"); THROW(INTERNAL.errors.ERROR_QUERY_FUNCTION_ARGUMENT_TYPE_MISMATCH, "UNSET");
} }
var keys = EXTRACT_KEYS(arguments, 1, "UNSET"), i; var result = { }, keys = EXTRACT_KEYS(arguments, 1, "UNSET"), i;
var result = { };
// copy over all that is left // copy over all that is left
for (i in value) { for (i in value) {
if (value.hasOwnProperty(i)) { if (value.hasOwnProperty(i)) {
@ -3118,7 +3087,7 @@ function MERGE_RECURSIVE () {
} }
} }
return r; return r;
}; };
for (i in arguments) { for (i in arguments) {
if (arguments.hasOwnProperty(i)) { if (arguments.hasOwnProperty(i)) {