home-written map extended with proper .set() method

This commit is contained in:
Wert_Lex 2015-10-10 21:00:00 +05:00
parent a5dd9c181a
commit 1cab0164c2
2 changed files with 28 additions and 6 deletions

View File

@ -495,8 +495,10 @@ Inclusion.include = function (objects, include, options, cb) {
function includeHasManySimple(callback) {
//Map for Indexing objects by their id for faster retrieval
var objIdMap2 = includeUtils.buildOneToOneIdentityMapWithOrigKeys(objs, relation.keyFrom);
// all ids of primary objects to use in query
var sourceIds = objIdMap2.originalKeys;//Object.keys(objIdMap);
console.log("### objIdMap2: " + JSON.stringify(objIdMap2));
// all ids of primary objects to use in query are original. So, ObjectIds stay ObjectIds and Ints stay Ints.
// No .toString() conversion.
var sourceIds = objIdMap2.originalKeys;
filter.where[relation.keyTo] = {
inq: uniq(sourceIds)

View File

@ -32,6 +32,14 @@ function buildOneToOneIdentityMap(objs, idName) {
function newIdMap(origKeyField, valueField) {
//var idMap = Object.create(null); // not any single properties within our identity map
var idMap = {};
Object.defineProperty(idMap, "set", {
value: function(origKey, value){
var key = origKey.toString();
this[key] = {};
this[key][origKeyField] = origKey;
this[key][valueField] = value;
}
});
Object.defineProperty(idMap, "keys", { // can ask for keys simply by idMap.keys
get: function(){ return Object.keys(this); },
enumerable: false // explicitly non-enumerable
@ -75,10 +83,7 @@ function buildOneToOneIdentityMapWithOrigKeys(objs, idName) {
for(var i = 0; i < objs.length; i++) {
var obj = objs[i];
var id = obj[idName];
idMap[id.toString()] = {
originalKey: id,
value: obj
};
idMap.set(id, obj);
}
return idMap;
}
@ -103,6 +108,21 @@ function buildOneToManyIdentityMap(objs, idName) {
return idMap;
}
function buildOneToManyIdentityMapWithOrigKeys(objs, idName) {
var idMap = newIdMap("originalKey", "value");
for(var i = 0; i < objs.length; i++) {
var obj = objs[i];
var id = obj[idName];
var idString = id.toString();
if(idString in idMap) {
idMap[idString]["value"].push(obj);
} else {
idMap.set(id, [obj]);
}
}
return idMap;
}
/**
* Yeah, it joins. You need three things id -> obj1 map, id -> [obj2] map and merge function.