Search in sources :

Example 1 with CommunicationException

use of jakarta.nosql.CommunicationException in project jnosql-diana-driver by eclipse.

the class QueryUtils method getUdtValue.

private static Object getUdtValue(UserDefinedType userType, Iterable elements, DataType type) {
    Collection<Object> udtValues = getCollectionUdt(type);
    UdtValue udtValue = userType.newValue();
    final List<String> udtNames = userType.getFieldNames().stream().map(CqlIdentifier::asInternal).collect(Collectors.toList());
    for (Object object : elements) {
        if (Column.class.isInstance(object)) {
            Column column = Column.class.cast(object);
            Object convert = ValueUtil.convert(column.getValue());
            final int index = udtNames.indexOf(column.getName());
            if (index < 0) {
                throw new CommunicationException("This field has not been found: " + column.getName() + " the fields available are " + udtNames + " in the UDT type " + userType.getName().asCql(true) + " at the keyspace " + userType.getKeyspace());
            }
            DataType fieldType = userType.getFieldTypes().get(index);
            TypeCodec<Object> objectTypeCodec = CodecRegistry.DEFAULT.codecFor(fieldType);
            if (fieldType instanceof SetType) {
                udtValue.set(getName(column), new HashSet<Object>((Collection<?>) convert), objectTypeCodec);
            } else {
                udtValue.set(getName(column), convert, objectTypeCodec);
            }
        } else if (Iterable.class.isInstance(object)) {
            udtValues.add(getUdtValue(userType, Iterable.class.cast(Iterable.class.cast(object)), type));
        }
    }
    if (udtValues.isEmpty()) {
        return udtValue;
    }
    return udtValues;
}
Also used : UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) CommunicationException(jakarta.nosql.CommunicationException) Column(jakarta.nosql.column.Column) SetType(com.datastax.oss.driver.api.core.type.SetType) DataType(com.datastax.oss.driver.api.core.type.DataType) Collection(java.util.Collection)

Example 2 with CommunicationException

use of jakarta.nosql.CommunicationException in project jnosql-diana-driver by eclipse.

the class DefaultElasticsearchDocumentCollectionManager method count.

@Override
public long count(String documentCollection) {
    Objects.requireNonNull(documentCollection, "query is required");
    SearchRequest searchRequest = new SearchRequest(index);
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.size(0);
    try {
        SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
        return search.getHits().getTotalHits().value;
    } catch (IOException e) {
        throw new CommunicationException("Error on ES when try to execute count to document collection:" + documentCollection, e);
    }
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) CommunicationException(jakarta.nosql.CommunicationException) IOException(java.io.IOException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

CommunicationException (jakarta.nosql.CommunicationException)2 UdtValue (com.datastax.oss.driver.api.core.data.UdtValue)1 DataType (com.datastax.oss.driver.api.core.type.DataType)1 SetType (com.datastax.oss.driver.api.core.type.SetType)1 Column (jakarta.nosql.column.Column)1 IOException (java.io.IOException)1 Collection (java.util.Collection)1 SearchRequest (org.elasticsearch.action.search.SearchRequest)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)1