Ejemplo n.º 1
0
 public static int compare(
     @NotNull final Buffer left,
     final long leftFrom,
     final long leftLength,
     @NotNull final Buffer right) {
   return compare(left, leftFrom, leftLength, right, right.position(), right.remaining());
 }
  @Test(expected = UnsupportedOperationException.class)
  public void unsupported() throws IOException {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();

    baos.write(Ints.toByteArray(Integer.MAX_VALUE));

    final Buffer buf = Buffer.from(baos.toByteArray());

    IndexToIndexMultiMapReader.from(buf);
  }
Ejemplo n.º 3
0
  @Test
  public void test() throws IOException {
    // Get current database format
    final DatabaseFormat format = DatabaseFormat.getCurrent();

    // Create a mutable database
    final DatabaseBuilder dbBuilder = format.newDatabaseBuilder();

    // Add some document
    dbBuilder.merge(
        format
            .newDocumentBuilder()
            .withField("id", 1, FILTERABLE)
            .withField("score", 0, SORTABLE)
            .withPayload("payload1".getBytes()));

    // Add another document
    dbBuilder.merge(
        format
            .newDocumentBuilder()
            .withField("id", 2, FILTERABLE)
            .withField("score", 1, SORTABLE)
            .withPayload("payload2".getBytes()));

    // Build and serialize immutable database
    final ByteArrayOutputStream os = new ByteArrayOutputStream();
    dbBuilder.buildWritable().writeTo(os);

    // Open the immutable database
    final Database db = format.getDatabaseReader().from(Buffer.from(os.toByteArray()));

    // Filter the second document
    final Query doc2 = select().where(eq("id", from(2)));
    assertTrue(db.count(doc2) == 1);

    // Filter and sort

    final Query sorted =
        select().where(and(gte("id", from(1)), lte("id", from(2)))).orderBy(desc("score"));

    final List<Integer> ids = new LinkedList<>();
    db.execute(
        sorted,
        new DocumentProcessor() {
          @Override
          public boolean process(final int document, @NotNull final Database database) {
            ids.add(document);
            return true;
          }
        });

    assertEquals(asList(1, 0), ids);
  }
  private Buffer preparePayload() throws IOException {
    final Collection<UnsignedByteArray> elements = new LinkedList<>();
    for (int i = 0; i < 15; i++) {
      final byte[] payload = ("payload" + i).getBytes();
      elements.add(UnsignedByteArrays.from(payload));
    }

    final V1FullPayloadSegment v1PayloadSegment = new V1FullPayloadSegment(elements);

    final ByteArrayOutputStream os = new ByteArrayOutputStream();
    final OutputStreamWritable outputStreamWritable = v1PayloadSegment.buildWritable();
    outputStreamWritable.writeTo(os);
    return Buffer.from(os.toByteArray());
  }
  @Test
  public void buildInt() throws IOException {
    final TreeMultimap<Integer, Integer> elements = TreeMultimap.create();
    for (int i = 0; i < DOCS; i++) {
      elements.put(i / 2, i);
    }
    final com.yandex.yoctodb.util.mutable.IndexToIndexMultiMap mutable =
        new com.yandex.yoctodb.util.mutable.impl.IntIndexToIndexMultiMap(elements.asMap().values());

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    mutable.writeTo(baos);

    final Buffer buf = Buffer.from(baos.toByteArray());

    final IndexToIndexMultiMap result = IndexToIndexMultiMapReader.from(buf);

    assertTrue(result instanceof IntIndexToIndexMultiMap);
  }
  @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());
    }
  }
Ejemplo n.º 7
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 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);
    }
  }
Ejemplo n.º 9
0
 @Test
 public void registerAndReturn() {
   final int ID = -2;
   SegmentRegistry.register(ID, DUMMY);
   assertEquals(PROBE, SegmentRegistry.read(ID, Buffer.from(new byte[] {})));
 }
Ejemplo n.º 10
0
 @Test(expected = NoSuchElementException.class)
 public void notExisting() {
   final int ID = -1;
   SegmentRegistry.read(ID, Buffer.from(new byte[] {}));
 }
Ejemplo n.º 11
0
 @NotNull
 public static UnsignedByteArray from(@NotNull final Buffer buffer) {
   return from(buffer.toByteArray());
 }
Ejemplo n.º 12
0
 public static int compare(@NotNull final Buffer left, @NotNull final Buffer right) {
   return compare(
       left, left.position(), left.remaining(), right, right.position(), right.remaining());
 }