net.openhft.chronicle.core.OS Java Examples
The following examples show how to use
net.openhft.chronicle.core.OS.
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: DumpQueueMainTest.java From Chronicle-Queue with Apache License 2.0 | 6 votes |
@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 #2
Source File: SingleChronicleQueueTest.java From Chronicle-Queue with Apache License 2.0 | 6 votes |
@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 #3
Source File: ReadmeTest.java From Chronicle-Queue with Apache License 2.0 | 6 votes |
@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 |
@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: ChronicleReaderTest.java From Chronicle-Queue with Apache License 2.0 | 6 votes |
@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 #6
Source File: RollEOFTest.java From Chronicle-Queue with Apache License 2.0 | 6 votes |
private void removeEOF(Path path) throws IOException { long blockSize = 64 << 10; long chunkSize = OS.pageAlign(blockSize); long overlapSize = OS.pageAlign(blockSize / 4); final MappedBytes mappedBytes = MappedBytes.mappedBytes(path.toFile(), chunkSize, overlapSize, false); mappedBytes.reserve(test); try { final Wire wire = WireType.BINARY_LIGHT.apply(mappedBytes); final Bytes<?> bytes = wire.bytes(); bytes.readLimitToCapacity(); bytes.readSkip(4); // move past header try (final SingleChronicleQueueStore qs = loadStore(wire)) { assertNotNull(qs); long l = qs.writePosition(); long len = Wires.lengthOf(bytes.readVolatileInt(l)); long eofOffset = l + len + 4L; bytes.writePosition(eofOffset); bytes.writeInt(0); } } finally { mappedBytes.release(test); } }
Example #7
Source File: ChronicleReaderTest.java From Chronicle-Queue with Apache License 2.0 | 6 votes |
@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 #8
Source File: OvertakeTest.java From Chronicle-Queue with Apache License 2.0 | 6 votes |
@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 #9
Source File: ReadWriteTest.java From Chronicle-Queue with Apache License 2.0 | 6 votes |
@Test public void testNonWriteableFilesSetToReadOnly() { assumeFalse(OS.isWindows()); expectException("Failback to readonly tablestore"); Arrays.stream(chroniclePath.list()).forEach(s -> assertTrue(new File(chroniclePath, s).setWritable(false))); try (ChronicleQueue out = SingleChronicleQueueBuilder .binary(chroniclePath) .testBlockSize() .readOnly(false) .build()) { ExcerptTailer tailer = out.createTailer(); tailer.toEnd(); long index = tailer.index(); assertTrue(index != 0); } }
Example #10
Source File: ReadWriteTest.java From Chronicle-Queue with Apache License 2.0 | 6 votes |
@Test public void testReadFromReadOnlyChronicle() { try (ChronicleQueue out = SingleChronicleQueueBuilder .binary(chroniclePath) .testBlockSize() .readOnly(!OS.isWindows()) .build()) { // check dump assertTrue(out.dump().length() > 1); // and tailer ExcerptTailer tailer = out.createTailer(); assertEquals(STR1, tailer.readText()); try (DocumentContext dc = tailer.readingDocument()) { assertEquals(STR2, dc.wire().bytes().readUtf8()); // even though this is read-only we can still call dc.wire().bytes().write... which causes java.lang.InternalError // Fixing this in a type-safe manner would require on Read/WriteDocumentContext to return WireIn/WireOut } } }
Example #11
Source File: TailerDirectionTest.java From Chronicle-Queue with Apache License 2.0 | 6 votes |
@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 #12
Source File: VanillaChronicleHash.java From Chronicle-Map with Apache License 2.0 | 6 votes |
private void mapTierBulksMapped(int upToBulkIndex) throws IOException { int firstBulkToMapIndex = tierBulkOffsets.size(); int bulksToMap = upToBulkIndex + 1 - firstBulkToMapIndex; long mapSize = bulksToMap * tierBulkSizeInBytes; long mappingOffsetInFile, firstBulkToMapOffsetWithinMapping; long firstBulkToMapOffset = bulkOffset(firstBulkToMapIndex); if (OS.mapAlign(firstBulkToMapOffset) == firstBulkToMapOffset) { mappingOffsetInFile = firstBulkToMapOffset; firstBulkToMapOffsetWithinMapping = 0; } else { // If the bulk was allocated on OS with 4K mapping granularity (linux) and we // are mapping it in OS with 64K mapping granularity (windows), we might need to // start the mapping earlier than the first tier to map actually starts mappingOffsetInFile = OS.mapAlign(firstBulkToMapOffset) - OS.mapAlignment(); firstBulkToMapOffsetWithinMapping = firstBulkToMapOffset - mappingOffsetInFile; // Now might need to have bigger mapSize mapSize += firstBulkToMapOffsetWithinMapping; } // mapping by hand, because MappedFile/MappedBytesStore doesn't allow to create a BS // which starts not from the beginning of the file, but has start() of 0 NativeBytesStore extraStore = map(mapSize, mappingOffsetInFile); appendBulkData(firstBulkToMapIndex, upToBulkIndex, extraStore, firstBulkToMapOffsetWithinMapping); }
Example #13
Source File: Issue125Test.java From Chronicle-Map with Apache License 2.0 | 6 votes |
@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 #14
Source File: ExternalizableTest.java From Chronicle-Map with Apache License 2.0 | 6 votes |
@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 #15
Source File: ForEachSegmentTest.java From Chronicle-Map with Apache License 2.0 | 6 votes |
@Test public void forEachSegmentEntryWhileReleasesLock() throws IOException { ChronicleMapBuilder<Integer, MyDto> builder = ChronicleMapBuilder.simpleMapOf(Integer.class, MyDto.class) .entries(256) .actualSegments(1); File tmp = new File(OS.TMP, "stressTest-" + System.nanoTime()); tmp.deleteOnExit(); try (ChronicleMap<Integer, MyDto> map = builder.createOrRecoverPersistedTo(tmp)) { map.put(1, new MyDto()); try (MapSegmentContext<Integer, MyDto, ?> context = map.segmentContext(0)) { context.forEachSegmentEntryWhile(e -> { System.out.println(e.key().get() + " = " + e.value().get()); return true; }); } System.out.println("Done"); } }
Example #16
Source File: ChronicleReaderTest.java From Chronicle-Queue with Apache License 2.0 | 5 votes |
@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 #17
Source File: DirectoryUtils.java From Chronicle-Queue with Apache License 2.0 | 5 votes |
/** * 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 #18
Source File: ChronicleReaderTest.java From Chronicle-Queue with Apache License 2.0 | 5 votes |
@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 #19
Source File: ChronicleMethodReaderTest.java From Chronicle-Queue with Apache License 2.0 | 5 votes |
@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 #20
Source File: ReadWriteTest.java From Chronicle-Queue with Apache License 2.0 | 5 votes |
@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 #21
Source File: AppenderFileHandleLeakTest.java From Chronicle-Queue with Apache License 2.0 | 5 votes |
public void tailerResourcesCanBeReleasedManually0() throws IOException, InterruptedException, TimeoutException, ExecutionException { assumeTrue(OS.isLinux()); GcControls.requestGcCycle(); Thread.sleep(100); try (ChronicleQueue queue = createQueue(SYSTEM_TIME_PROVIDER)) { final long openFileHandleCount = countFileHandlesOfCurrentProcess(); final List<Path> fileHandlesAtStart = new ArrayList<>(lastFileHandles); final List<Future<Boolean>> futures = new LinkedList<>(); final List<ExcerptTailer> gcGuard = new LinkedList<>(); for (int i = 0; i < THREAD_COUNT; i++) { futures.add(threadPool.submit(() -> { for (int j = 0; j < MESSAGES_PER_THREAD; j++) { writeMessage(j, queue); readMessage(queue, true, gcGuard::add); } return Boolean.TRUE; })); } for (Future<Boolean> future : futures) { assertTrue(future.get(1, TimeUnit.MINUTES)); } waitForFileHandleCountToDrop(openFileHandleCount, fileHandlesAtStart); assertFalse(gcGuard.isEmpty()); } }
Example #22
Source File: SingleChronicleQueueBuilder.java From Chronicle-Queue with Apache License 2.0 | 5 votes |
/** * @return - this is the size of a memory mapping chunk, a queue is read/written by using a number of blocks, you should avoid changing this unnecessarily. */ public long blockSize() { long bs = blockSize == null ? OS.is64Bit() ? 64L << 20 : TEST_BLOCK_SIZE : blockSize; // can add an index2index & an index in one go. long minSize = Math.max(TEST_BLOCK_SIZE, 32L * indexCount()); return Math.max(minSize, bs); }
Example #23
Source File: TailerIndexingQueueTest.java From Chronicle-Queue with Apache License 2.0 | 5 votes |
@Test public void tailerShouldBeAbleToMoveBackwardFromEndOfCycle() throws IOException { assumeFalse(OS.isWindows()); try (final ChronicleQueue queue = createQueue(path, clock::get)) { final ExcerptAppender appender = queue.acquireAppender(); // generate some cycle files range(0, 5).forEach(i -> { try (final DocumentContext ctx = appender.writingDocument()) { ctx.wire().write().int32(i); clock.addAndGet(TimeUnit.SECONDS.toMillis(10L)); } }); } // remove all but the first file final Path firstFile = Files.list(this.path.toPath()) .sorted(Comparator.comparing(Path::toString)) .findFirst() .orElseThrow(AssertionError::new); Files.list(this.path.toPath()) .filter(p -> !p.equals(firstFile)) .forEach(TailerIndexingQueueTest::deleteFile); try (final ChronicleQueue queue = createQueue(path, SystemTimeProvider.INSTANCE)) { final ExcerptTailer tailer = queue.createTailer().toEnd(); // move to END_OF_CYCLE try (final DocumentContext readCtx = tailer.readingDocument()) { assertFalse(readCtx.isPresent()); } assertEquals(TailerState.END_OF_CYCLE, tailer.state()); tailer.direction(TailerDirection.BACKWARD); tailer.toEnd(); assertTrue(tailer.readingDocument().isPresent()); } }
Example #24
Source File: SingleChronicleQueueBuilder.java From Chronicle-Queue with Apache License 2.0 | 5 votes |
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 #25
Source File: DumpQueueMain.java From Chronicle-Queue with Apache License 2.0 | 5 votes |
private static void dumpFile(@NotNull File file, @NotNull PrintStream out, long upperLimit) { Bytes<ByteBuffer> buffer = Bytes.elasticByteBuffer(); try (MappedBytes bytes = MappedBytes.mappedBytes(file, 4 << 20, OS.pageSize(), !OS.isWindows())) { bytes.readLimit(bytes.realCapacity()); StringBuilder sb = new StringBuilder(); WireDumper dumper = WireDumper.of(bytes, !UNALIGNED); while (bytes.readRemaining() >= 4) { sb.setLength(0); boolean last = dumper.dumpOne(sb, buffer); if (sb.indexOf("\nindex2index:") != -1 || sb.indexOf("\nindex:") != -1) { // truncate trailing zeros if (sb.indexOf(", 0\n]\n") == sb.length() - 6) { int i = indexOfLastZero(sb); if (i < sb.length()) sb.setLength(i - 5); sb.append(" # truncated trailing zeros\n]"); } } out.println(sb); if (last) break; if (bytes.readPosition() > upperLimit) { out.println("# limit reached."); return; } } } catch (IOException ioe) { err.println("Failed to read " + file + " " + ioe); } finally { buffer.releaseLast(); } }
Example #26
Source File: BenchmarkMain.java From Chronicle-Queue with Apache License 2.0 | 5 votes |
private static long readMessage(Bytes<?> bytes) { Jvm.safepoint(); long start = bytes.readLong(); long rp = bytes.readPosition(); long rl = bytes.readLimit(); long addr = bytes.addressForRead(rp); long addrEnd = bytes.addressForRead(rl); Memory memory = OS.memory(); for (addr += 8; addr + 7 < addrEnd; addr += 8) memory.readLong(addr); Jvm.safepoint(); return start; }
Example #27
Source File: ArrayTest.java From Chronicle-Map with Apache License 2.0 | 5 votes |
@Test public void test0() throws IOException, JSONException{ ClassAliasPool.CLASS_ALIASES.addAlias(MovingAverageArray.class); File file = new File(OS.getTarget() + "/pf-PosistionsAndClose-" + System.nanoTime()); ChronicleMap<Long, MovingAverageArray> mapWrite = ChronicleMap .of(Long.class, MovingAverageArray.class) .entries(100) .averageValue(createSampleWithSize(6, 0)) .createPersistedTo(file); mapWrite.put(1L, createSampleWithSize(6, 1)); mapWrite.put(2L, createSampleWithSize(4, 2)); mapWrite.close(); ChronicleMap<Long, MovingAverageArray> mapRead = ChronicleMapBuilder .of(Long.class, MovingAverageArray.class) .createPersistedTo(file); MovingAverageArray m = mapRead.get(1L); assertJSONEqualsAfterPrefix("!MovingAverageArray {\n" + " values: [\n" + " { movingAverage: 0.1, high: 0.1, low: 0.1, stdDev: 0.1 },\n" + " { movingAverage: 1.1, high: 1.1, low: 1.1, stdDev: 1.1 },\n" + " { movingAverage: 2.1, high: 2.1, low: 2.1, stdDev: 2.1 },\n" + " { movingAverage: 3.1, high: 3.1, low: 3.1, stdDev: 3.1 },\n" + " { movingAverage: 4.1, high: 4.1, low: 4.1, stdDev: 4.1 },\n" + " { movingAverage: 5.1, high: 5.1, low: 5.1, stdDev: 5.1 }\n" + " ]\n" + "}\n", m.toString()); MovingAverageArray m2 = mapRead.getUsing(2L, m); assertSame(m, m2); // object is recycled, so no objects are created. assertJSONEqualsAfterPrefix("!MovingAverageArray {\n" + " values: [\n" + " { movingAverage: 0.2, high: 0.2, low: 0.2, stdDev: 0.2 },\n" + " { movingAverage: 1.2, high: 1.2, low: 1.2, stdDev: 1.2 },\n" + " { movingAverage: 2.2, high: 2.2, low: 2.2, stdDev: 2.2 },\n" + " { movingAverage: 3.2, high: 3.2, low: 3.2, stdDev: 3.2 }\n" + " ]\n" + "}\n", m.toString()); }
Example #28
Source File: SingleCQFormatTest.java From Chronicle-Queue with Apache License 2.0 | 5 votes |
@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 #29
Source File: SingleCQFormatTest.java From Chronicle-Queue with Apache License 2.0 | 5 votes |
@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 #30
Source File: SingleChronicleQueueTest.java From Chronicle-Queue with Apache License 2.0 | 5 votes |
@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()); }