PcpString createPcpString(String text) { if (text == null) { return null; } PcpString string = new PcpString(text); stringInfo.add(string); return string; }
private long getStringOffset(PcpString text) { if (text == null) { return 0; } return text.getOffset(); }
@Test public void shouldSetTheByteBufferToTheCorrectOffsetBeforeWriting() { ByteBuffer byteBuffer = mock(ByteBuffer.class); PcpString pcpString = new PcpString(MY_STRING); pcpString.setOffset(BUFFER_POSITION); pcpString.writeToMmv(byteBuffer); InOrder inOrder = Mockito.inOrder(byteBuffer); inOrder.verify(byteBuffer).position(BUFFER_POSITION); inOrder.verify(byteBuffer).put(anyByte()); }
@Test public void shouldWriteToTheByteBufferWithANullTerminatingString() { ByteBuffer byteBuffer = mock(ByteBuffer.class); PcpString pcpString = new PcpString(MY_STRING); pcpString.writeToMmv(byteBuffer); InOrder inOrder = Mockito.inOrder(byteBuffer); inOrder.verify(byteBuffer).put(MY_STRING.getBytes(CHARSET)); inOrder.verify(byteBuffer).put((byte) 0); }
private long getStringOffset(PcpString text) { if (text == null) { return 0; } return text.getOffset(); }
@Test public void pcpStringStore_createNewPcpString_createsAndStoresAPcpString() { PcpStringStore pcpStringStore = new PcpStringStore(); pcpStringStore.createPcpString(MY_STRING); PcpString expected = new PcpString(MY_STRING); assertThat(pcpStringStore.getStrings(), CoreMatchers.<PcpString>hasItem(reflectivelyEqualing(expected))); }
final protected long getStringOffset(PcpString text) { if (text == null) { return 0; } return text.getOffset(); }
private void preparePerMetricBufferSlices() { for (PcpValueInfo info : metricData.values()) { TypeHandler<?> rawHandler = info.getTypeHandler(); int bufferPosition = rawHandler.requiresLargeStorage() ? info.getLargeValue() .getOffset() : info.getOffset(); // need to position the original buffer first, as the sliced buffer starts from there dataFileBuffer.position(bufferPosition); ByteBuffer metricByteBufferSlice = dataFileBuffer.slice(); metricByteBufferSlice.limit(rawHandler.getDataLength()); perMetricByteBuffers.put(info, metricByteBufferSlice); metricByteBufferSlice.order(dataFileBuffer.order()); } }
@SuppressWarnings({ "unchecked", "rawtypes" }) private void writeValueSection(ByteBuffer dataFileBuffer) { int originalPosition = dataFileBuffer.position(); TypeHandler rawHandler = getTypeHandler(); if (rawHandler.requiresLargeStorage()) { // API requires the length here but it's currently unused -- write out the maximum // possible length dataFileBuffer.putLong(STRING_BLOCK_LIMIT); dataFileBuffer.putLong(getLargeValue().getOffset()); dataFileBuffer.position(getLargeValue().getOffset()); } rawHandler.putBytes(dataFileBuffer, getInitialValue()); dataFileBuffer.position(originalPosition + DATA_VALUE_LENGTH); dataFileBuffer.putLong(getDescriptorOffset()); dataFileBuffer.putLong(getInstanceOffset()); }
private void writeValueWithGlobalLock(PcpValueInfo info, Object value, TypeHandler rawHandler) { synchronized (globalLock) { dataFileBuffer.position(rawHandler.requiresLargeStorage() ? info.getLargeValue() .getOffset() : info.getOffset()); rawHandler.putBytes(dataFileBuffer, value); } }
@Test public void writeToMmvShouldWriteTheCorrectContentToTheByteBuffer() { InstanceDomain instanceDomain = mock(InstanceDomain.class); PcpString pcpString = mock(PcpString.class); ByteBuffer byteBuffer = ByteBuffer.allocate(EXPECTED_BYTE_SIZE); InstanceV2 instanceV2 = new InstanceV2(instanceDomain, INSTANCE_NAME, INSTANCE_DOMAIN_ID, pcpString); when(instanceDomain.getOffset()).thenReturn(INSTANCE_DOMAIN_OFFSET); when(pcpString.getOffset()).thenReturn(STRING_OFFSET); instanceV2.writeToMmv(byteBuffer); /* * As defined in mmv(5), this is the format of the v2 Instance data structure. * * |-------------------------------------| * | 4 bytes | 4 bytes | * |-------------------------------------| * | indom section offset | * |-------------------------------------| * | padding | external inst id | * |-------------------------------------| * | external inst name string offset | * |-------------------------------------| */ byte[] expected = { 0, 0, 0, 0, 0, 0, 0, 40, /* indom section offset */ 0, 0, 0, 0, 0, 0, 0, 123, /* padding, external inst id */ 0, 0, 0, 0, 0, 0, 0, 30, /* external inst name string offset */ }; assertArrayEquals(expected, byteBuffer.array()); }
when(nameAsPcpString.getOffset()).thenReturn(STRING_OFFSET); when(typeHandler.getMetricType()).thenReturn(mmvMetricType); when(mmvMetricType.getIdentifier()).thenReturn(METRIC_TYPE_IDENTIFIER); when(shortHelpPcpString.getOffset()).thenReturn(SHORT_HELP_TEXT_OFFSET); when(longHelpPcpString.getOffset()).thenReturn(LONG_HELP_TEXT_OFFSET); when(semantics.getPcpValue()).thenReturn(SEMANTICS_VALUE);