/** * Allocate an indicator for tracking the status of a channel endpoint. * * @param tempBuffer to be used for labels and metadata. * @param name of the counter for the label. * @param typeId of the counter for classification. * @param countersManager from which to allocated the underlying storage. * @param channel for the stream of messages. * @return a new {@link AtomicCounter} for tracking the status. */ public static AtomicCounter allocate( final MutableDirectBuffer tempBuffer, final String name, final int typeId, final CountersManager countersManager, final String channel) { final int keyLength = tempBuffer.putStringWithoutLengthAscii( CHANNEL_OFFSET + SIZE_OF_INT, channel, 0, MAX_CHANNEL_LENGTH); tempBuffer.putInt(CHANNEL_OFFSET, keyLength); int labelLength = 0; labelLength += tempBuffer.putStringWithoutLengthAscii(keyLength + labelLength, name); labelLength += tempBuffer.putStringWithoutLengthAscii(keyLength + labelLength, ": "); labelLength += tempBuffer.putStringWithoutLengthAscii( keyLength + labelLength, channel, 0, MAX_LABEL_LENGTH - labelLength); return countersManager.newCounter(typeId, tempBuffer, 0, keyLength, tempBuffer, keyLength, labelLength); } }
tempBuffer.putInt(STREAM_ID_OFFSET, streamId); final int channelLength = tempBuffer.putStringWithoutLengthAscii( CHANNEL_OFFSET + SIZE_OF_INT, channel, 0, MAX_CHANNEL_LENGTH); tempBuffer.putInt(CHANNEL_OFFSET, channelLength); labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, name); labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, ": "); labelLength += tempBuffer.putLongAscii(labelOffset + labelLength, registrationId); labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, " "); labelLength += tempBuffer.putIntAscii(labelOffset + labelLength, sessionId); labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, " "); labelLength += tempBuffer.putIntAscii(labelOffset + labelLength, streamId); labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, " "); labelLength += tempBuffer.putStringWithoutLengthAscii( labelOffset + labelLength, channel, 0, MAX_LABEL_LENGTH - labelLength);
public static int allocateCounterId( final MutableDirectBuffer tempBuffer, final String name, final int typeId, final CountersManager countersManager, final long registrationId) { tempBuffer.putLong(REGISTRATION_ID_OFFSET, registrationId); final int keyLength = REGISTRATION_ID_OFFSET + SIZE_OF_LONG; final int labelOffset = BitUtil.align(keyLength, SIZE_OF_INT); int labelLength = 0; labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, name); labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, ": "); labelLength += tempBuffer.putLongAscii(labelOffset + labelLength, registrationId); return countersManager.allocate( typeId, tempBuffer, 0, keyLength, tempBuffer, labelOffset, labelLength); } }
/** * Allocate a counter to represent the heartbeat of a clustered service. * * @param aeron to allocate the counter. * @param tempBuffer to use for building the key and label without allocation. * @param serviceId of the service heartbeat. * @return the {@link Counter} for the commit position. */ public static Counter allocate( final Aeron aeron, final MutableDirectBuffer tempBuffer, final int serviceId) { tempBuffer.putInt(SERVICE_ID_OFFSET, serviceId); final int labelOffset = BitUtil.align(KEY_LENGTH, SIZE_OF_INT); int labelLength = 0; labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, NAME); labelLength += tempBuffer.putIntAscii(labelOffset + labelLength, serviceId); return aeron.addCounter( SERVICE_HEARTBEAT_TYPE_ID, tempBuffer, 0, KEY_LENGTH, tempBuffer, labelOffset, labelLength); }
@Theory public void shouldRoundTripAsciiStringWithoutLength(final MutableDirectBuffer buffer) { final String value = "Hello World"; buffer.putStringWithoutLengthAscii(INDEX, value); assertThat(buffer.getStringWithoutLengthAscii(INDEX, value.length()), is(value)); }
@Theory public void shouldAppendAsciiStringInParts(final MutableDirectBuffer buffer) { final String value = "Hello World Test"; int stringIndex = 0; int bufferIndex = INDEX + SIZE_OF_INT; bufferIndex += buffer.putStringWithoutLengthAscii(bufferIndex, value, stringIndex, 5); stringIndex += 5; bufferIndex += buffer.putStringWithoutLengthAscii(bufferIndex, value, stringIndex, 5); stringIndex += 5; bufferIndex += buffer.putStringWithoutLengthAscii( bufferIndex, value, stringIndex, value.length() - stringIndex); assertThat(bufferIndex, is(value.length() + INDEX + SIZE_OF_INT)); buffer.putInt(INDEX, value.length()); assertThat(buffer.getStringWithoutLengthAscii(INDEX + SIZE_OF_INT, value.length()), is(value)); assertThat(buffer.getStringAscii(INDEX), is(value)); }
@Test public void shouldReturnCorrectLength() { final String data = "a little bit of ascii"; buffer.putStringWithoutLengthAscii(INDEX, data); asciiSequenceView.wrap(buffer, INDEX, data.length()); assertThat(asciiSequenceView.length(), is(data.length())); }
labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, NAME); labelLength += tempBuffer.putLongAscii(keyLength + labelLength, leadershipTermId); labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, " logPosition="); labelLength += tempBuffer.putLongAscii(labelOffset + labelLength, logPosition); labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, " hasReplay=" + hasReplay);
@Test(expected = StringIndexOutOfBoundsException.class) public void shouldThrowExceptionWhenCharAtCalledWithNegativeIndex() { final String data = "foo"; buffer.putStringWithoutLengthAscii(INDEX, data); asciiSequenceView.wrap(buffer, INDEX, data.length()); asciiSequenceView.charAt(-1); } }
@Test(expected = StringIndexOutOfBoundsException.class) public void shouldThrowIndexOutOfBoundsExceptionWhenCharNotPresentAtGivenPosition() { final String data = "foo"; buffer.putStringWithoutLengthAscii(INDEX, data); asciiSequenceView.wrap(buffer, INDEX, data.length()); asciiSequenceView.charAt(4); }
tempBuffer.putInt(STREAM_ID_OFFSET, streamId); final int channelLength = tempBuffer.putStringWithoutLengthAscii( CHANNEL_OFFSET + SIZE_OF_INT, channel, 0, MAX_CHANNEL_LENGTH); tempBuffer.putInt(CHANNEL_OFFSET, channelLength); labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, name); labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, ": "); labelLength += tempBuffer.putLongAscii(labelOffset + labelLength, registrationId); labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, " "); labelLength += tempBuffer.putIntAscii(labelOffset + labelLength, sessionId); labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, " "); labelLength += tempBuffer.putIntAscii(labelOffset + labelLength, streamId); labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, " "); labelLength += tempBuffer.putStringWithoutLengthAscii( labelOffset + labelLength, channel, 0, MAX_LABEL_LENGTH - labelLength); labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, " @"); labelLength += tempBuffer.putLongAscii(labelOffset + labelLength, joinPosition);
@Test public void shouldToString() { final String data = "a little bit of ascii"; buffer.putStringWithoutLengthAscii(INDEX, data); asciiSequenceView.wrap(buffer, INDEX, data.length()); assertThat(asciiSequenceView.toString(), is(data)); }
@Test public void shouldSubSequence() { final String data = "a little bit of ascii"; buffer.putStringWithoutLengthAscii(INDEX, data); asciiSequenceView.wrap(buffer, INDEX, data.length()); final AsciiSequenceView subSequenceView = asciiSequenceView.subSequence(2, 8); assertThat(subSequenceView.toString(), is("little")); }
@Test public void shouldCopyDataUnderTheView() { final String data = "a little bit of ascii"; final int targetBufferOffset = 56; final MutableDirectBuffer targetBuffer = new UnsafeBuffer(new byte[128]); buffer.putStringWithoutLengthAscii(INDEX, data); asciiSequenceView.wrap(buffer, INDEX, data.length()); asciiSequenceView.getBytes(targetBuffer, targetBufferOffset); assertThat(targetBuffer.getStringWithoutLengthAscii(targetBufferOffset, data.length()), is(data)); }
@Test public void shouldBeAbleToGetChars() { final String data = "stringy"; buffer.putStringWithoutLengthAscii(INDEX, data); asciiSequenceView.wrap(buffer, INDEX, data.length()); assertThat(asciiSequenceView.charAt(0), is('s')); assertThat(asciiSequenceView.charAt(1), is('t')); assertThat(asciiSequenceView.charAt(2), is('r')); assertThat(asciiSequenceView.charAt(3), is('i')); assertThat(asciiSequenceView.charAt(4), is('n')); assertThat(asciiSequenceView.charAt(5), is('g')); assertThat(asciiSequenceView.charAt(6), is('y')); }
private void testAlignedWriteMethods(final MutableDirectBuffer buffer, final int offset) { buffer.putLong(offset + SIZE_OF_LONG, Long.MAX_VALUE); buffer.putLong(offset + SIZE_OF_LONG, Long.MAX_VALUE, BIG_ENDIAN); buffer.putDouble(offset + SIZE_OF_DOUBLE, Double.MAX_VALUE); buffer.putDouble(offset + SIZE_OF_DOUBLE, Double.MAX_VALUE, BIG_ENDIAN); buffer.putInt(offset + SIZE_OF_INT, Integer.MAX_VALUE); buffer.putInt(offset + SIZE_OF_INT, Integer.MAX_VALUE, BIG_ENDIAN); buffer.putFloat(offset + SIZE_OF_FLOAT, Float.MAX_VALUE); buffer.putFloat(offset + SIZE_OF_FLOAT, Float.MAX_VALUE, BIG_ENDIAN); buffer.putShort(offset + SIZE_OF_SHORT, Short.MAX_VALUE); buffer.putShort(offset + SIZE_OF_SHORT, Short.MAX_VALUE, BIG_ENDIAN); buffer.putChar(offset + SIZE_OF_CHAR, Character.MAX_VALUE); buffer.putChar(offset + SIZE_OF_CHAR, Character.MAX_VALUE, BIG_ENDIAN); buffer.putByte(offset + SIZE_OF_BYTE, Byte.MAX_VALUE); buffer.putByte(offset + SIZE_OF_BYTE, Byte.MAX_VALUE); buffer.putStringUtf8(offset + SIZE_OF_INT, TEST_STRING); buffer.putStringUtf8(offset + SIZE_OF_INT, TEST_STRING, BIG_ENDIAN); buffer.putStringUtf8(offset + SIZE_OF_INT, TEST_STRING, Integer.MAX_VALUE); buffer.putStringUtf8(offset + SIZE_OF_INT, TEST_STRING, BIG_ENDIAN, Integer.MAX_VALUE); buffer.putStringAscii(offset + SIZE_OF_INT, TEST_STRING); buffer.putStringAscii(offset + SIZE_OF_INT, TEST_STRING, BIG_ENDIAN); // string size is not read for these method => no need for 4-bytes // alignment buffer.putStringWithoutLengthUtf8(offset + SIZE_OF_BYTE, TEST_STRING); buffer.putStringWithoutLengthAscii(offset + SIZE_OF_BYTE, TEST_STRING); }
public ExtendRecordingRequestEncoder channel(final String value) { final int length = null == value ? 0 : value.length(); if (length > 1073741824) { throw new IllegalStateException("length > maxValue for type: " + length); } final int headerLength = 4; final int limit = parentMessage.limit(); parentMessage.limit(limit + headerLength + length); buffer.putInt(limit, (int)length, java.nio.ByteOrder.LITTLE_ENDIAN); buffer.putStringWithoutLengthAscii(limit + headerLength, value); return this; }
public RecordingStartedEncoder channel(final String value) { final int length = null == value ? 0 : value.length(); if (length > 1073741824) { throw new IllegalStateException("length > maxValue for type: " + length); } final int headerLength = 4; final int limit = parentMessage.limit(); parentMessage.limit(limit + headerLength + length); buffer.putInt(limit, (int)length, java.nio.ByteOrder.LITTLE_ENDIAN); buffer.putStringWithoutLengthAscii(limit + headerLength, value); return this; }
public ClusterMembersEncoder clusterMembers(final String value) { final int length = null == value ? 0 : value.length(); if (length > 1073741824) { throw new IllegalStateException("length > maxValue for type: " + length); } final int headerLength = 4; final int limit = parentMessage.limit(); parentMessage.limit(limit + headerLength + length); buffer.putInt(limit, (int)length, java.nio.ByteOrder.LITTLE_ENDIAN); buffer.putStringWithoutLengthAscii(limit + headerLength, value); return this; }
public AddPassiveMemberEncoder memberEndpoints(final String value) { final int length = null == value ? 0 : value.length(); if (length > 1073741824) { throw new IllegalStateException("length > maxValue for type: " + length); } final int headerLength = 4; final int limit = parentMessage.limit(); parentMessage.limit(limit + headerLength + length); buffer.putInt(limit, (int)length, java.nio.ByteOrder.LITTLE_ENDIAN); buffer.putStringWithoutLengthAscii(limit + headerLength, value); return this; }