Example usage for org.apache.lucene.util SentinelIntSet put

List of usage examples for org.apache.lucene.util SentinelIntSet put

Introduction

In this page you can find the example usage for org.apache.lucene.util SentinelIntSet put.

Prototype

public int put(int key) 

Source Link

Document

Puts this integer (key) in the set, and returns the slot index it was added to.

Usage

From source file:org.apache.solr.cloud.DistribCursorPagingTest.java

License:Apache License

/**
 * <p>/*  w  w  w.  java2s  . co m*/
 * Given a set of params, executes a cursor query using {@link #CURSOR_MARK_START} 
 * and then continuously walks the results using {@link #CURSOR_MARK_START} as long 
 * as a non-0 number of docs ar returned.  This method records the the set of all id's
 * (must be postive ints) encountered and throws an assertion failure if any id is 
 * encountered more then once, or if the set grows above maxSize
 * </p>
 *
 * <p>
 * Note that this method explicily uses the "cloudClient" for executing the queries, 
 * instead of relying on the test infrastructure to execute the queries redundently
 * aainst both the cloud client as well as a control client.  This is because term stat 
 * differences in a sharded setup can result in differnent scores for documents compared 
 * to the control index -- which can affect the sorting in some cases and cause false 
 * negatives in the response comparisons (even if we don't include "score" in the "fl")
 * </p>
 */
public SentinelIntSet assertFullWalkNoDups(int maxSize, SolrParams params) throws Exception {
    SentinelIntSet ids = new SentinelIntSet(maxSize, -1);
    String cursorMark = CURSOR_MARK_START;
    int docsOnThisPage = Integer.MAX_VALUE;
    while (0 < docsOnThisPage) {
        final SolrParams p = p(params, CURSOR_MARK_PARAM, cursorMark);
        QueryResponse rsp = cloudClient.query(p);
        String nextCursorMark = assertHashNextCursorMark(rsp);
        SolrDocumentList docs = extractDocList(rsp);
        docsOnThisPage = docs.size();
        if (null != params.getInt(CommonParams.ROWS)) {
            int rows = params.getInt(CommonParams.ROWS);
            assertTrue("Too many docs on this page: " + rows + " < " + docsOnThisPage, docsOnThisPage <= rows);
        }
        if (0 == docsOnThisPage) {
            assertEquals("no more docs, but " + CURSOR_MARK_NEXT + " isn't same", cursorMark, nextCursorMark);
        }

        for (SolrDocument doc : docs) {
            int id = ((Integer) doc.get("id")).intValue();
            if (ids.exists(id)) {
                String msg = "(" + p + ") walk already seen: " + id;
                try {
                    queryAndCompareShards(params("distrib", "false", "q", "id:" + id));
                } catch (AssertionError ae) {
                    throw (AssertionError) new AssertionError(
                            msg + ", found shard inconsistency that would explain it...").initCause(ae);
                }
                rsp = cloudClient.query(params("q", "id:" + id));
                throw new AssertionError(msg + ", don't know why; q=id:" + id + " gives: " + rsp.toString());
            }
            ids.put(id);
            assertFalse("id set bigger then max allowed (" + maxSize + "): " + ids.size(),
                    maxSize < ids.size());
        }
        cursorMark = nextCursorMark;
    }
    return ids;
}

From source file:org.apache.solr.CursorPagingTest.java

License:Apache License

/**
 * Given a set of params, executes a cursor query using {@link #CURSOR_MARK_START}
 * and then continuously walks the results using {@link #CURSOR_MARK_START} as long
 * as a non-0 number of docs ar returned.  This method records the the set of all id's
 * (must be positive ints) encountered and throws an assertion failure if any id is
 * encountered more than once, or if the set grows above maxSize
 *//*from  www .j ava  2 s.  co m*/
public SentinelIntSet assertFullWalkNoDups(int maxSize, SolrParams params) throws Exception {

    SentinelIntSet ids = new SentinelIntSet(maxSize, -1);
    String cursorMark = CURSOR_MARK_START;
    int docsOnThisPage = Integer.MAX_VALUE;
    while (0 < docsOnThisPage) {
        String json = assertJQ(req(params, CURSOR_MARK_PARAM, cursorMark));
        Map rsp = (Map) ObjectBuilder.fromJSON(json);
        assertTrue("response doesn't contain " + CURSOR_MARK_NEXT + ": " + json,
                rsp.containsKey(CURSOR_MARK_NEXT));
        String nextCursorMark = (String) rsp.get(CURSOR_MARK_NEXT);
        assertNotNull(CURSOR_MARK_NEXT + " is null", nextCursorMark);
        List<Map<Object, Object>> docs = (List) (((Map) rsp.get("response")).get("docs"));
        docsOnThisPage = docs.size();
        if (null != params.getInt(CommonParams.ROWS)) {
            int rows = params.getInt(CommonParams.ROWS);
            assertTrue("Too many docs on this page: " + rows + " < " + docsOnThisPage, docsOnThisPage <= rows);
        }
        if (0 == docsOnThisPage) {
            assertEquals("no more docs, but " + CURSOR_MARK_NEXT + " isn't same", cursorMark, nextCursorMark);
        }
        for (Map<Object, Object> doc : docs) {
            int id = ((Long) doc.get("id")).intValue();
            assertFalse("walk already seen: " + id, ids.exists(id));
            ids.put(id);
            assertFalse("id set bigger then max allowed (" + maxSize + "): " + ids.size(),
                    maxSize < ids.size());
        }
        cursorMark = nextCursorMark;
    }
    return ids;
}

From source file:org.apache.solr.CursorPagingTest.java

License:Apache License

/**
 * Given a set of params, executes a cursor query using {@link #CURSOR_MARK_START}
 * and then continuously walks the results using {@link #CURSOR_MARK_START} as long
 * as a non-0 number of docs ar returned.  This method records the the set of all id's
 * (must be positive ints) encountered and throws an assertion failure if any id is
 * encountered more than once, or if the set grows above maxSize.
 *
 * Also checks that facets are the same with each page, and that they are correct.
 *//*from  w  w w  .j  ava2s . c  o  m*/
public SentinelIntSet assertFullWalkNoDupsWithFacets(int maxSize, SolrParams params) throws Exception {

    final String facetField = params.get("facet.field");
    assertNotNull("facet.field param not specified", facetField);
    assertFalse("facet.field param contains multiple values", facetField.contains(","));
    assertEquals("facet.limit param not set to -1", "-1", params.get("facet.limit"));
    final Map<String, MutableValueInt> facetCounts = new HashMap<>();
    SentinelIntSet ids = new SentinelIntSet(maxSize, -1);
    String cursorMark = CURSOR_MARK_START;
    int docsOnThisPage = Integer.MAX_VALUE;
    List previousFacets = null;
    while (0 < docsOnThisPage) {
        String json = assertJQ(req(params, CURSOR_MARK_PARAM, cursorMark));
        Map rsp = (Map) ObjectBuilder.fromJSON(json);
        assertTrue("response doesn't contain " + CURSOR_MARK_NEXT + ": " + json,
                rsp.containsKey(CURSOR_MARK_NEXT));
        String nextCursorMark = (String) rsp.get(CURSOR_MARK_NEXT);
        assertNotNull(CURSOR_MARK_NEXT + " is null", nextCursorMark);
        List<Map<Object, Object>> docs = (List) (((Map) rsp.get("response")).get("docs"));
        docsOnThisPage = docs.size();
        if (null != params.getInt(CommonParams.ROWS)) {
            int rows = params.getInt(CommonParams.ROWS);
            assertTrue("Too many docs on this page: " + rows + " < " + docsOnThisPage, docsOnThisPage <= rows);
        }
        if (0 == docsOnThisPage) {
            assertEquals("no more docs, but " + CURSOR_MARK_NEXT + " isn't same", cursorMark, nextCursorMark);
        }
        for (Map<Object, Object> doc : docs) {
            int id = ((Long) doc.get("id")).intValue();
            assertFalse("walk already seen: " + id, ids.exists(id));
            ids.put(id);
            assertFalse("id set bigger then max allowed (" + maxSize + "): " + ids.size(),
                    maxSize < ids.size());
            Object facet = doc.get(facetField);
            String facetString = null == facet ? null : facet.toString(); // null: missing facet value
            MutableValueInt count = facetCounts.get(facetString);
            if (null == count) {
                count = new MutableValueInt();
                facetCounts.put(facetString, count);
            }
            ++count.value;
        }
        cursorMark = nextCursorMark;

        Map facetFields = (Map) ((Map) rsp.get("facet_counts")).get("facet_fields");
        List facets = (List) facetFields.get(facetField);
        if (null != previousFacets) {
            assertEquals("Facets not the same as on previous page:\nprevious page facets: "
                    + Arrays.toString(facets.toArray(new Object[facets.size()])) + "\ncurrent page facets: "
                    + Arrays.toString(previousFacets.toArray(new Object[previousFacets.size()])),
                    previousFacets, facets);
        }
        previousFacets = facets;
    }

    assertNotNull("previousFacets is null", previousFacets);
    assertEquals("Mismatch in number of facets: ", facetCounts.size(), previousFacets.size() / 2);
    int pos;
    for (pos = 0; pos < previousFacets.size(); pos += 2) {
        String label = (String) previousFacets.get(pos);
        int expectedCount = ((Number) previousFacets.get(pos + 1)).intValue();
        MutableValueInt count = facetCounts.get(label);
        assertNotNull("Expected facet label #" + (pos / 2) + " not found: '" + label + "'", count);
        assertEquals("Facet count mismatch for label #" + (pos / 2) + " '" + label + "'", expectedCount,
                facetCounts.get(label).value);
        pos += 2;
    }
    return ids;
}