Search in sources :

Example 11 with BsonDocument

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

the class OplogOperationParser method fromBson.

public static OplogOperation fromBson(@Nonnull BsonValue uncastedOp) throws BadValueException, TypesMismatchException, NoSuchKeyException {
    if (!uncastedOp.isDocument()) {
        throw new BadValueException("found a " + uncastedOp.getType().toString().toLowerCase(Locale.ROOT) + " where a document that represents a oplog operation " + "was expected");
    }
    BsonDocument doc = uncastedOp.asDocument();
    OplogOperationType opType;
    String opString = BsonReaderTool.getString(doc, "op");
    try {
        opType = OplogOperationType.fromOplogName(opString);
    } catch (IllegalArgumentException ex) {
        throw new BadValueException("Unknown oplog operation with type '" + opString + "'");
    }
    String ns;
    try {
        ns = BsonReaderTool.getString(doc, "ns");
    } catch (NoSuchKeyException ex) {
        throw new NoSuchKeyException("ns", "op does not contain required \"ns\" field: " + uncastedOp);
    } catch (TypesMismatchException ex) {
        throw ex.newWithMessage("\"ns\" field is not a string: " + uncastedOp);
    }
    if (ns.isEmpty() && !opType.equals(OplogOperationType.NOOP)) {
        throw new BadValueException("\"ns\" field value cannot be empty " + "when op type is not 'n': " + doc);
    }
    String db;
    String collection;
    int firstDotIndex = ns.indexOf('.');
    if (firstDotIndex == -1 || firstDotIndex + 1 == ns.length()) {
        db = ns;
        collection = null;
    } else {
        db = ns.substring(0, firstDotIndex);
        collection = ns.substring(firstDotIndex + 1);
    }
    OpTime optime = OpTime.fromOplogEntry(doc);
    long h = BsonReaderTool.getLong(doc, "h");
    OplogVersion version = OplogVersion.valueOf(BsonReaderTool.getInteger(doc, "v"));
    //Note: Mongodb v3 checks if the key exists or not, but doesn't check the value
    boolean fromMigrate = doc.containsKey("fromMigrate");
    BsonDocument o = BsonReaderTool.getDocument(doc, "o");
    switch(opType) {
        case DB:
            return new DbOplogOperation(db, optime, h, version, fromMigrate);
        case DB_CMD:
            return new DbCmdOplogOperation(o, db, optime, h, version, fromMigrate);
        case DELETE:
            return new DeleteOplogOperation(o, db, collection, optime, h, version, fromMigrate, BsonReaderTool.getBoolean(doc, "b", false));
        case INSERT:
            //TODO: parse b
            return new InsertOplogOperation(o, db, collection, optime, h, version, fromMigrate);
        case NOOP:
            return new NoopOplogOperation(o, db, optime, h, version, fromMigrate);
        case UPDATE:
            return new UpdateOplogOperation(BsonReaderTool.getDocument(doc, "o2"), db, collection, optime, h, version, fromMigrate, o, BsonReaderTool.getBoolean(doc, "b", false));
        default:
            throw new AssertionError(OplogOperationParser.class + " is not prepared to work with oplog operations of type " + opType);
    }
}
Also used : BadValueException(com.eightkdata.mongowp.exceptions.BadValueException) OplogOperationType(com.eightkdata.mongowp.server.api.oplog.OplogOperationType) DeleteOplogOperation(com.eightkdata.mongowp.server.api.oplog.DeleteOplogOperation) OpTime(com.eightkdata.mongowp.OpTime) UpdateOplogOperation(com.eightkdata.mongowp.server.api.oplog.UpdateOplogOperation) NoSuchKeyException(com.eightkdata.mongowp.exceptions.NoSuchKeyException) BsonDocument(com.eightkdata.mongowp.bson.BsonDocument) DbOplogOperation(com.eightkdata.mongowp.server.api.oplog.DbOplogOperation) NoopOplogOperation(com.eightkdata.mongowp.server.api.oplog.NoopOplogOperation) DbCmdOplogOperation(com.eightkdata.mongowp.server.api.oplog.DbCmdOplogOperation) OplogVersion(com.eightkdata.mongowp.server.api.oplog.OplogVersion) TypesMismatchException(com.eightkdata.mongowp.exceptions.TypesMismatchException) InsertOplogOperation(com.eightkdata.mongowp.server.api.oplog.InsertOplogOperation)

Example 12 with BsonDocument

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

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

the class IndexOptions method unmarshall.

public static IndexOptions unmarshall(BsonDocument requestDoc) throws BadValueException, TypesMismatchException, NoSuchKeyException {
    IndexVersion version = IndexVersion.V1;
    String name = null;
    String namespace = null;
    boolean background = false;
    boolean unique = false;
    boolean sparse = false;
    int expireAfterSeconds = 0;
    BsonDocument keyDoc = null;
    BsonDocument storageEngine = null;
    BsonDocumentBuilder otherBuilder = new BsonDocumentBuilder();
    for (Entry<?> entry : requestDoc) {
        String key = entry.getKey();
        switch(key) {
            case VERSION_FIELD_NAME:
                {
                    int vInt = BsonReaderTool.getNumeric(requestDoc, VERSION_FIELD).intValue();
                    try {
                        version = IndexVersion.fromInt(vInt);
                    } catch (IndexOutOfBoundsException ex) {
                        throw new BadValueException("Value " + vInt + " is not a valid version");
                    }
                    break;
                }
            case NAME_FIELD_NAME:
                {
                    name = BsonReaderTool.getString(entry);
                    break;
                }
            case NAMESPACE_FIELD_NAME:
                {
                    namespace = BsonReaderTool.getString(entry);
                    break;
                }
            case BACKGROUND_FIELD_NAME:
                {
                    background = BsonReaderTool.getBooleanOrNumeric(entry, BACKGROUND_FIELD);
                    break;
                }
            case UNIQUE_FIELD_NAME:
                {
                    unique = BsonReaderTool.getBooleanOrNumeric(entry, UNIQUE_FIELD);
                    break;
                }
            case SPARSE_FIELD_NAME:
                {
                    sparse = BsonReaderTool.getBooleanOrNumeric(entry, SPARSE_FIELD);
                    break;
                }
            case EXPIRE_AFTER_SECONDS_FIELD_NAME:
                {
                    expireAfterSeconds = BsonReaderTool.getNumeric(entry, EXPIRE_AFTER_SECONDS_FIELD.getFieldName()).intValue();
                    break;
                }
            case KEYS_FIELD_NAME:
                {
                    keyDoc = BsonReaderTool.getDocument(entry);
                    break;
                }
            case STORAGE_ENGINE_FIELD_NAME:
                {
                    storageEngine = BsonReaderTool.getDocument(entry);
                    break;
                }
            default:
                {
                    otherBuilder.appendUnsafe(key, entry.getValue());
                    break;
                }
        }
    }
    String db = null;
    String collection = null;
    if (namespace != null) {
        int dotIndex = namespace.indexOf('.');
        if (dotIndex < 1 || dotIndex > namespace.length() - 2) {
            throw new BadValueException("The not valid namespace " + namespace + " found");
        }
        db = namespace.substring(0, dotIndex);
        collection = namespace.substring(dotIndex + 1);
    }
    if (name == null) {
        throw new NoSuchKeyException(NAME_FIELD_NAME, "Indexes need names");
    }
    if (keyDoc == null) {
        throw new NoSuchKeyException(KEYS_FIELD_NAME, "Indexes need at least one key to index");
    }
    List<Key> keys = unmarshllKeys(keyDoc);
    return new IndexOptions(version, name, db, collection, background, unique, sparse, expireAfterSeconds, keys, storageEngine, otherBuilder.build());
}
Also used : BadValueException(com.eightkdata.mongowp.exceptions.BadValueException) BsonDocumentBuilder(com.eightkdata.mongowp.utils.BsonDocumentBuilder) NoSuchKeyException(com.eightkdata.mongowp.exceptions.NoSuchKeyException) BsonDocument(com.eightkdata.mongowp.bson.BsonDocument)

Example 14 with BsonDocument

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

the class HandshakeReplSetUpdatePositionCommand method unmarshallArg.

@Override
public HandshakeArgument unmarshallArg(BsonDocument requestDoc) throws TypesMismatchException, NoSuchKeyException, BadValueException {
    BsonDocument doc = BsonReaderTool.getDocument(requestDoc, "handshake");
    HandshakeArgument result = super.unmarshallArg(doc);
    if (result.getMemberId() == null) {
        throw new NoSuchKeyException("replSetUpdatePosition handshake was missing 'member' field");
    }
    return result;
}
Also used : NoSuchKeyException(com.eightkdata.mongowp.exceptions.NoSuchKeyException) BsonDocument(com.eightkdata.mongowp.bson.BsonDocument)

Example 15 with BsonDocument

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

the class ReplSetInitiateCommand method unmarshallArg.

@Override
public ReplicaSetConfig unmarshallArg(BsonDocument requestDoc) throws TypesMismatchException, BadValueException, NoSuchKeyException, FailedToParseException {
    ReplicaSetConfig config;
    BsonDocument configDoc;
    configDoc = BsonReaderTool.getDocument(requestDoc, getCommandName());
    config = ReplicaSetConfig.fromDocument(configDoc);
    return config;
}
Also used : ReplicaSetConfig(com.torodb.mongodb.commands.pojos.ReplicaSetConfig) BsonDocument(com.eightkdata.mongowp.bson.BsonDocument)

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