Skip to content

Commit

Permalink
revert and add new deserialization method
Browse files Browse the repository at this point in the history
  • Loading branch information
chaokunyang committed Oct 6, 2023
1 parent 686cdfd commit e86ea91
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,5 @@ public static void main(String[] args) {
state.setup();
state.bufferType = BufferType.directBuffer;
state.setup();

;
JSONObject json = new JSONObject();
json.put("k", 1);
Fury fury = Fury.builder().requireClassRegistration(false).build();
byte[] bytes = fury.serialize(json);
System.out.println(fury.deserialize(bytes));
}
}
35 changes: 23 additions & 12 deletions java/fury-core/src/main/java/io/fury/Fury.java
Original file line number Diff line number Diff line change
Expand Up @@ -635,24 +635,34 @@ public long readLong(MemoryBuffer buffer) {
* @param <T> result type of the data.
* @return deserialized object.
*/
public <T> T deserialize(byte[] bytes) {
public <T> T deserializeTyped(byte[] bytes) {
return (T) deserialize(MemoryUtils.wrap(bytes), null);
}

/**
* Deserialize <code>obj</code> from a byte array.
*
* @param bytes serialized data.
* @return deserialized object.
*/
public Object deserialize(byte[] bytes) {
return deserialize(MemoryUtils.wrap(bytes), null);
}

public <T> T deserialize(byte[] bytes, Iterable<MemoryBuffer> outOfBandBuffers) {
public Object deserialize(byte[] bytes, Iterable<MemoryBuffer> outOfBandBuffers) {
return deserialize(MemoryUtils.wrap(bytes), outOfBandBuffers);
}

/**
* Deserialize <code>obj</code> from a off-heap buffer specified by <code>address</code> and
* <code>size</code>.
*/
public <T> T deserialize(long address, int size) {
public Object deserialize(long address, int size) {
return deserialize(MemoryUtils.buffer(address, size), null);
}

/** Deserialize <code>obj</code> from a <code>buffer</code>. */
public <T> T deserialize(MemoryBuffer buffer) {
public Object deserialize(MemoryBuffer buffer) {
return deserialize(buffer, null);
}

Expand All @@ -669,7 +679,7 @@ public <T> T deserialize(MemoryBuffer buffer) {
* It is an error for <code>outOfBandBuffers</code> to be null if the serialized stream was
* produced with a non-null `bufferCallback`.
*/
public <T> T deserialize(MemoryBuffer buffer, Iterable<MemoryBuffer> outOfBandBuffers) {
public Object deserialize(MemoryBuffer buffer, Iterable<MemoryBuffer> outOfBandBuffers) {
try {
jitContext.lock();
byte bitmap = buffer.readByte();
Expand Down Expand Up @@ -706,7 +716,7 @@ public <T> T deserialize(MemoryBuffer buffer, Iterable<MemoryBuffer> outOfBandBu
}
obj = readRef(buffer);
}
return (T) obj;
return obj;
} finally {
resetRead();
jitContext.unlock();
Expand Down Expand Up @@ -1076,21 +1086,21 @@ public void serializeJavaObjectAndClass(OutputStream outputStream, Object obj) {
* Deserialize class info and java object from binary, serialization should use {@link
* #serializeJavaObjectAndClass}.
*/
public <T> T deserializeJavaObjectAndClass(byte[] data) {
public Object deserializeJavaObjectAndClass(byte[] data) {
return deserializeJavaObjectAndClass(MemoryBuffer.fromByteArray(data));
}

/**
* Deserialize class info and java object from binary, serialization should use {@link
* #serializeJavaObjectAndClass}.
*/
public <T> T deserializeJavaObjectAndClass(MemoryBuffer buffer) {
public Object deserializeJavaObjectAndClass(MemoryBuffer buffer) {
try {
jitContext.lock();
if (config.shareMetaContext()) {
classResolver.readClassDefs(buffer);
}
return (T) readRef(buffer);
return readRef(buffer);
} finally {
resetRead();
jitContext.unlock();
Expand All @@ -1101,7 +1111,7 @@ public <T> T deserializeJavaObjectAndClass(MemoryBuffer buffer) {
* Deserialize class info and java object from binary, serialization should use {@link
* #serializeJavaObjectAndClass}.
*/
public <T> T deserializeJavaObjectAndClass(InputStream inputStream) {
public Object deserializeJavaObjectAndClass(InputStream inputStream) {
return deserializeFromStream(inputStream, this::deserializeJavaObjectAndClass);
}

Expand Down Expand Up @@ -1134,7 +1144,8 @@ private void serializeToStream(OutputStream outputStream, Consumer<MemoryBuffer>
}
}

private <T> T deserializeFromStream(InputStream inputStream, Function<MemoryBuffer, T> function) {
private Object deserializeFromStream(
InputStream inputStream, Function<MemoryBuffer, Object> function) {
buffer.readerIndex(0);
try {
boolean isBis = inputStream.getClass() == ByteArrayInputStream.class;
Expand All @@ -1151,7 +1162,7 @@ private <T> T deserializeFromStream(InputStream inputStream, Function<MemoryBuff
read = inputStream.read(buffer.getHeapMemory(), 4, size);
Preconditions.checkArgument(read == size);
}
T o = function.apply(buffer);
Object o = function.apply(buffer);
if (isBis) {
inputStream.skip(buffer.readerIndex());
buffer.pointTo(oldBytes, 0, oldBytes.length);
Expand Down
12 changes: 8 additions & 4 deletions java/fury-core/src/main/java/io/fury/ThreadLocalFury.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,23 @@ public MemoryBuffer serialize(MemoryBuffer buffer, Object obj) {
return bindingThreadLocal.get().get().serialize(buffer, obj);
}

public <T> T deserialize(byte[] bytes) {
public <T> T deserializeTyped(byte[] bytes) {
return bindingThreadLocal.get().get().deserializeTyped(bytes);
}

public Object deserialize(byte[] bytes) {
return bindingThreadLocal.get().get().deserialize(bytes);
}

public <T> T deserialize(long address, int size) {
public Object deserialize(long address, int size) {
return bindingThreadLocal.get().get().deserialize(address, size);
}

public <T> T deserialize(MemoryBuffer buffer) {
public Object deserialize(MemoryBuffer buffer) {
return bindingThreadLocal.get().get().deserialize(buffer);
}

public <T> T deserialize(ByteBuffer byteBuffer) {
public Object deserialize(ByteBuffer byteBuffer) {
return bindingThreadLocal.get().get().deserialize(MemoryUtils.wrap(byteBuffer));
}

Expand Down
10 changes: 6 additions & 4 deletions java/fury-core/src/main/java/io/fury/ThreadSafeFury.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ public interface ThreadSafeFury {

MemoryBuffer serialize(MemoryBuffer buffer, Object obj);

<T> T deserialize(byte[] bytes);
<T> T deserializeTyped(byte[] bytes);

<T> T deserialize(long address, int size);
Object deserialize(byte[] bytes);

<T> T deserialize(MemoryBuffer buffer);
Object deserialize(long address, int size);

<T> T deserialize(ByteBuffer byteBuffer);
Object deserialize(MemoryBuffer buffer);

Object deserialize(ByteBuffer byteBuffer);

/**
* Set classLoader of serializer for current thread only.
Expand Down
18 changes: 14 additions & 4 deletions java/fury-core/src/main/java/io/fury/pool/ThreadPoolFury.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,17 @@ public MemoryBuffer serialize(MemoryBuffer buffer, Object obj) {
}
}

public <T> T deserialize(byte[] bytes) {
public <T> T deserializeTyped(byte[] bytes) {
Fury fury = null;
try {
fury = furyPooledObjectFactory.getFury();
return fury.deserializeTyped(bytes);
} finally {
furyPooledObjectFactory.returnFury(fury);
}
}

public Object deserialize(byte[] bytes) {
Fury fury = null;
try {
fury = furyPooledObjectFactory.getFury();
Expand All @@ -81,7 +91,7 @@ public <T> T deserialize(byte[] bytes) {
}
}

public <T> T deserialize(long address, int size) {
public Object deserialize(long address, int size) {
Fury fury = null;
try {
fury = furyPooledObjectFactory.getFury();
Expand All @@ -91,7 +101,7 @@ public <T> T deserialize(long address, int size) {
}
}

public <T> T deserialize(MemoryBuffer buffer) {
public Object deserialize(MemoryBuffer buffer) {
Fury fury = null;
try {
fury = furyPooledObjectFactory.getFury();
Expand All @@ -101,7 +111,7 @@ public <T> T deserialize(MemoryBuffer buffer) {
}
}

public <T> T deserialize(ByteBuffer byteBuffer) {
public Object deserialize(ByteBuffer byteBuffer) {
Fury fury = null;
try {
fury = furyPooledObjectFactory.getFury();
Expand Down
5 changes: 2 additions & 3 deletions java/fury-core/src/test/java/io/fury/FuryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package io.fury;

import static io.fury.FuryTestBase.getJavaFury;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertThrows;
Expand Down Expand Up @@ -541,12 +540,12 @@ public void testJavaOutputStream() throws IOException {
public void testDeserializationAutotypeCast() {
Fury fury = getJavaFury();
byte[] bytes = fury.serialize(1);
Integer i = fury.deserialize(bytes);
Integer i = fury.deserializeTyped(bytes);
Assert.assertEquals(i, 1);
Assert.assertThrows(
ClassCastException.class,
() -> {
String str = fury.deserialize(bytes);
String str = fury.deserializeTyped(bytes);
System.out.println(str);
});
}
Expand Down
2 changes: 1 addition & 1 deletion java/fury-core/src/test/java/io/fury/FuryTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public static Object serDeCheck(Fury fury, Object obj) {
public static <T> T serDe(Fury fury, T obj) {
try {
byte[] bytes = fury.serialize(obj);
return fury.deserialize(bytes);
return (T) (fury.deserialize(bytes));
} catch (Throwable t) {
// Catch for add breakpoint for debugging.
throw t;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public void testSerialize() throws Exception {
try {
fury.setClassLoader(beanA.getClass().getClassLoader());
assertEquals(fury.deserialize(fury.serialize(beanA)), beanA);
assertEquals(fury.deserializeTyped(fury.serialize(beanA)), beanA);
} catch (Exception e) {
hasException = true;
e.printStackTrace();
Expand Down

0 comments on commit e86ea91

Please sign in to comment.