Search in sources :

Example 11 with LongHashSet

use of com.carrotsearch.hppc.LongHashSet in project janusgraph by JanusGraph.

the class IDAuthorityTest method testSimpleIDAcquisition.

@Test
public void testSimpleIDAcquisition() throws BackendException {
    final IDBlockSizer blockSizer = new InnerIDBlockSizer();
    idAuthorities[0].setIDBlockSizer(blockSizer);
    int numTrials = 100;
    LongSet ids = new LongHashSet((int) blockSize * numTrials);
    long previous = 0;
    for (int i = 0; i < numTrials; i++) {
        IDBlock block = idAuthorities[0].getIDBlock(0, 0, GET_ID_BLOCK_TIMEOUT);
        checkBlock(block, ids);
        if (hasEmptyUid) {
            if (previous != 0)
                assertEquals(previous + 1, block.getId(0));
            previous = block.getId(block.numIds() - 1);
        }
    }
}
Also used : LongHashSet(com.carrotsearch.hppc.LongHashSet) IDBlockSizer(org.janusgraph.graphdb.database.idassigner.IDBlockSizer) LongSet(com.carrotsearch.hppc.LongSet) Test(org.junit.Test)

Example 12 with LongHashSet

use of com.carrotsearch.hppc.LongHashSet in project janusgraph by JanusGraph.

the class VertexIDAssignerTest method testCustomIdAssignment.

private void testCustomIdAssignment(CustomIdStrategy idStrategy) {
    LongSet vertexIds = new LongHashSet();
    final long maxCount = idAssigner.getIDManager().getVertexCountBound();
    long count = 1;
    for (int trial = 0; trial < 10; trial++) {
        final JanusGraph graph = getInMemoryGraph(true, true);
        int numVertices = 1000;
        final List<JanusGraphVertex> vertices = new ArrayList<>(numVertices);
        try {
            for (int i = 0; i < numVertices; i++, count++) {
                final long userVertexId;
                switch(idStrategy) {
                    case LOW:
                        userVertexId = count;
                        break;
                    case HIGH:
                        userVertexId = maxCount - count;
                        break;
                    default:
                        throw new RuntimeException("Unsupported custom id strategy: " + idStrategy);
                }
                final long id = idAssigner.getIDManager().toVertexId(userVertexId);
                JanusGraphVertex next = graph.addVertex(T.id, id, "user_id", userVertexId);
                vertices.add(next);
            }
            // Verify that ids are set, unique and consistent with user id basis
            for (JanusGraphVertex v : vertices) {
                assertTrue(v.hasId());
                long id = v.longId();
                assertTrue(id > 0 && id < Long.MAX_VALUE);
                assertTrue(vertexIds.add(id));
                assertEquals((long) v.value("user_id"), idAssigner.getIDManager().fromVertexId(id));
            }
        } finally {
            graph.tx().rollback();
            graph.close();
        }
    }
}
Also used : LongHashSet(com.carrotsearch.hppc.LongHashSet) LongSet(com.carrotsearch.hppc.LongSet) JanusGraph(org.janusgraph.core.JanusGraph) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) ArrayList(java.util.ArrayList)

Example 13 with LongHashSet

use of com.carrotsearch.hppc.LongHashSet in project cassandra by apache.

the class RangeIntersectionIteratorTest method testIntersectionOfRandomRanges.

private void testIntersectionOfRandomRanges(Strategy strategy) {
    for (int attempt = 0; attempt < 16; attempt++) {
        final ThreadLocalRandom random = ThreadLocalRandom.current();
        final int maxRanges = random.nextInt(2, 16);
        // generate randomize ranges
        long[][] ranges = new long[maxRanges][];
        for (int i = 0; i < ranges.length; i++) {
            int rangeSize = random.nextInt(16, 512);
            LongSet range = new LongHashSet(rangeSize);
            for (int j = 0; j < rangeSize; j++) range.add(random.nextLong(0, 100));
            ranges[i] = range.toArray();
            Arrays.sort(ranges[i]);
        }
        List<Long> expected = new ArrayList<>();
        // determine unique tokens which intersect every range
        for (long token : ranges[0]) {
            boolean intersectsAll = true;
            for (int i = 1; i < ranges.length; i++) {
                if (Arrays.binarySearch(ranges[i], token) < 0) {
                    intersectsAll = false;
                    break;
                }
            }
            if (intersectsAll)
                expected.add(token);
        }
        RangeIterator.Builder<Long, Token> builder = RangeIntersectionIterator.builder(strategy);
        for (long[] range : ranges) builder.add(new LongIterator(range));
        Assert.assertEquals(expected, convert(builder.build()));
    }
}
Also used : LongSet(com.carrotsearch.hppc.LongSet) Token(org.apache.cassandra.index.sasi.disk.Token) LongHashSet(com.carrotsearch.hppc.LongHashSet) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom)

Example 14 with LongHashSet

use of com.carrotsearch.hppc.LongHashSet in project elasticsearch by elastic.

the class BinaryRangeAggregatorTests method doTestSortedSetRangeLeafCollector.

private void doTestSortedSetRangeLeafCollector(int maxNumValuesPerDoc) throws Exception {
    final Set<BytesRef> termSet = new HashSet<>();
    final int numTerms = TestUtil.nextInt(random(), maxNumValuesPerDoc, 100);
    while (termSet.size() < numTerms) {
        termSet.add(new BytesRef(TestUtil.randomSimpleString(random(), randomInt(2))));
    }
    final BytesRef[] terms = termSet.toArray(new BytesRef[0]);
    Arrays.sort(terms);
    final int numRanges = randomIntBetween(1, 10);
    BinaryRangeAggregator.Range[] ranges = new BinaryRangeAggregator.Range[numRanges];
    for (int i = 0; i < numRanges; ++i) {
        ranges[i] = new BinaryRangeAggregator.Range(Integer.toString(i), randomBoolean() ? null : new BytesRef(TestUtil.randomSimpleString(random(), randomInt(2))), randomBoolean() ? null : new BytesRef(TestUtil.randomSimpleString(random(), randomInt(2))));
    }
    Arrays.sort(ranges, BinaryRangeAggregator.RANGE_COMPARATOR);
    FakeSortedSetDocValues values = new FakeSortedSetDocValues(terms);
    final int[] counts = new int[ranges.length];
    SortedSetRangeLeafCollector collector = new SortedSetRangeLeafCollector(values, ranges, null) {

        @Override
        protected void doCollect(LeafBucketCollector sub, int doc, long bucket) throws IOException {
            counts[(int) bucket]++;
        }
    };
    final int[] expectedCounts = new int[ranges.length];
    final int maxDoc = randomIntBetween(5, 10);
    for (int doc = 0; doc < maxDoc; ++doc) {
        LongHashSet ordinalSet = new LongHashSet();
        final int numValues = randomInt(maxNumValuesPerDoc);
        while (ordinalSet.size() < numValues) {
            ordinalSet.add(random().nextInt(terms.length));
        }
        final long[] ords = ordinalSet.toArray();
        Arrays.sort(ords);
        values.ords = ords;
        // simulate aggregation
        collector.collect(doc);
        // now do it the naive way
        for (int i = 0; i < ranges.length; ++i) {
            for (long ord : ords) {
                BytesRef term = terms[(int) ord];
                if ((ranges[i].from == null || ranges[i].from.compareTo(term) <= 0) && (ranges[i].to == null || ranges[i].to.compareTo(term) > 0)) {
                    expectedCounts[i]++;
                    break;
                }
            }
        }
    }
    assertArrayEquals(expectedCounts, counts);
}
Also used : SortedSetRangeLeafCollector(org.elasticsearch.search.aggregations.bucket.range.BinaryRangeAggregator.SortedSetRangeLeafCollector) LongHashSet(com.carrotsearch.hppc.LongHashSet) LeafBucketCollector(org.elasticsearch.search.aggregations.LeafBucketCollector) BytesRef(org.apache.lucene.util.BytesRef) HashSet(java.util.HashSet) LongHashSet(com.carrotsearch.hppc.LongHashSet)

Example 15 with LongHashSet

use of com.carrotsearch.hppc.LongHashSet in project elasticsearch by elastic.

the class HistogramIT method testSingleValuedFieldOrderedBySubAggregationDesc.

public void testSingleValuedFieldOrderedBySubAggregationDesc() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(histogram("histo").field(SINGLE_VALUED_FIELD_NAME).interval(interval).order(Histogram.Order.aggregation("sum", false)).subAggregation(sum("sum").field(SINGLE_VALUED_FIELD_NAME))).execute().actionGet();
    assertSearchResponse(response);
    Histogram histo = response.getAggregations().get("histo");
    assertThat(histo, notNullValue());
    assertThat(histo.getName(), equalTo("histo"));
    assertThat(histo.getBuckets().size(), equalTo(numValueBuckets));
    LongHashSet visited = new LongHashSet();
    double previousSum = Double.POSITIVE_INFINITY;
    // TODO: use diamond once JI-9019884 is fixed
    List<Histogram.Bucket> buckets = new ArrayList<>(histo.getBuckets());
    for (int i = 0; i < numValueBuckets; ++i) {
        Histogram.Bucket bucket = buckets.get(i);
        assertThat(bucket, notNullValue());
        long key = ((Number) bucket.getKey()).longValue();
        assertTrue(visited.add(key));
        int b = (int) (key / interval);
        assertThat(bucket.getDocCount(), equalTo(valueCounts[b]));
        assertThat(bucket.getAggregations().asList().isEmpty(), is(false));
        Sum sum = bucket.getAggregations().get("sum");
        assertThat(sum, notNullValue());
        long s = 0;
        for (int j = 0; j < numDocs; ++j) {
            if ((j + 1) / interval == b) {
                s += j + 1;
            }
        }
        assertThat(sum.getValue(), equalTo((double) s));
        assertThat(sum.getValue(), lessThanOrEqualTo(previousSum));
        previousSum = s;
    }
}
Also used : LongHashSet(com.carrotsearch.hppc.LongHashSet) Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) ArrayList(java.util.ArrayList) Sum(org.elasticsearch.search.aggregations.metrics.sum.Sum) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

LongHashSet (com.carrotsearch.hppc.LongHashSet)25 LongSet (com.carrotsearch.hppc.LongSet)15 ArrayList (java.util.ArrayList)10 Test (org.junit.Test)7 SearchResponse (org.elasticsearch.action.search.SearchResponse)5 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)5 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)5 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)5 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)4 ExecutorService (java.util.concurrent.ExecutorService)4 Future (java.util.concurrent.Future)4 HashSet (java.util.HashSet)3 ExecutionException (java.util.concurrent.ExecutionException)3 BytesRef (org.apache.lucene.util.BytesRef)3 LongArrayList (com.carrotsearch.hppc.LongArrayList)2 ConsistentKeyIDAuthority (com.thinkaurelius.titan.diskstorage.idmanagement.ConsistentKeyIDAuthority)2 IDBlockSizer (com.thinkaurelius.titan.graphdb.database.idassigner.IDBlockSizer)2 Callable (java.util.concurrent.Callable)2 Direction (org.apache.tinkerpop.gremlin.structure.Direction)2 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)2