Java Code Examples for net.openhft.chronicle.core.io.IOTools#deleteDirWithFiles()

The following examples show how to use net.openhft.chronicle.core.io.IOTools#deleteDirWithFiles() . 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: LatencyDistributionMain.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
public void run(String[] args) throws InterruptedException {
    File tmpDir = getTmpDir();
    SingleChronicleQueueBuilder builder = SingleChronicleQueueBuilder
            .fieldlessBinary(tmpDir)
            .blockSize(128 << 20);
    try (ChronicleQueue queue = builder
            .writeBufferMode(BUFFER_MODE)
            .readBufferMode(BufferMode.None)
            .build();
         ChronicleQueue queue2 = builder
                 .writeBufferMode(BufferMode.None)
                 .readBufferMode(BUFFER_MODE)
                 .build()) {

        runTest(queue, queue2);
    }
    IOTools.deleteDirWithFiles(tmpDir, 2);
}
 
Example 2
Source File: RollEOFTest.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 5000L)
public void testRollWritesEOF() throws IOException {
    final File path = DirectoryUtils.tempDir(getClass().getName());
    try {
        path.mkdirs();
        final SetTimeProvider timeProvider = new SetTimeProvider();
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, -1);
        timeProvider.currentTimeMillis(cal.getTimeInMillis());
        createQueueAndWriteData(timeProvider, path);
        assertEquals(1, getNumberOfQueueFiles(path));

        // adjust time
        timeProvider.currentTimeMillis(System.currentTimeMillis());
        createQueueAndWriteData(timeProvider, path);
        assertEquals(2, getNumberOfQueueFiles(path));

        List<String> l = new LinkedList<>();
        new ChronicleReader().withMessageSink(l::add).withBasePath(path.toPath()).execute();
        // 2 entries per message
        assertEquals(4, l.size());
    } finally {
        IOTools.deleteDirWithFiles(path, 20);
    }
}
 
Example 3
Source File: QueueSingleThreadedJLBHBenchmark.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
@Override
public void init(JLBH jlbh) {
    IOTools.deleteDirWithFiles("replica", 10);

    Byteable byteable = (Byteable) datum;
    long capacity = byteable.maxSize();
    byteable.bytesStore(NativeBytesStore.nativeStore(capacity), 0, capacity);
    datumBytes = ((Byteable) datum).bytesStore();
    datumWrite = datumBytes.bytesForWrite();

    sourceQueue = single("replica").build();
    sinkQueue = single("replica").build();
    appender = sourceQueue.acquireAppender();
    tailer = sinkQueue.createTailer();
    this.jlbh = jlbh;
}
 
Example 4
Source File: QueueMultiThreadedJLBHBenchmark.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
@Override
public void init(JLBH jlbh) {
    IOTools.deleteDirWithFiles("replica", 10);

    sourceQueue = single("replica").build();
    sinkQueue = single("replica").build();
    appender = sourceQueue.acquireAppender();
    tailer = sinkQueue.createTailer();
    new Thread(() -> {
        Datum datum2 = new Datum();
        while (true) {
            try (DocumentContext dc = tailer.readingDocument()) {
                if (dc.wire() == null)
                    continue;
                datum2.readMarshallable(dc.wire().bytes());
                jlbh.sample(System.nanoTime() - datum2.ts);
            }
        }
    }).start();
}
 
Example 5
Source File: DirectoryUtils.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
public static void deleteDir(@NotNull File dir) {
    try {
        IOTools.deleteDirWithFiles(dir, 20);
    } catch (Throwable e) {
        e.printStackTrace();
    }
}
 
Example 6
Source File: MarshallableTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Test
public void testWriteText() {
    File dir = DirectoryUtils.tempDir("testWriteText");
    try (ChronicleQueue queue = binary(dir)
            .testBlockSize()
            .build()) {

        ExcerptAppender appender = queue.acquireAppender();
        ExcerptTailer tailer = queue.createTailer();
        ExcerptTailer tailer2 = queue.createTailer();

        int runs = 1000;
        for (int i = 0; i < runs; i++)
            appender.writeText("" + i);
        for (int i = 0; i < runs; i++)
            assertEquals("" + i, tailer.readText());
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < runs; i++) {
            assertTrue(tailer2.readText(sb));
            assertEquals("" + i, sb.toString());
        }
    } finally {
        try {
            IOTools.deleteDirWithFiles(dir, 2);
        } catch (IORuntimeException e) {
            // ignored
        }
    }
}
 
Example 7
Source File: SingleChronicleQueueBuilderTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDetermineQueueDirectoryFromQueueFile() {
    final Path path = Paths.get(OS.USER_DIR, TEST_QUEUE_FILE);
    try (final ChronicleQueue queue =
                 ChronicleQueue.singleBuilder(path)
                         .testBlockSize()
                         .build()) {
        assertFalse(queue.createTailer().readingDocument().isPresent());
    } finally {
        IOTools.deleteDirWithFiles(path.toFile(), 20);
    }
}
 
Example 8
Source File: QueueLargeMessageJLBHBenchmark.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Override
public void init(JLBH jlbh) {
    IOTools.deleteDirWithFiles("large", 3);

    sourceQueue = single("large").blockSize(1L << 30).build();
    sinkQueue = single("large").blockSize(1L << 30).build();
    appender = sourceQueue.acquireAppender();
    tailer = sinkQueue.createTailer();
    this.jlbh = jlbh;
}
 
Example 9
Source File: BridgeMain.java    From Chronicle-Queue-Demo with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    IOTools.deleteDirWithFiles("in", 2);
    IOTools.deleteDirWithFiles("out", 2);

    long events = 0, lastPrint = 0;
    try (ChronicleQueue queue = ChronicleQueue.singleBuilder("in").sourceId(1).build()) {
        try (ChronicleQueue queue2 = ChronicleQueue.singleBuilder("out").sourceId(2).build()) {

            Events out = queue2.methodWriterBuilder(Events.class).recordHistory(true).build();
            Events bridge = new BridgeEvents(out);
            MethodReader methodReader = queue.createTailer("bridge")
                    .methodReader(bridge);
            System.out.println("Started");
            long last = 0;
            while (running) {
                if (methodReader.readOne()) {
                    events++;
                } else {
                    long now = System.currentTimeMillis();
                    if (lastPrint != events && now > last + 250) {
                        System.out.println("events: " + events);
                        lastPrint = events;
                        last = now;
                    } else {
                        Thread.yield();
                    }
                }
            }
        }
    }
    System.out.println("... finished");
}
 
Example 10
Source File: RollEOFTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 5000L)
public void testRollWithoutEOF() throws IOException {
    final File path = DirectoryUtils.tempDir(getClass().getName());
    try {
        path.mkdirs();
        final SetTimeProvider timeProvider = new SetTimeProvider();
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, -3);
        timeProvider.currentTimeMillis(cal.getTimeInMillis());
        createQueueAndWriteData(timeProvider, path);
        assertEquals(1, getNumberOfQueueFiles(path));

        // adjust time
        timeProvider.currentTimeMillis(System.currentTimeMillis());
        createQueueAndWriteData(timeProvider, path);
        assertEquals(2, getNumberOfQueueFiles(path));

        Optional<Path> firstQueueFile = Files.list(path.toPath()).filter(p -> p.toString().endsWith(SUFFIX)).sorted().findFirst();

        assertTrue(firstQueueFile.isPresent());

        // remove EOF from first file
        removeEOF(firstQueueFile.get());

        List<String> l = new LinkedList<>();
        new ChronicleReader().withMessageSink(l::add).withBasePath(path.toPath()).withReadOnly(false).execute();
        // 2 entries per message
        assertEquals(4, l.size());
    } finally {

        IOTools.deleteDirWithFiles(path, 20);
    }
}
 
Example 11
Source File: RollEOFTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 5000L)
public void testRollWithoutEOFDoesntBlowup() throws IOException {
    final File path = DirectoryUtils.tempDir(getClass().getName());
    try {
        path.mkdirs();
        final SetTimeProvider timeProvider = new SetTimeProvider();
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, -1);
        timeProvider.currentTimeMillis(cal.getTimeInMillis());
        createQueueAndWriteData(timeProvider, path);
        assertEquals(1, getNumberOfQueueFiles(path));

        // adjust time
        timeProvider.currentTimeMillis(System.currentTimeMillis());
        createQueueAndWriteData(timeProvider, path);
        assertEquals(2, getNumberOfQueueFiles(path));

        Optional<Path> firstQueueFile = Files.list(path.toPath()).filter(p -> p.toString().endsWith(SUFFIX)).sorted().findFirst();

        assertTrue(firstQueueFile.isPresent());

        // remove EOF from first file
        removeEOF(firstQueueFile.get());

        List<String> l = new LinkedList<>();
        new ChronicleReader().withMessageSink(l::add).withBasePath(path.toPath()).execute();
        // 2 entries per message
        assertEquals(4, l.size());
    } finally {

        IOTools.deleteDirWithFiles(path, 20);
    }
}
 
Example 12
Source File: OvertakeTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@After
public void after() {
    try {
        IOTools.deleteDirWithFiles(path, 2);
    } catch (Exception ignored) {
    }
}
 
Example 13
Source File: WriteBytesTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Test
public void testWriteBytes() {
    File dir = DirectoryUtils.tempDir("WriteBytesTest");
    try (ChronicleQueue queue = binary(dir)
            .testBlockSize()
            .build()) {

        ExcerptAppender appender = queue.acquireAppender();
        ExcerptTailer tailer = queue.createTailer();

        outgoingMsgBytes[0] = 'A';
        outgoingBytes.write(outgoingMsgBytes);
        postOneMessage(appender);
        fetchOneMessage(tailer, incomingMsgBytes);
        System.out.println(new String(incomingMsgBytes));

        outgoingBytes.clear();

        outgoingMsgBytes[0] = 'A';
        outgoingMsgBytes[1] = 'B';
        outgoingBytes.write(outgoingMsgBytes);

        postOneMessage(appender);
        fetchOneMessage(tailer, incomingMsgBytes);
        System.out.println(new String(incomingMsgBytes));

    } finally {
        try {
            IOTools.deleteDirWithFiles(dir, 2);
        } catch (IORuntimeException e) {
            // ignored
        }
    }
}
 
Example 14
Source File: HelloWorldTest.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
@Test
    @Ignore("TODO FIX")
    public void testWithAsQueueService() {
        // acts as three processes in one test
        // process A writes to the HelloWorld interface.
        // process B read fromt he HelloWorld interface and writes to the
        String input = OS.TARGET + "/input-" + System.nanoTime();
        String output = OS.TARGET + "/output-" + System.nanoTime();

        HelloReplier replier = createMock(HelloReplier.class);
        replier.reply("Hello April");
        replier.reply("Hello June");
        replay(replier);

        ServiceWrapperBuilder<HelloReplier> builder = ServiceWrapperBuilder
                .serviceBuilder(input, output, HelloReplier.class, HelloWorldImpl::new)
                .inputSourceId(1).outputSourceId(2);

        try (CloseableHelloWorld helloWorld = builder.inputWriter(CloseableHelloWorld.class);
             MethodReader replyReader = builder.outputReader(replier);
             ServiceWrapper helloWorldService = builder.get()) {

            helloWorld.hello("April");
            helloWorld.hello("June");

//            System.out.println(helloWorldService.inputQueues()[0].dump());
            for (int i = 0; i < 2; i++) {
                while (!replyReader.readOne()) {
                    Thread.yield();
                }
            }
//            System.out.println(helloWorldService.outputQueue().dump());
            verify(replier);
        } finally {
            builder.closeQueues();
            try {
                IOTools.deleteDirWithFiles(new File(input), 2);
                IOTools.deleteDirWithFiles(new File(output), 2);
            } catch (IORuntimeException e) {
                e.printStackTrace();
            }
        }
    }
 
Example 15
Source File: ThroughputPerfMain2.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
public static void main(String[] args) {
    String base = path + "/delete-" + System.nanoTime() + ".me";
    long start = System.nanoTime();
    long count = 0;
    nbs = NativeBytesStore.nativeStoreWithFixedCapacity(size);

    long blockSize = OS.is64Bit() ? 4L << 30 : 256L << 20;
    try (ChronicleQueue q = SingleChronicleQueueBuilder.binary(base)
            .rollCycle(RollCycles.LARGE_HOURLY_XSPARSE)
            .blockSize(blockSize)
            .build()) {

        ExcerptAppender appender = q.acquireAppender();
        count += appender.batchAppend(time * 1000, ThroughputPerfMain2::writeMessages);
    }

    nbs.releaseLast();
    long mid = System.nanoTime();
    long time1 = mid - start;

    Bytes bytes = Bytes.allocateElasticDirect(64);
    try (ChronicleQueue q = SingleChronicleQueueBuilder.binary(base)
            .rollCycle(RollCycles.LARGE_HOURLY_XSPARSE)
            .blockSize(blockSize)
            .build()) {
        ExcerptTailer tailer = q.createTailer();
        for (long i = 0; i < count; i++) {
            try (DocumentContext dc = tailer.readingDocument()) {
                bytes.clear();
                bytes.write(dc.wire().bytes());
            }
        }
    }
    bytes.releaseLast();
    long end = System.nanoTime();
    long time2 = end - mid;

    System.out.printf("Writing %,d messages took %.3f seconds, at a rate of %,d per second%n",
            count, time1 / 1e9, (long) (1e9 * count / time1));
    System.out.printf("Reading %,d messages took %.3f seconds, at a rate of %,d per second%n",
            count, time2 / 1e9, (long) (1e9 * count / time2));

    System.gc(); // make sure its cleaned up for windows to delete.
    IOTools.deleteDirWithFiles(base, 2);
}
 
Example 16
Source File: JDBCServiceTest.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
private void doCreateTable(int repeats, int noUpdates) {
    for (int t = 0; t < repeats; t++) {
        long start = System.nanoTime(), written;
        File path1 = DirectoryUtils.tempDir("createTable1");
        File path2 = DirectoryUtils.tempDir("createTable2");
        File file = new File(OS.TARGET, "hsqldb-" + System.nanoTime());
        file.deleteOnExit();

        try (ChronicleQueue in = SingleChronicleQueueBuilder
                .binary(path1)
                .testBlockSize()
                .build();
             ChronicleQueue out = SingleChronicleQueueBuilder
                     .binary(path2)
                     .testBlockSize()
                     .build()) {

            JDBCService service = new JDBCService(in, out, () -> DriverManager.getConnection("jdbc:hsqldb:file:" + file.getAbsolutePath(), "SA", ""));

            JDBCStatement writer = service.createWriter();
            writer.executeUpdate("CREATE TABLE tableName (\n" +
                    "name VARCHAR(64) NOT NULL,\n" +
                    "num INT\n" +
                    ")\n");

            for (int i = 1; i < (long) noUpdates; i++)
                writer.executeUpdate("INSERT INTO tableName (name, num)\n" +
                        "VALUES (?, ?)", "name", i);

            written = System.nanoTime() - start;
            AtomicLong queries = new AtomicLong();
            AtomicLong updates = new AtomicLong();
            CountingJDBCResult countingJDBCResult = new CountingJDBCResult(queries, updates);
            MethodReader methodReader = service.createReader(countingJDBCResult);
            while (updates.get() < noUpdates) {
                if (!methodReader.readOne())
                    Thread.yield();
            }
            Closeable.closeQuietly(service);

            long time = System.nanoTime() - start;
            System.out.printf("Average time to write each update %.1f us, average time to perform each update %.1f us%n",
                    written / noUpdates / 1e3,
                    time / noUpdates / 1e3);
        } finally {
            try {
                IOTools.deleteDirWithFiles(path1, 2);
                IOTools.deleteDirWithFiles(path2, 2);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
 
Example 17
Source File: ChronicleQueuePeekDocumentTest.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
@Test
public void testUsingPeekDocument() throws IOException {
    Path tempDir = null;
    try {
        tempDir = IOTools.createTempDirectory("ChronicleQueueLoggerTest");
        // Read back the data
        try (ChronicleQueue queue = SingleChronicleQueueBuilder.binary(tempDir).build()) {
            ExcerptTailer tailer = queue.createTailer();

            try (ChronicleQueue writeQueue = SingleChronicleQueueBuilder.binary(tempDir).build()) {
                ExcerptAppender appender = writeQueue.acquireAppender();

                try (DocumentContext dc = appender.writingDocument()) {
                    dc.wire().write("field1").int32(123534)
                            .write("field2").float64(123.423)
                            .write("time").int64(12053432432L);
                }

                try (DocumentContext dc = appender.writingDocument()) {
                    dc.wire().write("field1").int32(323242)
                            .write("field2").float64(543.1233)
                            .write("time").int64(12053432900L);
                }
            }

            assertEquals("field1: !int 123534\n" +
                            "field2: 123.423\n" +
                            "time: 12053432432\n",
                    read(tailer));

            assertEquals("field1: !int 323242\n" +
                            "field2: 543.1233\n" +
                            "time: 12053432900\n",
                    read(tailer));
        }
    } finally {
        if (tempDir != null) {
            IOTools.deleteDirWithFiles(tempDir.toFile(), 2);
        }
    }
}