Java Code Examples for org.apache.commons.collections4.iterators.TransformIterator#hasNext()

The following examples show how to use org.apache.commons.collections4.iterators.TransformIterator#hasNext() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DashboardQueryLogic.java    From datawave with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public TransformIterator getTransformIterator(Query settings) {
    TransformIterator origIter = super.getTransformIterator(settings);
    QueryLogicTransformer transformer = super.getTransformer(settings);
    DashboardSummary summary = new DashboardSummary(settings.getEndDate());
    while (origIter.hasNext()) {
        EventBase event = (EventBase) transformer.transform(origIter.next());
        DashboardFields.addEvent(summary, event);
    }
    
    return new TransformIterator(Arrays.asList(summary).iterator(), this);
}
 
Example 2
Source File: CompositeQueryLogicTest.java    From datawave with Apache License 2.0 5 votes vote down vote up
@Test
public void testQueryLogicNoData() throws Exception {
    List<BaseQueryLogic<?>> logics = new ArrayList<>();
    TestQueryLogic logic1 = new TestQueryLogic();
    TestQueryLogic2 logic2 = new TestQueryLogic2();
    logics.add(logic1);
    logics.add(logic2);
    
    QueryImpl settings = new QueryImpl();
    settings.setPagesize(100);
    settings.setQueryAuthorizations(auths.serialize());
    settings.setQuery("FOO == 'BAR'");
    settings.setParameters(new HashSet<>());
    settings.setId(UUID.randomUUID());
    
    CompositeQueryLogic c = new CompositeQueryLogic();
    /**
     * RunningQuery.setupConnection()
     */
    c.setQueryLogics(logics);
    c.initialize((Connector) null, (Query) settings, Collections.singleton(auths));
    c.setupQuery(null);
    TransformIterator iter = c.getTransformIterator((Query) settings);
    
    /**
     * RunningQuery.next() - iterate over results coming from tablet server through the TransformIterator to turn them into the objects.
     */
    List<Object> results = new ArrayList<>();
    while (iter.hasNext()) {
        Object o = iter.next();
        if (null == o)
            break;
        Assert.assertTrue(o instanceof TestQueryResponse);
        results.add((TestQueryResponse) o);
    }
    Assert.assertEquals(0, results.size());
    c.close();
    
}
 
Example 3
Source File: UniqueTest.java    From datawave with Apache License 2.0 4 votes vote down vote up
protected void runTestQueryWithUniqueness(Set<Set<String>> expected, String querystr, Date startDate, Date endDate, Map<String,String> extraParms,
                Connector connector) throws Exception {
    log.debug("runTestQueryWithUniqueness");
    
    QueryImpl settings = new QueryImpl();
    settings.setBeginDate(startDate);
    settings.setEndDate(endDate);
    settings.setPagesize(Integer.MAX_VALUE);
    settings.setQueryAuthorizations(auths.serialize());
    settings.setQuery(querystr);
    settings.setParameters(extraParms);
    settings.setId(UUID.randomUUID());
    
    log.debug("query: " + settings.getQuery());
    log.debug("logic: " + settings.getQueryLogicName());
    
    GenericQueryConfiguration config = logic.initialize(connector, settings, authSet);
    logic.setupQuery(config);
    
    DocumentTransformer transformer = (DocumentTransformer) (logic.getTransformer(settings));
    TransformIterator iter = new DatawaveTransformIterator(logic.iterator(), transformer);
    List<Object> eventList = new ArrayList<>();
    while (iter.hasNext()) {
        eventList.add(iter.next());
    }
    
    BaseQueryResponse response = transformer.createResponse(eventList);
    
    // un-comment to look at the json output
    // ObjectMapper mapper = new ObjectMapper();
    // mapper.enable(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME);
    // mapper.writeValue(new File("/tmp/grouped2.json"), response);
    
    Assert.assertTrue(response instanceof DefaultEventQueryResponse);
    DefaultEventQueryResponse eventQueryResponse = (DefaultEventQueryResponse) response;
    
    for (EventBase event : eventQueryResponse.getEvents()) {
        boolean found = false;
        for (Iterator<Set<String>> it = expected.iterator(); it.hasNext();) {
            Set<String> expectedSet = it.next();
            if (expectedSet.contains(event.getMetadata().getInternalId())) {
                it.remove();
                found = true;
                break;
            }
        }
        Assert.assertTrue(found);
    }
    Assert.assertTrue(expected.isEmpty());
}
 
Example 4
Source File: GroupingTestWithModel.java    From datawave with Apache License 2.0 4 votes vote down vote up
protected BaseQueryResponse runTestQueryWithGrouping(Map<String,Integer> expected, String querystr, Date startDate, Date endDate,
                Map<String,String> extraParms, Connector connector) throws Exception {
    log.debug("runTestQueryWithGrouping");
    
    QueryImpl settings = new QueryImpl();
    settings.setBeginDate(startDate);
    settings.setEndDate(endDate);
    settings.setPagesize(Integer.MAX_VALUE);
    settings.setQueryAuthorizations(auths.serialize());
    settings.setQuery(querystr);
    settings.setParameters(extraParms);
    settings.setId(UUID.randomUUID());
    
    log.debug("query: " + settings.getQuery());
    log.debug("logic: " + settings.getQueryLogicName());
    
    GenericQueryConfiguration config = logic.initialize(connector, settings, authSet);
    logic.setupQuery(config);
    
    DocumentTransformer transformer = (DocumentTransformer) (logic.getTransformer(settings));
    TransformIterator iter = new DatawaveTransformIterator(logic.iterator(), transformer);
    List<Object> eventList = new ArrayList<>();
    while (iter.hasNext()) {
        eventList.add(iter.next());
    }
    
    BaseQueryResponse response = transformer.createResponse(eventList);
    
    // un-comment to look at the json output
    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME);
    mapper.writeValue(new File("/tmp/grouped2.json"), response);
    
    Assert.assertTrue(response instanceof DefaultEventQueryResponse);
    DefaultEventQueryResponse eventQueryResponse = (DefaultEventQueryResponse) response;
    
    Assert.assertEquals("Got the wrong number of events", expected.size(), (long) eventQueryResponse.getReturnedEvents());
    
    for (EventBase event : eventQueryResponse.getEvents()) {
        
        String genderKey = "";
        String ageKey = "";
        Integer value = null;
        for (Object field : event.getFields()) {
            FieldBase fieldBase = (FieldBase) field;
            switch (fieldBase.getName()) {
                case "COUNT":
                    value = Integer.valueOf(fieldBase.getValueString());
                    break;
                case "GENDER":
                    genderKey = fieldBase.getValueString();
                    break;
                case "GEN":
                    genderKey = fieldBase.getValueString();
                    break;
                case "AGE":
                    ageKey = fieldBase.getValueString();
                    break;
                case "AG":
                    ageKey = fieldBase.getValueString();
                    break;
            }
        }
        
        log.debug("mapping is " + genderKey + "-" + ageKey + " count:" + value);
        String key;
        if (!genderKey.isEmpty() && !ageKey.isEmpty()) {
            key = genderKey + "-" + ageKey;
        } else if (!genderKey.isEmpty()) {
            key = genderKey;
        } else {
            key = ageKey;
        }
        Assert.assertEquals(expected.get(key), value);
    }
    return response;
}
 
Example 5
Source File: GroupingTest.java    From datawave with Apache License 2.0 4 votes vote down vote up
protected BaseQueryResponse runTestQueryWithGrouping(Map<String,Integer> expected, String querystr, Date startDate, Date endDate,
                Map<String,String> extraParms, Connector connector) throws Exception {
    log.debug("runTestQueryWithGrouping");
    
    QueryImpl settings = new QueryImpl();
    settings.setBeginDate(startDate);
    settings.setEndDate(endDate);
    settings.setPagesize(Integer.MAX_VALUE);
    settings.setQueryAuthorizations(auths.serialize());
    settings.setQuery(querystr);
    settings.setParameters(extraParms);
    settings.setId(UUID.randomUUID());
    
    log.debug("query: " + settings.getQuery());
    log.debug("logic: " + settings.getQueryLogicName());
    
    GenericQueryConfiguration config = logic.initialize(connector, settings, authSet);
    logic.setupQuery(config);
    
    DocumentTransformer transformer = (DocumentTransformer) (logic.getTransformer(settings));
    TransformIterator iter = new DatawaveTransformIterator(logic.iterator(), transformer);
    List<Object> eventList = new ArrayList<>();
    while (iter.hasNext()) {
        eventList.add(iter.next());
    }
    
    BaseQueryResponse response = transformer.createResponse(eventList);
    
    // un-comment to look at the json output
    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME);
    mapper.writeValue(new File("/tmp/grouped2.json"), response);
    
    Assert.assertTrue(response instanceof DefaultEventQueryResponse);
    DefaultEventQueryResponse eventQueryResponse = (DefaultEventQueryResponse) response;
    
    Assert.assertEquals("Got the wrong number of events", expected.size(), (long) eventQueryResponse.getReturnedEvents());
    
    for (EventBase event : eventQueryResponse.getEvents()) {
        
        String genderKey = "";
        String ageKey = "";
        Integer value = null;
        for (Object field : event.getFields()) {
            FieldBase fieldBase = (FieldBase) field;
            switch (fieldBase.getName()) {
                case "COUNT":
                    value = Integer.valueOf(fieldBase.getValueString());
                    break;
                case "GENDER":
                    genderKey = fieldBase.getValueString();
                    break;
                case "GEN":
                    genderKey = fieldBase.getValueString();
                    break;
                case "AGE":
                    ageKey = fieldBase.getValueString();
                    break;
                case "AG":
                    ageKey = fieldBase.getValueString();
                    break;
            }
        }
        
        log.debug("mapping is " + genderKey + "-" + ageKey + " count:" + value);
        String key;
        if (!genderKey.isEmpty() && !ageKey.isEmpty()) {
            key = genderKey + "-" + ageKey;
        } else if (!genderKey.isEmpty()) {
            key = genderKey;
        } else {
            key = ageKey;
        }
        Assert.assertEquals(expected.get(key), value);
    }
    return response;
}
 
Example 6
Source File: CompositeQueryLogicTest.java    From datawave with Apache License 2.0 4 votes vote down vote up
@Test
// testQueryLogic with max.results.override not set
public void testQueryLogic() throws Exception {
    Logger.getLogger(CompositeQueryLogic.class).setLevel(Level.TRACE);
    Logger.getLogger(CompositeQueryLogicResults.class).setLevel(Level.TRACE);
    Logger.getLogger(CompositeQueryLogicTransformer.class).setLevel(Level.TRACE);
    List<BaseQueryLogic<?>> logics = new ArrayList<>();
    TestQueryLogic logic1 = new TestQueryLogic();
    TestQueryLogic2 logic2 = new TestQueryLogic2();
    logics.add(logic1);
    logics.add(logic2);
    
    logic1.getData().put(key1, value1);
    logic1.getData().put(key2, value2);
    logic2.getData().put(key3, value3);
    logic2.getData().put(key4, value4);
    logic1.getData().put(key5, value5);
    logic1.getData().put(key6, value6);
    logic2.getData().put(key7, value7);
    logic2.getData().put(key8, value8);
    
    QueryImpl settings = new QueryImpl();
    settings.setPagesize(100);
    settings.setQueryAuthorizations(auths.toString());
    settings.setQuery("FOO == 'BAR'");
    settings.setParameters(new HashSet<>());
    settings.setId(UUID.randomUUID());
    
    CompositeQueryLogic c = new CompositeQueryLogic();
    // max.results.override is set to -1 when it is not passed in as it is an optional paramter
    logic1.setMaxResults(-1);
    logic2.setMaxResults(-1);
    /**
     * RunningQuery.setupConnection()
     */
    c.setQueryLogics(logics);
    c.initialize((Connector) null, (Query) settings, Collections.singleton(auths));
    c.setupQuery(null);
    TransformIterator iter = c.getTransformIterator((Query) settings);
    
    /**
     * RunningQuery.next() - iterate over results coming from tablet server through the TransformIterator to turn them into the objects.
     */
    List<Object> results = new ArrayList<>();
    while (iter.hasNext()) {
        Object o = iter.next();
        if (null == o)
            break;
        Assert.assertTrue(o instanceof TestQueryResponse);
        results.add((TestQueryResponse) o);
    }
    Assert.assertEquals(8, results.size());
    ResultsPage page = new ResultsPage(results, Status.COMPLETE);
    
    /**
     * QueryExecutorBean.next() - transform list of objects into JAXB response
     */
    TestQueryResponseList response = (TestQueryResponseList) c.getTransformer((Query) settings).createResponse(page);
    Assert.assertEquals(8, response.getResponses().size());
    for (TestQueryResponse r : response.getResponses()) {
        Assert.assertNotNull(r);
    }
    
    c.close();
    
}
 
Example 7
Source File: CompositeQueryLogicTest.java    From datawave with Apache License 2.0 4 votes vote down vote up
@Test
// testQueryLogic with max.results.override is set
public void testQueryLogicWithMaxResultsOverride() throws Exception {
    Logger.getLogger(CompositeQueryLogic.class).setLevel(Level.TRACE);
    Logger.getLogger(CompositeQueryLogicResults.class).setLevel(Level.TRACE);
    Logger.getLogger(CompositeQueryLogicTransformer.class).setLevel(Level.TRACE);
    List<BaseQueryLogic<?>> logics = new ArrayList<>();
    TestQueryLogic logic1 = new TestQueryLogic();
    TestQueryLogic2 logic2 = new TestQueryLogic2();
    logics.add(logic1);
    logics.add(logic2);
    
    logic1.getData().put(key1, value1);
    logic1.getData().put(key2, value2);
    logic2.getData().put(key3, value3);
    logic2.getData().put(key4, value4);
    logic1.getData().put(key5, value5);
    logic1.getData().put(key6, value6);
    logic2.getData().put(key7, value7);
    logic2.getData().put(key8, value8);
    
    QueryImpl settings = new QueryImpl();
    settings.setPagesize(100);
    settings.setQueryAuthorizations(auths.toString());
    settings.setQuery("FOO == 'BAR'");
    settings.setParameters(new HashSet<>());
    settings.setId(UUID.randomUUID());
    
    CompositeQueryLogic c = new CompositeQueryLogic();
    // max.results.override is set to -1 when it is not passed in as it is an optional parameter
    logic1.setMaxResults(0);
    logic2.setMaxResults(4);
    /**
     * RunningQuery.setupConnection()
     */
    c.setQueryLogics(logics);
    c.initialize((Connector) null, (Query) settings, Collections.singleton(auths));
    c.setupQuery(null);
    TransformIterator iter = c.getTransformIterator((Query) settings);
    
    /**
     * RunningQuery.next() - iterate over results coming from tablet server through the TransformIterator to turn them into the objects.
     */
    List<Object> results = new ArrayList<>();
    while (iter.hasNext()) {
        Object o = iter.next();
        if (null == o)
            break;
        Assert.assertTrue(o instanceof TestQueryResponse);
        results.add((TestQueryResponse) o);
    }
    Assert.assertEquals(4, results.size());
    ResultsPage page = new ResultsPage(results, Status.COMPLETE);
    
    /**
     * QueryExecutorBean.next() - transform list of objects into JAXB response
     */
    TestQueryResponseList response = (TestQueryResponseList) c.getTransformer((Query) settings).createResponse(page);
    Assert.assertEquals(4, response.getResponses().size());
    for (TestQueryResponse r : response.getResponses()) {
        Assert.assertNotNull(r);
    }
    
    c.close();
    
}
 
Example 8
Source File: CompositeQueryLogicTest.java    From datawave with Apache License 2.0 4 votes vote down vote up
@Test
public void testQueryLogicNoDataLogic1() throws Exception {
    List<BaseQueryLogic<?>> logics = new ArrayList<>();
    TestQueryLogic logic1 = new TestQueryLogic();
    TestQueryLogic2 logic2 = new TestQueryLogic2();
    logics.add(logic1);
    logics.add(logic2);
    
    logic2.getData().put(key1, value1);
    logic2.getData().put(key2, value2);
    logic2.getData().put(key3, value3);
    logic2.getData().put(key4, value4);
    logic2.getData().put(key5, value5);
    logic2.getData().put(key6, value6);
    logic2.getData().put(key7, value7);
    logic2.getData().put(key8, value8);
    
    QueryImpl settings = new QueryImpl();
    settings.setPagesize(100);
    settings.setQueryAuthorizations(auths.serialize());
    settings.setQuery("FOO == 'BAR'");
    settings.setParameters(new HashSet<>());
    settings.setId(UUID.randomUUID());
    
    CompositeQueryLogic c = new CompositeQueryLogic();
    // max.results.override is set to -1 when it is not passed in as it is an optional paramter
    logic2.setMaxResults(-1);
    logic1.setMaxResults(-1);
    /**
     * RunningQuery.setupConnection()
     */
    c.setQueryLogics(logics);
    c.initialize((Connector) null, (Query) settings, Collections.singleton(auths));
    c.setupQuery(null);
    TransformIterator iter = c.getTransformIterator((Query) settings);
    
    /**
     * RunningQuery.next() - iterate over results coming from tablet server through the TransformIterator to turn them into the objects.
     */
    List<Object> results = new ArrayList<>();
    while (iter.hasNext()) {
        Object o = iter.next();
        if (null == o)
            break;
        Assert.assertTrue(o instanceof TestQueryResponse);
        results.add((TestQueryResponse) o);
    }
    Assert.assertEquals(8, results.size());
    ResultsPage page = new ResultsPage(results, Status.COMPLETE);
    
    /**
     * QueryExecutorBean.next() - transform list of objects into JAXB response
     */
    TestQueryResponseList response = (TestQueryResponseList) c.getTransformer((Query) settings).createResponse(page);
    Assert.assertEquals(8, response.getResponses().size());
    for (TestQueryResponse r : response.getResponses()) {
        Assert.assertNotNull(r);
    }
    
    c.close();
    
}
 
Example 9
Source File: CompositeQueryLogicTest.java    From datawave with Apache License 2.0 4 votes vote down vote up
@Test
public void testQueryLogicNoDataLogic2() throws Exception {
    List<BaseQueryLogic<?>> logics = new ArrayList<>();
    TestQueryLogic logic1 = new TestQueryLogic();
    TestQueryLogic2 logic2 = new TestQueryLogic2();
    logics.add(logic1);
    logics.add(logic2);
    
    logic1.getData().put(key1, value1);
    logic1.getData().put(key2, value2);
    logic1.getData().put(key3, value3);
    logic1.getData().put(key4, value4);
    logic1.getData().put(key5, value5);
    logic1.getData().put(key6, value6);
    logic1.getData().put(key7, value7);
    logic1.getData().put(key8, value8);
    
    QueryImpl settings = new QueryImpl();
    settings.setPagesize(100);
    settings.setQueryAuthorizations(auths.serialize());
    settings.setQuery("FOO == 'BAR'");
    settings.setParameters(new HashSet<>());
    settings.setId(UUID.randomUUID());
    
    CompositeQueryLogic c = new CompositeQueryLogic();
    // max.results.override is set to -1 when it is not passed in as it is an optional paramter
    logic2.setMaxResults(-1);
    logic1.setMaxResults(-1);
    /**
     * RunningQuery.setupConnection()
     */
    c.setQueryLogics(logics);
    c.initialize((Connector) null, (Query) settings, Collections.singleton(auths));
    c.setupQuery(null);
    TransformIterator iter = c.getTransformIterator((Query) settings);
    
    /**
     * RunningQuery.next() - iterate over results coming from tablet server through the TransformIterator to turn them into the objects.
     */
    List<Object> results = new ArrayList<>();
    while (iter.hasNext()) {
        Object o = iter.next();
        if (null == o)
            break;
        Assert.assertTrue(o instanceof TestQueryResponse);
        results.add((TestQueryResponse) o);
    }
    Assert.assertEquals(8, results.size());
    ResultsPage page = new ResultsPage(results, Status.COMPLETE);
    
    /**
     * QueryExecutorBean.next() - transform list of objects into JAXB response
     */
    TestQueryResponseList response = (TestQueryResponseList) c.getTransformer((Query) settings).createResponse(page);
    Assert.assertEquals(8, response.getResponses().size());
    for (TestQueryResponse r : response.getResponses()) {
        Assert.assertNotNull(r);
    }
    
    c.close();
    
}