Java Code Examples for net.openhft.chronicle.core.OS#TARGET

The following examples show how to use net.openhft.chronicle.core.OS#TARGET . 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: ExternalizableTest.java    From Chronicle-Map with Apache License 2.0 6 votes vote down vote up
@Test
public void externalizable() throws IOException {
    String path = OS.TARGET + "/test-" + System.nanoTime() + ".map";
    new File(path).deleteOnExit();
    try (ChronicleMap<Long, SomeClass> storage = ChronicleMapBuilder
            .of(Long.class, SomeClass.class)
            .averageValueSize(128)
            .entries(128)
            .createPersistedTo(new File(path))) {
        SomeClass value = new SomeClass();
        value.hits.add("one");
        value.hits.add("two");
        storage.put(1L, value);

        SomeClass value2 = storage.get(1L);
        assertEquals(value.hits, value2.hits);
    }
}
 
Example 2
Source File: TailerDirectionTest.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
@Test
public void uninitialisedTailerCreatedBeforeFirstAppendWithDirectionNoneShouldNotFindDocument() {
    final AtomicLong clock = new AtomicLong(System.currentTimeMillis());
    String path = OS.TARGET + "/" + getClass().getSimpleName() + "-" + System.nanoTime();
    try (final ChronicleQueue queue = SingleChronicleQueueBuilder.single(path).timeProvider(clock::get).testBlockSize()
            .rollCycle(RollCycles.TEST_SECONDLY).build()) {

        final ExcerptTailer tailer = queue.createTailer();
        tailer.direction(TailerDirection.NONE);

        final ExcerptAppender excerptAppender = queue.acquireAppender();
        for (int i = 0; i < 10; i++) {
            excerptAppender.writeDocument(i, (out, value) -> {
                out.int32(value);
            });
        }

        DocumentContext document = tailer.readingDocument();
        assertFalse(document.isPresent());
    }
}
 
Example 3
Source File: ReadmeTest.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
@Test
    public void createAQueue() {
        final String basePath = OS.TARGET + "/" + getClass().getSimpleName() + "-" + System.nanoTime();
        try (ChronicleQueue queue = SingleChronicleQueueBuilder.single(basePath)
                .testBlockSize()
                .rollCycle(RollCycles.TEST_DAILY)
                .build()) {
            // Obtain an ExcerptAppender
            ExcerptAppender appender = queue.acquireAppender();

            // write - {msg: TestMessage}
            appender.writeDocument(w -> w.write(() -> "msg").text("TestMessage"));

//            System.out.println(queue.dump());
            // write - TestMessage
            appender.writeText("TestMessage");

            ExcerptTailer tailer = queue.createTailer();

            tailer.readDocument(w -> System.out.println("msg: " + w.read(() -> "msg").text()));

            assertEquals("TestMessage", tailer.readText());
        }
    }
 
Example 4
Source File: MoveIndexAfterFailedTailerTest.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
@Test
    public void test() {
        String basePath = OS.TARGET + "/" + getClass().getSimpleName() + "-" + System.nanoTime();
        final SingleChronicleQueueBuilder myBuilder = SingleChronicleQueueBuilder.single(basePath)
                .testBlockSize()
                .timeProvider(System::currentTimeMillis)
                .rollCycle(HOURLY);

        int messages = 10;
        try (final ChronicleQueue myWrite = myBuilder.build()) {
            write(myWrite, messages);
//            System.out.println(myWrite.dump());
        }

        try (final ChronicleQueue myRead = myBuilder.build()) {
            read(myRead, messages);
        }
    }
 
Example 5
Source File: OvertakeTest.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
@Before
public void before() {
    path = OS.TARGET + "/" + getClass().getSimpleName() + "-" + System.nanoTime();
    try (ChronicleQueue appender_queue = ChronicleQueue.singleBuilder(path)
            .testBlockSize()
            .writeBufferMode(BufferMode.None)
            .build()) {
        ExcerptAppender appender = appender_queue.acquireAppender();
        for (int i = 0; i < messages; i++) {
            final long l = i;
            appender.writeDocument(wireOut -> wireOut.write("log").marshallable(m -> {
                        m.write("msg").text("hello world ola multi-verse");
                        m.write("ts").int64(l);
                    }
            ));
        }
        a_index = appender.lastIndexAppended();
    }
}
 
Example 6
Source File: Issue125Test.java    From Chronicle-Map with Apache License 2.0 6 votes vote down vote up
@Test
    public void test() throws IOException {
        final File cacheRoot = new File(OS.TARGET + "/test.cm3");
        ChronicleMapBuilder<byte[], byte[]> shaToNodeBuilder =
                ChronicleMapBuilder.of(byte[].class, byte[].class)
//                        .name("bytes-to-bytes")
                        .entries(1000000).
                        averageKeySize(20).
                        averageValueSize(30);

        ChronicleMap<byte[], byte[]> shaToNode =
                shaToNodeBuilder.createPersistedTo(cacheRoot);

        shaToNode.put("1".getBytes(), "2".getBytes());

        shaToNodeBuilder.createPersistedTo(cacheRoot);

    }
 
Example 7
Source File: DirectoryUtils.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
/**
 * Beware, this can give different results depending on whether you are
 * a) running inside maven
 * b) are running in a clean directory (without a "target" dir)
 * See OS.TARGET
 */
@NotNull
public static File tempDir(String name) {
    String replacedName = name.replaceAll("[\\[\\]\\s]+", "_").replace(':', '_');
    final File tmpDir = new File(OS.TARGET, replacedName + "-" + Long.toString(TIMESTAMPER.getAndIncrement(), 36));
    DeleteStatic.INSTANCE.add(tmpDir);

    // Log the temporary directory in OSX as it is quite obscure
    if (OS.isMacOSX()) {
        LOGGER.info("Tmp dir: {}", tmpDir);
    }

    return tmpDir;
}
 
Example 8
Source File: ReadWriteTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
    chroniclePath = new File(OS.TARGET, "read_only_" + System.currentTimeMillis());
    try (ChronicleQueue readWrite = ChronicleQueue.singleBuilder(chroniclePath)
            .readOnly(false)
            .testBlockSize()
            .build()) {
        final ExcerptAppender appender = readWrite.acquireAppender();
        appender.writeText(STR1);
        try (DocumentContext dc = appender.writingDocument()) {
            dc.wire().bytes().writeUtf8(STR2);
        }
    }
}
 
Example 9
Source File: SingleCQFormatTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Test
public void testEmptyDirectory() {
    @NotNull File dir = new File(OS.TARGET, getClass().getSimpleName() + "-" + System.nanoTime());
    dir.mkdir();
    @NotNull RollingChronicleQueue queue = binary(dir).testBlockSize().build();
    assertEquals(Integer.MAX_VALUE, queue.firstCycle());
    assertEquals(Long.MAX_VALUE, queue.firstIndex());
    assertEquals(Integer.MIN_VALUE, queue.lastCycle());
    queue.close();

    IOTools.shallowDeleteDirWithFiles(dir.getAbsolutePath());
}
 
Example 10
Source File: SingleCQFormatTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Test(expected = TimeoutException.class)
public void testNoHeader() throws IOException {
    @NotNull File dir = new File(OS.TARGET + "/deleteme-" + System.nanoTime());
    dir.mkdir();

    File file = new File(dir, "19700101" + SingleChronicleQueue.SUFFIX);
    try (FileOutputStream fos = new FileOutputStream(file)) {
        byte[] bytes = new byte[1024];
        for (int i = 0; i < 128; i++) {
            fos.write(bytes);
        }
    }

    try (@NotNull ChronicleQueue queue = binary(dir)
            .rollCycle(RollCycles.TEST4_DAILY)
            .timeoutMS(500L)
            .testBlockSize()
            .build()) {
        testQueue(queue);
    } finally {
        try {
            IOTools.shallowDeleteDirWithFiles(dir.getAbsolutePath());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 
Example 11
Source File: ToEndTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Test
public void tailerToEndIncreasesRefCount() throws NoSuchFieldException, IllegalAccessException {
    String path = OS.TARGET + "/toEndIncRefCount-" + System.nanoTime();
    IOTools.shallowDeleteDirWithFiles(path);

    SetTimeProvider time = new SetTimeProvider();
    long now = System.currentTimeMillis();
    time.currentTimeMillis(now);

    try (ChronicleQueue queue = SingleChronicleQueueBuilder.binary(path)
            .testBlockSize()
            .rollCycle(RollCycles.TEST_SECONDLY)
            .timeProvider(time)
            .build()) {

        final StoreAppender appender = (StoreAppender) queue.acquireAppender();
        Field storeF1 = StoreAppender.class.getDeclaredField("store");
        Jvm.setAccessible(storeF1);
        SingleChronicleQueueStore store1 = (SingleChronicleQueueStore) storeF1.get(appender);
        System.out.println(store1);

        appender.writeDocument(wire -> wire.write(() -> "msg").int32(1));

        final StoreTailer tailer = (StoreTailer) queue.createTailer();
        System.out.println(tailer);
        tailer.toEnd();
        System.out.println(tailer);

        Field storeF2 = StoreTailer.class.getDeclaredField("store");
        Jvm.setAccessible(storeF2);
        SingleChronicleQueueStore store2 = (SingleChronicleQueueStore) storeF2.get(tailer);

        // the reference count here is 1, the queue itself
        assertFalse(store2.isClosed());
    }
}
 
Example 12
Source File: WriteReadTextTest.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
private void doTest(@NotNull String... problematic) {

        String myPath = OS.TARGET + "/writeReadText-" + System.nanoTime();

        try (ChronicleQueue theQueue = SingleChronicleQueueBuilder
                .single(myPath)
                .blockSize(Maths.nextPower2(EXTREMELY_LARGE.length() * 4, 256 << 10))
                // .testBlockSize() not suitable as large message sizes.
                .build()) {

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

            StringBuilder tmpReadback = new StringBuilder();

            // If the tests don't fail, try increasing the number of iterations
            // Setting it very high may give you a JVM crash
            final int tmpNumberOfIterations = 5;

            for (int l = 0; l < tmpNumberOfIterations; l++) {
                for (int p = 0; p < problematic.length; p++) {
                    appender.writeText(problematic[p]);
                }
                for (int p = 0; p < problematic.length; p++) {
                    tailer.readText(tmpReadback);
                    Assert.assertEquals("write/readText", problematic[p], tmpReadback.toString());
                }
            }

            for (int l = 0; l < tmpNumberOfIterations; l++) {
                for (int p = 0; p < problematic.length; p++) {
                    final String tmpText = problematic[p];
                    appender.writeDocument(writer -> writer.getValueOut().text(tmpText));

                    tailer.readDocument(reader -> reader.getValueIn().textTo(tmpReadback));
                    String actual = tmpReadback.toString();
                    Assert.assertEquals(problematic[p].length(), actual.length());
                    for (int i = 0; i < actual.length(); i += 1024)
                        Assert.assertEquals("i: " + i, problematic[p].substring(i, Math.min(actual.length(), i + 1024)), actual.substring(i, Math.min(actual.length(), i + 1024)));
                    Assert.assertEquals(problematic[p], actual);
                }
            }
        }
    }
 
Example 13
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 14
Source File: SingleCQFormatTest.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
@Test
    public void testCompleteHeader2() throws FileNotFoundException {
        @NotNull File dir = new File(OS.TARGET, getClass().getSimpleName() + "-" + System.nanoTime());
        dir.mkdir();

        @NotNull MappedBytes bytes = MappedBytes.mappedBytes(new File(dir, "19700101-02" + SingleChronicleQueue.SUFFIX), ChronicleQueue.TEST_BLOCK_SIZE * 2);
        @NotNull Wire wire = new BinaryWire(bytes);
        try (final SingleChronicleQueueStore store = new SingleChronicleQueueStore(RollCycles.HOURLY, WireType.BINARY, bytes, 4 << 10, 4)) {
            try (DocumentContext dc = wire.writingDocument(true)) {

                dc.wire().write("header").typedMarshallable(store);
            }
            assertEquals("--- !!meta-data #binary\n" +
                    "header: !SCQStore {\n" +
                    "  writePosition: [\n" +
                    "    0,\n" +
                    "    0\n" +
                    "  ],\n" +
                    "  indexing: !SCQSIndexing {\n" +
                    "    indexCount: !short 4096,\n" +
                    "    indexSpacing: 4,\n" +
                    "    index2Index: 0,\n" +
                    "    lastIndex: 0\n" +
                    "  },\n" +
                    "  dataFormat: 1\n" +
                    "}\n", Wires.fromSizePrefixedBlobs(bytes.readPosition(0)));
        }

@NotNull RollingChronicleQueue queue = binary(dir)
        .testBlockSize()
        .rollCycle(RollCycles.HOURLY)
        .build();
        testQueue(queue);
        assertEquals(2, queue.firstCycle());
        queue.close();
        try {
            IOTools.shallowDeleteDirWithFiles(dir.getAbsolutePath());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
Example 15
Source File: RestartableTailerTest.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
@Test
public void restartable() {
    String tmp = OS.TARGET + "/restartable-" + System.nanoTime();
    try (ChronicleQueue cq = SingleChronicleQueueBuilder.binary(tmp).build()) {
        for (int i = 0; i < 7; i++)
            cq.acquireAppender().writeText("test " + i);
    }

    try (ChronicleQueue cq = SingleChronicleQueueBuilder.binary(tmp).build();
         ExcerptTailer atailer = cq.createTailer("a")) {
        assertEquals("test 0", atailer.readText());
        assertEquals("test 1", atailer.readText());
        assertEquals("test 2", atailer.readText());

        try (ExcerptTailer btailer = cq.createTailer("b")) {
            assertEquals("test 0", btailer.readText());
        }
    }

    try (ChronicleQueue cq = SingleChronicleQueueBuilder.binary(tmp).build();
         ExcerptTailer atailer = cq.createTailer("a")) {
        assertEquals("test 3", atailer.readText());
        assertEquals("test 4", atailer.readText());
        assertEquals("test 5", atailer.readText());

        try (ExcerptTailer btailer = cq.createTailer("b")) {
            assertEquals("test 1", btailer.readText());
        }
    }

    try (ChronicleQueue cq = SingleChronicleQueueBuilder.binary(tmp).build();
         ExcerptTailer atailer = cq.createTailer("a")) {
        assertEquals("test 6", atailer.readText());
        assertNull(atailer.readText());

        try (ExcerptTailer btailer = cq.createTailer("b")) {
            assertEquals("test 2", btailer.readText());
        }
    }

    try (ChronicleQueue cq = SingleChronicleQueueBuilder.binary(tmp).build();
         ExcerptTailer atailer = cq.createTailer("a")) {
        assertNull(atailer.readText());

        try (ExcerptTailer btailer = cq.createTailer("b")) {
            assertEquals("test 3", btailer.readText());
        }
    }
}
 
Example 16
Source File: TailerDirectionTest.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
@Test
public void testTailerForwardBackwardRead() {
    String basePath = OS.TARGET + "/tailerForwardBackward-" + System.nanoTime();

    ChronicleQueue queue = ChronicleQueue.singleBuilder(basePath)
            .testBlockSize()
            .rollCycle(RollCycles.HOURLY)
            .build();
    ExcerptAppender appender = queue.acquireAppender();
    ExcerptTailer tailer = queue.createTailer();

    //
    // Prepare test messages in queue
    //
    // Map of test messages with their queue index position
    HashMap<String, Long> msgIndexes = new HashMap<>();

    for (int i = 0; i < 4; i++) {
        String msg = testMessage(i);
        long idx = appendEntry(appender, msg);
        msgIndexes.put(msg, idx);
    }

    assertEquals("[Forward 1] Wrong message 0", testMessage(0), readNextEntry(tailer));
    assertEquals("[Forward 1] Wrong Tailer index after reading msg 0", msgIndexes.get(testMessage(1)).longValue(), tailer.index());
    assertEquals("[Forward 1] Wrong message 1", testMessage(1), readNextEntry(tailer));
    assertEquals("[Forward 1] Wrong Tailer index after reading msg 1", msgIndexes.get(testMessage(2)).longValue(), tailer.index());

    tailer.direction(TailerDirection.BACKWARD);

    assertEquals("[Backward] Wrong message 2", testMessage(2), readNextEntry(tailer));
    assertEquals("[Backward] Wrong Tailer index after reading msg 2", msgIndexes.get(testMessage(1)).longValue(), tailer.index());
    assertEquals("[Backward] Wrong message 1", testMessage(1), readNextEntry(tailer));
    assertEquals("[Backward] Wrong Tailer index after reading msg 1", msgIndexes.get(testMessage(0)).longValue(), tailer.index());
    assertEquals("[Backward] Wrong message 0", testMessage(0), readNextEntry(tailer));

    String res = readNextEntry(tailer);
    assertNull("Backward: res is" + res, res);

    tailer.direction(TailerDirection.FORWARD);

    res = readNextEntry(tailer);
    assertNull("Forward: res is" + res, res);

    assertEquals("[Forward 2] Wrong message 0", testMessage(0), readNextEntry(tailer));
    assertEquals("[Forward 2] Wrong Tailer index after reading msg 0", msgIndexes.get(testMessage(1)).longValue(), tailer.index());
    assertEquals("[Forward 2] Wrong message 1", testMessage(1), readNextEntry(tailer));
    assertEquals("[Forward 2] Wrong Tailer index after reading msg 1", msgIndexes.get(testMessage(2)).longValue(), tailer.index());

    queue.close();
}
 
Example 17
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();
            }
        }
    }
}