Java Code Examples for net.openhft.chronicle.core.OS#isWindows()

The following examples show how to use net.openhft.chronicle.core.OS#isWindows() . 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: SingleChronicleQueueTest.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldBeAbleToLoadQueueFromReadOnlyFiles() throws IOException {
    if (OS.isWindows()) {
        System.err.println("#460 Cannot test read only mode on windows");
        return;
    }

    final File queueDir = getTmpDir();
    try (final ChronicleQueue queue = builder(queueDir, wireType).
            testBlockSize().build()) {
        queue.acquireAppender().writeDocument("foo", (v, t) -> {
            v.text(t);
        });
    }

    Files.list(queueDir.toPath())
            .forEach(p -> assertTrue(p.toFile().setReadOnly()));

    try (final ChronicleQueue queue = builder(queueDir, wireType).
            readOnly(true).
            testBlockSize().build()) {
        assertTrue(queue.createTailer().readingDocument().isPresent());
    }
}
 
Example 2
Source File: ChronicleReaderTest.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 10_000L)
public void shouldReadQueueWithNonDefaultRollCycle() {
    if (OS.isWindows())
        return;

    Path path = DirectoryUtils.tempDir("shouldReadQueueWithNonDefaultRollCycle").toPath();
    path.toFile().mkdirs();
    try (final ChronicleQueue queue = SingleChronicleQueueBuilder.binary(path).rollCycle(RollCycles.MINUTELY).
            testBlockSize().build()) {
        final ExcerptAppender excerptAppender = queue.acquireAppender();
        final VanillaMethodWriterBuilder<StringEvents> methodWriterBuilder = excerptAppender.methodWriterBuilder(StringEvents.class);
        methodWriterBuilder.recordHistory(true);
        final StringEvents events = methodWriterBuilder.build();

        for (int i = 0; i < 24; i++) {
            events.say(i % 2 == 0 ? "hello" : "goodbye");
        }
    }

    new ChronicleReader().withBasePath(path).withMessageSink(capturedOutput::add).execute();
    assertFalse(capturedOutput.isEmpty());
}
 
Example 3
Source File: ChronicleReaderTest.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 10_000L)
public void shouldReadQueueWithNonDefaultRollCycleWhenMetadataDeleted() throws IOException {
    if (OS.isWindows())
        return;
    expectException("Failback to readonly tablestore");
    Path path = DirectoryUtils.tempDir("shouldReadQueueWithNonDefaultRollCycle").toPath();
    path.toFile().mkdirs();
    try (final ChronicleQueue queue = SingleChronicleQueueBuilder.binary(path).rollCycle(RollCycles.MINUTELY).
            testBlockSize().build()) {
        final ExcerptAppender excerptAppender = queue.acquireAppender();
        final VanillaMethodWriterBuilder<StringEvents> methodWriterBuilder = excerptAppender.methodWriterBuilder(StringEvents.class);
        methodWriterBuilder.recordHistory(true);
        final StringEvents events = methodWriterBuilder.build();

        for (int i = 0; i < 24; i++) {
            events.say(i % 2 == 0 ? "hello" : "goodbye");
        }
    }
    Files.list(path).filter(f -> f.getFileName().toString().endsWith(SingleTableStore.SUFFIX)).findFirst().ifPresent(p -> p.toFile().delete());
    waitForGcCycle();

    new ChronicleReader().withBasePath(path).withMessageSink(capturedOutput::add).execute();
    assertFalse(capturedOutput.isEmpty());
}
 
Example 4
Source File: SingleChronicleQueueTest.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
@Test
public void testCleanupDir() {
    File tmpDir = getTmpDir();
    try (final ChronicleQueue queue =
                 builder(tmpDir, wireType)
                         .build()) {
        final ExcerptAppender appender = queue.acquireAppender();

        try (DocumentContext dc = appender.writingDocument()) {
            dc.wire().write("hello").text("world");
        }
    }
    DirectoryUtils.deleteDir(tmpDir);
    if (OS.isWindows()) {
        System.err.println("#460 Directory clean up not supported on Windows");
    } else {
        assertFalse(tmpDir.exists());
    }
}
 
Example 5
Source File: DumpQueueMainTest.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldBeAbleToDumpReadOnlyQueueFile() throws IOException {
    if (OS.isWindows())
        return;

    final File dataDir = DirectoryUtils.tempDir(DumpQueueMainTest.class.getSimpleName());
    try (final ChronicleQueue queue = SingleChronicleQueueBuilder.
            binary(dataDir).
            build()) {

        final ExcerptAppender excerptAppender = queue.acquireAppender();
        excerptAppender.writeText("first");
        excerptAppender.writeText("last");

        final Path queueFile = Files.list(dataDir.toPath()).
                filter(p -> p.toString().endsWith(SingleChronicleQueue.SUFFIX)).
                findFirst().orElseThrow(() ->
                new AssertionError("Could not find queue file in directory " + dataDir));
        assertTrue(queueFile.toFile().setWritable(false));

        final CountingOutputStream countingOutputStream = new CountingOutputStream();
        DumpQueueMain.dump(queueFile.toFile(), new PrintStream(countingOutputStream), Long.MAX_VALUE);

        assertThat(countingOutputStream.bytes, is(not(0L)));
    }
}
 
Example 6
Source File: ChronicleReaderTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldBeAbleToReadFromReadOnlyFile() throws IOException {
    if (OS.isWindows()) {
        System.err.println("#460 read-only not supported on Windows");
        return;
    }
    final Path queueFile = Files.list(dataDir).
            filter(f -> f.getFileName().toString().endsWith(SingleChronicleQueue.SUFFIX)).findFirst().
            orElseThrow(() ->
                    new AssertionError("Could not find queue file in directory " + dataDir));

    assertTrue(queueFile.toFile().setWritable(false));

    basicReader().execute();
}
 
Example 7
Source File: SingleChronicleQueueTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldCreateQueueInCurrentDirectory() {
    if (OS.isWindows()) {
        System.err.println("#460 Cannot test delete after close on windows");
        return;
    }

    try (final ChronicleQueue ignored =
                 builder(new File(""), wireType).
                         testBlockSize().build()) {

    }

    assertTrue(new File(QUEUE_METADATA_FILE).delete());
}
 
Example 8
Source File: SingleChronicleQueueTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Test
public void checkReferenceCountingAndCheckFileDeletion() {

    MappedFile mappedFile;

    try (ChronicleQueue queue =
                 binary(getTmpDir())
                         .rollCycle(RollCycles.TEST_SECONDLY)
                         .build()) {
        ExcerptAppender appender = queue.acquireAppender();

        try (DocumentContext documentContext1 = appender.writingDocument()) {
            documentContext1.wire().write().text("some text");
        }

        try (DocumentContext documentContext = queue.createTailer().readingDocument()) {
            mappedFile = toMappedFile(documentContext);
            assertEquals("some text", documentContext.wire().read().text());
        }
    }

    waitFor(mappedFile::isClosed, "mappedFile is not closed");

    if (OS.isWindows()) {
        System.err.println("#460 Cannot test delete after close on windows");
        return;
    }
    // this used to fail on windows
    assertTrue(mappedFile.file().delete());

}
 
Example 9
Source File: SingleChronicleQueueTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Test
public void testToEndOnDeletedQueueFiles() throws IOException {
    if (OS.isWindows()) {
        System.err.println("#460 Cannot test delete after close on windows");
        return;
    }

    File dir = getTmpDir();
    try (ChronicleQueue q = builder(dir, wireType).build()) {
        ExcerptAppender append = q.acquireAppender();
        append.writeDocument(w -> w.write(() -> "test").text("before text"));

        ExcerptTailer tailer = q.createTailer();

        // move to the end even though it doesn't exist yet.
        tailer.toEnd();

        append.writeDocument(w -> w.write(() -> "test").text("text"));

        assertTrue(tailer.readDocument(w -> w.read(() -> "test").text("text", Assert::assertEquals)));

        Files.find(dir.toPath(), 1, (p, basicFileAttributes) -> p.toString().endsWith("cq4"), FileVisitOption.FOLLOW_LINKS)
                .forEach(path -> assertTrue(path.toFile().delete()));

        try (ChronicleQueue q2 = builder(dir, wireType).build()) {
            tailer = q2.createTailer();
            tailer.toEnd();
            assertEquals(TailerState.UNINITIALISED, tailer.state());
            append = q2.acquireAppender();
            append.writeDocument(w -> w.write(() -> "test").text("before text"));

            assertTrue(tailer.readDocument(w -> w.read(() -> "test").text("before text", Assert::assertEquals)));
        }
    }
}
 
Example 10
Source File: SingleChronicleQueueTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldAllowDirectoryToBeDeletedWhenQueueIsClosed() throws IOException {
    if (OS.isWindows()) {
        System.err.println("#460 Cannot test deleting after close on windows");
        return;
    }

    final File dir = DirectoryUtils.tempDir("to-be-deleted");
    try (final ChronicleQueue queue =
                 builder(dir, wireType).
                         testBlockSize().build()) {
        try (final DocumentContext dc = queue.acquireAppender().writingDocument()) {
            dc.wire().write().text("foo");
        }
        try (final DocumentContext dc = queue.createTailer().readingDocument()) {
            assertEquals("foo", dc.wire().read().text());
        }
    }

    Files.walk(dir.toPath()).forEach(p -> {
        if (!Files.isDirectory(p)) {
            assertTrue(p.toString(), p.toFile().delete());
        }
    });

    assertTrue(dir.delete());
}
 
Example 11
Source File: ChronicleMethodReaderTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldBeAbleToReadFromReadOnlyFile() throws IOException {
    if (OS.isWindows()) {
        System.err.println("#460 read-only not supported on Windows");
        return;
    }
    final Path queueFile = Files.list(dataDir).
            filter(f -> f.getFileName().toString().endsWith(SingleChronicleQueue.SUFFIX)).findFirst().
            orElseThrow(() ->
                    new AssertionError("Could not find queue file in directory " + dataDir));

    assertTrue(queueFile.toFile().setWritable(false));

    basicReader().execute();
}
 
Example 12
Source File: VanillaChronicleHash.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
private void msync(long address, long length) throws IOException {
    // address should be a multiple of page size
    if (OS.pageAlign(address) != address) {
        long oldAddress = address;
        address = OS.pageAlign(address) - OS.pageSize();
        length += oldAddress - address;
    }
    if (OS.isWindows()) {
        WindowsMsync.msync(raf, address, length);
    } else {
        PosixMsync.msync(address, length);
    }
}
 
Example 13
Source File: ChronicleReaderTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 5000)
public void readOnlyQueueTailerShouldObserveChangesAfterInitiallyObservedReadLimit() throws IOException, InterruptedException, TimeoutException, ExecutionException {
    DirectoryUtils.deleteDir(dataDir.toFile());
    dataDir.toFile().mkdirs();
    try (final ChronicleQueue queue = SingleChronicleQueueBuilder.binary(dataDir).testBlockSize().build()) {

        final StringEvents events = queue.acquireAppender().methodWriterBuilder(StringEvents.class).build();
        events.say("hello");

        final long readerCapacity = getCurrentQueueFileLength(dataDir);

        final RecordCounter recordCounter = new RecordCounter();
        final ChronicleReader chronicleReader = basicReader().withMessageSink(recordCounter);

        final ExecutorService executorService = Executors.newSingleThreadExecutor(
                new NamedThreadFactory("executor"));
        Future<?> submit = executorService.submit(chronicleReader::execute);

        final long expectedReadingDocumentCount = (readerCapacity / ONE_KILOBYTE.length) + 1;
        int i;
        for (i = 0; i < expectedReadingDocumentCount; i++) {
            events.say(new String(ONE_KILOBYTE));
        }

        recordCounter.latch.countDown();
        executorService.shutdown();
        executorService.awaitTermination(Jvm.isDebug() ? 50 : 5, TimeUnit.SECONDS);
        submit.get(1, TimeUnit.SECONDS);

        // #460 read only not supported on windows.
        if (!OS.isWindows())
            assertEquals(expectedReadingDocumentCount, recordCounter.recordCount.get() - 1);
    }
}
 
Example 14
Source File: ReadWriteTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void testWriteToReadOnlyChronicle() {
    if (OS.isWindows()) {
        System.err.println("#460 Cannot test read only mode on windows");
        throw new IllegalStateException("not run");
    }

    try (ChronicleQueue out = SingleChronicleQueueBuilder
            .binary(chroniclePath)
            .testBlockSize()
            .readOnly(true)
            .build()) {
        out.acquireAppender();
    }
}
 
Example 15
Source File: ReadWriteTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@After
public void teardown() {
    try {
        IOTools.shallowDeleteDirWithFiles(chroniclePath);
    } catch (Exception e) {
        if (e instanceof AccessDeniedException && OS.isWindows())
            System.err.println(e);
        else
            throw e;
    }
}
 
Example 16
Source File: SingleChronicleQueueBuilder.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
protected void initializeMetadata() {
    File metapath = metapath();
    validateRollCycle(metapath);
    SCQMeta metadata = new SCQMeta(new SCQRoll(rollCycle(), epoch(), rollTime, rollTimeZone), deltaCheckpointInterval(),
            sourceId());
    try {

        boolean readOnly = readOnly();
        metaStore = SingleTableBuilder.binary(metapath, metadata).readOnly(readOnly).build();
        // check if metadata was overridden
        SCQMeta newMeta = metaStore.metadata();
        if (sourceId() == 0)
            sourceId(newMeta.sourceId());

        String format = newMeta.roll().format();
        if (!format.equals(rollCycle().format())) {
            // roll cycle changed
            overrideRollCycleForFileName(format);
        }

        // if it was overridden - reset
        rollTime = newMeta.roll().rollTime();
        rollTimeZone = newMeta.roll().rollTimeZone();
        epoch = newMeta.roll().epoch();
    } catch (IORuntimeException ex) {
        // readonly=true and file doesn't exist
        if (OS.isWindows())
            throw ex; // we cant have a read-only table store on windows so we have no option but to throw the ex.
        Jvm.warn().on(getClass(), "Failback to readonly tablestore", ex);
        metaStore = new ReadonlyTableStore<>(metadata);
    }
}
 
Example 17
Source File: SingleChronicleQueueBuilder.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
public boolean readOnly() {
    return readOnly == Boolean.TRUE && !OS.isWindows();
}
 
Example 18
Source File: SingleChronicleQueueTest.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
@Test
public void checkReferenceCountingWhenRollingAndCheckFileDeletion() {
    SetTimeProvider timeProvider = new SetTimeProvider();

    @SuppressWarnings("unused")
    MappedFile mappedFile1, mappedFile2, mappedFile3, mappedFile4;

    try (ChronicleQueue queue =
                 binary(getTmpDir())
                         .rollCycle(RollCycles.TEST_SECONDLY)
                         .timeProvider(timeProvider)
                         .build();
         ExcerptAppender appender = queue.acquireAppender()) {

        try (DocumentContext dc = appender.writingDocument()) {
            dc.wire().write().text("some text");
            mappedFile1 = toMappedFile(dc);
        }
        timeProvider.advanceMillis(1100);
        try (DocumentContext dc = appender.writingDocument()) {
            dc.wire().write().text("some more text");
            mappedFile2 = toMappedFile(dc);
        }

        try (ExcerptTailer tailer = queue.createTailer()) {
            try (DocumentContext documentContext = tailer.readingDocument()) {
                mappedFile3 = toMappedFile(documentContext);
                assertEquals("some text", documentContext.wire().read().text());

            }

            try (DocumentContext documentContext = tailer.readingDocument()) {
                mappedFile4 = toMappedFile(documentContext);
                assertEquals("some more text", documentContext.wire().read().text());

            }
        }
    }

    waitFor(mappedFile1::isClosed, "mappedFile1 is not closed");
    waitFor(mappedFile2::isClosed, "mappedFile2 is not closed");

    if (OS.isWindows()) {
        System.err.println("#460 Cannot test delete after close on windows");
        return;
    }
    // this used to fail on windows
    assertTrue(mappedFile1.file().delete());
    assertTrue(mappedFile2.file().delete());
}
 
Example 19
Source File: FileUtil.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
private static void assertOsSupported() {
    if (OS.isWindows()) {
        throw new UnsupportedOperationException("This operation is not supported under Windows.");
    }
}