Skip to content

Commit

Permalink
optimize: protostuff and kryo serialize timestamp (apache#2320)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbxyyx committed Mar 11, 2020
1 parent 6d63d72 commit c277984
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,14 @@ public Clob read(Kryo kryo, Input input, Class<Clob> type) {
private class TimestampSerializer extends Serializer<Timestamp> {
@Override
public void write(Kryo kryo, Output output, Timestamp object) {
output.writeLong(object.getTime());
output.writeInt(object.getNanos());
output.writeLong(object.getTime(), true);
output.writeInt(object.getNanos(), true);
}

@Override
public Timestamp read(Kryo kryo, Input input, Class<Timestamp> type) {
Timestamp timestamp = new Timestamp(input.readLong());
timestamp.setNanos(input.readInt());
Timestamp timestamp = new Timestamp(input.readLong(true));
timestamp.setNanos(input.readInt(true));
return timestamp;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.seata.rm.datasource.undo.parser;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.sql.Timestamp;

import io.protostuff.Input;
Expand Down Expand Up @@ -95,7 +96,7 @@ public static class TimestampDelegate implements Delegate<java.sql.Timestamp> {

@Override
public FieldType getFieldType() {
return FieldType.FIXED64;
return FieldType.BYTES;
}

@Override
Expand All @@ -105,20 +106,27 @@ public Class<?> typeClass() {

@Override
public java.sql.Timestamp readFrom(Input input) throws IOException {
String[] strs = input.readString().split("_");
java.sql.Timestamp timestamp = new Timestamp(Long.parseLong(strs[0]));
timestamp.setNanos(Integer.parseInt(strs[1]));
ByteBuffer buffer = input.readByteBuffer();
long time = buffer.getLong();
int nanos = buffer.getInt();
buffer.flip();
java.sql.Timestamp timestamp = new Timestamp(time);
timestamp.setNanos(nanos);
return timestamp;
}

@Override
public void writeTo(Output output, int number, java.sql.Timestamp value, boolean repeated) throws IOException {
output.writeString(number, value.getTime() + "_" + value.getNanos(), repeated);
ByteBuffer buffer = ByteBuffer.allocate(12);
buffer.putLong(value.getTime());
buffer.putInt(value.getNanos());
buffer.flip();
output.writeBytes(number, buffer, repeated);
}

@Override
public void transfer(Pipe pipe, Input input, Output output, int number, boolean repeated) throws IOException {
output.writeString(number, input.readString(), repeated);
output.writeBytes(number, input.readByteBuffer(), repeated);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public void testTimestampEncodeAndDecode() {
field.setName("gmt_create");
field.setKeyType(KeyType.PRIMARY_KEY);
field.setType(JDBCType.TIMESTAMP.getVendorTypeNumber());
Timestamp timestampEncode = new Timestamp(System.currentTimeMillis());
Timestamp timestampEncode = new Timestamp(Integer.MAX_VALUE + 1L);
timestampEncode.setNanos(999999);
field.setValue(timestampEncode);
row.add(field);
Expand Down

0 comments on commit c277984

Please sign in to comment.