Search in sources :

Example 1 with TupleFileReader

use of org.bboxdb.tools.TupleFileReader in project bboxdb by jnidzwetzki.

the class TestFixedGrid method runExperiment.

/**
 * Run this experiment
 * @param cellGrid
 */
protected void runExperiment(final CellGrid cellGrid) {
    final TupleFileReader tupleFile = new TupleFileReader(filename, format);
    final Map<BoundingBox, Integer> bboxes = new HashMap<>();
    final ExecutorService executor = ExecutorUtil.getBoundThreadPoolExecutor(10, 100);
    tupleFile.addTupleListener(t -> {
        executor.submit(() -> {
            final Set<BoundingBox> intersectedBoxes = cellGrid.getAllInersectedBoundingBoxes(t.getBoundingBox());
            synchronized (bboxes) {
                for (final BoundingBox box : intersectedBoxes) {
                    if (bboxes.containsKey(box)) {
                        final int oldValue = bboxes.get(box);
                        bboxes.put(box, oldValue + 1);
                    } else {
                        bboxes.put(box, 1);
                    }
                }
            }
        });
    });
    try {
        System.out.println("# Processing tuples");
        tupleFile.processFile();
        executor.shutdown();
    } catch (IOException e) {
        System.err.println("Got an IOException during experiment: " + e);
        System.exit(-1);
    }
    calculateResult(bboxes);
}
Also used : HashMap(java.util.HashMap) TupleFileReader(org.bboxdb.tools.TupleFileReader) BoundingBox(org.bboxdb.commons.math.BoundingBox) ExecutorService(java.util.concurrent.ExecutorService) IOException(java.io.IOException)

Example 2 with TupleFileReader

use of org.bboxdb.tools.TupleFileReader in project bboxdb by jnidzwetzki.

the class TestKDTreeSplit method runExperiment.

/**
 * Run the experiment with the given sample size
 * @param sampleSize
 * @throws IOException
 */
protected void runExperiment(final int maxRegionSize) {
    System.out.println("# Simulating with max element size: " + maxRegionSize);
    elements.clear();
    boxDimension.clear();
    for (Entry<String, String> elements : filesAndFormats.entrySet()) {
        final String filename = elements.getKey();
        final String format = elements.getValue();
        System.out.println("Processing file: " + filename);
        final TupleFileReader tupleFile = new TupleFileReader(filename, format);
        tupleFile.addTupleListener(t -> {
            insertNextBoundingBox(t.getBoundingBox(), maxRegionSize);
        });
        try {
            tupleFile.processFile();
        } catch (IOException e) {
            System.err.println("Got an IOException during experiment: " + e);
            System.exit(-1);
        }
    }
    // Print results
    final List<Integer> buckets = elements.values().stream().map(l -> l.size()).collect(Collectors.toList());
    IntStream.range(0, buckets.size()).forEach(i -> System.out.format("%d\t%d\n", i, buckets.get(i)));
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) Predicate(java.util.function.Predicate) Set(java.util.Set) IOException(java.io.IOException) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) BoundingBox(org.bboxdb.commons.math.BoundingBox) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Objects(java.util.Objects) List(java.util.List) TupleFileReader(org.bboxdb.tools.TupleFileReader) Map(java.util.Map) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Entry(java.util.Map.Entry) MathUtil(org.bboxdb.commons.MathUtil) TupleFileReader(org.bboxdb.tools.TupleFileReader) IOException(java.io.IOException)

Example 3 with TupleFileReader

use of org.bboxdb.tools.TupleFileReader in project bboxdb by jnidzwetzki.

the class CreateInitialPartitioning method run.

@Override
public void run() {
    final TupleFileReader tupleFile = new TupleFileReader(filename, format);
    final List<BoundingBox> samples = new ArrayList<>();
    tupleFile.addTupleListener(t -> {
        final BoundingBox polygonBoundingBox = t.getBoundingBox();
        samples.add(polygonBoundingBox);
    });
    try {
        tupleFile.processFile();
        final SpacePartitioner spacePartitioner = SpacePartitionerCache.getInstance().getSpacePartitionerForGroupName(distributionGroup);
        final DistributionRegionAdapter adapter = ZookeeperClientFactory.getZookeeperClient().getDistributionRegionAdapter();
        while (getActiveRegions(spacePartitioner).size() < partitions) {
            logger.info("We have now {} of {} active partitons, executing split", getActiveRegions(spacePartitioner).size() < partitions);
            final List<DistributionRegion> activeRegions = getActiveRegions(spacePartitioner);
            final DistributionRegion regionToSplit = ListHelper.getElementRandom(activeRegions);
            logger.info("Splitting region {}", regionToSplit.getRegionId());
            final List<DistributionRegion> destination = spacePartitioner.splitRegion(regionToSplit, samples);
            spacePartitioner.splitComplete(regionToSplit, destination);
        }
        // Prevent merging of nodes
        for (DistributionRegion region : spacePartitioner.getRootNode().getAllChildren()) {
            adapter.setMergingSupported(region, false);
        }
    } catch (Exception e) {
        logger.error("Got an exception", e);
        System.exit(-1);
    }
}
Also used : DistributionRegion(org.bboxdb.distribution.region.DistributionRegion) TupleFileReader(org.bboxdb.tools.TupleFileReader) BoundingBox(org.bboxdb.commons.math.BoundingBox) ArrayList(java.util.ArrayList) DistributionRegionAdapter(org.bboxdb.distribution.zookeeper.DistributionRegionAdapter) ZookeeperNotFoundException(org.bboxdb.distribution.zookeeper.ZookeeperNotFoundException) ZookeeperException(org.bboxdb.distribution.zookeeper.ZookeeperException) BBoxDBException(org.bboxdb.misc.BBoxDBException) SpacePartitioner(org.bboxdb.distribution.partitioner.SpacePartitioner)

Example 4 with TupleFileReader

use of org.bboxdb.tools.TupleFileReader in project bboxdb by jnidzwetzki.

the class TestTupleBuilder method testTupleFile3.

/**
 * Test the tuple file builder
 * @throws IOException
 */
@Test
public void testTupleFile3() throws IOException {
    final File tempFile = File.createTempFile("temp", ".txt");
    tempFile.deleteOnExit();
    // The reference tuple
    final TupleBuilder tupleBuilder = TupleBuilderFactory.getBuilderForFormat(TupleBuilderFactory.Name.GEOJSON);
    final Tuple tuple = tupleBuilder.buildTuple("1", GEO_JSON_LINE);
    final BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
    writer.write(GEO_JSON_LINE);
    writer.write("\n");
    writer.write(GEO_JSON_LINE);
    writer.write("\n");
    writer.close();
    final TupleFileReader tupleFile = new TupleFileReader(tempFile.getAbsolutePath(), TupleBuilderFactory.Name.GEOJSON);
    final AtomicInteger seenTuples = new AtomicInteger(0);
    tupleFile.addTupleListener(t -> {
        Assert.assertEquals(tuple.getKey(), t.getKey());
        Assert.assertEquals(tuple.getBoundingBox(), t.getBoundingBox());
        Assert.assertArrayEquals(tuple.getDataBytes(), t.getDataBytes());
        seenTuples.incrementAndGet();
    });
    tupleFile.processFile(1);
    Assert.assertEquals(1, seenTuples.get());
    Assert.assertEquals(2, tupleFile.getProcessedLines());
    Assert.assertEquals(GEO_JSON_LINE, tupleFile.getLastReadLine());
}
Also used : TupleBuilder(org.bboxdb.tools.converter.tuple.TupleBuilder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FileWriter(java.io.FileWriter) TupleFileReader(org.bboxdb.tools.TupleFileReader) File(java.io.File) Tuple(org.bboxdb.storage.entity.Tuple) BufferedWriter(java.io.BufferedWriter) Test(org.junit.Test)

Example 5 with TupleFileReader

use of org.bboxdb.tools.TupleFileReader in project bboxdb by jnidzwetzki.

the class TestTupleBuilder method testTupleFile1.

/**
 * Test the tuple file builder - Process non existing file
 * @throws IOException
 */
@Test(expected = IOException.class)
public void testTupleFile1() throws IOException {
    final File tempFile = File.createTempFile("temp", ".txt");
    tempFile.delete();
    Assert.assertFalse(tempFile.exists());
    final TupleFileReader tupleFile = new TupleFileReader(tempFile.getAbsolutePath(), TupleBuilderFactory.Name.GEOJSON);
    tupleFile.processFile();
}
Also used : TupleFileReader(org.bboxdb.tools.TupleFileReader) File(java.io.File) Test(org.junit.Test)

Aggregations

TupleFileReader (org.bboxdb.tools.TupleFileReader)11 IOException (java.io.IOException)6 BoundingBox (org.bboxdb.commons.math.BoundingBox)5 File (java.io.File)4 ArrayList (java.util.ArrayList)4 Tuple (org.bboxdb.storage.entity.Tuple)4 Test (org.junit.Test)4 BufferedWriter (java.io.BufferedWriter)3 FileWriter (java.io.FileWriter)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 TupleBuilder (org.bboxdb.tools.converter.tuple.TupleBuilder)3 Arrays (java.util.Arrays)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Objects (java.util.Objects)2 Collectors (java.util.stream.Collectors)2 BBoxDBException (org.bboxdb.misc.BBoxDBException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1