Search in sources :

Example 1 with BsonValue

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

the class CollectionOptions method unmarshal.

public static CollectionOptions unmarshal(BsonDocument doc) throws BadValueException {
    boolean capped;
    try {
        capped = BsonReaderTool.getBooleanOrNumeric(doc, CAPPED_FIELD, false);
    } catch (TypesMismatchException ex) {
        capped = true;
    }
    long cappedSize;
    try {
        cappedSize = BsonReaderTool.getLong(doc, CAPPED_SIZE_FIELD, 0);
        if (cappedSize < 0) {
            throw new BadValueException("size has to be >= 0");
        }
    } catch (TypesMismatchException ex) {
        //backward compatibility
        cappedSize = 0;
    }
    long cappedMaxDocs;
    try {
        cappedMaxDocs = BsonReaderTool.getLong(doc, CAPPED_MAX_DOCS_FIELD, 0);
        if (cappedMaxDocs < 0) {
            throw new BadValueException("max has to be >= 0");
        }
    } catch (TypesMismatchException ex) {
        //backward compatibility
        cappedMaxDocs = 0;
    }
    final ImmutableList.Builder<Long> initialExtentSizes = ImmutableList.builder();
    Long initialNumExtends;
    BsonArray array;
    try {
        array = BsonReaderTool.getArray(doc, INITIAL_EXTENT_SIZES_FIELD, null);
        if (array == null) {
            initialNumExtends = null;
        } else {
            initialNumExtends = null;
            for (int i = 0; i < array.size(); i++) {
                BsonValue element = array.get(i);
                if (!element.isNumber()) {
                    throw new BadValueException("'$nExtents'.'" + i + "' has " + "the value " + element.toString() + " which is " + "not a number");
                }
                initialExtentSizes.add(element.asNumber().longValue());
            }
        }
    } catch (TypesMismatchException ex) {
        try {
            initialNumExtends = BsonReaderTool.getLong(doc, INITIAL_NUM_EXTENDS_FIELD);
        } catch (NoSuchKeyException | TypesMismatchException ex1) {
            initialNumExtends = null;
        }
    }
    AutoIndexMode autoIndexMode;
    try {
        if (BsonReaderTool.getBoolean(doc, AUTO_INDEX_MODE_FIELD)) {
            autoIndexMode = AutoIndexMode.YES;
        } else {
            autoIndexMode = AutoIndexMode.NO;
        }
    } catch (NoSuchKeyException | TypesMismatchException ex) {
        autoIndexMode = AutoIndexMode.DEFAULT;
    }
    EnumSet<Flag> flags = EnumSet.noneOf(Flag.class);
    try {
        int flagInt = BsonReaderTool.getInteger(doc, FLAGS_FIELD, 0);
        for (Flag value : Flag.values()) {
            if ((flagInt & value.bit) != 0) {
                flags.add(value);
            }
        }
    } catch (TypesMismatchException ignore) {
    //Nothing to do here
    }
    BsonDocument storageEngine;
    try {
        storageEngine = BsonReaderTool.getDocument(doc, STORAGE_ENGINE_FIELD, null);
    } catch (TypesMismatchException ex) {
        throw new BadValueException("'storageEngine' has to be a document.");
    }
    if (storageEngine != null) {
        if (storageEngine.isEmpty()) {
            throw new BadValueException("Empty 'storageEngine' options are invalid. " + "Please remove, or include valid options");
        }
        for (Entry<?> entry : storageEngine) {
            if (!entry.getValue().isDocument()) {
                throw new BadValueException("'storageEngie'.'" + entry.getKey() + "' has to be an embedded document");
            }
        }
    }
    boolean temp;
    try {
        temp = BsonReaderTool.getBoolean(doc, TEMP_FIELD, false);
    } catch (TypesMismatchException ex) {
        throw new BadValueException("Temp field must be a boolean");
    }
    return new DefaultCollectionOptions(capped, cappedSize, cappedMaxDocs, initialNumExtends, initialExtentSizes.build(), autoIndexMode, flags, storageEngine, temp);
}
Also used : BadValueException(com.eightkdata.mongowp.exceptions.BadValueException) ImmutableList(com.google.common.collect.ImmutableList) NoSuchKeyException(com.eightkdata.mongowp.exceptions.NoSuchKeyException) BsonDocument(com.eightkdata.mongowp.bson.BsonDocument) BsonArray(com.eightkdata.mongowp.bson.BsonArray) TypesMismatchException(com.eightkdata.mongowp.exceptions.TypesMismatchException) BsonValue(com.eightkdata.mongowp.bson.BsonValue)

Example 2 with BsonValue

use of com.eightkdata.mongowp.bson.BsonValue 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)

Example 3 with BsonValue

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

the class OplogTestParser method getOps.

private static List<OplogOperation> getOps(BsonDocument doc) {
    BsonValue<?> oplogValue = doc.get("oplog");
    if (oplogValue == null) {
        throw new AssertionError("Does not contain oplog");
    }
    AtomicInteger tsFactory = new AtomicInteger();
    AtomicInteger tFactory = new AtomicInteger();
    BsonInt32 twoInt32 = DefaultBsonValues.newInt(2);
    return oplogValue.asArray().asList().stream().map(BsonValue::asDocument).map(child -> {
        BsonDocumentBuilder builder = new BsonDocumentBuilder(child);
        if (child.get("ts") == null) {
            builder.appendUnsafe("ts", DefaultBsonValues.newTimestamp(tsFactory.incrementAndGet(), tFactory.incrementAndGet()));
        }
        if (child.get("h") == null) {
            builder.appendUnsafe("h", DefaultBsonValues.INT32_ONE);
        }
        if (child.get("v") == null) {
            builder.appendUnsafe("v", twoInt32);
        }
        return builder.build();
    }).map(child -> {
        try {
            return OplogOperationParser.fromBson(child);
        } catch (MongoException ex) {
            throw new AssertionError("Invalid oplog operation", ex);
        }
    }).collect(Collectors.toList());
}
Also used : java.util(java.util) BsonDocument(com.eightkdata.mongowp.bson.BsonDocument) Truth(com.google.common.truth.Truth) KvValue(com.torodb.kvdocument.values.KvValue) BsonValue(com.eightkdata.mongowp.bson.BsonValue) OplogOperation(com.eightkdata.mongowp.server.api.oplog.OplogOperation) MongoWpConverter(com.torodb.kvdocument.conversion.mongowp.MongoWpConverter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TorodTransaction(com.torodb.torod.TorodTransaction) BsonDocumentBuilder(com.eightkdata.mongowp.utils.BsonDocumentBuilder) MongoException(com.eightkdata.mongowp.exceptions.MongoException) DefaultBsonValues(com.eightkdata.mongowp.bson.utils.DefaultBsonValues) MongoBsonTranslator(com.eightkdata.mongowp.bson.org.bson.utils.MongoBsonTranslator) OplogOperationParser(com.torodb.mongodb.commands.pojos.OplogOperationParser) Charsets(com.google.common.base.Charsets) Files(java.nio.file.Files) BsonInt32(com.eightkdata.mongowp.bson.BsonInt32) KvDocument(com.torodb.kvdocument.values.KvDocument) Collectors(java.util.stream.Collectors) WriteMongodTransaction(com.torodb.mongodb.core.WriteMongodTransaction) Stream(java.util.stream.Stream) java.io(java.io) Paths(java.nio.file.Paths) ReadOnlyMongodTransaction(com.torodb.mongodb.core.ReadOnlyMongodTransaction) Assert(org.junit.Assert) BsonInt32(com.eightkdata.mongowp.bson.BsonInt32) MongoException(com.eightkdata.mongowp.exceptions.MongoException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BsonDocumentBuilder(com.eightkdata.mongowp.utils.BsonDocumentBuilder)

Example 4 with BsonValue

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

the class MemberConfig method fromDocument.

public static MemberConfig fromDocument(BsonDocument bson) throws TypesMismatchException, NoSuchKeyException, BadValueException {
    int id = BsonReaderTool.getNumeric(bson, "_id").intValue();
    HostAndPort host = BsonReaderTool.getHostAndPort(bson, "host");
    Builder builder = new Builder(id, host).setVotes(BsonReaderTool.getInteger(bson, "votes", DEFAULT_VOTES)).setPriority(BsonReaderTool.getDouble(bson, "priority", DEFAULT_PRIORITY)).setArbiterOnly(BsonReaderTool.getBooleanOrNumeric(bson, "arbiterOnly", DEFAULT_ARBITER_ONLY)).setSlaveDelay(BsonReaderTool.getNumeric(bson, "slaveDelay", DEFAULT_SLAVE_DELAY).longValue()).setHidden(BsonReaderTool.getBooleanOrNumeric(bson, "hidden", DEFAULT_HIDDEN)).setBuildIndexes(BsonReaderTool.getBooleanOrNumeric(bson, "buildIndexes", DEFAULT_BUILD_INDEXES));
    BsonDocument castedTags = BsonReaderTool.getDocument(bson, "tags");
    for (Entry entry : castedTags) {
        BsonValue value = entry.getValue();
        if (value.isString()) {
            throw new TypesMismatchException(entry.getKey(), "string", value.getType());
        }
        String castedValue = value.asString().getValue();
        builder.putTag(entry.getKey(), castedValue);
    }
    return builder.build();
}
Also used : HostAndPort(com.google.common.net.HostAndPort) Entry(com.eightkdata.mongowp.bson.BsonDocument.Entry) BsonDocument(com.eightkdata.mongowp.bson.BsonDocument) BsonDocumentBuilder(com.eightkdata.mongowp.utils.BsonDocumentBuilder) TypesMismatchException(com.eightkdata.mongowp.exceptions.TypesMismatchException) BsonValue(com.eightkdata.mongowp.bson.BsonValue)

Aggregations

BsonDocument (com.eightkdata.mongowp.bson.BsonDocument)4 BsonValue (com.eightkdata.mongowp.bson.BsonValue)4 TypesMismatchException (com.eightkdata.mongowp.exceptions.TypesMismatchException)3 BsonDocumentBuilder (com.eightkdata.mongowp.utils.BsonDocumentBuilder)3 BsonArray (com.eightkdata.mongowp.bson.BsonArray)2 NoSuchKeyException (com.eightkdata.mongowp.exceptions.NoSuchKeyException)2 WriteConcern (com.eightkdata.mongowp.WriteConcern)1 Entry (com.eightkdata.mongowp.bson.BsonDocument.Entry)1 BsonInt32 (com.eightkdata.mongowp.bson.BsonInt32)1 MongoBsonTranslator (com.eightkdata.mongowp.bson.org.bson.utils.MongoBsonTranslator)1 DefaultBsonValues (com.eightkdata.mongowp.bson.utils.DefaultBsonValues)1 BadValueException (com.eightkdata.mongowp.exceptions.BadValueException)1 MongoException (com.eightkdata.mongowp.exceptions.MongoException)1 OplogOperation (com.eightkdata.mongowp.server.api.oplog.OplogOperation)1 BsonArrayBuilder (com.eightkdata.mongowp.utils.BsonArrayBuilder)1 Charsets (com.google.common.base.Charsets)1 ImmutableList (com.google.common.collect.ImmutableList)1 HostAndPort (com.google.common.net.HostAndPort)1 Truth (com.google.common.truth.Truth)1 MongoWpConverter (com.torodb.kvdocument.conversion.mongowp.MongoWpConverter)1