Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.facebook.presto.block;
 
 
 import java.util.List;
 
 import static com.facebook.presto.block.PagesSerde.readPages;
 import static com.facebook.presto.block.PagesSerde.writePages;
 import static com.facebook.presto.operator.PageAssertions.assertPageEquals;
 import static com.facebook.presto.spi.type.BigintType.BIGINT;
 import static com.facebook.presto.spi.type.VarcharType.VARCHAR;
 import static com.facebook.presto.testing.TestingBlockEncodingManager.createTestingBlockEncodingManager;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertFalse;
 
 public class TestPagesSerde
 {
     @Test
     public void testRoundTrip()
     {
         BlockBuilder expectedBlockBuilder = .createBlockBuilder(new BlockBuilderStatus());
         .writeString(expectedBlockBuilder"alice");
         .writeString(expectedBlockBuilder"bob");
         .writeString(expectedBlockBuilder"charlie");
         .writeString(expectedBlockBuilder"dave");
         Block expectedBlock = expectedBlockBuilder.build();
 
         Page expectedPage = new Page(expectedBlockexpectedBlockexpectedBlock);
 
         DynamicSliceOutput sliceOutput = new DynamicSliceOutput(1024);
         writePages(createTestingBlockEncodingManager(), sliceOutputexpectedPageexpectedPageexpectedPage);
 
         List<Typetypes = ImmutableList.<Type>of();
         Iterator<PagepageIterator = readPages(createTestingBlockEncodingManager(), sliceOutput.slice().getInput());
         assertPageEquals(typespageIterator.next(), expectedPage);
         assertPageEquals(typespageIterator.next(), expectedPage);
         assertPageEquals(typespageIterator.next(), expectedPage);
         assertFalse(pageIterator.hasNext());
     }
 
     @Test
     public void testBigintSerializedSize()
     {
         BlockBuilder builder = .createBlockBuilder(new BlockBuilderStatus());
 
         // empty page
         Page page = new Page(builder.build());
         int pageSize = serializedSize(ImmutableList.of(), page);
         assertEquals(pageSize, 35); // page overhead
 
         // page with one value
         .writeLong(builder, 123);
         page = new Page(builder.build());
         int firstValueSize = serializedSize(ImmutableList.of(), page) - pageSize;
         assertEquals(firstValueSize, 9); // value size + value overhead
 
         // page with two values
         .writeLong(builder, 456);
         page = new Page(builder.build());
         int secondValueSize = serializedSize(ImmutableList.of(), page) - (pageSize + firstValueSize);
         assertEquals(secondValueSize, 8); // value size (value overhead is shared with previous value)
     }
 
     @Test
     public void testVarcharSerializedSize()
     {
         BlockBuilder builder = .createBlockBuilder(new BlockBuilderStatus());
 
         // empty page
         Page page = new Page(builder.build());
         int pageSize = serializedSize(ImmutableList.of(), page);
         assertEquals(pageSize, 34); // page overhead
 
         // page with one value
         .writeString(builder"alice");
         page = new Page(builder.build());
         int firstValueSize = serializedSize(ImmutableList.of(), page) - pageSize;
        assertEquals(firstValueSize, 4 + 5 + 1); // length + "alice" + null
        // page with two values
        .writeString(builder"bob");
        page = new Page(builder.build());
        int secondValueSize = serializedSize(ImmutableList.of(), page) - (pageSize + firstValueSize);
        assertEquals(secondValueSize, 4  + 3); // length + "bob" (null shared with first entry)
    }
    private static int serializedSize(List<? extends TypetypesPage expectedPage)
    {
        DynamicSliceOutput sliceOutput = new DynamicSliceOutput(1024);
        writePages(createTestingBlockEncodingManager(), sliceOutputexpectedPage);
        Slice slice = sliceOutput.slice();
        Iterator<PagepageIterator = readPages(createTestingBlockEncodingManager(), slice.getInput());
        if (pageIterator.hasNext()) {
            assertPageEquals(typespageIterator.next(), expectedPage);
        }
        else {
            assertEquals(expectedPage.getPositionCount(), 0);
        }
        assertFalse(pageIterator.hasNext());
        return slice.length();
    }
New to GrepCode? Check out our FAQ X