Search in sources :

Example 1 with BsonType

use of com.eightkdata.mongowp.bson.BsonType in project torodb by torodb.

the class ReplicaSetConfig method parseCustomWriteConcerns.

private static Map<String, ReplicaSetTagPattern> parseCustomWriteConcerns(BsonDocument bson) throws TypesMismatchException, NoSuchKeyException, BadValueException {
    Map<String, ReplicaSetTagPattern> map = new HashMap<>(bson.size());
    for (Entry<?> customWriteNameEntry : bson) {
        BsonDocument constraintDoc = BsonReaderTool.getDocument(bson, customWriteNameEntry.getKey());
        Map<String, Integer> constraintMap = new HashMap<>(constraintDoc.size());
        for (Entry<?> tagEntry : constraintDoc) {
            int intValue;
            try {
                intValue = tagEntry.getValue().asNumber().intValue();
            } catch (UnsupportedOperationException ex) {
                String fieldName = SETTINGS_FIELD.getFieldName() + '.' + GET_LAST_ERROR_MODES_FIELD.getFieldName() + '.' + customWriteNameEntry + '.' + constraintDoc;
                BsonType tagType = tagEntry.getValue().getType();
                throw new TypesMismatchException(fieldName, "number", tagType, "Expected " + fieldName + " to be a number, not " + tagType.toString().toLowerCase(Locale.ROOT));
            }
            if (intValue <= 0) {
                String fieldName = SETTINGS_FIELD.getFieldName() + '.' + GET_LAST_ERROR_MODES_FIELD.getFieldName() + '.' + customWriteNameEntry + '.' + constraintDoc;
                throw new BadValueException("Value of " + fieldName + " must be positive, but found " + intValue);
            }
            constraintMap.put(tagEntry.getKey(), intValue);
        }
        map.put(customWriteNameEntry.getKey(), new ReplicaSetTagPattern(constraintMap));
    }
    return map;
}
Also used : BadValueException(com.eightkdata.mongowp.exceptions.BadValueException) HashMap(java.util.HashMap) BsonType(com.eightkdata.mongowp.bson.BsonType) BsonDocument(com.eightkdata.mongowp.bson.BsonDocument) TypesMismatchException(com.eightkdata.mongowp.exceptions.TypesMismatchException)

Aggregations

BsonDocument (com.eightkdata.mongowp.bson.BsonDocument)1 BsonType (com.eightkdata.mongowp.bson.BsonType)1 BadValueException (com.eightkdata.mongowp.exceptions.BadValueException)1 TypesMismatchException (com.eightkdata.mongowp.exceptions.TypesMismatchException)1 HashMap (java.util.HashMap)1