Search in sources :

Example 1 with IntHashSet

use of org.janusgraph.graphdb.util.IntHashSet in project janusgraph by JanusGraph.

the class IDPoolTest method testIDPoolWith.

private void testIDPoolWith(IDPoolFactory poolFactory, final int numPartitions, final int numThreads, final int attemptsPerThread) throws InterruptedException {
    final Random random = new Random();
    final IntSet[] ids = new IntSet[numPartitions];
    final StandardIDPool[] idPools = new StandardIDPool[numPartitions];
    for (int i = 0; i < numPartitions; i++) {
        ids[i] = new IntHashSet(attemptsPerThread * numThreads / numPartitions);
        int partition = i * 100;
        idPools[i] = poolFactory.get(partition);
    }
    Thread[] threads = new Thread[numThreads];
    for (int i = 0; i < numThreads; i++) {
        threads[i] = new Thread(() -> {
            for (int attempt = 0; attempt < attemptsPerThread; attempt++) {
                int offset = random.nextInt(numPartitions);
                long id = idPools[offset].nextID();
                assertTrue(id < Integer.MAX_VALUE);
                IntSet idSet = ids[offset];
                synchronized (idSet) {
                    assertFalse(idSet.contains((int) id));
                    idSet.add((int) id);
                }
            }
        });
        threads[i].start();
    }
    for (int i = 0; i < numThreads; i++) threads[i].join();
    for (final StandardIDPool idPool : idPools) idPool.close();
    // Verify consecutive id assignment
    for (int i = 0; i < ids.length; i++) {
        IntSet set = ids[i];
        int max = 0;
        int[] all = set.getAll();
        for (int id : all) if (id > max)
            max = id;
        for (int j = 1; j <= max; j++) assertTrue(set.contains(j), i + " contains: " + j);
    }
}
Also used : StandardIDPool(org.janusgraph.graphdb.database.idassigner.StandardIDPool) Random(java.util.Random) IntSet(org.janusgraph.graphdb.util.IntSet) IntHashSet(org.janusgraph.graphdb.util.IntHashSet)

Aggregations

Random (java.util.Random)1 StandardIDPool (org.janusgraph.graphdb.database.idassigner.StandardIDPool)1 IntHashSet (org.janusgraph.graphdb.util.IntHashSet)1 IntSet (org.janusgraph.graphdb.util.IntSet)1