@Override public BulkOperations insert(Object document) { Assert.notNull(document, "Document must not be null!"); if (document instanceof Document) { models.add(new InsertOneModel<>((Document) document)); return this; } Document sink = new Document(); mongoOperations.getConverter().write(document, sink); models.add(new InsertOneModel<>(sink)); return this; }
MixedBulkWriteOperation insertOne(final TDocument document, final InsertOneOptions options) { return bulkWrite(singletonList(new InsertOneModel<TDocument>(document)), new BulkWriteOptions().bypassDocumentValidation(options.getBypassDocumentValidation())); }
@Override public BulkOperations insert(Object document) { Assert.notNull(document, "Document must not be null!"); if (document instanceof Document) { models.add(new InsertOneModel<>((Document) document)); return this; } Document sink = new Document(); mongoOperations.getConverter().write(document, sink); models.add(new InsertOneModel<>(sink)); return this; }
public void Create(Document doc) { ops.add(new InsertOneModel<Document>(doc)); FlushOpsIfFull(); }
private TestRecord insertNewRecord(List<WriteModel<Document>> bulkWriter) { TestRecord tr = createNewRecord(); bulkWriter.add(new InsertOneModel<Document>(tr.internalDoc)); return tr; }
private List<WriteModel<Document>> createBulk() { final List<WriteModel<Document>> ops = new ArrayList<WriteModel<Document>>(); Document payload; for(int i=0; i<BULK_SIZE; i++) { payload = new Document(); payload.put("i", i); payload.put("t", new Date()); ops.add( new InsertOneModel<Document>(payload) ); } return ops; } }
@Test public void testBulkInsert() throws Exception { List<WriteModel<Document>> inserts = new ArrayList<>(); inserts.add(new InsertOneModel<>(json("_id: 1"))); inserts.add(new InsertOneModel<>(json("_id: 2"))); inserts.add(new InsertOneModel<>(json("_id: 3"))); BulkWriteResult result = collection.bulkWrite(inserts); assertThat(result.getInsertedCount()).isEqualTo(3); }
@Override protected void add(List<Object[]> records) throws ConnectionUnavailableException { List<InsertOneModel<Document>> parsedRecords = records.stream().map(record -> { Map<String, Object> insertMap = MongoTableUtils.mapValuesToAttributes(record, this.attributeNames); Document insertDocument = new Document(insertMap); if (log.isDebugEnabled()) { log.debug("Event formatted as document '" + insertDocument.toJson() + "' is used for building " + "Mongo Insert Model"); } return new InsertOneModel<>(insertDocument); }).collect(Collectors.toList()); this.bulkWrite(parsedRecords); }
@Override public void onEvent(List<SipMessage> messages) throws Exception { if (messages.isEmpty()) { return; } Map<String, List> documents = new HashMap<>(); for (SipMessage message : messages) { String suffix = defineSuffix(message); if (message.isOrigin()) { Document document = convertToIndex(message); documents.computeIfAbsent("index_" + suffix, k -> new ArrayList()).add(new InsertOneModel<>(document)); } Document document = convertToRaw(message); documents.computeIfAbsent("raw_" + suffix, k -> new ArrayList()).add(new InsertOneModel<>(document)); } documents.forEach((k, v) -> db.getCollection(k).bulkWrite(v)); }
Document record = buffer.poll(MAX_TIME_BEFORE_WRITE, TimeUnit.MILLISECONDS); if (record != null) { writes.add(new InsertOneModel<Document>(record)); count++;
Document record = buffer.poll(MAX_TIME_BEFORE_WRITE, TimeUnit.MILLISECONDS); if (record != null) { writes.add(new InsertOneModel<Document>(record)); count++;
@Override public void storeAll(Map<String, Supplement> map) { List<InsertOneModel> batch = new LinkedList<InsertOneModel>(); for (Map.Entry<String, Supplement> entry : map.entrySet()) { String key = entry.getKey(); Supplement value = entry.getValue(); batch.add(new InsertOneModel( new Document("name", value.getName()).append("price", value.getPrice()) .append("_id", key))); } this.collection.bulkWrite(batch, new BulkWriteOptions().ordered(false)); }
private <T extends ThingEvent> List<WriteModel<Document>> createPolicyUpdates(final T thingEvent, final Enforcer policyEnforcer) { final List<PolicyUpdate> updates = persistenceStrategyFactory .getStrategy(thingEvent) .policyUpdates(thingEvent, policyEnforcer); final List<WriteModel<Document>> writeModels = new ArrayList<>(); updates.forEach(policyUpdate -> { writeModels.add(new DeleteManyModel<>(policyUpdate.getPolicyIndexRemoveFilter())); policyUpdate.getPolicyIndexInsertEntries() .forEach(document -> writeModels.add(new InsertOneModel<>(document))); }); return writeModels; }
private <T extends ThingEvent> List<WriteModel<Document>> createPolicyUpdates(final T thingEvent, final Enforcer policyEnforcer) { final List<PolicyUpdate> updates = persistenceStrategyFactory .getStrategy(thingEvent) .policyUpdates(thingEvent, policyEnforcer); final List<WriteModel<Document>> writeModels = new ArrayList<>(); updates.forEach(policyUpdate -> { writeModels.add(new DeleteManyModel<>(policyUpdate.getPolicyIndexRemoveFilter())); policyUpdate.getPolicyIndexInsertEntries() .forEach(document -> writeModels.add(new InsertOneModel<>(document))); }); return writeModels; }
private void insertUpdateInBulk(boolean ordered) { List<WriteModel<Document>> ops = new ArrayList<>(); ops.add(new InsertOneModel<>(json("_id: 1, field: 'x'"))); ops.add(new InsertOneModel<>(json("_id: 2, field: 'x'"))); ops.add(new InsertOneModel<>(json("_id: 3, field: 'x'"))); ops.add(new UpdateManyModel<Document>(json("field: 'x'"), set("field", "y"))); BulkWriteResult result = collection.bulkWrite(ops, new BulkWriteOptions().ordered(ordered)); assertThat(result.getInsertedCount()).isEqualTo(3); assertThat(result.getDeletedCount()).isEqualTo(0); assertThat(result.getModifiedCount()).isEqualTo(3); assertThat(result.getMatchedCount()).isEqualTo(3); long totalDocuments = collection.countDocuments(); assertThat(totalDocuments).isEqualTo(3); long documentsWithY = collection.countDocuments(json("field: 'y'")); assertThat(documentsWithY).isEqualTo(3); }
MixedBulkWriteOperation insertOne(final TDocument document, final InsertOneOptions options) { return bulkWrite(singletonList(new InsertOneModel<TDocument>(document)), new BulkWriteOptions().bypassDocumentValidation(options.getBypassDocumentValidation())); }
@Override public StorageWriteResult write(List<DataContainer> containers) throws Exception { MongoCollection<Document> collection = MongoStorageAdapter.getCollection(MongoStorageAdapter.collectionEventRecordsName); // Build an array of documents List<WriteModel<Document>> documents = new ArrayList<>(); for (DataContainer container : containers) { Document document = documentFromView(container); //Prism.getInstance().getLogger().debug(DataUtil.jsonFromDataView(container).toString()); // TTL document.append("Expires", DateUtil.parseTimeStringToDate(expiration, true)); // Insert documents.add(new InsertOneModel<>(document)); } // Write collection.bulkWrite(documents, bulkWriteOptions); // @todo implement real results, BulkWriteResult return new StorageWriteResult(); }
/** * This method insert a list of documents into a collection. Params w and wtimeout are read from QueryOptions. * * @param documentList The new list of documents to be inserted * @param options Some options like timeout * @return A BulkWriteResult from MongoDB API */ public BulkWriteResult insert(List<Document> documentList, QueryOptions options) { List<WriteModel<Document>> actions = new ArrayList<>(documentList.size()); for (Document document : documentList) { actions.add(new InsertOneModel<>(document)); } int writeConcern = 1; int ms = 0; if (options != null && (options.containsKey("w") || options.containsKey("wtimeout"))) { writeConcern = options.getInt("w", 1); ms = options.getInt("wtimeout", 0); } dbCollection.withWriteConcern(new WriteConcern(writeConcern, ms)); return dbCollection.bulkWrite(actions, new BulkWriteOptions().ordered(false)); }
private static int doInsert(final MongoDBQueryDescriptor queryDesc, final MongoCollection<Document> collection) { Document options = queryDesc.getOptions(); Boolean ordered = FALSE; WriteConcern wc = null; if ( options != null ) { ordered = (Boolean) options.get( "ordered" ); ordered = ( ordered != null ) ? ordered : FALSE; Document o = (Document) options.get( "writeConcern" ); wc = getWriteConcern( o ); } // Need to use BulkWriteOperation here rather than collection.insert(..) because the WriteResult returned // by the latter returns 0 for getN() even if the insert was successful (which is bizarre, but that's the way it // is defined...) List<InsertOneModel<Document>> operationList = null; if ( queryDesc.getUpdateOrInsertMany() != null ) { operationList = new ArrayList<>( queryDesc.getUpdateOrInsertMany().size() ); for ( Document doc : queryDesc.getUpdateOrInsertMany() ) { operationList.add( new InsertOneModel<>( doc ) ); } } else { operationList = new ArrayList<>( 1 ); operationList.add( new InsertOneModel<>( queryDesc.getUpdateOrInsertOne() ) ); } final BulkWriteResult result = collection.withWriteConcern( ( wc != null ? wc : collection.getWriteConcern() ) ).bulkWrite( operationList, new BulkWriteOptions().ordered( ordered ) ); if ( result.wasAcknowledged() ) { return result.getInsertedCount(); } return -1; // Not sure if we should throw an exception instead? }
private static int doInsert(final MongoDBQueryDescriptor queryDesc, final MongoCollection<Document> collection) { Document options = queryDesc.getOptions(); Boolean ordered = FALSE; WriteConcern wc = null; if ( options != null ) { ordered = (Boolean) options.get( "ordered" ); ordered = ( ordered != null ) ? ordered : FALSE; Document o = (Document) options.get( "writeConcern" ); wc = getWriteConcern( o ); } // Need to use BulkWriteOperation here rather than collection.insert(..) because the WriteResult returned // by the latter returns 0 for getN() even if the insert was successful (which is bizarre, but that's the way it // is defined...) List<InsertOneModel<Document>> operationList = null; if ( queryDesc.getUpdateOrInsertMany() != null ) { operationList = new ArrayList<>( queryDesc.getUpdateOrInsertMany().size() ); for ( Document doc : queryDesc.getUpdateOrInsertMany() ) { operationList.add( new InsertOneModel<>( doc ) ); } } else { operationList = new ArrayList<>( 1 ); operationList.add( new InsertOneModel<>( queryDesc.getUpdateOrInsertOne() ) ); } final BulkWriteResult result = collection.withWriteConcern( ( wc != null ? wc : collection.getWriteConcern() ) ).bulkWrite( operationList, new BulkWriteOptions().ordered( ordered ) ); if ( result.wasAcknowledged() ) { return result.getInsertedCount(); } return -1; // Not sure if we should throw an exception instead? }