Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

THRIFT-5443: add support for partial Thrift deserialization #2439

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
b111ab5
add partial Thrift support
Aug 16, 2021
b2a7518
THRIFT-5443: add support for partial Thrift deserialization
Aug 16, 2021
180e916
merge with master
Aug 31, 2021
65e67dc
add clarification on map field paths
Aug 31, 2021
e41b268
Merge branch 'apache:master' into kpandit/thrift-partial
bhalchandrap Aug 31, 2021
85e2dcb
Merge branch 'kpandit/thrift-partial' of github.com:bhalchandrap/thri…
Aug 31, 2021
9b667f0
Merge branch 'apache:master' into kpandit/thrift-partial
bhalchandrap Sep 6, 2021
02811b4
merge with master
Sep 6, 2021
93851a1
addressed review comments
Sep 6, 2021
28bedae
Merge branch 'kpandit/thrift-partial' of github.com:bhalchandrap/thri…
Sep 6, 2021
9df82f5
Merge branch 'apache:master' into kpandit/thrift-partial
bhalchandrap Sep 22, 2021
df30051
move skip() functionality to T*Protocol, add partial deser method to …
Oct 1, 2021
de2bcf3
Merge branch 'kpandit/thrift-partial' of github.com:bhalchandrap/thri…
Oct 1, 2021
2780489
use correct name for overloaded method partialDeserializeObject()
Oct 7, 2021
c4e4139
Merge branch 'apache:master' into kpandit/thrift-partial
bhalchandrap Oct 7, 2021
28af5f6
use read() as the default implementation of skip()
Oct 12, 2021
6d66622
Merge branch 'apache:master' into kpandit/thrift-partial
bhalchandrap Oct 21, 2021
4e3a7f4
Merge branch 'apache:master' into kpandit/thrift-partial
bhalchandrap Oct 21, 2021
e132925
Merge branch 'apache:master' into kpandit/thrift-partial
bhalchandrap Oct 28, 2021
0aeb0f0
Merge branch 'apache:master' into kpandit/thrift-partial
bhalchandrap Nov 5, 2021
e42321a
trigger build
Nov 8, 2021
55da9c4
merge with master
Nov 15, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/java/gradle.properties
Expand Up @@ -33,3 +33,4 @@ tomcat.embed.version=9.0.43
junit.version=4.12
mockito.version=1.10.19
javax.annotation.version=1.3.2
commons-lang3.version=3.12
1 change: 1 addition & 0 deletions lib/java/gradle/environment.gradle
Expand Up @@ -69,6 +69,7 @@ dependencies {
compile "org.apache.httpcomponents:httpcore:${httpcoreVersion}"
compile "javax.servlet:javax.servlet-api:${servletVersion}"
compile "javax.annotation:javax.annotation-api:${javaxAnnotationVersion}"
compile "org.apache.commons:commons-lang3:3.12.0"

testCompile "junit:junit:${junitVersion}"
testCompile "org.mockito:mockito-all:${mockitoVersion}"
Expand Down
1 change: 1 addition & 0 deletions lib/java/gradle/generateTestThrift.gradle
Expand Up @@ -81,6 +81,7 @@ task generateJava(group: 'Build') {
thriftCompile(it, 'JavaDeepCopyTest.thrift')
thriftCompile(it, 'EnumContainersTest.thrift')
thriftCompile(it, 'JavaBinaryDefault.thrift')
thriftCompile(it, 'partial/thrift_test_schema.thrift')
}

task generateBeanJava(group: 'Build') {
Expand Down
92 changes: 92 additions & 0 deletions lib/java/src/org/apache/thrift/partial/EnumCache.java
@@ -0,0 +1,92 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.thrift.partial;

import org.apache.thrift.partial.Validate;

import org.apache.thrift.TEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
* Provides a memoized way to lookup an enum by its value.
*
* This class is used internally by {@link PartialThriftDeserializer}.
* It is not intended to be used separately on its own.
*/
public class EnumCache {
private static Logger LOG = LoggerFactory.getLogger(EnumCache.class);

private Map<Class<? extends TEnum>, Map<Integer, TEnum>> classMap;

public EnumCache() {
this.classMap = new HashMap<>();
}

/**
* Gets an instance of the enum type {@code enumClass}
* corresponding to the given {@code value}.
*
* @param enumClass class of the enum to be returned.
* @param value value returned by {@code getValue()}.
*/
public TEnum get(Class<? extends TEnum> enumClass, int value) {
Validate.checkNotNull(enumClass, "enumClass");

Map<Integer, TEnum> valueMap = classMap.get(enumClass);
if (valueMap == null) {
valueMap = addClass(enumClass);
if (valueMap == null) {
return null;
}
}

return valueMap.get(value);
}

private Map<Integer, TEnum> addClass(Class<? extends TEnum> enumClass) {
try {
Method valuesMethod = enumClass.getMethod("values");
TEnum[] enumValues = (TEnum[]) valuesMethod.invoke(null);
Map<Integer, TEnum> valueMap = new HashMap<>();

for (TEnum enumValue : enumValues) {
valueMap.put(enumValue.getValue(), enumValue);
}

classMap.put(enumClass, valueMap);
return valueMap;
} catch (NoSuchMethodException e) {
LOG.error("enum class does not have values() method", e);
return null;
} catch (IllegalAccessException e) {
LOG.error("Enum.values() method should be public!", e);
return null;
} catch (InvocationTargetException e) {
LOG.error("Enum.values() threw exception", e);
return null;
}
}
}
@@ -0,0 +1,58 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.thrift.partial;

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.protocol.TType;

import java.io.Serializable;

/**
* Enables partial deserialization of binary-encoded thrift objects.
*
* This class is meant to be a helper class for {@link PartialThriftDeserializer}.
* It cannot be used separately on its own.
*/
public class PartialThriftBinaryProtocol extends PartialThriftProtocol implements Serializable {

public PartialThriftBinaryProtocol() {
}

@Override
protected TProtocol createProtocol() {
return new TBinaryProtocol(transport);
}

// -----------------------------------------------------------------
// Additional methods to improve performance.

@Override
public int readFieldBeginData() throws TException {
byte type = readByte();
if (type == TType.STOP) {
return TFieldData.encode(type);
}

short id = readI16();
return TFieldData.encode(type, id);
}
}
@@ -0,0 +1,93 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.thrift.partial;

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TField;
import org.apache.thrift.protocol.TProtocol;

import java.io.Serializable;

/**
* Enables partial deserialization of compact-encoded thrift objects.
*
* This class is meant to be a helper class for {@link PartialThriftDeserializer}.
* It cannot be used separately on its own.
*/
public class PartialThriftCompactProtocol extends PartialThriftProtocol implements Serializable {

public PartialThriftCompactProtocol() {
}

@Override
protected TProtocol createProtocol() {
return new TCompactProtocol(transport);
}

// -----------------------------------------------------------------
// Additional methods to improve performance.

@Override
public int readFieldBeginData() throws TException {
// Having to call readFieldBegin() to compute TFieldData really results in lower
// performance. However, readFieldBegin() accesses some private vars that this method
// does not have access to. We could make it more performant when contributing to
// origianl source code.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are contributing to the original source code here, so maybe this comment should be updated accordingly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a TODO comment for now. That way we can better separate original contribution from subsequent performance improvement changes.


TField tfield = readFieldBegin();
return TFieldData.encode(tfield.type, tfield.id);
}

@Override
protected void skipBinary() throws TException {
int size = intToZigZag(readI32());
this.skipBytes(size);
}

// -------------------------------------------------------
// Implementing skip for the following methods is tricky (but not impossible).
// For now, we call the corresponding read() method.

@Override
protected void skipBool() throws TException {
this.readBool();
}

@Override
protected void skipI16() throws TException {
this.readI16();
}

@Override
protected void skipI32() throws TException {
this.readI32();
}

@Override
protected void skipI64() throws TException {
this.readI64();
}
// -------------------------------------------------------

private int intToZigZag(int n) {
return (n << 1) ^ (n >> 31);
}
}