Search in sources :

Example 11 with ResultsPage

use of datawave.webservice.query.cache.ResultsPage in project datawave by NationalSecurityAgency.

the class CompositeQueryLogic method getTransformer.

/**
 * Method used to check that the configuration is correct and to get the response class by QueryExecutorBean.listQueryLogic()
 */
@Override
public synchronized QueryLogicTransformer getTransformer(Query settings) {
    ResultsPage emptyList = new ResultsPage();
    Class<? extends BaseResponse> responseClass = null;
    List<QueryLogicTransformer> delegates = new ArrayList<>();
    for (BaseQueryLogic<?> logic : queryLogics) {
        QueryLogicTransformer t = logic.getTransformer(settings);
        delegates.add(t);
        BaseResponse refResponse = t.createResponse(emptyList);
        if (null == responseClass) {
            responseClass = refResponse.getClass();
        } else {
            if (!responseClass.equals(refResponse.getClass())) {
                throw new RuntimeException("All query logics must use transformers that return the same object type");
            }
        }
    }
    if (null == this.transformer) {
        this.transformer = new CompositeQueryLogicTransformer(delegates);
    }
    return this.transformer;
}
Also used : BaseResponse(datawave.webservice.result.BaseResponse) ArrayList(java.util.ArrayList) ResultsPage(datawave.webservice.query.cache.ResultsPage) QueryLogicTransformer(datawave.webservice.query.logic.QueryLogicTransformer)

Example 12 with ResultsPage

use of datawave.webservice.query.cache.ResultsPage in project datawave by NationalSecurityAgency.

the class ExtendedRunningQueryTest method testNext_HappyPathUsingDeprecatedConstructor.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testNext_HappyPathUsingDeprecatedConstructor() throws Exception {
    // Set local test input
    String userDN = "userDN";
    List<String> dnList = Lists.newArrayList(userDN);
    String userSid = "userSid";
    UUID queryId = UUID.randomUUID();
    String methodAuths = "AUTH_1";
    String columnVisibility = "AUTH_1";
    DatawaveUser user = new DatawaveUser(SubjectIssuerDNPair.of("userDN", "issuerDN"), UserType.USER, Collections.singleton(methodAuths), null, null, 0L);
    DatawavePrincipal principal = new DatawavePrincipal(Collections.singletonList(user));
    String query = "query";
    String queryLogicName = "queryLogicName";
    String queryName = "queryName";
    long currentTime = System.currentTimeMillis();
    Date beginDate = new Date(currentTime - 5000);
    Date endDate = new Date(currentTime - 1000);
    Date expirationDate = new Date(currentTime + 9999);
    int pageSize = 3;
    int maxPageSize = 10;
    long pageByteTrigger = 4 * 1024L;
    long maxWork = Long.MAX_VALUE;
    long maxResults = 100L;
    List<Object> resultObjects = Arrays.asList(new Object(), "resultObject1", null);
    // Set expectations
    expect(this.queryLogic.getCollectQueryMetrics()).andReturn(true);
    expect(this.query.getUncaughtExceptionHandler()).andReturn(exceptionHandler).times(5);
    expect(this.exceptionHandler.getThrowable()).andReturn(null).times(5);
    expect(this.query.getId()).andReturn(queryId).times(4);
    expect(this.query.getOwner()).andReturn(userSid).times(2);
    expect(this.query.getQuery()).andReturn(query).times(2);
    expect(this.query.getQueryLogicName()).andReturn(queryLogicName).times(2);
    expect(this.query.getQueryName()).andReturn(queryName).times(2);
    expect(this.query.getBeginDate()).andReturn(beginDate).times(2);
    expect(this.query.getEndDate()).andReturn(endDate).times(2);
    expect(this.query.isMaxResultsOverridden()).andReturn(false).anyTimes();
    expect(this.query.getExpirationDate()).andReturn(expirationDate);
    expect(this.query.getParameters()).andReturn(new HashSet<>()).times(2);
    expect(this.query.getQueryAuthorizations()).andReturn(methodAuths).times(2);
    expect(this.query.getColumnVisibility()).andReturn(columnVisibility);
    expect(this.query.getUserDN()).andReturn(userDN).times(3);
    expect(this.query.getDnList()).andReturn(dnList);
    expect(this.queryLogic.initialize(eq(this.connector), eq(this.query), isA(Set.class))).andReturn(this.genericConfiguration);
    this.queryLogic.setupQuery(this.genericConfiguration);
    expect(this.queryLogic.getTransformIterator(this.query)).andReturn(this.transformIterator);
    Iterator<Object> iterator = resultObjects.iterator();
    while (iterator.hasNext()) {
        expect(this.transformIterator.hasNext()).andReturn(iterator.hasNext());
        expect(this.transformIterator.next()).andReturn(iterator.next());
        expect(this.transformIterator.getTransformer()).andReturn(transformer);
    }
    expect(this.query.getPagesize()).andReturn(pageSize).anyTimes();
    expect(this.queryLogic.getMaxPageSize()).andReturn(maxPageSize).anyTimes();
    expect(this.queryLogic.getPageByteTrigger()).andReturn(pageByteTrigger).anyTimes();
    expect(this.queryLogic.getMaxWork()).andReturn(maxWork).anyTimes();
    expect(this.queryLogic.getMaxResults()).andReturn(maxResults).anyTimes();
    expect(this.genericConfiguration.getQueryString()).andReturn(query).once();
    expect(this.queryLogic.getResultLimit(eq(dnList))).andReturn(maxResults);
    // Run the test
    PowerMock.replayAll();
    RunningQuery subject = new RunningQuery(this.connector, Priority.NORMAL, this.queryLogic, this.query, methodAuths, principal, new QueryMetricFactoryImpl());
    ResultsPage result1 = subject.next();
    String result2 = subject.toString();
    QueryMetric.Lifecycle status = subject.getMetric().getLifecycle();
    PowerMock.verifyAll();
    // Verify results
    assertNotNull("Expected a non-null page", result1);
    assertNotNull("Expected a non-null list of results", result1.getResults());
    assertEquals("Expected 2 non-null items in the list of results", 2, result1.getResults().size());
    assertSame("Expected status to be closed", status, QueryMetric.Lifecycle.RESULTS);
    assertNotNull("Expected a non-null toString() representation", result2);
    assertSame("Expected lifecycle to be results", QueryMetric.Lifecycle.RESULTS, subject.getMetric().getLifecycle());
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) DatawaveUser(datawave.security.authorization.DatawaveUser) ResultsPage(datawave.webservice.query.cache.ResultsPage) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) Date(java.util.Date) QueryMetric(datawave.microservice.querymetric.QueryMetric) UUID(java.util.UUID) QueryMetricFactoryImpl(datawave.microservice.querymetric.QueryMetricFactoryImpl) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 13 with ResultsPage

use of datawave.webservice.query.cache.ResultsPage in project datawave by NationalSecurityAgency.

the class CachedRunningQuery method previous.

/**
 * Return the previous page of results
 *
 * @return previous page of results
 * @throws SQLException
 */
public ResultsPage previous(long pageByteTrigger) throws SQLException {
    // update timestamp in case this operation takes a long time.
    updateTimestamp();
    // only auto-activate before the first call to next/previous
    this.shouldAutoActivate = false;
    long pageStartTime = System.currentTimeMillis();
    if (currentRow == position.AFTER_LAST) {
        this.lastPageNumber = ((int) Math.ceil((float) this.totalRows / (float) this.pagesize)) + 1;
    }
    ResultsPage resultList = new ResultsPage();
    if (previousPageOfResults()) {
        resultList = convert(this.crs, pageByteTrigger);
    }
    if (!resultList.getResults().isEmpty()) {
        currentRow = position.MIDDLE;
        this.lastPageNumber--;
    } else {
        currentRow = position.BEFORE_FIRST;
        this.lastPageNumber = 0;
    }
    // Update the metric
    long now = System.currentTimeMillis();
    this.getMetric().addPageTime(resultList.getResults().size(), (now - pageStartTime), pageStartTime, now);
    updateTimestamp();
    return resultList;
}
Also used : ResultsPage(datawave.webservice.query.cache.ResultsPage)

Example 14 with ResultsPage

use of datawave.webservice.query.cache.ResultsPage in project datawave by NationalSecurityAgency.

the class CachedRunningQuery method convert.

private ResultsPage convert(CachedRowSet cachedRowSet, Integer rowBegin, Integer rowEnd, long pageByteTrigger) {
    boolean hitPageByteTrigger = false;
    List<CacheableQueryRow> cacheableQueryRowList = new ArrayList<>();
    try {
        long resultBytes = 0;
        while (cachedRowSet.next() && cachedRowSet.getRow() <= rowEnd && !hitPageByteTrigger) {
            if (log.isTraceEnabled())
                log.trace("CRS.position: " + cachedRowSet.getRow() + ", size: " + cachedRowSet.size());
            CacheableQueryRow row = CacheableQueryRowReader.createRow(cachedRowSet, this.fixedFieldsInEvent);
            cacheableQueryRowList.add(row);
            if (pageByteTrigger != 0) {
                resultBytes += ObjectSizeOf.Sizer.getObjectSize(row);
                if (resultBytes >= pageByteTrigger) {
                    hitPageByteTrigger = true;
                }
            }
        }
    } catch (SQLException | RuntimeException e) {
        log.error(e.getMessage(), e);
    }
    if (this.cacheableLogic == null) {
        return new ResultsPage();
    } else {
        return new ResultsPage(this.cacheableLogic.readFromCache(cacheableQueryRowList), (hitPageByteTrigger ? ResultsPage.Status.PARTIAL : ResultsPage.Status.COMPLETE));
    }
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) CacheableQueryRow(datawave.webservice.query.cachedresults.CacheableQueryRow) ResultsPage(datawave.webservice.query.cache.ResultsPage)

Example 15 with ResultsPage

use of datawave.webservice.query.cache.ResultsPage in project datawave by NationalSecurityAgency.

the class CachedRunningQuery method getRows.

/**
 * Return a specific page of results.
 *
 * @return next page of results
 * @throws SQLException
 */
public ResultsPage getRows(Integer rowBegin, Integer rowEnd, long pageByteTrigger) throws SQLException {
    if (log.isTraceEnabled())
        log.trace("getRows: begin=" + rowBegin + ", end=" + rowEnd);
    // update timestamp in case this operation takes a long time.
    updateTimestamp();
    long pageStartTime = System.currentTimeMillis();
    // We need to apply a default ORDER BY clause if one does not EXIST in the query
    StringBuilder query = new StringBuilder(this.sqlQuery);
    if (!this.sqlQuery.toUpperCase().contains(" ORDER BY ")) {
        query.append(DEFAULT_ORDER_BY);
    }
    ResultsPage resultList;
    int pagesize = (rowEnd - rowBegin) + 1;
    try (PreparedStatement ps = connection.prepareStatement(query.toString());
        CachedRowSet crs = RowSetProvider.newFactory().createCachedRowSet()) {
        log.debug("Get Rows query: " + query);
        ps.setFetchSize(pagesize);
        crs.setPageSize(pagesize);
        try (ResultSet rs = ps.executeQuery()) {
            crs.populate(rs, rowBegin);
            resultList = convert(crs, rowBegin, rowEnd, pageByteTrigger);
        }
        // Update the metric
        long now = System.currentTimeMillis();
        this.getMetric().addPageTime(resultList.getResults().size(), (now - pageStartTime), pageStartTime, now);
        updateTimestamp();
        return resultList;
    }
}
Also used : ResultSet(java.sql.ResultSet) ResultsPage(datawave.webservice.query.cache.ResultsPage) PreparedStatement(java.sql.PreparedStatement) CachedRowSet(javax.sql.rowset.CachedRowSet)

Aggregations

ResultsPage (datawave.webservice.query.cache.ResultsPage)23 DatawavePrincipal (datawave.security.authorization.DatawavePrincipal)9 Test (org.junit.Test)8 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 NoResultsException (datawave.webservice.common.exception.NoResultsException)6 NoResultsQueryException (datawave.webservice.query.exception.NoResultsQueryException)6 PreConditionFailedQueryException (datawave.webservice.query.exception.PreConditionFailedQueryException)6 QueryException (datawave.webservice.query.exception.QueryException)6 BaseQueryResponse (datawave.webservice.result.BaseQueryResponse)6 SQLException (java.sql.SQLException)6 Date (java.util.Date)6 DatawaveWebApplicationException (datawave.webservice.common.exception.DatawaveWebApplicationException)5 UnauthorizedException (datawave.webservice.common.exception.UnauthorizedException)5 BadRequestQueryException (datawave.webservice.query.exception.BadRequestQueryException)5 NotFoundQueryException (datawave.webservice.query.exception.NotFoundQueryException)5 UnauthorizedQueryException (datawave.webservice.query.exception.UnauthorizedQueryException)5 Principal (java.security.Principal)5 Timed (com.codahale.metrics.annotation.Timed)4 NotFoundException (datawave.webservice.common.exception.NotFoundException)4