Refine search
/** * @param rollCycle the current rollCycle * @param wireType the wire type that is being used * @param mappedBytes used to mapped the data store file * @param indexCount the number of entries in each index. * @param indexSpacing the spacing between indexed entries. */ public SingleChronicleQueueStore(@NotNull RollCycle rollCycle, @NotNull final WireType wireType, @NotNull MappedBytes mappedBytes, int indexCount, int indexSpacing) { this.mappedBytes = mappedBytes; this.mappedFile = mappedBytes.mappedFile(); this.refCount = ReferenceCounter.onReleased(this::onCleanup); indexCount = Maths.nextPower2(indexCount, 8); indexSpacing = Maths.nextPower2(indexSpacing, 1); this.indexing = new SCQIndexing(wireType, indexCount, indexSpacing); this.indexing.writePosition = this.writePosition = wireType.newTwoLongReference().get(); this.indexing.sequence = this.sequence = new RollCycleEncodeSequence(writePosition, rollCycle.defaultIndexCount(), rollCycle.defaultIndexSpacing()); }
RollCycles(String format, int length, int indexCount, int indexSpacing) { this.format = format; this.length = length; this.indexCount = Maths.nextPower2(indexCount, 8); this.indexSpacing = Maths.nextPower2(indexSpacing, 1); cycleShift = Math.max(32, Maths.intLog2(indexCount) * 2 + Maths.intLog2(indexSpacing)); sequenceMask = (1L << cycleShift) - 1; }
/** * Cache some resources for a rollCycle number. * * @param cycle the rollCycle number to format * @return the Resource */ @NotNull public Resource resourceFor(long cycle) { long millisSinceBeginningOfEpoch = (cycle * length); int hash = Maths.hash32(millisSinceBeginningOfEpoch) & (CACHE_SIZE - 1); Resource dv = values[hash]; if (dv == null || dv.millis != millisSinceBeginningOfEpoch) { final Instant instant = Instant.ofEpochMilli(millisSinceBeginningOfEpoch + epoch); @NotNull String text = formatter.format(instant); values[hash] = dv = new Resource(millisSinceBeginningOfEpoch, text, fileFactory.apply(text)); } return dv; }
@Nullable public String intern(@Nullable CharSequence cs) { if (cs == null) return null; if (cs.length() > interner.length) return cs.toString(); int hash = Maths.hash32(cs); int h = hash & mask; String s = interner[h]; if (StringUtils.isEqual(cs, s)) return s; int h2 = (hash >> shift) & mask; String s2 = interner[h2]; if (StringUtils.isEqual(cs, s2)) return s2; @NotNull String s3 = cs.toString(); interner[s == null || (s2 != null && toggle()) ? h : h2] = s3; return s3; }
@NotNull @Override public ByteBuffer toTemporaryDirectByteBuffer() { ByteBuffer bb = ByteBuffer.allocateDirect(0); try { BB_ADDRESS.setLong(bb, address); BB_CAPACITY.setInt(bb, Maths.toUInt31(readRemaining())); BB_ATT.set(bb, this); } catch (Exception e) { throw new AssertionError(e); } bb.clear(); return bb; }
private static void readUtf8_SB1( @org.jetbrains.annotations.NotNull @NotNull Bytes bytes, @org.jetbrains.annotations.NotNull @NotNull StringBuilder appendable, @org.jetbrains.annotations.NotNull @NotNull StopCharTester tester) throws IOException, BufferUnderflowException { @org.jetbrains.annotations.Nullable NativeBytesStore nb = (NativeBytesStore) bytes.bytesStore(); int i = 0, len = Maths.toInt32(bytes.readRemaining()); long address = nb.address + nb.translate(bytes.readPosition()); @org.jetbrains.annotations.Nullable Memory memory = nb.memory; if (Jvm.isJava9Plus()) { int appendableLength = appendable.capacity(); for (; i < len && i < appendableLength; i++) { if (tester.isStopChar(c)) { bytes.readSkip(i + 1); StringUtils.setCount(appendable, i); return; final char[] chars = StringUtils.extractChars(appendable); for (; i < len && i < chars.length; i++) { int c = memory.readByte(address + i); if (tester.isStopChar(c)) { bytes.readSkip(i + 1); StringUtils.setCount(appendable, i); return;
private int parseCount0(@NotNull String name) { try { TemporalAccessor parse = formatter.parse(name); long epochDay = parse.getLong(ChronoField.EPOCH_DAY) * 86400; if (parse.isSupported(ChronoField.SECOND_OF_DAY)) epochDay += parse.getLong(ChronoField.SECOND_OF_DAY); return Maths.toInt32((epochDay - ((epoch) / 1000)) / (length / 1000)); } catch (DateTimeParseException e) { throw new RuntimeException(String.format( "Unable to parse %s using format %s", name, format), e); } }
@Override public long recoverAndWriteHeader(@NotNull Wire wire, long timeoutMS, final LongValue lastPosition, Sequence sequence) throws UnrecoverableTimeoutException { Bytes<?> bytes = wire.bytes(); long offset = bytes.writePosition(); int num = bytes.readVolatileInt(offset); if (bytes instanceof MappedBytes) { MappedBytes mb = (MappedBytes) bytes; sizeToSkip = Maths.toUInt31(mb.mappedFile().overlapSize() / 2); long pos = bytes.writePosition(); try { bytes.writeSkip(4);
/** * @return the maximum PID. */ public static long getPidMax() throws NumberFormatException { if (isLinux()) { @NotNull File file = new File("/proc/sys/kernel/pid_max"); if (file.canRead()) try { return Maths.nextPower2(new Scanner(file).nextLong(), 1); } catch (FileNotFoundException e) { Jvm.debug().on(OS.class, e); } catch (IllegalArgumentException e) { throw new AssertionError(e); } } else if (isMacOSX()) { return 1L << 24; } // the default. return IS_WIN10 ? 1L << 32 : 1L << 16; }
long sequenceForPosition(@NotNull ExcerptContext ec, final long position, boolean inclusive) throws StreamCorruptedException { long indexOfNext = 0; long lastKnownAddress = 0; @NotNull Wire wire = ec.wireForIndex(); try { final LongArrayValues index2indexArr = getIndex2index(wire); int used2 = Maths.toUInt31(index2indexArr.getUsed()); assert used2 > 0; Outer: int used = Maths.toUInt31(indexValues.getUsed()); assert used >= 0; if (used == 0) if (Jvm.isDebugEnabled(getClass())) Jvm.debug().on(getClass(), "Attempt to find " + Long.toHexString(position), e);
@Override public long applyAsLong(@NotNull BytesStore store) { final int remaining = Maths.toInt32(store.readRemaining()); return applyAsLong(store, remaining); }
/** * Write an unsigned byte at an offset. * * @param offset to write to * @param i the value * @return this * @throws BufferOverflowException if the capacity was exceeded * @throws IllegalArgumentException if the value cannot be cast to the type without loss. */ @NotNull default R writeUnsignedByte(long offset, int i) throws BufferOverflowException, IllegalArgumentException { return writeByte(offset, (byte) Maths.toUInt8(i)); }
@org.jetbrains.annotations.NotNull public static char[] toCharArray(@org.jetbrains.annotations.NotNull Bytes bytes) { @org.jetbrains.annotations.NotNull final char[] chars = new char[Maths.toUInt31(bytes.readRemaining())]; for (int i = 0; i < bytes.readRemaining(); i++) { chars[i] = (char) bytes.readUnsignedByte(i + bytes.readPosition()); } return chars; }
@NotNull default BigDecimal readBigDecimal() { return new BigDecimal(readBigInteger(), Maths.toUInt31(readStopBit())); }
public String intern(@NotNull final Bytes bytes) { return intern(bytes, Maths.toUInt31(bytes.readRemaining())); }
/** * Write a byte at an offset. * * @param offset to write to * @param i the value * @return this * @throws BufferOverflowException if the capacity was exceeded * @throws IllegalArgumentException if the value cannot be cast to the type without loss. */ @NotNull default R writeByte(long offset, int i) throws BufferOverflowException, IllegalArgumentException { return writeByte(offset, Maths.toInt8(i)); }
public static String to8bitString(@org.jetbrains.annotations.NotNull @NotNull BytesStore bytes) throws IllegalArgumentException { long pos = bytes.readPosition(); int len = Maths.toInt32(bytes.readRemaining()); @org.jetbrains.annotations.NotNull char[] chars = new char[len]; if (bytes instanceof VanillaBytes) { ((VanillaBytes) bytes).read8Bit(chars, len); } else { for (int i = 0; i < len; i++) try { chars[i] = (char) bytes.readUnsignedByte(pos + i); } catch (Exception e) { return new String(chars, 0, len) + ' ' + e; } } return StringUtils.newString(chars); }
@Override protected void setValue(Object o, @NotNull BytesIn read) throws IllegalAccessException, IORuntimeException { @NotNull Bytes bytes = (Bytes) field.get(o); long stopBit = read.readStopBit(); if (stopBit == -1) { if (bytes != null) bytes.release(); field.set(o, null); return; } int length = Maths.toUInt31(stopBit); @NotNull Bytes bs; if (bytes == null) { bs = Bytes.elasticHeapByteBuffer(length); } else { bs = bytes; } Object uo = bs.underlyingObject(); if (uo instanceof ByteBuffer && !(bs.bytesStore() instanceof NativeBytesStore)) { read.read(((ByteBuffer) uo).array(), 0, length); } else { bs.clear(); read.read(bs, length); } bs.readLimit(length); field.set(o, bs); } }