Search in sources :

Example 1 with ExecuteAsyncQueryException

use of org.jnosql.diana.api.ExecuteAsyncQueryException 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);
}
Also used : QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Document(org.jnosql.diana.api.document.Document) JsonbBuilder(javax.json.bind.JsonbBuilder) ExecuteAsyncQueryException(org.jnosql.diana.api.ExecuteAsyncQueryException) EntityConverter.getMap(org.jnosql.diana.elasticsearch.document.EntityConverter.getMap) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IOException(java.io.IOException) SearchRequest(org.elasticsearch.action.search.SearchRequest) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) DocumentDeleteQuery(org.jnosql.diana.api.document.DocumentDeleteQuery) ID_FIELD(org.jnosql.diana.elasticsearch.document.EntityConverter.ID_FIELD) DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) Consumer(java.util.function.Consumer) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) List(java.util.List) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) Duration(java.time.Duration) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) Jsonb(javax.json.bind.Jsonb) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) ActionListener(org.elasticsearch.action.ActionListener) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) ExecuteAsyncQueryException(org.jnosql.diana.api.ExecuteAsyncQueryException) IOException(java.io.IOException) DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) ExecuteAsyncQueryException(org.jnosql.diana.api.ExecuteAsyncQueryException) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) ActionListener(org.elasticsearch.action.ActionListener) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 2 with ExecuteAsyncQueryException

use of org.jnosql.diana.api.ExecuteAsyncQueryException in project jnosql-diana-driver by eclipse.

the class MongoDBDocumentCollectionManagerAsync method select.

@Override
public void select(DocumentQuery query, Consumer<List<DocumentEntity>> callBack) throws ExecuteAsyncQueryException, UnsupportedOperationException {
    String collectionName = query.getDocumentCollection();
    MongoCollection<Document> collection = asyncMongoDatabase.getCollection(collectionName);
    Bson mongoDBQuery = query.getCondition().map(DocumentQueryConversor::convert).orElse(EMPTY);
    List<DocumentEntity> entities = new CopyOnWriteArrayList<>();
    FindIterable<Document> result = collection.find(mongoDBQuery);
    Block<Document> documentBlock = d -> entities.add(createEntity(collectionName, d));
    SingleResultCallback<Void> voidSingleResultCallback = (v, e) -> callBack.accept(entities);
    result.forEach(documentBlock, voidSingleResultCallback);
}
Also used : FindIterable(com.mongodb.async.client.FindIterable) Document(org.bson.Document) MongoDBUtils.getDocument(org.jnosql.diana.mongodb.document.MongoDBUtils.getDocument) ExecuteAsyncQueryException(org.jnosql.diana.api.ExecuteAsyncQueryException) DocumentCollectionManagerAsync(org.jnosql.diana.api.document.DocumentCollectionManagerAsync) MongoCollection(com.mongodb.async.client.MongoCollection) DocumentDeleteQuery(org.jnosql.diana.api.document.DocumentDeleteQuery) BsonDocument(org.bson.BsonDocument) DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) Bson(org.bson.conversions.Bson) Consumer(java.util.function.Consumer) List(java.util.List) Documents(org.jnosql.diana.api.document.Documents) Block(com.mongodb.Block) MongoDatabase(com.mongodb.async.client.MongoDatabase) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) Duration(java.time.Duration) DeleteResult(com.mongodb.client.result.DeleteResult) ID_FIELD(org.jnosql.diana.mongodb.document.MongoDBUtils.ID_FIELD) SingleResultCallback(com.mongodb.async.SingleResultCallback) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) Document(org.bson.Document) MongoDBUtils.getDocument(org.jnosql.diana.mongodb.document.MongoDBUtils.getDocument) BsonDocument(org.bson.BsonDocument) Bson(org.bson.conversions.Bson) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 3 with ExecuteAsyncQueryException

use of org.jnosql.diana.api.ExecuteAsyncQueryException in project jnosql-diana-driver by eclipse.

the class DefaultOrientDBDocumentCollectionManagerAsync method insert.

@Override
public void insert(DocumentEntity entity, Consumer<DocumentEntity> callBack) throws ExecuteAsyncQueryException, UnsupportedOperationException {
    requireNonNull(entity, "Entity is required");
    requireNonNull(callBack, "Callback is required");
    ODatabaseDocumentTx tx = pool.acquire();
    ODocument document = new ODocument(entity.getName());
    Map<String, Object> entityValues = entity.toMap();
    entityValues.keySet().stream().forEach(k -> document.field(k, entityValues.get(k)));
    ORecordCallback<Number> createCallBack = (rid, clusterPosition) -> {
        entity.add(Document.of(RID_FIELD, rid.toString()));
        callBack.accept(entity);
    };
    ORecordCallback<Integer> updateCallback = (rid, version) -> {
        entity.add(Document.of(RID_FIELD, rid.toString()));
        entity.add(Document.of(VERSION_FIELD, version));
        callBack.accept(entity);
    };
    tx.save(document, null, ASYNCHRONOUS, false, createCallBack, updateCallback);
}
Also used : ASYNCHRONOUS(com.orientechnologies.orient.core.db.ODatabase.OPERATION_MODE.ASYNCHRONOUS) VERSION_FIELD(org.jnosql.diana.orientdb.document.OrientDBConverter.VERSION_FIELD) Document(org.jnosql.diana.api.document.Document) ExecuteAsyncQueryException(org.jnosql.diana.api.ExecuteAsyncQueryException) ORecordCallback(com.orientechnologies.orient.core.storage.ORecordCallback) RID_FIELD(org.jnosql.diana.orientdb.document.OrientDBConverter.RID_FIELD) DocumentDeleteQuery(org.jnosql.diana.api.document.DocumentDeleteQuery) DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) Consumer(java.util.function.Consumer) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) Duration(java.time.Duration) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OPartitionedDatabasePool(com.orientechnologies.orient.core.db.OPartitionedDatabasePool) ORecordAbstract(com.orientechnologies.orient.core.record.ORecordAbstract) QueryOSQLFactory.toAsync(org.jnosql.diana.orientdb.document.QueryOSQLFactory.toAsync) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 4 with ExecuteAsyncQueryException

use of org.jnosql.diana.api.ExecuteAsyncQueryException in project jnosql-diana-driver by eclipse.

the class CassandraReturnQueryAsync method run.

@Override
public void run() {
    try {
        ResultSet resultSet = this.resultSet.get();
        List<ColumnEntity> entities = resultSet.all().stream().map(CassandraConverter::toDocumentEntity).collect(Collectors.toList());
        consumer.accept(entities);
    } catch (InterruptedException | ExecutionException e) {
        throw new ExecuteAsyncQueryException(e);
    }
}
Also used : ExecuteAsyncQueryException(org.jnosql.diana.api.ExecuteAsyncQueryException) ColumnEntity(org.jnosql.diana.api.column.ColumnEntity) ResultSet(com.datastax.driver.core.ResultSet) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

ExecuteAsyncQueryException (org.jnosql.diana.api.ExecuteAsyncQueryException)4 Duration (java.time.Duration)3 List (java.util.List)3 Consumer (java.util.function.Consumer)3 DocumentDeleteQuery (org.jnosql.diana.api.document.DocumentDeleteQuery)3 DocumentEntity (org.jnosql.diana.api.document.DocumentEntity)3 DocumentQuery (org.jnosql.diana.api.document.DocumentQuery)3 Map (java.util.Map)2 Objects.requireNonNull (java.util.Objects.requireNonNull)2 Document (org.jnosql.diana.api.document.Document)2 ResultSet (com.datastax.driver.core.ResultSet)1 Block (com.mongodb.Block)1 SingleResultCallback (com.mongodb.async.SingleResultCallback)1 FindIterable (com.mongodb.async.client.FindIterable)1 MongoCollection (com.mongodb.async.client.MongoCollection)1 MongoDatabase (com.mongodb.async.client.MongoDatabase)1 DeleteResult (com.mongodb.client.result.DeleteResult)1 ASYNCHRONOUS (com.orientechnologies.orient.core.db.ODatabase.OPERATION_MODE.ASYNCHRONOUS)1 OPartitionedDatabasePool (com.orientechnologies.orient.core.db.OPartitionedDatabasePool)1 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)1