Search in sources :

Example 6 with InMemorySymbolTable

use of com.linkedin.data.codec.symbol.InMemorySymbolTable in project rest.li by linkedin.

the class TestRuntimeSymbolTableGenerator method testSymbolTableGenerator.

@Test
public void testSymbolTableGenerator() {
    DataSchema schema = DataTemplateUtil.getSchema(SimpleGreeting.class);
    SymbolTableNameHandler handler = new SymbolTableNameHandler("Haha", "https://localhost:1000/service");
    InMemorySymbolTable symbolTable = RuntimeSymbolTableGenerator.generate(handler, Collections.singleton(schema));
    Assert.assertEquals(37, symbolTable.size());
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) InMemorySymbolTable(com.linkedin.data.codec.symbol.InMemorySymbolTable) Test(org.testng.annotations.Test)

Example 7 with InMemorySymbolTable

use of com.linkedin.data.codec.symbol.InMemorySymbolTable in project rest.li by linkedin.

the class TestCodec method perfTest.

private void perfTest(int count, DataMap map) throws IOException {
    List<DataCodec> codecs = new ArrayList<>();
    codecs.add(new JacksonDataCodec());
    codecs.add(new BsonDataCodec());
    codecs.add(new JacksonSmileDataCodec());
    Set<String> symbols = new HashSet<>();
    collectSymbols(symbols, map);
    final String sharedSymbolTableName = "SHARED";
    SymbolTable symbolTable = new InMemorySymbolTable(sharedSymbolTableName, new ArrayList<>(symbols));
    SymbolTableProvider provider = new SymbolTableProvider() {

        @Override
        public SymbolTable getSymbolTable(String symbolTableName) {
            if (sharedSymbolTableName.equals(symbolTableName)) {
                return symbolTable;
            }
            return null;
        }
    };
    SymbolTableProviderHolder.INSTANCE.setSymbolTableProvider(provider);
    codecs.add(new JacksonLICORBinaryDataCodec(symbolTable));
    codecs.add(new JacksonLICORTextDataCodec(symbolTable));
    codecs.add(new ProtobufDataCodec(new ProtobufCodecOptions.Builder().setSymbolTable(symbolTable).build()));
    for (DataCodec codec : codecs) {
        byte[] bytes = codec.mapToBytes(map);
        out.println(codec.getClass().getName() + " serialized size " + bytes.length);
    }
    for (DataCodec codec : codecs) {
        dataMapToBytesPerfTest(count, codec, map);
    }
    for (DataCodec codec : codecs) {
        byte[] bytes = codec.mapToBytes(map);
        bytesToDataMapPerfTest(count, codec, bytes);
    }
}
Also used : SymbolTableProvider(com.linkedin.data.codec.symbol.SymbolTableProvider) ArrayList(java.util.ArrayList) InMemorySymbolTable(com.linkedin.data.codec.symbol.InMemorySymbolTable) SymbolTable(com.linkedin.data.codec.symbol.SymbolTable) ByteString(com.linkedin.data.ByteString) HashSet(java.util.HashSet) InMemorySymbolTable(com.linkedin.data.codec.symbol.InMemorySymbolTable)

Example 8 with InMemorySymbolTable

use of com.linkedin.data.codec.symbol.InMemorySymbolTable in project rest.li by linkedin.

the class TestStreamCodec method getCodecs.

private List<StreamDataCodec> getCodecs(int bufferSize, DataComplex data) {
    List<StreamDataCodec> codecs = new ArrayList<>();
    codecs.add(new JacksonSmileStreamDataCodec(bufferSize));
    codecs.add(new JacksonStreamDataCodec(bufferSize));
    codecs.add(new ProtobufStreamDataCodec(bufferSize));
    Set<String> symbols = new HashSet<>();
    if (data instanceof DataMap) {
        collectSymbols(symbols, (DataMap) data);
    } else {
        collectSymbols(symbols, (DataList) data);
    }
    final String sharedSymbolTableName = "SHARED";
    SymbolTable symbolTable = new InMemorySymbolTable(sharedSymbolTableName, new ArrayList<>(symbols));
    codecs.add(new JacksonLICORStreamDataCodec(bufferSize, true, symbolTable));
    codecs.add(new ProtobufStreamDataCodec(bufferSize, new ProtobufCodecOptions.Builder().setSymbolTable(symbolTable).setEnableASCIIOnlyStrings(true).build()));
    return codecs;
}
Also used : ArrayList(java.util.ArrayList) InMemorySymbolTable(com.linkedin.data.codec.symbol.InMemorySymbolTable) SymbolTable(com.linkedin.data.codec.symbol.SymbolTable) ByteString(com.linkedin.data.ByteString) DataMap(com.linkedin.data.DataMap) ProtobufCodecOptions(com.linkedin.data.codec.ProtobufCodecOptions) HashSet(java.util.HashSet) InMemorySymbolTable(com.linkedin.data.codec.symbol.InMemorySymbolTable)

Example 9 with InMemorySymbolTable

use of com.linkedin.data.codec.symbol.InMemorySymbolTable in project rest.li by linkedin.

the class TestRestLiSymbolTableRequestHandler method testReturnSelfSymbolTable.

@Test
public void testReturnSelfSymbolTable() throws Exception {
    SymbolTable symbolTable = new InMemorySymbolTable("TestName", ImmutableList.of("Haha", "Hehe", "Hoho"));
    URI uri = URI.create("/symbolTable");
    RestRequest request = new RestRequestBuilder(uri).build();
    when(_symbolTableProvider.getResponseSymbolTable(eq(uri), eq(Collections.emptyMap()))).thenReturn(symbolTable);
    CompletableFuture<RestResponse> future = new CompletableFuture<>();
    _requestHandler.handleRequest(request, mock(RequestContext.class), new Callback<RestResponse>() {

        @Override
        public void onError(Throwable e) {
            future.completeExceptionally(e);
        }

        @Override
        public void onSuccess(RestResponse result) {
            future.complete(result);
        }
    });
    Assert.assertFalse(future.isCompletedExceptionally());
    Assert.assertTrue(future.isDone());
    RestResponse response = future.get();
    Assert.assertEquals(response.getStatus(), HttpStatus.S_200_OK.getCode());
    Assert.assertEquals(response.getHeader(RestConstants.HEADER_CONTENT_TYPE), ContentType.PROTOBUF2.getHeaderKey());
    Assert.assertEquals(symbolTable, SymbolTableSerializer.fromByteString(response.getEntity(), ContentType.PROTOBUF2.getCodec()));
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) InMemorySymbolTable(com.linkedin.data.codec.symbol.InMemorySymbolTable) SymbolTable(com.linkedin.data.codec.symbol.SymbolTable) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) URI(java.net.URI) InMemorySymbolTable(com.linkedin.data.codec.symbol.InMemorySymbolTable) Test(org.testng.annotations.Test)

Aggregations

InMemorySymbolTable (com.linkedin.data.codec.symbol.InMemorySymbolTable)9 SymbolTable (com.linkedin.data.codec.symbol.SymbolTable)8 Test (org.testng.annotations.Test)7 RestRequest (com.linkedin.r2.message.rest.RestRequest)6 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)6 RequestContext (com.linkedin.r2.message.RequestContext)4 RestResponse (com.linkedin.r2.message.rest.RestResponse)4 URI (java.net.URI)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 ByteString (com.linkedin.data.ByteString)2 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 DataMap (com.linkedin.data.DataMap)1 ProtobufCodecOptions (com.linkedin.data.codec.ProtobufCodecOptions)1 SymbolTableProvider (com.linkedin.data.codec.symbol.SymbolTableProvider)1 DataSchema (com.linkedin.data.schema.DataSchema)1