@Test @RunWithCustomExecutor public void should_skip_when_store_two_document_with_same_id() { String id1 = GUIDFactory.newUnitGUID(TENANT_ID).toString(); String id3 = GUIDFactory.newUnitGUID(TENANT_ID).toString(); Unit unit1 = createUnit(id1); Unit unit2 = createUnit(id1); Unit unit3 = createUnit(id3); assertThatCode(() -> unitMongoDbMetadataRepository.insert(Lists.newArrayList(unit1, unit2, unit3))) .doesNotThrowAnyException(); }
public void insert(List<T> metadataDocuments) throws MetaDataExecutionException { BulkWriteOptions options = new BulkWriteOptions(); options.ordered(false); try { List<InsertOneModel<T>> collect = new ArrayList<>(); metadataDocuments.forEach(metadataDocument -> { metadataDocument.append(VERSION, 0); metadataDocument.append(ATOMIC_VERSION, 0); metadataDocument.append(TENANT_ID, ParameterHelper.getTenantParameter()); InsertOneModel<T> tInsertOneModel = new InsertOneModel<>(metadataDocument); collect.add(tInsertOneModel); }); BulkWriteResult bulkWriteResult = mongoCollectionSupplier.get().bulkWrite(collect, options); if (bulkWriteResult.getInsertedCount() != metadataDocuments.size()) { throw new MetaDataExecutionException( String.format("Error while bulk save document count : %s != size : %s :", bulkWriteResult.getInsertedCount(), metadataDocuments.size())); } } catch (final MongoBulkWriteException e) { boolean hasBlockerErrors = false; for (BulkWriteError bulkWriteError : e.getWriteErrors()) { if (bulkWriteError.getCategory() == ErrorCategory.DUPLICATE_KEY) { LOGGER.warn("Document already exists " + metadataDocuments.get(bulkWriteError.getIndex()).getId() + ". Ignoring quietly (idempotency)"); } else { hasBlockerErrors = true; LOGGER.error("An error occurred during metadata insert " + bulkWriteError); } } if(hasBlockerErrors) { throw new MetaDataExecutionException(e); } } catch (final MongoException | IllegalArgumentException e) { throw new MetaDataExecutionException(e); } }
MongoDbMetadataRepository { public void insert(List<T> metadataDocuments) throws MetaDataExecutionException { BulkWriteOptions options = new BulkWriteOptions(); options.ordered(false); try { List<InsertOneModel<T>> collect = new ArrayList<>(); metadataDocuments.forEach(metadataDocument -> { metadataDocument.append(VERSION, 0); metadataDocument.append(ATOMIC_VERSION, 0); metadataDocument.append(TENANT_ID, ParameterHelper.getTenantParameter()); InsertOneModel<T> tInsertOneModel = new InsertOneModel<>(metadataDocument); collect.add(tInsertOneModel); }); BulkWriteResult bulkWriteResult = mongoCollectionSupplier.get().bulkWrite(collect, options); if (bulkWriteResult.getInsertedCount() != metadataDocuments.size()) { throw new MetaDataExecutionException( String.format("Error while bulk save document count : %s != size : %s :", bulkWriteResult.getInsertedCount(), metadataDocuments.size())); } } catch (final MongoBulkWriteException e) { boolean hasBlockerErrors = false; for (BulkWriteError bulkWriteError : e.getWriteErrors()) { if (bulkWriteError.getCategory() == ErrorCategory.DUPLICATE_KEY) { LOGGER.warn("Document already exists " + metadataDocuments.get(bulkWriteError.getIndex()).getId() + ". Ignoring quietly (idempotency)"); } else { hasBlockerErrors = true; LOGGER.error("An error occurred during metadata insert " + bulkWriteError); } } if(hasBlockerErrors) { throw new MetaDataExecutionException(e); } } catch (final MongoException | IllegalArgumentException e) { throw new MetaDataExecutionException(e); } } }
MongoDbMetadataRepository { public void insert(List<T> metadataDocuments) throws MetaDataExecutionException { BulkWriteOptions options = new BulkWriteOptions(); options.ordered(false); try { List<InsertOneModel<T>> collect = new ArrayList<>(); metadataDocuments.forEach(metadataDocument -> { metadataDocument.append(VERSION, 0); metadataDocument.append(ATOMIC_VERSION, 0); metadataDocument.append(TENANT_ID, ParameterHelper.getTenantParameter()); InsertOneModel<T> tInsertOneModel = new InsertOneModel<>(metadataDocument); collect.add(tInsertOneModel); }); BulkWriteResult bulkWriteResult = mongoCollectionSupplier.get().bulkWrite(collect, options); if (bulkWriteResult.getInsertedCount() != metadataDocuments.size()) { throw new MetaDataExecutionException( String.format("Error while bulk save document count : %s != size : %s :", bulkWriteResult.getInsertedCount(), metadataDocuments.size())); } } catch (final MongoBulkWriteException e) { boolean hasBlockerErrors = false; for (BulkWriteError bulkWriteError : e.getWriteErrors()) { if (bulkWriteError.getCategory() == ErrorCategory.DUPLICATE_KEY) { LOGGER.warn("Document already exists " + metadataDocuments.get(bulkWriteError.getIndex()).getId() + ". Ignoring quietly (idempotency)"); } else { hasBlockerErrors = true; LOGGER.error("An error occurred during metadata insert " + bulkWriteError); } } if(hasBlockerErrors) { throw new MetaDataExecutionException(e); } } catch (final MongoException | IllegalArgumentException e) { throw new MetaDataExecutionException(e); } } MongoDbMetadataRepository(Supplier<MongoCollection<T>> mongoCollectionSupplier); }
MongoDbMetadataRepository { public void insert(List<T> metadataDocuments) throws MetaDataExecutionException { BulkWriteOptions options = new BulkWriteOptions(); options.ordered(false); try { List<InsertOneModel<T>> collect = new ArrayList<>(); metadataDocuments.forEach(metadataDocument -> { metadataDocument.append(VERSION, 0); metadataDocument.append(ATOMIC_VERSION, 0); metadataDocument.append(TENANT_ID, ParameterHelper.getTenantParameter()); InsertOneModel<T> tInsertOneModel = new InsertOneModel<>(metadataDocument); collect.add(tInsertOneModel); }); BulkWriteResult bulkWriteResult = mongoCollectionSupplier.get().bulkWrite(collect, options); if (bulkWriteResult.getInsertedCount() != metadataDocuments.size()) { throw new MetaDataExecutionException( String.format("Error while bulk save document count : %s != size : %s :", bulkWriteResult.getInsertedCount(), metadataDocuments.size())); } } catch (final MongoBulkWriteException e) { boolean hasBlockerErrors = false; for (BulkWriteError bulkWriteError : e.getWriteErrors()) { if (bulkWriteError.getCategory() == ErrorCategory.DUPLICATE_KEY) { LOGGER.warn("Document already exists " + metadataDocuments.get(bulkWriteError.getIndex()).getId() + ". Ignoring quietly (idempotency)"); } else { hasBlockerErrors = true; LOGGER.error("An error occurred during metadata insert " + bulkWriteError); } } if(hasBlockerErrors) { throw new MetaDataExecutionException(e); } } catch (final MongoException | IllegalArgumentException e) { throw new MetaDataExecutionException(e); } } MongoDbMetadataRepository(Supplier<MongoCollection<T>> mongoCollectionSupplier); Collection<T> selectByIds(Iterable<? extends String> ids, BasicDBObject projection); void insert(List<T> metadataDocuments); void delete(List<T> metadataDocuments); void update(Map<String, Bson> updates); }
MongoDbMetadataRepository { public void insert(List<T> metadataDocuments) throws MetaDataExecutionException { BulkWriteOptions options = new BulkWriteOptions(); options.ordered(false); try { List<InsertOneModel<T>> collect = new ArrayList<>(); metadataDocuments.forEach(metadataDocument -> { metadataDocument.append(VERSION, 0); metadataDocument.append(ATOMIC_VERSION, 0); metadataDocument.append(TENANT_ID, ParameterHelper.getTenantParameter()); InsertOneModel<T> tInsertOneModel = new InsertOneModel<>(metadataDocument); collect.add(tInsertOneModel); }); BulkWriteResult bulkWriteResult = mongoCollectionSupplier.get().bulkWrite(collect, options); if (bulkWriteResult.getInsertedCount() != metadataDocuments.size()) { throw new MetaDataExecutionException( String.format("Error while bulk save document count : %s != size : %s :", bulkWriteResult.getInsertedCount(), metadataDocuments.size())); } } catch (final MongoBulkWriteException e) { boolean hasBlockerErrors = false; for (BulkWriteError bulkWriteError : e.getWriteErrors()) { if (bulkWriteError.getCategory() == ErrorCategory.DUPLICATE_KEY) { LOGGER.warn("Document already exists " + metadataDocuments.get(bulkWriteError.getIndex()).getId() + ". Ignoring quietly (idempotency)"); } else { hasBlockerErrors = true; LOGGER.error("An error occurred during metadata insert " + bulkWriteError); } } if(hasBlockerErrors) { throw new MetaDataExecutionException(e); } } catch (final MongoException | IllegalArgumentException e) { throw new MetaDataExecutionException(e); } } MongoDbMetadataRepository(Supplier<MongoCollection<T>> mongoCollectionSupplier); Collection<T> selectByIds(Iterable<? extends String> ids, BasicDBObject projection); void insert(List<T> metadataDocuments); void delete(List<T> metadataDocuments); void update(Map<String, Bson> updates);  }