Join.js
function buildHashTableIndices (bin, leftValue, index, accessors, rightKeys) {
            var i,
                length = accessors.length,
                lastBin,
                val;

            for (i = 0; i < length; i += 1) {
                val = accessors[i](leftValue);
                if (typeof val === "undefined") {
                    return;
                }

                if (i + 1 === length) {
                    if (Array.isArray(val)) {
                        //for each value in val, put that key in
                        val.forEach(function (subValue) {
                            var boot = putKeyInBin(bin, subValue);
                            bin[subValue] = pushOrPut(boot, index);
                            bin[subValue].$val = subValue;
                        });

                    } else {
                        bin[val] = pushOrPut(bin[val], index);
                        bin[val].$val = val;
                    }
                } else if (Array.isArray(val)) {
                    lastBin = bin;
                    val.forEach(function (subDocumentValue) {//sub vals are for basically supposed to be payment ids
                        var rightKeySubset = rightKeys.slice(i + 1);

                        if (isNullOrUndefined(bin[subDocumentValue])) {
                            bin[subDocumentValue] = {$val: subDocumentValue};//Store $val to maintain the data type
                        }

                        buildHashTableIndices(bin[subDocumentValue], leftValue, index, accessors.slice(i + 1), rightKeySubset);
                    });
                    return;//don't go through the rest of the accessors, this recursion will take care of those
                } else {
                    bin = putKeyInBin(bin, val);
                }
            }
        }
function buildHashTableIndices (bin, leftValue, index, accessors, rightKeys) {
            var i,
                length = accessors.length,
                lastBin,
                val;

            for (i = 0; i < length; i += 1) {
                val = accessors[i](leftValue);
                if (typeof val === "undefined") {
                    return;
                }

                if (i + 1 === length) {
                    if (Array.isArray(val)) {
                        //for each value in val, put that key in
                        val.forEach(function (subValue) {
                            var boot = putKeyInBin(bin, subValue);
                            bin[subValue] = pushOrPut(boot, index);
                            bin[subValue].$val = subValue;
                        });

                    } else {
                        bin[val] = pushOrPut(bin[val], index);
                        bin[val].$val = val;
                    }
                } else if (Array.isArray(val)) {
                    lastBin = bin;
                    val.forEach(function (subDocumentValue) {//sub vals are for basically supposed to be payment ids
                        var rightKeySubset = rightKeys.slice(i + 1);

                        if (isNullOrUndefined(bin[subDocumentValue])) {
                            bin[subDocumentValue] = {$val: subDocumentValue};//Store $val to maintain the data type
                        }

                        buildHashTableIndices(bin[subDocumentValue], leftValue, index, accessors.slice(i + 1), rightKeySubset);
                    });
                    return;//don't go through the rest of the accessors, this recursion will take care of those
                } else {
                    bin = putKeyInBin(bin, val);
                }
            }
        }
function buildHashTableIndices ( bin , leftValue , index , accessors , rightKeys ) { var i , length = accessors . length , lastBin , val ; for ( i = 0 ; i < length ; i += 1 ) { val = accessors [ i ] ( leftValue ) ; if ( typeof val === "undefined" ) { return ; } if ( i + 1 === length ) { if ( Array . isArray ( val ) ) { //for each value in val, put that key in val . forEach ( function ( subValue ) { var boot = putKeyInBin ( bin , subValue ) ; bin [ subValue ] = pushOrPut ( boot , index ) ; bin [ subValue ] . $val = subValue ; } ) ; } else { bin [ val ] = pushOrPut ( bin [ val ] , index ) ; bin [ val ] . $val = val ; } } else if ( Array . isArray ( val ) ) { lastBin = bin ; val . forEach ( function ( subDocumentValue ) { //sub vals are for basically supposed to be payment ids var rightKeySubset = rightKeys . slice ( i + 1 ) ; if ( isNullOrUndefined ( bin [ subDocumentValue ] ) ) { bin [ subDocumentValue ] = { $val : subDocumentValue } ; //Store $val to maintain the data type } buildHashTableIndices ( bin [ subDocumentValue ] , leftValue , index , accessors . slice ( i + 1 ) , rightKeySubset ) ; } ) ; return ; //don't go through the rest of the accessors, this recursion will take care of those } else { bin = putKeyInBin ( bin , val ) ; } } }
Create the hash table which maps unique left key combination to an array of numbers representing the indexes of the
source data where those unique left key combinations were found. Use this map in the joining process to add
join subdocuments to the source data.
@param bin The bin object into which keys and indexes will be put
@param leftValue the left document from which to retrieve key values
@param index the index in the source data that the leftvalue lives at
@param accessors The accessor functions which retrieve the value for each join key from the leftValue.
@param rightKeys The keys in the right hand set which are being joined on
Create the hash table which maps unique left key combination to an array of numbers representing the indexes of the source data where those unique left key combinations were found . Use this map in the joining process to add join subdocuments to the source data .