Beispiel #1
0
  private static int compare(
      @NotNull final Buffer left,
      long leftFrom,
      final long leftLength,
      @NotNull final Buffer right,
      long rightFrom,
      final long rightLength) {
    assert leftLength > 0;
    assert rightLength > 0;

    // Adapted from Guava UnsignedBytes

    long length = Math.min(leftLength, rightLength);

    for (;
        length >= Longs.BYTES;
        leftFrom += Longs.BYTES, rightFrom += Longs.BYTES, length -= Longs.BYTES) {
      final long lw = left.getLong(leftFrom);
      final long rw = right.getLong(rightFrom);
      if (lw != rw) {
        return UnsignedLongs.compare(lw, rw);
      }
    }

    if (length >= Ints.BYTES) {
      final int lw = left.getInt(leftFrom);
      final int rw = right.getInt(rightFrom);
      if (lw != rw) {
        return UnsignedInts.compare(lw, rw);
      }
      leftFrom += Ints.BYTES;
      rightFrom += Ints.BYTES;
      length -= Ints.BYTES;
    }

    for (; length > 0; leftFrom++, rightFrom++, length--) {
      final int result = UnsignedBytes.compare(left.get(leftFrom), right.get(rightFrom));

      if (result != 0) {
        return result;
      }
    }

    return (leftLength < rightLength) ? -1 : ((leftLength == rightLength) ? 0 : 1);
  }
  @Test
  public void readingWithSegmentRegistryTest() throws IOException {
    final Buffer byteBuffer = preparePayload();
    final long fullSize = byteBuffer.getLong();
    // full size without 4 bytes for segment_code value
    Assert.assertEquals(fullSize + 4, byteBuffer.remaining());

    final int code = byteBuffer.getInt();
    Payload payloadSegment = (Payload) SegmentRegistry.read(code, byteBuffer);

    for (int i = 0; i < 15; i++) {
      final Buffer currentPayload = payloadSegment.getPayload(i);
      final byte[] expectedPayloadBytes = ("payload" + i).getBytes();

      for (byte expectedByte : expectedPayloadBytes) {
        final byte actualByte = currentPayload.get();
        Assert.assertEquals(actualByte, expectedByte);
      }
      Assert.assertFalse(currentPayload.hasRemaining());
    }
  }
  @Test
  public void hugePayload() throws IOException {
    final byte[] payload = new byte[1024 * 1024];
    for (int i = 0; i < payload.length; i++) payload[i] = (byte) i;

    final Collection<UnsignedByteArray> elements = new LinkedList<>();
    final int docs = 4 * 1024;
    for (int i = 0; i < docs; i++) {
      elements.add(UnsignedByteArrays.from(payload));
    }

    final com.yandex.yoctodb.v1.mutable.segment.V1FullPayloadSegment v1PayloadSegment =
        new V1FullPayloadSegment(elements);

    final OutputStreamWritable outputStreamWritable = v1PayloadSegment.buildWritable();

    final File f = File.createTempFile("huge", "dat");
    f.deleteOnExit();

    try (OutputStream os = new BufferedOutputStream(new FileOutputStream(f))) {
      outputStreamWritable.writeTo(os);
    }

    final Buffer buf = Buffer.from(new RandomAccessFile(f, "r").getChannel());

    final long segmentSize = buf.getLong();
    Assert.assertEquals(segmentSize + 4, buf.remaining());

    final int code = buf.getInt();
    final Payload segment = (Payload) SegmentRegistry.read(code, buf);

    final Buffer expectedPayload = Buffer.from(payload);
    for (int i = 0; i < docs; i += docs >> 2) {
      final Buffer currentPayload = segment.getPayload(i);
      Assert.assertEquals(expectedPayload, currentPayload);
    }
  }