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

Performance tuning on QReader and Writer #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/main/java/com/exxeleron/qjava/DefaultQReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,9 @@ protected QFunction readFunction( final QType qtype ) throws QException, IOExcep
* @throws QReaderException
*/
public static Class<?> getType( final QType type ) throws QReaderException {
if ( fromQ.containsKey(type) ) {
return fromQ.get(type);
Class aClass = fromQ.get(type);
if ( aClass != null ) {
return aClass;
} else {
throw new QReaderException("Cannot deserialize object of type: " + type);
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/exxeleron/qjava/DefaultQWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -624,8 +624,11 @@ protected void writeProjection( final QProjection p ) throws IOException, QExcep
public static QType getQType( final Object obj ) throws QWriterException {
if ( obj == null ) {
return QType.NULL_ITEM;
} else if ( toQ.containsKey(obj.getClass()) ) {
return toQ.get(obj.getClass());
}

QType qType = toQ.get(obj.getClass());
if ( qType != null ) {
return qType;
} else {
throw new QWriterException("Cannot serialize object of type: " + obj.getClass().getCanonicalName());
}
Expand Down
32 changes: 16 additions & 16 deletions src/main/java/com/exxeleron/qjava/QType.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,13 @@ public byte getTypeCode() {
}
});

private static final Map<Byte, QType> lookup = Collections.unmodifiableMap(new HashMap<Byte, QType>() {
private static final long serialVersionUID = 7199217298785029441L;

{
for ( final QType qtype : EnumSet.allOf(QType.class) ) {
put(qtype.getTypeCode(), qtype);
}
private static final int LOOKUP_SHIFT = 128;
private static final QType[] lookup = new QType[256];
static {
for ( final QType qtype : EnumSet.allOf(QType.class) ) {
lookup[qtype.getTypeCode() + LOOKUP_SHIFT] = qtype;
}

});
}

/**
* Returns {@link QType} based on type code identifier.
Expand All @@ -138,8 +135,9 @@ public byte getTypeCode() {
* in case q type code is unknown
*/
public static QType valueOf( final byte typecode ) {
if ( lookup.containsKey(typecode) ) {
return lookup.get(typecode);
QType qType = lookup[typecode + LOOKUP_SHIFT];
if ( qType != null ) {
return qType;
} else {
throw new IllegalArgumentException("Invalid q type code: " + typecode);
}
Expand All @@ -153,9 +151,10 @@ public static QType valueOf( final byte typecode ) {
* @return {@link QType} enum bound with type code identifier
* @throws QReaderException
*/
public static QType getQType( final Byte typecode ) throws QReaderException {
if ( lookup.containsKey(typecode) ) {
return lookup.get(typecode);
public static QType getQType( final byte typecode ) throws QReaderException {
QType qType = lookup[typecode + LOOKUP_SHIFT];
if ( qType != null ) {
return qType;
} else {
throw new QReaderException("Cannot deserialize object of type: " + typecode);
}
Expand All @@ -170,8 +169,9 @@ public static QType getQType( final Byte typecode ) throws QReaderException {
* @throws QException
*/
public static Object getQNull( final QType type ) throws QException {
if ( qNulls.containsKey(type) ) {
return qNulls.get(type);
Object obj = qNulls.get(type);
if ( obj != null ) {
return obj;
} else {
throw new QException("Cannot find null value of type: " + type);
}
Expand Down