Search in sources :

Example 1 with InsertOplogOperation

use of com.eightkdata.mongowp.server.api.oplog.InsertOplogOperation 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)

Aggregations

OpTime (com.eightkdata.mongowp.OpTime)1 BsonDocument (com.eightkdata.mongowp.bson.BsonDocument)1 BadValueException (com.eightkdata.mongowp.exceptions.BadValueException)1 NoSuchKeyException (com.eightkdata.mongowp.exceptions.NoSuchKeyException)1 TypesMismatchException (com.eightkdata.mongowp.exceptions.TypesMismatchException)1 DbCmdOplogOperation (com.eightkdata.mongowp.server.api.oplog.DbCmdOplogOperation)1 DbOplogOperation (com.eightkdata.mongowp.server.api.oplog.DbOplogOperation)1 DeleteOplogOperation (com.eightkdata.mongowp.server.api.oplog.DeleteOplogOperation)1 InsertOplogOperation (com.eightkdata.mongowp.server.api.oplog.InsertOplogOperation)1 NoopOplogOperation (com.eightkdata.mongowp.server.api.oplog.NoopOplogOperation)1 OplogOperationType (com.eightkdata.mongowp.server.api.oplog.OplogOperationType)1 OplogVersion (com.eightkdata.mongowp.server.api.oplog.OplogVersion)1 UpdateOplogOperation (com.eightkdata.mongowp.server.api.oplog.UpdateOplogOperation)1