Search in sources :

Example 1 with Try

use of com.lambdista.util.Try in project hopsworks by logicalclocks.

the class LibraryController method pipList.

/**
 * @param <R> parsed elastic item
 * @param <S1> intermediate result wrapped in Try
 * @param <S2> final result
 * @return
 * @throws ProvenanceException
 */
private <R, S1, S2> S2 pipList(String query, Integer offset, Integer limit, LibraryController.HandlerFactory<R, S1, S2> handlerFactory, String index) throws ServiceException {
    Pair<Long, Try<S1>> searchResult;
    try {
        CheckedSupplier<SearchRequest, ProvenanceException> srF = ElasticHelper.baseSearchRequest(index, settings.getElasticDefaultScrollPageSize()).andThen(ElasticHelper.withPagination(offset, limit, settings.getElasticDefaultScrollPageSize()));
        SearchRequest request = srF.get();
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(ElasticHelper.fullTextSearch("library", query));
        request.source(sourceBuilder);
        searchResult = provElasticController.search(request, handlerFactory.getHandler());
    } catch (ElasticException | ProvenanceException pe) {
        throw new ServiceException(RESTCodes.ServiceErrorCode.ANACONDA_LIST_LIB_ERROR, Level.FINE, "Unable to list python libraries", pe.getMessage(), pe);
    }
    return handlerFactory.checkedResult(searchResult);
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) ElasticException(io.hops.hopsworks.exceptions.ElasticException) ProvenanceException(io.hops.hopsworks.exceptions.ProvenanceException) ServiceException(io.hops.hopsworks.exceptions.ServiceException) Try(com.lambdista.util.Try) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 2 with Try

use of com.lambdista.util.Try in project hopsworks by logicalclocks.

the class ProvAppController method provAppState.

public Map<String, Map<Provenance.AppState, ProvAppStateElastic>> provAppState(Map<ProvParser.Field, ProvParser.FilterVal> filterBy, List<Pair<ProvParser.Field, SortOrder>> sortBy, Integer offset, Integer limit) throws ProvenanceException {
    CheckedSupplier<SearchRequest, ProvenanceException> srF = ElasticHelper.scrollingSearchRequest(Settings.ELASTIC_INDEX_APP_PROVENANCE, settings.getElasticDefaultScrollPageSize()).andThen(provAppStateQB(filterBy)).andThen(ElasticHelper.sortBy(sortBy)).andThen(ElasticHelper.withPagination(offset, limit, settings.getElasticMaxScrollPageSize()));
    SearchRequest request = srF.get();
    Pair<Long, Try<ElasticAppStatesObj>> searchResult;
    try {
        searchResult = client.searchScrolling(request, ElasticAppStatesObj.getHandler());
    } catch (ElasticException e) {
        String msg = "provenance - elastic query problem";
        throw ProvHelper.fromElastic(e, msg, msg + " - app state");
    }
    return ElasticAppStatesObj.checkedResult(searchResult);
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) ElasticException(io.hops.hopsworks.exceptions.ElasticException) ProvenanceException(io.hops.hopsworks.exceptions.ProvenanceException) Try(com.lambdista.util.Try)

Example 3 with Try

use of com.lambdista.util.Try in project hopsworks by logicalclocks.

the class ProvStateController method provFileState.

/**
 * @param <R> parsed elastic item
 * @param <S1> intermediate result wrapped in Try
 * @param <S2> final result
 * @return
 * @throws ProvenanceException
 */
private <R, S1, S2> S2 provFileState(Long projectIId, Map<ProvParser.Field, ProvParser.FilterVal> fileStateFilters, List<Pair<ProvParser.Field, SortOrder>> fileStateSortBy, Map<String, String> xAttrsFilters, Map<String, String> likeXAttrsFilters, Set<String> hasXAttrsFilters, List<ProvStateParamBuilder.SortE> xattrSortBy, Integer offset, Integer limit, HandlerFactory<R, S1, S2> handlerFactory) throws ProvenanceException {
    CheckedSupplier<SearchRequest, ProvenanceException> srF = ElasticHelper.baseSearchRequest(settings.getProvFileIndex(projectIId), settings.getElasticDefaultScrollPageSize()).andThen(filterByStateParams(fileStateFilters, xAttrsFilters, likeXAttrsFilters, hasXAttrsFilters)).andThen(ElasticHelper.withFileStateOrder(fileStateSortBy, xattrSortBy)).andThen(ElasticHelper.withPagination(offset, limit, settings.getElasticMaxScrollPageSize()));
    SearchRequest request = srF.get();
    Pair<Long, Try<S1>> searchResult;
    try {
        searchResult = client.search(request, handlerFactory.getHandler());
    } catch (ElasticException e) {
        String msg = "provenance - elastic query problem";
        throw ProvHelper.fromElastic(e, msg, msg + " - file state");
    }
    return handlerFactory.checkedResult(searchResult);
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) ElasticException(io.hops.hopsworks.exceptions.ElasticException) ProvenanceException(io.hops.hopsworks.exceptions.ProvenanceException) Try(com.lambdista.util.Try)

Example 4 with Try

use of com.lambdista.util.Try in project hopsworks by logicalclocks.

the class TestElasticClientController method testScrollingHasLeftoverWithScrollId.

@Test
public void testScrollingHasLeftoverWithScrollId() throws ElasticException {
    // arrange
    int totalHits = 2;
    SearchRequest request = new SearchRequest();
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    sourceBuilder.size(totalHits);
    request.source(sourceBuilder);
    SearchResponse response = createResponse(new SearchHit[] { createHit }, totalHits, "");
    // region Mock
    Mockito.doReturn(null).when(client).baseSearch(Mockito.any());
    Mockito.doReturn(createResponse(new SearchHit[] { readHit }, totalHits, "")).when(client).searchScrollingInt(Mockito.any());
    Mockito.doReturn(null).when(client).clearScrollingContext(Mockito.any());
    // endregion
    // act
    Pair<Long, Try<Object>> result = client.scrolling(response, handler, request);
    // assert
    Assert.assertEquals(2, result.getValue0().intValue());
    Mockito.verify(client, Mockito.times(0)).baseSearch(Mockito.any());
    Mockito.verify(client, Mockito.times(1)).searchScrollingInt(Mockito.any());
    Mockito.verify(client, Mockito.times(1)).clearScrollingContext(Mockito.any());
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) SearchHit(org.elasticsearch.search.SearchHit) Try(com.lambdista.util.Try) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) Test(org.junit.Test)

Example 5 with Try

use of com.lambdista.util.Try in project hopsworks by logicalclocks.

the class TestElasticClientController method testScrollingWithScrollId.

@Test
public void testScrollingWithScrollId() throws ElasticException {
    // arrange
    int totalHits = 1;
    SearchRequest request = new SearchRequest();
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    sourceBuilder.size(totalHits);
    request.source(sourceBuilder);
    SearchResponse response = createResponse(new SearchHit[] { createHit }, totalHits, "");
    // region Mock
    Mockito.doReturn(null).when(client).baseSearch(Mockito.any());
    Mockito.doReturn(null).when(client).searchScrollingInt(Mockito.any());
    Mockito.doReturn(null).when(client).clearScrollingContext(Mockito.any());
    // endregion
    // act
    Pair<Long, Try<Object>> result = client.scrolling(response, handler, request);
    // assert
    Assert.assertEquals(1, result.getValue0().intValue());
    Mockito.verify(client, Mockito.times(0)).baseSearch(Mockito.any());
    Mockito.verify(client, Mockito.times(0)).searchScrollingInt(Mockito.any());
    Mockito.verify(client, Mockito.times(1)).clearScrollingContext(Mockito.any());
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) Try(com.lambdista.util.Try) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) Test(org.junit.Test)

Aggregations

Try (com.lambdista.util.Try)7 SearchRequest (org.elasticsearch.action.search.SearchRequest)7 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)5 SearchResponse (org.elasticsearch.action.search.SearchResponse)4 Test (org.junit.Test)4 ElasticException (io.hops.hopsworks.exceptions.ElasticException)3 ProvenanceException (io.hops.hopsworks.exceptions.ProvenanceException)3 SearchHit (org.elasticsearch.search.SearchHit)2 ServiceException (io.hops.hopsworks.exceptions.ServiceException)1