Search in sources :

Example 6 with BsonDocument

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

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

the class FindImplementation method apply.

@Override
public Status<FindResult> apply(Request req, Command<? super FindArgument, ? super FindResult> command, FindArgument arg, MongodTransaction context) {
    logFindCommand(arg);
    BsonDocument filter = arg.getFilter();
    Cursor<BsonDocument> cursor;
    switch(filter.size()) {
        case 0:
            {
                cursor = context.getTorodTransaction().findAll(req.getDatabase(), arg.getCollection()).asDocCursor().transform(t -> t.getRoot()).transform(ToBsonDocumentTranslator.getInstance());
                break;
            }
        case 1:
            {
                try {
                    cursor = getByAttributeCursor(context.getTorodTransaction(), req.getDatabase(), arg.getCollection(), filter).transform(ToBsonDocumentTranslator.getInstance());
                } catch (CommandFailed ex) {
                    return Status.from(ex);
                }
                break;
            }
        default:
            {
                return Status.from(ErrorCode.COMMAND_FAILED, "The given query is not supported right now");
            }
    }
    if (Long.valueOf(arg.getBatchSize()) > (long) Integer.MAX_VALUE) {
        return Status.from(ErrorCode.COMMAND_FAILED, "Only batchSize equals or lower than " + Integer.MAX_VALUE + " is supported");
    }
    OptionalLong batchSize = arg.getEffectiveBatchSize();
    List<BsonDocument> batch = cursor.getNextBatch(batchSize.isPresent() ? (int) batchSize.getAsLong() : 101);
    cursor.close();
    return Status.ok(new FindResult(CursorResult.createSingleBatchCursor(req.getDatabase(), arg.getCollection(), batch.iterator())));
}
Also used : Request(com.eightkdata.mongowp.server.api.Request) FindResult(com.torodb.mongodb.commands.signatures.general.FindCommand.FindResult) AttributeReference(com.torodb.core.language.AttributeReference) Cursor(com.torodb.core.cursors.Cursor) KvDocument(com.torodb.kvdocument.values.KvDocument) BsonDocument(com.eightkdata.mongowp.bson.BsonDocument) Command(com.eightkdata.mongowp.server.api.Command) ToBsonDocumentTranslator(com.torodb.kvdocument.conversion.mongowp.ToBsonDocumentTranslator) Singleton(javax.inject.Singleton) CursorResult(com.torodb.mongodb.commands.pojos.CursorResult) MongodTransaction(com.torodb.mongodb.core.MongodTransaction) OptionalLong(java.util.OptionalLong) KvValue(com.torodb.kvdocument.values.KvValue) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Status(com.eightkdata.mongowp.Status) FindArgument(com.torodb.mongodb.commands.signatures.general.FindCommand.FindArgument) TorodTransaction(com.torodb.torod.TorodTransaction) Builder(com.torodb.core.language.AttributeReference.Builder) ErrorCode(com.eightkdata.mongowp.ErrorCode) ReadTorodbCommandImpl(com.torodb.mongodb.commands.impl.ReadTorodbCommandImpl) CommandFailed(com.eightkdata.mongowp.exceptions.CommandFailed) LogManager(org.apache.logging.log4j.LogManager) BsonDocument(com.eightkdata.mongowp.bson.BsonDocument) CommandFailed(com.eightkdata.mongowp.exceptions.CommandFailed) OptionalLong(java.util.OptionalLong) FindResult(com.torodb.mongodb.commands.signatures.general.FindCommand.FindResult)

Example 8 with BsonDocument

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

the class UpdateImplementation method apply.

@Override
public Status<UpdateResult> apply(Request req, Command<? super UpdateArgument, ? super UpdateResult> command, UpdateArgument arg, WriteMongodTransaction context) {
    UpdateStatus updateStatus = new UpdateStatus();
    try {
        if (!context.getTorodTransaction().existsCollection(req.getDatabase(), arg.getCollection())) {
            context.getTorodTransaction().createIndex(req.getDatabase(), arg.getCollection(), Constants.ID_INDEX, ImmutableList.<IndexFieldInfo>of(new IndexFieldInfo(new AttributeReference(Arrays.asList(new Key[] { new ObjectKey(Constants.ID) })), FieldIndexOrdering.ASC.isAscending())), true);
        }
        for (UpdateStatement updateStatement : arg.getStatements()) {
            BsonDocument query = updateStatement.getQuery();
            UpdateAction updateAction = UpdateActionTranslator.translate(updateStatement.getUpdate());
            Cursor<ToroDocument> candidatesCursor;
            switch(query.size()) {
                case 0:
                    {
                        candidatesCursor = context.getTorodTransaction().findAll(req.getDatabase(), arg.getCollection()).asDocCursor();
                        break;
                    }
                case 1:
                    {
                        try {
                            candidatesCursor = findByAttribute(context.getTorodTransaction(), req.getDatabase(), arg.getCollection(), query);
                        } catch (CommandFailed ex) {
                            return Status.from(ex);
                        }
                        break;
                    }
                default:
                    {
                        return Status.from(ErrorCode.COMMAND_FAILED, "The given query is not supported right now");
                    }
            }
            if (candidatesCursor.hasNext()) {
                try {
                    Stream<List<ToroDocument>> candidatesbatchStream;
                    if (updateStatement.isMulti()) {
                        candidatesbatchStream = StreamSupport.stream(Spliterators.spliteratorUnknownSize(candidatesCursor.batch(100), Spliterator.ORDERED), false);
                    } else {
                        candidatesbatchStream = Stream.of(ImmutableList.of(candidatesCursor.next()));
                    }
                    Stream<KvDocument> updatedCandidates = candidatesbatchStream.map(candidates -> {
                        updateStatus.increaseCandidates(candidates.size());
                        context.getTorodTransaction().delete(req.getDatabase(), arg.getCollection(), candidates);
                        return candidates;
                    }).flatMap(l -> l.stream()).map(candidate -> {
                        try {
                            updateStatus.increaseUpdated();
                            return update(updateAction, candidate);
                        } catch (UserException userException) {
                            throw new UserWrappedException(userException);
                        }
                    });
                    context.getTorodTransaction().insert(req.getDatabase(), arg.getCollection(), updatedCandidates);
                } catch (UserWrappedException userWrappedException) {
                    throw userWrappedException.getCause();
                }
            } else if (updateStatement.isUpsert()) {
                KvDocument toInsertCandidate;
                if (updateAction instanceof SetDocumentUpdateAction) {
                    toInsertCandidate = ((SetDocumentUpdateAction) updateAction).getNewValue();
                } else {
                    toInsertCandidate = update(updateAction, new ToroDocument(-1, (KvDocument) MongoWpConverter.translate(query)));
                }
                if (!toInsertCandidate.containsKey(Constants.ID)) {
                    KvDocument.Builder builder = new KvDocument.Builder();
                    for (DocEntry<?> entry : toInsertCandidate) {
                        builder.putValue(entry.getKey(), entry.getValue());
                    }
                    builder.putValue(Constants.ID, MongoWpConverter.translate(objectIdFactory.consumeObjectId()));
                    toInsertCandidate = builder.build();
                }
                updateStatus.increaseCandidates(1);
                updateStatus.increaseCreated(toInsertCandidate.get(Constants.ID));
                Stream<KvDocument> toInsertCandidates = Stream.of(toInsertCandidate);
                context.getTorodTransaction().insert(req.getDatabase(), arg.getCollection(), toInsertCandidates);
            }
        }
    } catch (UserException ex) {
        //TODO: Improve error reporting
        return Status.from(ErrorCode.COMMAND_FAILED, ex.getLocalizedMessage());
    }
    mongodMetrics.getUpdateModified().mark(updateStatus.updated);
    mongodMetrics.getUpdateMatched().mark(updateStatus.candidates);
    mongodMetrics.getUpdateUpserted().mark(updateStatus.upsertResults.size());
    return Status.ok(new UpdateResult(updateStatus.updated, updateStatus.candidates, ImmutableList.copyOf(updateStatus.upsertResults)));
}
Also used : Request(com.eightkdata.mongowp.server.api.Request) UpdateActionTranslator(com.torodb.mongodb.language.UpdateActionTranslator) Arrays(java.util.Arrays) UpdatedToroDocumentBuilder(com.torodb.mongodb.language.update.UpdatedToroDocumentBuilder) FieldIndexOrdering(com.torodb.core.transaction.metainf.FieldIndexOrdering) Spliterators(java.util.Spliterators) BsonDocument(com.eightkdata.mongowp.bson.BsonDocument) WriteTorodbCommandImpl(com.torodb.mongodb.commands.impl.WriteTorodbCommandImpl) UpdateAction(com.torodb.mongodb.language.update.UpdateAction) UpdateStatement(com.torodb.mongodb.commands.signatures.general.UpdateCommand.UpdateStatement) Singleton(javax.inject.Singleton) ToroDocument(com.torodb.core.document.ToroDocument) UpdateResult(com.torodb.mongodb.commands.signatures.general.UpdateCommand.UpdateResult) ArrayList(java.util.ArrayList) ObjectKey(com.torodb.core.language.AttributeReference.ObjectKey) IndexFieldInfo(com.torodb.torod.IndexFieldInfo) Inject(javax.inject.Inject) KvValue(com.torodb.kvdocument.values.KvValue) ImmutableList(com.google.common.collect.ImmutableList) MongoWpConverter(com.torodb.kvdocument.conversion.mongowp.MongoWpConverter) StreamSupport(java.util.stream.StreamSupport) Builder(com.torodb.core.language.AttributeReference.Builder) ErrorCode(com.eightkdata.mongowp.ErrorCode) CommandFailed(com.eightkdata.mongowp.exceptions.CommandFailed) UpdateException(com.torodb.core.exceptions.user.UpdateException) SharedWriteTorodTransaction(com.torodb.torod.SharedWriteTorodTransaction) AttributeReference(com.torodb.core.language.AttributeReference) Constants(com.torodb.mongodb.language.Constants) Cursor(com.torodb.core.cursors.Cursor) KvDocument(com.torodb.kvdocument.values.KvDocument) UpsertResult(com.torodb.mongodb.commands.signatures.general.UpdateCommand.UpsertResult) Command(com.eightkdata.mongowp.server.api.Command) UserException(com.torodb.core.exceptions.user.UserException) SetDocumentUpdateAction(com.torodb.mongodb.language.update.SetDocumentUpdateAction) ObjectIdFactory(com.torodb.mongodb.language.ObjectIdFactory) DocEntry(com.torodb.kvdocument.values.KvDocument.DocEntry) MongodMetrics(com.torodb.mongodb.core.MongodMetrics) WriteMongodTransaction(com.torodb.mongodb.core.WriteMongodTransaction) List(java.util.List) Stream(java.util.stream.Stream) Status(com.eightkdata.mongowp.Status) UserWrappedException(com.torodb.core.exceptions.UserWrappedException) UpdateArgument(com.torodb.mongodb.commands.signatures.general.UpdateCommand.UpdateArgument) Spliterator(java.util.Spliterator) Key(com.torodb.core.language.AttributeReference.Key) KvDocument(com.torodb.kvdocument.values.KvDocument) UpdateStatement(com.torodb.mongodb.commands.signatures.general.UpdateCommand.UpdateStatement) UpdateAction(com.torodb.mongodb.language.update.UpdateAction) SetDocumentUpdateAction(com.torodb.mongodb.language.update.SetDocumentUpdateAction) AttributeReference(com.torodb.core.language.AttributeReference) UpdatedToroDocumentBuilder(com.torodb.mongodb.language.update.UpdatedToroDocumentBuilder) Builder(com.torodb.core.language.AttributeReference.Builder) ObjectKey(com.torodb.core.language.AttributeReference.ObjectKey) DocEntry(com.torodb.kvdocument.values.KvDocument.DocEntry) BsonDocument(com.eightkdata.mongowp.bson.BsonDocument) UserWrappedException(com.torodb.core.exceptions.UserWrappedException) ToroDocument(com.torodb.core.document.ToroDocument) CommandFailed(com.eightkdata.mongowp.exceptions.CommandFailed) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Stream(java.util.stream.Stream) IndexFieldInfo(com.torodb.torod.IndexFieldInfo) UserException(com.torodb.core.exceptions.user.UserException) UpdateResult(com.torodb.mongodb.commands.signatures.general.UpdateCommand.UpdateResult) SetDocumentUpdateAction(com.torodb.mongodb.language.update.SetDocumentUpdateAction)

Example 9 with BsonDocument

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

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

the class DeleteImplementation method apply.

@Override
public Status<Long> apply(Request req, Command<? super DeleteArgument, ? super Long> command, DeleteArgument arg, WriteMongodTransaction context) {
    Long deleted = 0L;
    for (DeleteStatement deleteStatement : arg.getStatements()) {
        BsonDocument query = deleteStatement.getQuery();
        switch(query.size()) {
            case 0:
                {
                    deleted += context.getTorodTransaction().deleteAll(req.getDatabase(), arg.getCollection());
                    break;
                }
            case 1:
                {
                    try {
                        logDeleteCommand(arg);
                        deleted += deleteByAttribute(context.getTorodTransaction(), req.getDatabase(), arg.getCollection(), query);
                    } catch (CommandFailed ex) {
                        return Status.from(ex);
                    }
                    break;
                }
            default:
                {
                    return Status.from(ErrorCode.COMMAND_FAILED, "The given query is not supported right now");
                }
        }
    }
    mongodMetrics.getDeletes().mark(deleted);
    return Status.ok(deleted);
}
Also used : BsonDocument(com.eightkdata.mongowp.bson.BsonDocument) CommandFailed(com.eightkdata.mongowp.exceptions.CommandFailed) DeleteStatement(com.torodb.mongodb.commands.signatures.general.DeleteCommand.DeleteStatement)

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