コード例 #1
0
  @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);
  }
コード例 #2
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);
  }
コード例 #3
0
  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());
  }
コード例 #4
0
  @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);
    }
  }
コード例 #5
0
  @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);
  }
コード例 #6
0
 @Test
 public void registerAndReturn() {
   final int ID = -2;
   SegmentRegistry.register(ID, DUMMY);
   assertEquals(PROBE, SegmentRegistry.read(ID, Buffer.from(new byte[] {})));
 }
コード例 #7
0
 @Test(expected = NoSuchElementException.class)
 public void notExisting() {
   final int ID = -1;
   SegmentRegistry.read(ID, Buffer.from(new byte[] {}));
 }