private static int compareBsonValue()

in flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mongodb-cdc/src/main/java/org/apache/flink/cdc/connectors/mongodb/source/utils/BsonUtils.java [47:106]


    private static int compareBsonValue(BsonValue o1, BsonValue o2, boolean isTopLevel) {
        if (isTopLevel) {
            BsonValue element1 = o1;
            BsonValue element2 = o2;
            // With top level comparison, we should extract the smallest value from the array
            if (isArray(o1)) {
                element1 = smallestValueOfArray(o1.asArray());
            }
            if (isArray(o2)) {
                element2 = smallestValueOfArray(o2.asArray());
            }
            return compareBsonValue(element1, element2, false);
        }

        // Different type: respect to type order
        if (typeOrder(o1) != typeOrder(o2)) {
            return Integer.compare(typeOrder(o1), typeOrder(o2));
        }

        if (isNull(o1) || isMinKey(o1) || isMaxKey(o1)) {
            return 0; // Null == Null, MinKey == MinKey, MaxKey == MaxKey
        }
        if (isBsonNumber(o1)) {
            return toDecimal128(o1).compareTo(toDecimal128(o2)); // Number compare
        }
        if (o1.isString() || o1.isSymbol()) {
            return toJavaString(o1).compareTo(toJavaString(o2)); // String compare
        }
        if (o1.isDocument() || o1.isDBPointer()) { // Object compare
            return compareBsonDocument(toBsonDocument(o1), toBsonDocument(o2));
        }
        if (o1.isArray()) {
            return compareBsonArray(o1.asArray(), o2.asArray()); // Array compare
        }
        if (o1.isBinary()) {
            return compareBsonBinary(o1.asBinary(), o2.asBinary()); // Binary compare
        }
        if (o1.isObjectId()) {
            return o1.asObjectId().compareTo(o2.asObjectId()); // ObjectId compare
        }
        if (o1.isBoolean()) {
            return o1.asBoolean().compareTo(o2.asBoolean()); // Booleans compare
        }
        if (o1.isDateTime()) {
            return o1.asDateTime().compareTo(o2.asDateTime()); // DateTime compare
        }
        if (o1.isTimestamp()) {
            return o1.asTimestamp().compareTo(o2.asTimestamp()); // Timestamp compare
        }
        if (o1.isRegularExpression() || o1.isJavaScript()) {
            return toJavaString(o1).compareTo(toJavaString(o2)); // String compare
        }
        if (o1.isJavaScriptWithScope()) {
            return compareJavascriptWithScope(
                    o1.asJavaScriptWithScope(), o2.asJavaScriptWithScope()); // Code compare
        }

        throw new IllegalArgumentException(
                String.format("Unable to compare bson values between %s and %s", o1, o2));
    }