@Override public int toCycle(long index) { return delegate.toCycle(index); } }
private boolean indexIsCloseToAndAheadOfLastIndexMove( final long index, final TailerState state, final TailerDirection direction, final ChronicleQueue queue) { return lastMovedToIndex != Long.MIN_VALUE && index - lastMovedToIndex < INDEXING_LINEAR_SCAN_THRESHOLD && state == FOUND_CYCLE && direction == directionAtLastMoveTo && queue.rollCycle().toCycle(index) == queue.rollCycle().toCycle(lastMovedToIndex) && index > lastMovedToIndex; }
private boolean canReuseLastIndexMove( final long index, final TailerState state, final TailerDirection direction, final ChronicleQueue queue, final Wire wire) { return ((wire == null) || wire.bytes().readPosition() == readPositionAtLastMove) && index == this.lastMovedToIndex && index != 0 && state == FOUND_CYCLE && direction == directionAtLastMoveTo && queue.rollCycle().toCycle(index) == queue.rollCycle().toCycle(lastMovedToIndex); }
@NotNull @Override public String toString() { long index = index(); return "StoreTailer{" + "index sequence=" + queue.rollCycle().toSequenceNumber(index) + ", index cycle=" + queue.rollCycle().toCycle(index) + ", store=" + store + ", queue=" + queue + '}'; }
private boolean endOfCycle() { long oldIndex = this.index(); int currentCycle = queue.rollCycle().toCycle(oldIndex); long nextIndex = nextIndexWithNextAvailableCycle(currentCycle); if (nextIndex != Long.MIN_VALUE) { return nextEndOfCycle(nextIndex); } else { state = END_OF_CYCLE; } return false; }
@NotNull private String toTextIndex(ChronicleQueue q, long index) { return Long.toHexString(q.rollCycle().toCycle(index)) + "_" + Long.toHexString(q.rollCycle().toSequenceNumber(index)); }
int cycle = queue.rollCycle().toCycle(index);
private boolean nextEndOfCycle(long nextIndex) { if (moveToIndexInternal(nextIndex)) { state = FOUND_CYCLE; return true; } if (state == END_OF_CYCLE) return true; if (cycle < queue.lastCycle()) { // we have encountered an empty file without an EOF marker // TODO: more work needed - I thought that the appender and/or tailer would write an EOF into this file state = END_OF_CYCLE; return true; } // We are here because we are waiting for an entry to be written to this file. // Winding back to the previous cycle results in a re-initialisation of all the objects => garbage int nextCycle = queue.rollCycle().toCycle(nextIndex); cycle(nextCycle); state = CYCLE_NOT_FOUND; return false; }
int nextIndexCycle = queue.rollCycle().toCycle(nextIndex); if (nextIndex != Long.MIN_VALUE && nextIndexCycle - 1 != cycle) {
ScanResult moveToIndexResult(long index) { final int cycle = queue.rollCycle().toCycle(index); final long sequenceNumber = queue.rollCycle().toSequenceNumber(index); if (LOG.isTraceEnabled()) { Jvm.debug().on(getClass(), "moveToIndex: " + Long.toHexString(cycle) + " " + Long.toHexString(sequenceNumber)); } if (cycle != this.cycle || state != FOUND_CYCLE) { // moves to the expected cycle if (!cycle(cycle)) return ScanResult.NOT_REACHED; } index(index); ScanResult scanResult = this.store().moveToIndexForRead(this, sequenceNumber); Bytes<?> bytes = wire().bytes(); if (scanResult == FOUND) { state = FOUND_CYCLE; moveToState.onSuccessfulLookup(index, direction, bytes.readPosition()); return scanResult; } else if (scanResult == END_OF_FILE) { state = END_OF_CYCLE; return scanResult; } else if (scanResult == NOT_FOUND && this.cycle < this.queue.lastCycle) { state = END_OF_CYCLE; return END_OF_FILE; } return scanResult; }
private void writeBytesInternal(long index, @NotNull BytesStore bytes) { assert writeLock.locked(); try { if (wire == null) { int cycle = queue.rollCycle().toCycle(index); setCycle2(cycle, true); } int safeLength = (int) queue.overlapSize(); openContext(false, safeLength); try { context.wire().bytes().write(bytes); } finally { context.close(false); } } finally { context.isClosed = true; } }
private boolean beyondStartOfCycleBackward() throws StreamCorruptedException { // give the position of the last entry and // flag we want to count it even though we don't know if it will be meta data or not. boolean foundCycle = cycle(queue.rollCycle().toCycle(index())); if (foundCycle) { long lastSequenceNumberInThisCycle = store().sequenceForPosition(this, Long.MAX_VALUE, false); long nextIndex = queue.rollCycle().toIndex(this.cycle, lastSequenceNumberInThisCycle); moveToIndexInternal(nextIndex); state = FOUND_CYCLE; return true; } int cycle = queue.rollCycle().toCycle(index()); long nextIndex = nextIndexWithNextAvailableCycle(cycle); if (nextIndex != Long.MIN_VALUE) { moveToIndexInternal(nextIndex); state = FOUND_CYCLE; return true; } state = BEYOND_START_OF_CYCLE; return false; }
@PackageLocal void incrementIndex() { RollCycle rollCycle = queue.rollCycle(); long index = this.index(); long seq = rollCycle.toSequenceNumber(index); int cycle = rollCycle.toCycle(index); seq += direction.add(); switch (direction) { case NONE: break; case FORWARD: // if it runs out of seq number it will flow over to tomorrows cycle file if (rollCycle.toSequenceNumber(seq) < seq) { cycle(cycle + 1); LOG.warn("we have run out of sequence numbers, so will start to write to " + "the next .cq4 file, the new cycle=" + cycle); seq = 0; } break; case BACKWARD: if (seq < 0) { windBackCycle(cycle); return; } break; } index0(rollCycle.toIndex(cycle, seq)); }
private long approximateLastIndex(int cycle, SingleChronicleQueue queue, SingleChronicleQueueExcerpts.StoreTailer tailer) { try { WireStore wireStore = queue.storeForCycle(cycle, queue.epoch(), false); if (wireStore == null) { return noIndex; } long baseIndex = rollCycle.toIndex(cycle, 0); tailer.moveToIndex(baseIndex); long seq = wireStore.sequenceForPosition(tailer, Long.MAX_VALUE, false); long sequenceNumber = seq + 1; long index = rollCycle.toIndex(cycle, sequenceNumber); int cycleOfIndex = rollCycle.toCycle(index); if (cycleOfIndex != cycle) { throw new IllegalStateException( "Expected cycle " + cycle + " but got " + cycleOfIndex); } return index; } catch (StreamCorruptedException | UnrecoverableTimeoutException e) { throw new IllegalStateException(e); } }
int cycle = q.rollCycle().toCycle(0x18406e100000000L); WireStore wireStore = q.storeForCycle(cycle, q.epoch(), false); String absolutePath = wireStore.file().getAbsolutePath();
@Ignore("long running test") @Test public void testReadAtIndex4MB() { try (final ChronicleQueue queue = SingleChronicleQueueBuilder.builder(getTmpDir(), this.wireType).rollCycle(SMALL_DAILY) .build()) { final ExcerptAppender appender = queue.acquireAppender(); System.out.print("Percent written="); for (long i = 0; i < TIMES; i++) { final long j = i; appender.writeDocument(wire -> wire.write(() -> "key").text("value=" + j)); if (i % (TIMES / 20) == 0) { System.out.println("" + (i * 100 / TIMES) + "%, "); } } long lastIndex = appender.lastIndexAppended(); final int cycle = queue.rollCycle().toCycle(lastIndex); final ExcerptTailer tailer = queue.createTailer(); // QueueDumpMain.dump(file, new PrintWriter(System.out)); StringBuilder sb = new StringBuilder(); for (long i = 0; i < (4L << 20L); i++) { assertTrue(tailer.moveToIndex(queue.rollCycle().toIndex(cycle, i))); tailer.readDocument(wire -> wire.read(() -> "key").text(sb)); Assert.assertEquals("value=" + i, sb.toString()); if (i % (TIMES / 20) == 0) { System.out.println("Percent read= " + (i * 100 / TIMES) + "%"); } } } }
@Test public void testIndex() { try (final ChronicleQueue queue = builder(getTmpDir(), this.wireType) .rollCycle(RollCycles.HOURLY) .build()) { final ExcerptAppender appender = queue.acquireAppender(); int cycle = appender.cycle(); // create 100 documents for (int i = 0; i < 5; i++) { final int j = i; appender.writeDocument(wire -> wire.write(() -> "key").text("value=" + j)); if (i == 2) { final long cycle1 = queue.rollCycle().toCycle(appender.lastIndexAppended()); Assert.assertEquals(cycle1, cycle); } } final ExcerptTailer tailer = queue.createTailer(); assertTrue(tailer.moveToIndex(queue.rollCycle().toIndex(cycle, 2))); StringBuilder sb = new StringBuilder(); tailer.readDocument(wire -> wire.read(() -> "key").text(sb)); Assert.assertEquals("value=2", sb.toString()); tailer.readDocument(wire -> wire.read(() -> "key").text(sb)); Assert.assertEquals("value=3", sb.toString()); tailer.readDocument(wire -> wire.read(() -> "key").text(sb)); Assert.assertEquals("value=4", sb.toString()); } }
final int cycle = queue.rollCycle().toCycle(lastIndex); assertEquals(queue.firstCycle(), cycle); assertEquals(queue.lastCycle(), cycle);
cycle = queue.rollCycle().toCycle(dc.index()); final long index = dc.index(); final long seq = queue.rollCycle().toSequenceNumber(index);