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
Closed
Changes from 6 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
b111ab5
add partial Thrift support
b2a7518
THRIFT-5443: add support for partial Thrift deserialization
180e916
merge with master
65e67dc
add clarification on map field paths
e41b268
Merge branch 'apache:master' into kpandit/thrift-partial
bhalchandrap 85e2dcb
Merge branch 'kpandit/thrift-partial' of github.com:bhalchandrap/thri…
9b667f0
Merge branch 'apache:master' into kpandit/thrift-partial
bhalchandrap 02811b4
merge with master
93851a1
addressed review comments
28bedae
Merge branch 'kpandit/thrift-partial' of github.com:bhalchandrap/thri…
9df82f5
Merge branch 'apache:master' into kpandit/thrift-partial
bhalchandrap df30051
move skip() functionality to T*Protocol, add partial deser method to …
de2bcf3
Merge branch 'kpandit/thrift-partial' of github.com:bhalchandrap/thri…
2780489
use correct name for overloaded method partialDeserializeObject()
c4e4139
Merge branch 'apache:master' into kpandit/thrift-partial
bhalchandrap 28af5f6
use read() as the default implementation of skip()
6d66622
Merge branch 'apache:master' into kpandit/thrift-partial
bhalchandrap 4e3a7f4
Merge branch 'apache:master' into kpandit/thrift-partial
bhalchandrap e132925
Merge branch 'apache:master' into kpandit/thrift-partial
bhalchandrap 0aeb0f0
Merge branch 'apache:master' into kpandit/thrift-partial
bhalchandrap e42321a
trigger build
55da9c4
merge with master
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
lib/java/src/org/apache/thrift/partial/PartialThriftBinaryProtocol.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
lib/java/src/org/apache/thrift/partial/PartialThriftCompactProtocol.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
|
||
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); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.