Search in sources :

Example 1 with SymbolTableProvider

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

the class RestLiSymbolTableRequestHandler method handleRequest.

@Override
public void handleRequest(RestRequest request, RequestContext requestContext, Callback<RestResponse> callback) {
    if (HttpMethod.GET != HttpMethod.valueOf(request.getMethod())) {
        LOGGER.error("GET is expected, but " + request.getMethod() + " received");
        callback.onError(RestException.forError(HttpStatus.S_405_METHOD_NOT_ALLOWED.getCode(), "Invalid method"));
        return;
    }
    // 
    // Determine response content type based on accept header.
    // Assume protobuf2 if no accept header is specified. Note that this is a deviation from the rest of rest.li
    // which assumes JSON as the default, for efficiency reasons.
    // 
    ContentType type;
    String mimeType = Optional.ofNullable(request.getHeader(RestConstants.HEADER_ACCEPT)).orElse(RestConstants.HEADER_VALUE_APPLICATION_PROTOBUF2);
    try {
        type = ContentType.getContentType(mimeType).orElseThrow(() -> new MimeTypeParseException("Invalid accept type: " + mimeType));
    } catch (MimeTypeParseException e) {
        LOGGER.error("Could not handle accept type", e);
        callback.onError(RestException.forError(HttpStatus.S_406_NOT_ACCEPTABLE.getCode(), "Invalid accept type: " + mimeType));
        return;
    }
    final String path = request.getURI().getRawPath();
    final List<UriComponent.PathSegment> pathSegments = UriComponent.decodePath(path, true);
    final SymbolTableProvider provider = SymbolTableProviderHolder.INSTANCE.getSymbolTableProvider();
    SymbolTable symbolTable = null;
    // at this point, `handleRequest` has verified that the incoming request is a symbolTable request.
    // The URL can be one of two options:
    // .../symbolTable/tableName
    // .../symbolTable
    // We check if the last path segments is "symbolTable", and if it is, we call provider.getResponseSymbolTable
    // because we do not know the table name.
    // Otherwise, we call provider.getSymbolTable
    int pathSize = pathSegments.size();
    try {
        if (pathSegments.get(pathSize - 1).getPath().equals(SYMBOL_TABLE_URI_PATH)) {
            symbolTable = provider.getResponseSymbolTable(request.getURI(), request.getHeaders());
        } else if (pathSegments.get(pathSize - 2).getPath().equals(SYMBOL_TABLE_URI_PATH)) {
            symbolTable = provider.getSymbolTable(pathSegments.get(pathSize - 1).getPath());
        } else {
            LOGGER.error("request is malformed for handling symbolTable" + request.getURI());
        }
    } catch (IllegalStateException e) {
        LOGGER.error("Exception retrieving symbol table for URI " + request.getURI());
        symbolTable = null;
    }
    if (symbolTable == null) {
        LOGGER.error("Did not find symbol table for path " + path);
        callback.onError(RestException.forError(HttpStatus.S_404_NOT_FOUND.getCode(), "Did not find symbol table"));
        return;
    }
    try {
        // Cache key is the name of the symbol table concatenated with the type used to serialize the payload.
        String cacheKey = symbolTable.getName() + ":" + type.getHeaderKey();
        ByteString serializedTable = _symbolTableNameToSerializedBytesCache.getIfPresent(cacheKey);
        if (serializedTable == null) {
            serializedTable = SymbolTableSerializer.toByteString(type.getCodec(), symbolTable);
            _symbolTableNameToSerializedBytesCache.put(cacheKey, serializedTable);
        }
        RestResponse restResponse = new RestResponseBuilder().setStatus(HttpStatus.S_200_OK.getCode()).setHeader(RestConstants.HEADER_CONTENT_TYPE, type.getHeaderKey()).setEntity(serializedTable).build();
        callback.onSuccess(restResponse);
    } catch (IOException e) {
        callback.onError(e);
    }
}
Also used : MimeTypeParseException(javax.activation.MimeTypeParseException) SymbolTableProvider(com.linkedin.data.codec.symbol.SymbolTableProvider) DefaultSymbolTableProvider(com.linkedin.data.codec.symbol.DefaultSymbolTableProvider) ContentType(com.linkedin.restli.common.ContentType) ByteString(com.linkedin.data.ByteString) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) SymbolTable(com.linkedin.data.codec.symbol.SymbolTable) ByteString(com.linkedin.data.ByteString) IOException(java.io.IOException)

Example 2 with SymbolTableProvider

use of com.linkedin.data.codec.symbol.SymbolTableProvider 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)

Aggregations

ByteString (com.linkedin.data.ByteString)2 SymbolTable (com.linkedin.data.codec.symbol.SymbolTable)2 SymbolTableProvider (com.linkedin.data.codec.symbol.SymbolTableProvider)2 DefaultSymbolTableProvider (com.linkedin.data.codec.symbol.DefaultSymbolTableProvider)1 InMemorySymbolTable (com.linkedin.data.codec.symbol.InMemorySymbolTable)1 RestResponse (com.linkedin.r2.message.rest.RestResponse)1 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)1 ContentType (com.linkedin.restli.common.ContentType)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 MimeTypeParseException (javax.activation.MimeTypeParseException)1