LUCENE-7936: Improve serialization by using a shortcut way of describing classes.

This commit is contained in:
Karl Wright 2017-08-30 10:40:38 -04:00
parent 1cc3d8050c
commit faad8ae6a8
3 changed files with 173 additions and 40 deletions

View File

@ -17,15 +17,15 @@
package org.apache.lucene.spatial3d.geom; package org.apache.lucene.spatial3d.geom;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.lang.reflect.Constructor;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.List; import java.nio.charset.StandardCharsets;
import java.util.BitSet; import java.util.BitSet;
import java.util.List;
/** /**
* Indicates that a geo3d object can be serialized and deserialized. * Indicates that a geo3d object can be serialized and deserialized.
@ -66,7 +66,7 @@ public interface SerializableObject {
* @param object is the object to write. * @param object is the object to write.
*/ */
public static void writeObject(final OutputStream outputStream, final SerializableObject object) throws IOException { public static void writeObject(final OutputStream outputStream, final SerializableObject object) throws IOException {
writeString(outputStream, object.getClass().getName()); writeClass(outputStream, object.getClass());
object.write(outputStream); object.write(outputStream);
} }
@ -76,14 +76,12 @@ public interface SerializableObject {
* @return the deserialized object. * @return the deserialized object.
*/ */
public static SerializableObject readObject(final PlanetModel planetModel, final InputStream inputStream) throws IOException { public static SerializableObject readObject(final PlanetModel planetModel, final InputStream inputStream) throws IOException {
// Read the class name
final String className = readString(inputStream);
try { try {
// Look for the class // Read the class
final Class<?> clazz = Class.forName(className); final Class<?> clazz = readClass(inputStream);
return readObject(planetModel, inputStream, clazz); return readObject(planetModel, inputStream, clazz);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
throw new IOException("Can't find class "+className+" for deserialization: "+e.getMessage(), e); throw new IOException("Can't find class for deserialization: "+e.getMessage(), e);
} }
} }
@ -92,14 +90,12 @@ public interface SerializableObject {
* @return the deserialized object. * @return the deserialized object.
*/ */
public static SerializableObject readObject(final InputStream inputStream) throws IOException { public static SerializableObject readObject(final InputStream inputStream) throws IOException {
// Read the class name
final String className = readString(inputStream);
try { try {
// Look for the class // read the class
final Class<?> clazz = Class.forName(className); final Class<?> clazz = readClass(inputStream);
return readObject(inputStream, clazz); return readObject(inputStream, clazz);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
throw new IOException("Can't find class "+className+" for deserialization: "+e.getMessage(), e); throw new IOException("Can't find class for deserialization: "+e.getMessage(), e);
} }
} }
@ -158,6 +154,39 @@ public interface SerializableObject {
} }
/** Write a class to a stream.
* @param outputStream is the output stream.
* @param clazz is the class to write.
*/
static void writeClass(final OutputStream outputStream, final Class<?> clazz) throws IOException {
Integer index = StandardObjects.classRegsitry.get(clazz);
if (index == null){
writeBoolean(outputStream, false);
writeString(outputStream, clazz.getName());
}
else {
writeBoolean(outputStream, true);
outputStream.write(index);
}
}
/**
* Read the class from the stream
* @param inputStream is the stream to read from.
* @return is the class read
*/
static Class<?> readClass(final InputStream inputStream) throws IOException, ClassNotFoundException {
boolean standard = readBoolean(inputStream);
if (standard) {
int index = inputStream.read();
return StandardObjects.codeRegsitry.get(index);
}
else {
String className = readString(inputStream);
return Class.forName(className);
}
}
/** Write a string to a stream. /** Write a string to a stream.
* @param outputStream is the output stream. * @param outputStream is the output stream.
* @param value is the string to write. * @param value is the string to write.
@ -438,3 +467,4 @@ public interface SerializableObject {
} }
} }

View File

@ -0,0 +1,81 @@
/*
* 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.lucene.spatial3d.geom;
import java.util.HashMap;
import java.util.Map;
/**
* Lookup tables for classes that can be serialized using a code.
*
* @lucene.internal
*/
class StandardObjects {
/**
* Registry of standard classes to corresponding code
*/
static Map<Class<?>, Integer> classRegsitry = new HashMap<>();
/**
* Registry of codes to corresponding classes
*/
static Map<Integer, Class<?>> codeRegsitry = new HashMap<>();
static {
classRegsitry.put(GeoPoint.class, 0);
classRegsitry.put(GeoRectangle.class, 1);
classRegsitry.put(GeoStandardCircle.class, 2);
classRegsitry.put(GeoStandardPath.class, 3);
classRegsitry.put(GeoConvexPolygon.class, 4);
classRegsitry.put(GeoConcavePolygon.class, 5);
classRegsitry.put(GeoComplexPolygon.class, 6);
classRegsitry.put(GeoCompositePolygon.class, 7);
classRegsitry.put(GeoCompositeMembershipShape.class, 8);
classRegsitry.put(GeoCompositeAreaShape.class, 9);
classRegsitry.put(GeoDegeneratePoint.class, 10);
classRegsitry.put(GeoDegenerateHorizontalLine.class, 11);
classRegsitry.put(GeoDegenerateLatitudeZone.class, 12);
classRegsitry.put(GeoDegenerateLongitudeSlice.class, 13);
classRegsitry.put(GeoDegenerateVerticalLine.class, 14);
classRegsitry.put(GeoLatitudeZone.class, 15);
classRegsitry.put(GeoLongitudeSlice.class, 16);
classRegsitry.put(GeoNorthLatitudeZone.class, 17);
classRegsitry.put(GeoNorthRectangle.class, 18);
classRegsitry.put(GeoSouthLatitudeZone.class, 19);
classRegsitry.put(GeoSouthRectangle.class, 20);
classRegsitry.put(GeoWideDegenerateHorizontalLine.class, 21);
classRegsitry.put(GeoWideLongitudeSlice.class, 22);
classRegsitry.put(GeoWideNorthRectangle.class, 23);
classRegsitry.put(GeoWideRectangle.class, 24);
classRegsitry.put(GeoWideSouthRectangle.class, 25);
classRegsitry.put(GeoWorld.class, 26);
classRegsitry.put(dXdYdZSolid.class, 27);
classRegsitry.put(dXdYZSolid.class, 28);
classRegsitry.put(dXYdZSolid.class, 29);
classRegsitry.put(dXYZSolid.class, 30);
classRegsitry.put(XdYdZSolid.class, 31);
classRegsitry.put(XdYZSolid.class, 32);
classRegsitry.put(XYdZSolid.class, 33);
classRegsitry.put(StandardXYZSolid.class, 34);
classRegsitry.put(PlanetModel.class, 35);
for (Class<?> clazz : classRegsitry.keySet()){
codeRegsitry.put(classRegsitry.get(clazz), clazz);
}
}
}

View File

@ -30,12 +30,10 @@ import org.junit.Test;
public class RandomBinaryCodecTest extends RandomGeoShapeGenerator{ public class RandomBinaryCodecTest extends RandomGeoShapeGenerator{
@Test @Test
@Repeat(iterations = 100) @Repeat(iterations = 10)
public void testRandomShapeCodec() throws IOException{ public void testRandomPointCodec() throws IOException{
PlanetModel planetModel = randomPlanetModel(); PlanetModel planetModel = randomPlanetModel();
int type = randomShapeType(); GeoPoint shape = randomGeoPoint(planetModel, getEmptyConstraint());
GeoShape shape = randomGeoShape(type, planetModel);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
SerializableObject.writeObject(outputStream, shape); SerializableObject.writeObject(outputStream, shape);
ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
@ -43,5 +41,29 @@ public class RandomBinaryCodecTest extends RandomGeoShapeGenerator{
assertEquals(shape, shapeCopy); assertEquals(shape, shapeCopy);
} }
@Test
@Repeat(iterations = 100)
public void testRandomPlanetObjectCodec() throws IOException{
PlanetModel planetModel = randomPlanetModel();
int type = randomShapeType();
GeoShape shape = randomGeoShape(type, planetModel);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
SerializableObject.writePlanetObject(outputStream, shape);
ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
SerializableObject shapeCopy = SerializableObject.readPlanetObject(inputStream);
assertEquals(shape, shapeCopy);
}
@Test
@Repeat(iterations = 100)
public void testRandomShapeCodec() throws IOException{
PlanetModel planetModel = randomPlanetModel();
int type = randomShapeType();
GeoShape shape = randomGeoShape(type, planetModel);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
SerializableObject.writeObject(outputStream, shape);
ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
SerializableObject shapeCopy = SerializableObject.readObject(planetModel, inputStream);
assertEquals(shape, shapeCopy);
}
} }