jstests/change_streams/oplog_rewrite/change_stream_null_existence_eq_rewrite_test.js
// Function to generate a list of all paths to be tested from those observed in the event stream.
Function to generate a list of all paths to be tested from those observed in the event stream.
Function to generate a list of all paths to be tested from those observed in the event stream .
function traverseEvent(event, outputMap, prefixPath = "") {
    const maxValuesPerPath = 1;
    for (let fieldName in event) {
        const fieldPath = (prefixPath.length > 0 ? prefixPath + "." : "") + fieldName;
        const fieldVal = event[fieldName];
        if (!outputMap[fieldPath]) {
            outputMap[fieldPath] = {extraValues: [], values: []};
        }
        for (let subField of standardSubFieldsToTest) {
            const subFieldPathToAdd = fieldPath + "." + subField;
            if (!outputMap[subFieldPathToAdd]) {
                outputMap[subFieldPathToAdd] = {extraValues: [], values: []};
            }
        }
        function addToPredicatesList(fieldPath, fieldVal) {
            const alreadyExists =
                outputMap[fieldPath].values.some((elem) => friendlyEqual(elem, fieldVal));
            const numValues = outputMap[fieldPath].values.length;
            if (!alreadyExists && numValues < maxValuesPerPath) {
                outputMap[fieldPath].values.push(fieldVal);
            }
        }
        addToPredicatesList(fieldPath, fieldVal);
        if (isPlainObject(fieldVal)) {
            traverseEvent(fieldVal, outputMap, fieldPath);
        }
        if (Array.isArray(fieldVal)) {
            for (let arrayElem of fieldVal) {
                if (isPlainObject(arrayElem)) {
                    traverseEvent(arrayElem, outputMap, fieldPath);
                } else {
                    addToPredicatesList(fieldPath, arrayElem);
                }
            }
            traverseEvent(fieldVal, outputMap, fieldPath);
        }
    }
}
function traverseEvent ( event , outputMap , prefixPath = "" ) { const maxValuesPerPath = 1 ; for ( let fieldName in event ) { const fieldPath = ( prefixPath . length > 0 ? prefixPath + "." : "" ) + fieldName ; const fieldVal = event [ fieldName ] ; if ( ! outputMap [ fieldPath ] ) { outputMap [ fieldPath ] = { extraValues : [ ] , values : [ ] } ; } for ( let subField of standardSubFieldsToTest ) { const subFieldPathToAdd = fieldPath + "." + subField ; if ( ! outputMap [ subFieldPathToAdd ] ) { outputMap [ subFieldPathToAdd ] = { extraValues : [ ] , values : [ ] } ; } } function addToPredicatesList ( fieldPath , fieldVal ) { const alreadyExists = outputMap [ fieldPath ] . values . some ( ( elem ) => friendlyEqual ( elem , fieldVal ) ) ; const numValues = outputMap [ fieldPath ] . values . length ; if ( ! alreadyExists && numValues < maxValuesPerPath ) { outputMap [ fieldPath ] . values . push ( fieldVal ) ; } } addToPredicatesList ( fieldPath , fieldVal ) ; if ( isPlainObject ( fieldVal ) ) { traverseEvent ( fieldVal , outputMap , fieldPath ) ; } if ( Array . isArray ( fieldVal ) ) { for ( let arrayElem of fieldVal ) { if ( isPlainObject ( arrayElem ) ) { traverseEvent ( arrayElem , outputMap , fieldPath ) ; } else { addToPredicatesList ( fieldPath , arrayElem ) ; } } traverseEvent ( fieldVal , outputMap , fieldPath ) ; } } }
Function to generate a list of all paths to be tested from those observed in the event stream.
Function to generate a list of all paths to be tested from those observed in the event stream .
// The number of values to equality-test for each path. Set this to Infinity to test everything. // Begin traversing through the event, adding paths and values into 'outputMap'. // Create an entry for this field if it doesn't already exist. // Add entries for each of the standard subfields that we test for every existent field. // Helper function to add a new value into the fields list. // Add a predicate on the full field, whether scalar, object, or array. // If the field is an object, traverse through it. // If the field is an array, find any subobjects and traverse them. // Traverse through the array itself as an object. This will descend into the array by // index, allowing us to test fieldname-or-array-index matching semantics.