Example #1
0
  @Test
  public void testStackRepresentation() throws Exception {
    Block array = arrayBlockOf(BIGINT, 1L, 2L);
    Block actualBlock = mapBlockOf(DOUBLE, new ArrayType(BIGINT), ImmutableMap.of(1.0, array));
    DynamicSliceOutput actualSliceOutput = new DynamicSliceOutput(100);
    writeBlock(actualSliceOutput, actualBlock);

    Block expectedBlock =
        new InterleavedBlockBuilder(
                ImmutableList.<Type>of(DOUBLE, new ArrayType(BIGINT)), new BlockBuilderStatus(), 3)
            .writeDouble(1.0)
            .closeEntry()
            .writeObject(
                BIGINT
                    .createBlockBuilder(new BlockBuilderStatus(), 1)
                    .writeLong(1L)
                    .closeEntry()
                    .writeLong(2L)
                    .closeEntry()
                    .build())
            .closeEntry()
            .build();
    DynamicSliceOutput expectedSliceOutput = new DynamicSliceOutput(100);
    writeBlock(expectedSliceOutput, expectedBlock);

    assertEquals(actualSliceOutput.slice(), expectedSliceOutput.slice());
  }
  @Test
  public void testRoundTrip() {
    int positionCount = 40;

    // build dictionary
    BlockBuilder dictionaryBuilder = VARCHAR.createBlockBuilder(new BlockBuilderStatus(), 4);
    VARCHAR.writeString(dictionaryBuilder, "alice");
    VARCHAR.writeString(dictionaryBuilder, "bob");
    VARCHAR.writeString(dictionaryBuilder, "charlie");
    VARCHAR.writeString(dictionaryBuilder, "dave");
    Block dictionary = dictionaryBuilder.build();

    // build ids
    int[] ids = new int[positionCount];
    for (int i = 0; i < 40; i++) {
      ids[i] = i % 4;
    }

    BlockEncoding blockEncoding = new DictionaryBlockEncoding(new VariableWidthBlockEncoding());
    DictionaryBlock dictionaryBlock = new DictionaryBlock(positionCount, dictionary, ids);

    DynamicSliceOutput sliceOutput = new DynamicSliceOutput(1024);
    blockEncoding.writeBlock(sliceOutput, dictionaryBlock);
    Block actualBlock = blockEncoding.readBlock(sliceOutput.slice().getInput());

    assertTrue(actualBlock instanceof DictionaryBlock);
    DictionaryBlock actualDictionaryBlock = (DictionaryBlock) actualBlock;
    assertBlockEquals(VARCHAR, actualDictionaryBlock.getDictionary(), dictionary);
    for (int position = 0; position < actualDictionaryBlock.getPositionCount(); position++) {
      assertEquals(actualDictionaryBlock.getId(position), ids[position]);
    }
    assertEquals(
        actualDictionaryBlock.getDictionarySourceId(), dictionaryBlock.getDictionarySourceId());
  }
    @Override
    public Response apply(Request request) {
      ImmutableList<String> parts =
          ImmutableList.copyOf(
              Splitter.on("/").omitEmptyStrings().split(request.getUri().getPath()));
      if (request.getMethod().equals("DELETE")) {
        assertEquals(parts.size(), 1);
        return new TestingResponse(HttpStatus.OK, ImmutableListMultimap.of(), new byte[0]);
      }

      assertEquals(parts.size(), 2);
      String taskId = parts.get(0);
      int pageToken = Integer.parseInt(parts.get(1));

      Builder<String, String> headers = ImmutableListMultimap.builder();
      headers.put(PRESTO_PAGE_TOKEN, String.valueOf(pageToken));

      TaskBuffer taskBuffer = taskBuffers.getUnchecked(taskId);
      Page page = taskBuffer.getPage(pageToken);
      headers.put(CONTENT_TYPE, PRESTO_PAGES);
      if (page != null) {
        headers.put(PRESTO_PAGE_NEXT_TOKEN, String.valueOf(pageToken + 1));
        headers.put(PRESTO_BUFFER_COMPLETE, String.valueOf(false));
        DynamicSliceOutput output = new DynamicSliceOutput(256);
        PagesSerde.writePages(blockEncodingSerde, output, page);
        return new TestingResponse(HttpStatus.OK, headers.build(), output.slice().getInput());
      } else if (taskBuffer.isFinished()) {
        headers.put(PRESTO_PAGE_NEXT_TOKEN, String.valueOf(pageToken));
        headers.put(PRESTO_BUFFER_COMPLETE, String.valueOf(true));
        return new TestingResponse(HttpStatus.OK, headers.build(), new byte[0]);
      } else {
        headers.put(PRESTO_PAGE_NEXT_TOKEN, String.valueOf(pageToken));
        headers.put(PRESTO_BUFFER_COMPLETE, String.valueOf(false));
        return new TestingResponse(HttpStatus.NO_CONTENT, headers.build(), new byte[0]);
      }
    }