Search in sources :

Example 16 with BsonDocument

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

Example 17 with BsonDocument

use of com.eightkdata.mongowp.bson.BsonDocument 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 18 with BsonDocument

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

the class ReplicaSetConfig method toBson.

public BsonDocument toBson() {
    BsonDocumentBuilder result = new BsonDocumentBuilder();
    result.append(ID_FIELD, setName);
    result.append(VERSION_FIELD, version);
    BsonArrayBuilder membersList = new BsonArrayBuilder();
    for (MemberConfig member : members) {
        membersList.add(member.toBson());
    }
    result.append(MEMBERS_FIELD, membersList.build());
    BsonDocumentBuilder settingsBuilder = new BsonDocumentBuilder();
    settingsBuilder.append(CHAINING_ALLOWED_FIELD, chainingAllowed);
    settingsBuilder.append(HEARTHBEAT_TIMEOUT_FIELD, heartbeatTimeoutPeriod);
    BsonDocumentBuilder customWrites = new BsonDocumentBuilder();
    for (Map.Entry<String, ReplicaSetTagPattern> entry : customWriteConcern.entrySet()) {
        String customWriteName = entry.getKey();
        if (customWriteName.startsWith("$")) {
            //MongoDB uses $ as an internal mode
            continue;
        }
        BsonDocument tagMap = entry.getValue().toBson();
        customWrites.appendUnsafe(customWriteName, tagMap);
    }
    settingsBuilder.append(GET_LAST_ERROR_MODES_FIELD, customWrites);
    settingsBuilder.append(GET_LAST_ERROR_DEFAULTS_FIELD, defaultWriteConcern.toDocument());
    settingsBuilder.append(PROTOCOL_VERSION_FIELD, protocolVersion);
    result.append(SETTINGS_FIELD, settingsBuilder);
    return result.build();
}
Also used : BsonArrayBuilder(com.eightkdata.mongowp.utils.BsonArrayBuilder) BsonDocument(com.eightkdata.mongowp.bson.BsonDocument) HashMap(java.util.HashMap) Map(java.util.Map) BsonDocumentBuilder(com.eightkdata.mongowp.utils.BsonDocumentBuilder)

Example 19 with BsonDocument

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

the class CollectionOptions method marshall.

public void marshall(BsonDocumentBuilder builder) {
    if (isCapped()) {
        builder.append(CAPPED_FIELD, true);
        long cappedSize = getCappedSize();
        if (cappedSize != 0) {
            builder.append(CAPPED_SIZE_FIELD, cappedSize);
        }
        long cappedMaxDocs = getCappedMaxDocs();
        if (cappedMaxDocs != 0) {
            builder.append(CAPPED_MAX_DOCS_FIELD, cappedMaxDocs);
        }
    }
    Long initialNumExtents = getInitialNumExtents();
    if (initialNumExtents != null && initialNumExtents != 0) {
        builder.append(INITIAL_NUM_EXTENDS_FIELD, initialNumExtents);
    }
    List<Long> initialExtentSizes = getInitialExtentSizes();
    if (initialExtentSizes != null && !initialExtentSizes.isEmpty()) {
        BsonArrayBuilder arrBuilder = new BsonArrayBuilder(initialExtentSizes.size());
        for (Long initialExtentSize : initialExtentSizes) {
            arrBuilder.add(initialExtentSize);
        }
        builder.append(INITIAL_EXTENT_SIZES_FIELD, arrBuilder.build());
    }
    AutoIndexMode autoIndexMode = getAutoIndexMode();
    if (!autoIndexMode.equals(AutoIndexMode.DEFAULT)) {
        boolean value = autoIndexMode.equals(AutoIndexMode.YES);
        builder.append(AUTO_INDEX_MODE_FIELD, value);
    }
    Set<Flag> flags = getFlags();
    if (!flags.isEmpty()) {
        int value = 0;
        for (Flag flag : flags) {
            value |= flag.getBit();
        }
        builder.append(FLAGS_FIELD, value);
    }
    BsonDocument storageEngine = getStorageEngine();
    if (storageEngine != null && storageEngine.isEmpty()) {
        builder.append(STORAGE_ENGINE_FIELD, storageEngine);
    }
    boolean temp = isTemp();
    if (temp) {
        builder.append(TEMP_FIELD, temp);
    }
}
Also used : BsonArrayBuilder(com.eightkdata.mongowp.utils.BsonArrayBuilder) BsonDocument(com.eightkdata.mongowp.bson.BsonDocument)

Example 20 with BsonDocument

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

the class AbstractMongoOplogReader method query.

public MongoCursor<OplogOperation> query(BsonDocument query, EnumSet<QueryOption> flags, BsonDocument sortBy) throws MongoException {
    Preconditions.checkState(!isClosed(), "You have to connect this client before");
    MongoConnection connection = consumeConnection();
    MongoCursor<BsonDocument> cursor = connection.query(DATABASE, COLLECTION, query, 0, 0, new QueryOptions(flags), sortBy, null);
    return new MyCursor<>(connection, TransformationMongoCursor.create(cursor, OplogOperationParser.asFunction()));
}
Also used : BsonDocument(com.eightkdata.mongowp.bson.BsonDocument) MongoConnection(com.eightkdata.mongowp.client.core.MongoConnection) QueryOptions(com.eightkdata.mongowp.messages.request.QueryMessage.QueryOptions)

Aggregations

BsonDocument (com.eightkdata.mongowp.bson.BsonDocument)22 NoSuchKeyException (com.eightkdata.mongowp.exceptions.NoSuchKeyException)6 TypesMismatchException (com.eightkdata.mongowp.exceptions.TypesMismatchException)6 BsonDocumentBuilder (com.eightkdata.mongowp.utils.BsonDocumentBuilder)6 BadValueException (com.eightkdata.mongowp.exceptions.BadValueException)5 BsonValue (com.eightkdata.mongowp.bson.BsonValue)4 Request (com.eightkdata.mongowp.server.api.Request)4 Status (com.eightkdata.mongowp.Status)3 CommandFailed (com.eightkdata.mongowp.exceptions.CommandFailed)3 MongoException (com.eightkdata.mongowp.exceptions.MongoException)3 QueryOption (com.eightkdata.mongowp.messages.request.QueryMessage.QueryOption)3 QueryOptions (com.eightkdata.mongowp.messages.request.QueryMessage.QueryOptions)3 Command (com.eightkdata.mongowp.server.api.Command)3 KvDocument (com.torodb.kvdocument.values.KvDocument)3 KvValue (com.torodb.kvdocument.values.KvValue)3 ErrorCode (com.eightkdata.mongowp.ErrorCode)2 OpTime (com.eightkdata.mongowp.OpTime)2 BsonArray (com.eightkdata.mongowp.bson.BsonArray)2 DefaultBsonValues (com.eightkdata.mongowp.bson.utils.DefaultBsonValues)2 MongoConnection (com.eightkdata.mongowp.client.core.MongoConnection)2