use of org.jnosql.diana.api.document.DocumentDeleteQuery in project jnosql-diana-driver by eclipse.
the class DefaultElasticsearchDocumentCollectionManager method delete.
@Override
public void delete(DocumentDeleteQuery query) throws NullPointerException {
requireNonNull(query, "query is required");
query.getCondition().orElseThrow(() -> new IllegalArgumentException("condition is required"));
DocumentQuery select = new ElasticsearchDocumentQuery(query);
List<DocumentEntity> entities = select(select);
if (entities.isEmpty()) {
return;
}
BulkRequest bulk = new BulkRequest();
entities.stream().map(entity -> entity.find(ID_FIELD).get().get(String.class)).map(id -> new DeleteRequest(index, query.getDocumentCollection(), id)).forEach(bulk::add);
try {
client.bulk(bulk);
} catch (IOException e) {
throw new ElasticsearchException("An error to delete entities on elasticsearch", e);
}
}
use of org.jnosql.diana.api.document.DocumentDeleteQuery in project jnosql-diana-driver by eclipse.
the class DefaultElasticsearchDocumentCollectionManagerAsync method delete.
@Override
public void delete(DocumentDeleteQuery query, Consumer<Void> callBack) {
requireNonNull(query, "query is required");
requireNonNull(callBack, "callBack is required");
query.getCondition().orElseThrow(() -> new IllegalArgumentException("condition is required"));
DocumentQuery select = new ElasticsearchDocumentQuery(query);
List<DocumentEntity> entities = EntityConverter.query(select, client, index);
if (entities.isEmpty()) {
callBack.accept(null);
return;
}
BulkRequest bulk = new BulkRequest();
entities.stream().map(entity -> entity.find(ID_FIELD).get().get(String.class)).map(id -> new DeleteRequest(index, query.getDocumentCollection(), id)).forEach(bulk::add);
ActionListener<BulkResponse> listener = new ActionListener<BulkResponse>() {
@Override
public void onResponse(BulkResponse bulkItemResponses) {
callBack.accept(null);
}
@Override
public void onFailure(Exception e) {
throw new ExecuteAsyncQueryException("An error when delete on elasticsearch", e);
}
};
client.bulkAsync(bulk, listener);
}
use of org.jnosql.diana.api.document.DocumentDeleteQuery in project jnosql-diana-driver by eclipse.
the class OrientDBDocumentCollectionManagerAsyncTest method shouldRemoveEntityAsync.
@Test
public void shouldRemoveEntityAsync() throws InterruptedException {
DocumentEntity documentEntity = entityManager.insert(getEntity());
Document id = documentEntity.find("name").get();
DocumentQuery query = select().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
DocumentDeleteQuery deleteQuery = delete().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
AtomicBoolean condition = new AtomicBoolean(false);
entityManagerAsync.delete(deleteQuery, c -> condition.set(true));
await().untilTrue(condition);
assertTrue(entityManager.select(query).isEmpty());
}
use of org.jnosql.diana.api.document.DocumentDeleteQuery in project jnosql-diana-driver by eclipse.
the class OrientDBDocumentCollectionManagerAsyncTest method shouldRemoveEntityAsyncWithoutCallback.
@Test
public void shouldRemoveEntityAsyncWithoutCallback() throws InterruptedException {
DocumentEntity entity = entityManager.insert(getEntity());
Document id = entity.find(OrientDBConverter.RID_FIELD).get();
DocumentQuery query = select().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
DocumentDeleteQuery deleteQuery = delete().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
entityManagerAsync.delete(deleteQuery);
Thread.sleep(1000L);
assertTrue(entityManager.select(query).isEmpty());
}
use of org.jnosql.diana.api.document.DocumentDeleteQuery in project jnosql-diana-driver by eclipse.
the class OrientDBDocumentCollectionManagerAsyncTest method removePersons.
@AfterEach
void removePersons() {
DocumentDeleteQuery query = delete().from(COLLECTION_NAME).build();
entityManager.delete(query);
}
Aggregations