Search in sources :

Example 6 with TypesMismatchException

use of com.eightkdata.mongowp.exceptions.TypesMismatchException 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)

Example 7 with TypesMismatchException

use of com.eightkdata.mongowp.exceptions.TypesMismatchException in project torodb by torodb.

the class ReplSetHeartbeatReplyMarshaller method parseAppliedOpTime.

private static void parseAppliedOpTime(BsonDocument bson, ReplSetHeartbeatReplyBuilder builder, long term) throws TypesMismatchException, NoSuchKeyException {
    // In order to support both the 3.0(V0) and 3.2(V1) heartbeats we must parse the OpTime
    // field based on its type. If it is a Date, we parse it as the timestamp and use
    // initialize's term argument to complete the OpTime type. If it is an Object, then it's
    // V1 and we construct an OpTime out of its nested fields.
    Entry<?> entry = BsonReaderTool.getEntry(bson, APPLIED_OP_TIME_FIELD_NAME, null);
    if (entry == null) {
        return;
    }
    BsonValue<?> value = entry.getValue();
    OpTime opTime;
    switch(value.getType()) {
        case TIMESTAMP:
            opTime = new OpTime(value.asTimestamp(), term);
            break;
        case DATETIME:
            BsonTimestamp ts = BsonReaderTool.getTimestampFromDateTime(entry);
            opTime = new OpTime(ts, term);
            break;
        case //repl v1
        DOCUMENT:
            opTime = OpTime.fromBson(value.asDocument());
            builder.setIsReplSet(true);
            break;
        default:
            throw new TypesMismatchException(APPLIED_OP_TIME_FIELD_NAME, "Date or Timestamp", value.getType());
    }
    builder.setAppliedOpTime(opTime);
}
Also used : TypesMismatchException(com.eightkdata.mongowp.exceptions.TypesMismatchException) OpTime(com.eightkdata.mongowp.OpTime) BsonTimestamp(com.eightkdata.mongowp.bson.BsonTimestamp)

Aggregations

TypesMismatchException (com.eightkdata.mongowp.exceptions.TypesMismatchException)7 BsonDocument (com.eightkdata.mongowp.bson.BsonDocument)6 BadValueException (com.eightkdata.mongowp.exceptions.BadValueException)4 NoSuchKeyException (com.eightkdata.mongowp.exceptions.NoSuchKeyException)4 BsonValue (com.eightkdata.mongowp.bson.BsonValue)3 OpTime (com.eightkdata.mongowp.OpTime)2 BsonArray (com.eightkdata.mongowp.bson.BsonArray)2 BsonDocumentBuilder (com.eightkdata.mongowp.utils.BsonDocumentBuilder)2 HashMap (java.util.HashMap)2 WriteConcern (com.eightkdata.mongowp.WriteConcern)1 Entry (com.eightkdata.mongowp.bson.BsonDocument.Entry)1 BsonTimestamp (com.eightkdata.mongowp.bson.BsonTimestamp)1 BsonType (com.eightkdata.mongowp.bson.BsonType)1 MongoConnection (com.eightkdata.mongowp.client.core.MongoConnection)1 OplogOperationUnsupported (com.eightkdata.mongowp.exceptions.OplogOperationUnsupported)1 OplogStartMissingException (com.eightkdata.mongowp.exceptions.OplogStartMissingException)1 QueryOption (com.eightkdata.mongowp.messages.request.QueryMessage.QueryOption)1 QueryOptions (com.eightkdata.mongowp.messages.request.QueryMessage.QueryOptions)1 DbCmdOplogOperation (com.eightkdata.mongowp.server.api.oplog.DbCmdOplogOperation)1 DbOplogOperation (com.eightkdata.mongowp.server.api.oplog.DbOplogOperation)1