Search in sources :

Example 1 with WriteConcern

use of com.eightkdata.mongowp.WriteConcern in project torodb by torodb.

the class ReplicaSetConfig method fromDocument.

public static ReplicaSetConfig fromDocument(@Nonnull BsonDocument bson) throws BadValueException, TypesMismatchException, NoSuchKeyException, FailedToParseException {
    BsonReaderTool.checkOnlyHasFields("replica set configuration", bson, VALID_FIELD_NAMES);
    String id = BsonReaderTool.getString(bson, ID_FIELD);
    int version = BsonReaderTool.getInteger(bson, VERSION_FIELD);
    Builder builder = new Builder(id, version);
    BsonArray uncastedMembers = BsonReaderTool.getArray(bson, MEMBERS_FIELD);
    int i = 0;
    for (BsonValue uncastedMember : uncastedMembers) {
        if (uncastedMember == null || !uncastedMember.isDocument()) {
            throw new TypesMismatchException(Integer.toString(i), "object", uncastedMember == null ? null : uncastedMember.getType());
        }
        builder.addMemberConfig(MemberConfig.fromDocument(uncastedMember.asDocument()));
        i++;
    }
    BsonDocument settings;
    try {
        settings = BsonReaderTool.getDocument(bson, SETTINGS_FIELD);
    } catch (NoSuchKeyException ex) {
        settings = DefaultBsonValues.EMPTY_DOC;
    }
    builder.setHbTimeout(BsonReaderTool.getInteger(settings, HEARTHBEAT_TIMEOUT_FIELD, DEFAULT_HEARTBEAT_TIMEOUT_MILLIS)).setChainingAllowed(BsonReaderTool.getBoolean(settings, CHAINING_ALLOWED_FIELD, DEFAULT_CHAINING_ALLOWED));
    BsonDocument uncastedGetLastErrorDefaults = BsonReaderTool.getDocument(settings, GET_LAST_ERROR_DEFAULTS_FIELD);
    WriteConcern wc = WriteConcern.fromDocument(uncastedGetLastErrorDefaults);
    builder.setWriteConcern(wc);
    BsonDocument uncastedCustomWriteConcerns;
    try {
        uncastedCustomWriteConcerns = BsonReaderTool.getDocument(settings, GET_LAST_ERROR_MODES_FIELD);
    } catch (NoSuchKeyException ex) {
        uncastedCustomWriteConcerns = DefaultBsonValues.EMPTY_DOC;
    }
    Map<String, ReplicaSetTagPattern> customWriteConcernsBuilder = parseCustomWriteConcerns(uncastedCustomWriteConcerns);
    for (Map.Entry<String, ReplicaSetTagPattern> customWriteConcern : customWriteConcernsBuilder.entrySet()) {
        builder.putCustomWriteConcern(customWriteConcern.getKey(), customWriteConcern.getValue());
    }
    builder.setProtocolVersion(BsonReaderTool.getLong(bson, PROTOCOL_VERSION_FIELD));
    return builder.build();
}
Also used : BsonArrayBuilder(com.eightkdata.mongowp.utils.BsonArrayBuilder) BsonDocumentBuilder(com.eightkdata.mongowp.utils.BsonDocumentBuilder) NoSuchKeyException(com.eightkdata.mongowp.exceptions.NoSuchKeyException) BsonDocument(com.eightkdata.mongowp.bson.BsonDocument) WriteConcern(com.eightkdata.mongowp.WriteConcern) BsonArray(com.eightkdata.mongowp.bson.BsonArray) TypesMismatchException(com.eightkdata.mongowp.exceptions.TypesMismatchException) HashMap(java.util.HashMap) Map(java.util.Map) BsonValue(com.eightkdata.mongowp.bson.BsonValue)

Aggregations

WriteConcern (com.eightkdata.mongowp.WriteConcern)1 BsonArray (com.eightkdata.mongowp.bson.BsonArray)1 BsonDocument (com.eightkdata.mongowp.bson.BsonDocument)1 BsonValue (com.eightkdata.mongowp.bson.BsonValue)1 NoSuchKeyException (com.eightkdata.mongowp.exceptions.NoSuchKeyException)1 TypesMismatchException (com.eightkdata.mongowp.exceptions.TypesMismatchException)1 BsonArrayBuilder (com.eightkdata.mongowp.utils.BsonArrayBuilder)1 BsonDocumentBuilder (com.eightkdata.mongowp.utils.BsonDocumentBuilder)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1