Search in sources :

Example 1 with UpdateOneModel

use of com.mongodb.client.model.UpdateOneModel in project mongo-java-driver by mongodb.

the class MongoCollectionImpl method bulkWrite.

@SuppressWarnings("unchecked")
@Override
public void bulkWrite(final List<? extends WriteModel<? extends TDocument>> requests, final BulkWriteOptions options, final SingleResultCallback<BulkWriteResult> callback) {
    notNull("requests", requests);
    List<WriteRequest> writeRequests = new ArrayList<WriteRequest>(requests.size());
    for (WriteModel<? extends TDocument> writeModel : requests) {
        WriteRequest writeRequest;
        if (writeModel == null) {
            throw new IllegalArgumentException("requests can not contain a null value");
        } else if (writeModel instanceof InsertOneModel) {
            TDocument document = ((InsertOneModel<TDocument>) writeModel).getDocument();
            if (getCodec() instanceof CollectibleCodec) {
                document = ((CollectibleCodec<TDocument>) getCodec()).generateIdIfAbsentFromDocument(document);
            }
            writeRequest = new InsertRequest(documentToBsonDocument(document));
        } else if (writeModel instanceof ReplaceOneModel) {
            ReplaceOneModel<TDocument> replaceOneModel = (ReplaceOneModel<TDocument>) writeModel;
            writeRequest = new UpdateRequest(toBsonDocument(replaceOneModel.getFilter()), documentToBsonDocument(replaceOneModel.getReplacement()), WriteRequest.Type.REPLACE).upsert(replaceOneModel.getOptions().isUpsert()).collation(replaceOneModel.getOptions().getCollation());
        } else if (writeModel instanceof UpdateOneModel) {
            UpdateOneModel<TDocument> updateOneModel = (UpdateOneModel<TDocument>) writeModel;
            writeRequest = new UpdateRequest(toBsonDocument(updateOneModel.getFilter()), toBsonDocument(updateOneModel.getUpdate()), WriteRequest.Type.UPDATE).multi(false).upsert(updateOneModel.getOptions().isUpsert()).collation(updateOneModel.getOptions().getCollation());
        } else if (writeModel instanceof UpdateManyModel) {
            UpdateManyModel<TDocument> updateManyModel = (UpdateManyModel<TDocument>) writeModel;
            writeRequest = new UpdateRequest(toBsonDocument(updateManyModel.getFilter()), toBsonDocument(updateManyModel.getUpdate()), WriteRequest.Type.UPDATE).multi(true).upsert(updateManyModel.getOptions().isUpsert()).collation(updateManyModel.getOptions().getCollation());
        } else if (writeModel instanceof DeleteOneModel) {
            DeleteOneModel<TDocument> deleteOneModel = (DeleteOneModel<TDocument>) writeModel;
            writeRequest = new DeleteRequest(toBsonDocument(deleteOneModel.getFilter())).multi(false).collation(deleteOneModel.getOptions().getCollation());
        } else if (writeModel instanceof DeleteManyModel) {
            DeleteManyModel<TDocument> deleteManyModel = (DeleteManyModel<TDocument>) writeModel;
            writeRequest = new DeleteRequest(toBsonDocument(deleteManyModel.getFilter())).multi(true).collation(deleteManyModel.getOptions().getCollation());
        } else {
            throw new UnsupportedOperationException(format("WriteModel of type %s is not supported", writeModel.getClass()));
        }
        writeRequests.add(writeRequest);
    }
    executor.execute(new MixedBulkWriteOperation(namespace, writeRequests, options.isOrdered(), writeConcern).bypassDocumentValidation(options.getBypassDocumentValidation()), callback);
}
Also used : InsertOneModel(com.mongodb.client.model.InsertOneModel) CollectibleCodec(org.bson.codecs.CollectibleCodec) UpdateRequest(com.mongodb.bulk.UpdateRequest) WriteRequest(com.mongodb.bulk.WriteRequest) InsertRequest(com.mongodb.bulk.InsertRequest) ReplaceOneModel(com.mongodb.client.model.ReplaceOneModel) ArrayList(java.util.ArrayList) DeleteOneModel(com.mongodb.client.model.DeleteOneModel) MixedBulkWriteOperation(com.mongodb.operation.MixedBulkWriteOperation) UpdateOneModel(com.mongodb.client.model.UpdateOneModel) UpdateManyModel(com.mongodb.client.model.UpdateManyModel) DeleteRequest(com.mongodb.bulk.DeleteRequest) DeleteManyModel(com.mongodb.client.model.DeleteManyModel)

Example 2 with UpdateOneModel

use of com.mongodb.client.model.UpdateOneModel in project mongo-java-driver by mongodb.

the class MongoCollectionImpl method bulkWrite.

@Override
@SuppressWarnings("unchecked")
public BulkWriteResult bulkWrite(final List<? extends WriteModel<? extends TDocument>> requests, final BulkWriteOptions options) {
    notNull("requests", requests);
    List<WriteRequest> writeRequests = new ArrayList<WriteRequest>(requests.size());
    for (WriteModel<? extends TDocument> writeModel : requests) {
        WriteRequest writeRequest;
        if (writeModel == null) {
            throw new IllegalArgumentException("requests can not contain a null value");
        } else if (writeModel instanceof InsertOneModel) {
            TDocument document = ((InsertOneModel<TDocument>) writeModel).getDocument();
            if (getCodec() instanceof CollectibleCodec) {
                document = ((CollectibleCodec<TDocument>) getCodec()).generateIdIfAbsentFromDocument(document);
            }
            writeRequest = new InsertRequest(documentToBsonDocument(document));
        } else if (writeModel instanceof ReplaceOneModel) {
            ReplaceOneModel<TDocument> replaceOneModel = (ReplaceOneModel<TDocument>) writeModel;
            writeRequest = new UpdateRequest(toBsonDocument(replaceOneModel.getFilter()), documentToBsonDocument(replaceOneModel.getReplacement()), WriteRequest.Type.REPLACE).upsert(replaceOneModel.getOptions().isUpsert()).collation(replaceOneModel.getOptions().getCollation());
        } else if (writeModel instanceof UpdateOneModel) {
            UpdateOneModel<TDocument> updateOneModel = (UpdateOneModel<TDocument>) writeModel;
            writeRequest = new UpdateRequest(toBsonDocument(updateOneModel.getFilter()), toBsonDocument(updateOneModel.getUpdate()), WriteRequest.Type.UPDATE).multi(false).upsert(updateOneModel.getOptions().isUpsert()).collation(updateOneModel.getOptions().getCollation());
        } else if (writeModel instanceof UpdateManyModel) {
            UpdateManyModel<TDocument> updateManyModel = (UpdateManyModel<TDocument>) writeModel;
            writeRequest = new UpdateRequest(toBsonDocument(updateManyModel.getFilter()), toBsonDocument(updateManyModel.getUpdate()), WriteRequest.Type.UPDATE).multi(true).upsert(updateManyModel.getOptions().isUpsert()).collation(updateManyModel.getOptions().getCollation());
        } else if (writeModel instanceof DeleteOneModel) {
            DeleteOneModel<TDocument> deleteOneModel = (DeleteOneModel<TDocument>) writeModel;
            writeRequest = new DeleteRequest(toBsonDocument(deleteOneModel.getFilter())).multi(false).collation(deleteOneModel.getOptions().getCollation());
        } else if (writeModel instanceof DeleteManyModel) {
            DeleteManyModel<TDocument> deleteManyModel = (DeleteManyModel<TDocument>) writeModel;
            writeRequest = new DeleteRequest(toBsonDocument(deleteManyModel.getFilter())).multi(true).collation(deleteManyModel.getOptions().getCollation());
        } else {
            throw new UnsupportedOperationException(format("WriteModel of type %s is not supported", writeModel.getClass()));
        }
        writeRequests.add(writeRequest);
    }
    return executor.execute(new MixedBulkWriteOperation(namespace, writeRequests, options.isOrdered(), writeConcern).bypassDocumentValidation(options.getBypassDocumentValidation()));
}
Also used : InsertOneModel(com.mongodb.client.model.InsertOneModel) CollectibleCodec(org.bson.codecs.CollectibleCodec) UpdateRequest(com.mongodb.bulk.UpdateRequest) WriteRequest(com.mongodb.bulk.WriteRequest) InsertRequest(com.mongodb.bulk.InsertRequest) ReplaceOneModel(com.mongodb.client.model.ReplaceOneModel) ArrayList(java.util.ArrayList) DeleteOneModel(com.mongodb.client.model.DeleteOneModel) MixedBulkWriteOperation(com.mongodb.operation.MixedBulkWriteOperation) UpdateOneModel(com.mongodb.client.model.UpdateOneModel) UpdateManyModel(com.mongodb.client.model.UpdateManyModel) DeleteRequest(com.mongodb.bulk.DeleteRequest) DeleteManyModel(com.mongodb.client.model.DeleteManyModel)

Example 3 with UpdateOneModel

use of com.mongodb.client.model.UpdateOneModel in project YCSB by brianfrankcooper.

the class MongoDbClient method insert.

/**
   * Insert a record in the database. Any field/value pairs in the specified
   * values HashMap will be written into the record with the specified record
   * key.
   * 
   * @param table
   *          The name of the table
   * @param key
   *          The record key of the record to insert.
   * @param values
   *          A HashMap of field/value pairs to insert in the record
   * @return Zero on success, a non-zero error code on error. See the {@link DB}
   *         class's description for a discussion of error codes.
   */
@Override
public Status insert(String table, String key, HashMap<String, ByteIterator> values) {
    try {
        MongoCollection<Document> collection = database.getCollection(table);
        Document toInsert = new Document("_id", key);
        for (Map.Entry<String, ByteIterator> entry : values.entrySet()) {
            toInsert.put(entry.getKey(), entry.getValue().toArray());
        }
        if (batchSize == 1) {
            if (useUpsert) {
                // this is effectively an insert, but using an upsert instead due
                // to current inability of the framework to clean up after itself
                // between test runs.
                collection.replaceOne(new Document("_id", toInsert.get("_id")), toInsert, UPDATE_WITH_UPSERT);
            } else {
                collection.insertOne(toInsert);
            }
        } else {
            bulkInserts.add(toInsert);
            if (bulkInserts.size() == batchSize) {
                if (useUpsert) {
                    List<UpdateOneModel<Document>> updates = new ArrayList<UpdateOneModel<Document>>(bulkInserts.size());
                    for (Document doc : bulkInserts) {
                        updates.add(new UpdateOneModel<Document>(new Document("_id", doc.get("_id")), doc, UPDATE_WITH_UPSERT));
                    }
                    collection.bulkWrite(updates);
                } else {
                    collection.insertMany(bulkInserts, INSERT_UNORDERED);
                }
                bulkInserts.clear();
            } else {
                return Status.BATCHED_OK;
            }
        }
        return Status.OK;
    } catch (Exception e) {
        System.err.println("Exception while trying bulk insert with " + bulkInserts.size());
        e.printStackTrace();
        return Status.ERROR;
    }
}
Also used : UpdateOneModel(com.mongodb.client.model.UpdateOneModel) ByteArrayByteIterator(com.yahoo.ycsb.ByteArrayByteIterator) ByteIterator(com.yahoo.ycsb.ByteIterator) ArrayList(java.util.ArrayList) Document(org.bson.Document) HashMap(java.util.HashMap) Map(java.util.Map) DBException(com.yahoo.ycsb.DBException)

Example 4 with UpdateOneModel

use of com.mongodb.client.model.UpdateOneModel in project mongo-java-driver by mongodb.

the class JsonPoweredCrudTestHelper method getBulkWriteResult.

BsonDocument getBulkWriteResult(final BsonDocument arguments) {
    WriteConcern writeConcern = WriteConcern.ACKNOWLEDGED;
    if (arguments.containsKey("writeConcern")) {
        if (arguments.getDocument("writeConcern").size() > 1) {
            throw new UnsupportedOperationException("Write concern document contains unexpected keys: " + arguments.getDocument("writeConcern").keySet());
        }
        writeConcern = new WriteConcern(arguments.getDocument("writeConcern").getInt32("w").intValue());
    }
    List<WriteModel<BsonDocument>> writeModels = new ArrayList<WriteModel<BsonDocument>>();
    for (Iterator<BsonValue> iter = arguments.getArray("requests").iterator(); iter.hasNext(); ) {
        BsonDocument cur = iter.next().asDocument();
        if (cur.get("insertOne") != null) {
            writeModels.add(new InsertOneModel<BsonDocument>(cur.getDocument("insertOne").getDocument("document")));
        } else if (cur.get("updateOne") != null) {
            writeModels.add(new UpdateOneModel<BsonDocument>(cur.getDocument("updateOne").getDocument("filter"), cur.getDocument("updateOne").getDocument("update")));
        } else {
            throw new UnsupportedOperationException("Unsupported write request type");
        }
    }
    return toResult(collection.withWriteConcern(writeConcern).bulkWrite(writeModels, new BulkWriteOptions().ordered(arguments.getBoolean("ordered", BsonBoolean.TRUE).getValue())));
}
Also used : UpdateOneModel(com.mongodb.client.model.UpdateOneModel) BsonDocument(org.bson.BsonDocument) BulkWriteOptions(com.mongodb.client.model.BulkWriteOptions) WriteConcern(com.mongodb.WriteConcern) ArrayList(java.util.ArrayList) WriteModel(com.mongodb.client.model.WriteModel) BsonValue(org.bson.BsonValue)

Aggregations

UpdateOneModel (com.mongodb.client.model.UpdateOneModel)4 ArrayList (java.util.ArrayList)4 DeleteRequest (com.mongodb.bulk.DeleteRequest)2 InsertRequest (com.mongodb.bulk.InsertRequest)2 UpdateRequest (com.mongodb.bulk.UpdateRequest)2 WriteRequest (com.mongodb.bulk.WriteRequest)2 DeleteManyModel (com.mongodb.client.model.DeleteManyModel)2 DeleteOneModel (com.mongodb.client.model.DeleteOneModel)2 InsertOneModel (com.mongodb.client.model.InsertOneModel)2 ReplaceOneModel (com.mongodb.client.model.ReplaceOneModel)2 UpdateManyModel (com.mongodb.client.model.UpdateManyModel)2 MixedBulkWriteOperation (com.mongodb.operation.MixedBulkWriteOperation)2 CollectibleCodec (org.bson.codecs.CollectibleCodec)2 WriteConcern (com.mongodb.WriteConcern)1 BulkWriteOptions (com.mongodb.client.model.BulkWriteOptions)1 WriteModel (com.mongodb.client.model.WriteModel)1 ByteArrayByteIterator (com.yahoo.ycsb.ByteArrayByteIterator)1 ByteIterator (com.yahoo.ycsb.ByteIterator)1 DBException (com.yahoo.ycsb.DBException)1 HashMap (java.util.HashMap)1