Esempio n. 1
0
  @Test
  public void shouldClaimRegionForZeroCopyEncoding() {
    final int headerLength = DEFAULT_HEADER.capacity();
    final int msgLength = 20;
    final int frameLength = msgLength + headerLength;
    final int alignedFrameLength = align(frameLength, FRAME_ALIGNMENT);
    final int tail = 0;
    final BufferClaim bufferClaim = new BufferClaim();

    when(metaDataBuffer.getAndAddInt(TERM_TAIL_COUNTER_OFFSET, alignedFrameLength)).thenReturn(0);

    assertThat(termAppender.claim(msgLength, bufferClaim), is(alignedFrameLength));

    assertThat(bufferClaim.offset(), is(tail + headerLength));
    assertThat(bufferClaim.length(), is(msgLength));

    // Map flyweight or encode to buffer directly then call commit() when done
    bufferClaim.commit();

    final InOrder inOrder = inOrder(termBuffer, metaDataBuffer);
    inOrder
        .verify(metaDataBuffer, times(1))
        .getAndAddInt(TERM_TAIL_COUNTER_OFFSET, alignedFrameLength);
    verifyDefaultHeader(inOrder, termBuffer, tail, frameLength);
    inOrder.verify(termBuffer, times(1)).putInt(termOffsetOffset(tail), tail, LITTLE_ENDIAN);
  }
Esempio n. 2
0
  public int claim(
      final long defaultVersionFlagsType,
      final long defaultSessionId,
      final int length,
      final BufferClaim bufferClaim) {
    final int frameLength = length + HEADER_LENGTH;
    final int alignedLength = align(frameLength, FRAME_ALIGNMENT);
    final int termOffset = metaDataBuffer.getAndAddInt(TERM_TAIL_COUNTER_OFFSET, alignedLength);
    final UnsafeBuffer termBuffer = this.termBuffer;
    final int capacity = termBuffer.capacity();

    int resultingOffset = termOffset + alignedLength;
    if (resultingOffset > (capacity - HEADER_LENGTH)) {
      resultingOffset =
          handleEndOfLogCondition(
              termBuffer, termOffset, defaultVersionFlagsType, defaultSessionId, capacity);
    } else {
      applyDefaultHeader(
          termBuffer, termOffset, frameLength, defaultVersionFlagsType, defaultSessionId);
      bufferClaim.wrap(termBuffer, termOffset, frameLength);
    }

    return resultingOffset;
  }