Skip to content

Commit

Permalink
Auto-select most optimal index externalizer wherever possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
pferraro committed Dec 5, 2016
1 parent 746c472 commit f9ce3e2
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 27 deletions.
Expand Up @@ -27,8 +27,6 @@
import java.util.List; import java.util.List;
import java.util.ServiceLoader; import java.util.ServiceLoader;


import org.wildfly.clustering.marshalling.Externalizer;
import org.wildfly.clustering.marshalling.spi.IndexExternalizer;
import org.wildfly.clustering.marshalling.spi.MarshalledValue; import org.wildfly.clustering.marshalling.spi.MarshalledValue;


/** /**
Expand All @@ -38,11 +36,7 @@
public class DynamicClassTable extends SimpleClassTable { public class DynamicClassTable extends SimpleClassTable {


public DynamicClassTable(ClassLoader loader) { public DynamicClassTable(ClassLoader loader) {
this(IndexExternalizer.VARIABLE, loader); super(findClasses(loader));
}

public DynamicClassTable(Externalizer<Integer> indexExternalizer, ClassLoader loader) {
super(indexExternalizer, findClasses(loader));
} }


private static Class<?>[] findClasses(ClassLoader loader) { private static Class<?>[] findClasses(ClassLoader loader) {
Expand Down
Expand Up @@ -43,18 +43,22 @@ public class ExternalizerObjectTable implements ObjectTable {


private final Externalizer<?>[] externalizers; private final Externalizer<?>[] externalizers;
private final Map<Class<?>, Writer> writers = new IdentityHashMap<>(); private final Map<Class<?>, Writer> writers = new IdentityHashMap<>();
final Externalizer<Integer> indexExternalizer; private final Externalizer<Integer> indexExternalizer;


public ExternalizerObjectTable(ClassLoader loader) { public ExternalizerObjectTable(ClassLoader loader) {
this(IndexExternalizer.VARIABLE, Stream.concat(loadExternalizers(ExternalizerObjectTable.class.getClassLoader()), loadExternalizers(loader)).toArray(Externalizer[]::new)); this(Stream.concat(loadExternalizers(ExternalizerObjectTable.class.getClassLoader()), loadExternalizers(loader)).toArray(Externalizer[]::new));
} }


@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
private static Stream<Externalizer> loadExternalizers(ClassLoader loader) { private static Stream<Externalizer> loadExternalizers(ClassLoader loader) {
return StreamSupport.stream(ServiceLoader.load(Externalizer.class, loader).spliterator(), false); return StreamSupport.stream(ServiceLoader.load(Externalizer.class, loader).spliterator(), false);
} }


public ExternalizerObjectTable(Externalizer<Integer> indexExternalizer, Externalizer<?>... externalizers) { public ExternalizerObjectTable(Externalizer<?>... externalizers) {
this(IndexExternalizer.select(externalizers.length), externalizers);
}

private ExternalizerObjectTable(Externalizer<Integer> indexExternalizer, Externalizer<?>... externalizers) {
this.indexExternalizer = indexExternalizer; this.indexExternalizer = indexExternalizer;
this.externalizers = externalizers; this.externalizers = externalizers;
for (int i = 0; i < externalizers.length; ++i) { for (int i = 0; i < externalizers.length; ++i) {
Expand All @@ -66,7 +70,7 @@ public ExternalizerObjectTable(Externalizer<Integer> indexExternalizer, External
Writer writer = new Writer() { Writer writer = new Writer() {
@Override @Override
public void writeObject(Marshaller marshaller, Object object) throws IOException { public void writeObject(Marshaller marshaller, Object object) throws IOException {
ExternalizerObjectTable.this.indexExternalizer.writeObject(marshaller, index); indexExternalizer.writeObject(marshaller, index);
externalizer.writeObject(marshaller, object); externalizer.writeObject(marshaller, object);
} }
}; };
Expand Down
Expand Up @@ -29,6 +29,7 @@
import org.jboss.marshalling.Marshaller; import org.jboss.marshalling.Marshaller;
import org.jboss.marshalling.Unmarshaller; import org.jboss.marshalling.Unmarshaller;
import org.wildfly.clustering.marshalling.Externalizer; import org.wildfly.clustering.marshalling.Externalizer;
import org.wildfly.clustering.marshalling.spi.IndexExternalizer;


/** /**
* Simple {@link ClassTable} implementation based on an array of recognized classes. * Simple {@link ClassTable} implementation based on an array of recognized classes.
Expand All @@ -38,17 +39,21 @@ public class SimpleClassTable implements ClassTable {


private final Class<?>[] classes; private final Class<?>[] classes;
private final Map<Class<?>, Writer> writers = new IdentityHashMap<>(); private final Map<Class<?>, Writer> writers = new IdentityHashMap<>();
final Externalizer<Integer> indexExternalizer; private final Externalizer<Integer> indexExternalizer;


public SimpleClassTable(Externalizer<Integer> indexExternalizer, Class<?>... classes) { public SimpleClassTable(Class<?>... classes) {
this(IndexExternalizer.select(classes.length), classes);
}

private SimpleClassTable(Externalizer<Integer> indexExternalizer, Class<?>... classes) {
this.indexExternalizer = indexExternalizer; this.indexExternalizer = indexExternalizer;
this.classes = classes; this.classes = classes;
for (int i = 0; i < classes.length; i++) { for (int i = 0; i < classes.length; i++) {
final int index = i; final int index = i;
Writer writer = new Writer() { Writer writer = new Writer() {
@Override @Override
public void writeClass(Marshaller output, Class<?> clazz) throws IOException { public void writeClass(Marshaller output, Class<?> clazz) throws IOException {
SimpleClassTable.this.indexExternalizer.writeObject(output, index); indexExternalizer.writeObject(output, index);
} }
}; };
this.writers.put(classes[i], writer); this.writers.put(classes[i], writer);
Expand Down
Expand Up @@ -29,26 +29,27 @@
import org.wildfly.clustering.marshalling.Externalizer; import org.wildfly.clustering.marshalling.Externalizer;


/** /**
* Base {@link Externalizer} for enums with fewer than 256 constants. * Base {@link Externalizer} for enumerations.
* @author Paul Ferraro * @author Paul Ferraro
*/ */
public class EnumExternalizer<E extends Enum<E>> implements Externalizer<E> { public class EnumExternalizer<E extends Enum<E>> implements Externalizer<E> {
private static final Externalizer<Integer> ORDINAL_EXTERNALIZER = IndexExternalizer.UNSIGNED_BYTE;


private final Externalizer<Integer> ordinalExternalizer;
private final Class<E> enumClass; private final Class<E> enumClass;


public EnumExternalizer(Class<E> enumClass) { public EnumExternalizer(Class<E> enumClass) {
this.ordinalExternalizer = IndexExternalizer.select(enumClass.getEnumConstants().length);
this.enumClass = enumClass; this.enumClass = enumClass;
} }


@Override @Override
public void writeObject(ObjectOutput output, E value) throws IOException { public void writeObject(ObjectOutput output, E value) throws IOException {
ORDINAL_EXTERNALIZER.writeObject(output, value.ordinal()); this.ordinalExternalizer.writeObject(output, value.ordinal());
} }


@Override @Override
public E readObject(ObjectInput input) throws IOException, ClassNotFoundException { public E readObject(ObjectInput input) throws IOException, ClassNotFoundException {
return this.enumClass.getEnumConstants()[ORDINAL_EXTERNALIZER.readObject(input)]; return this.enumClass.getEnumConstants()[this.ordinalExternalizer.readObject(input)];
} }


@Override @Override
Expand Down
Expand Up @@ -106,6 +106,17 @@ public void writeData(DataOutput output, int index) throws IOException {
}, },
; ;


/**
* Returns the most efficient externalizer for a given index size.
* @param size the size of the index
* @return an index externalizer
*/
public static final Externalizer<Integer> select(int size) {
if (size <= (Byte.MAX_VALUE - Byte.MIN_VALUE)) return UNSIGNED_BYTE;
if (size <= (Short.MAX_VALUE - Short.MIN_VALUE)) return UNSIGNED_SHORT;
return VARIABLE;
}

/** /**
* Read an index from the specified input stream. * Read an index from the specified input stream.
* @param input a data input stream * @param input a data input stream
Expand Down
Expand Up @@ -50,7 +50,6 @@
import org.wildfly.clustering.marshalling.jboss.MarshallingContext; import org.wildfly.clustering.marshalling.jboss.MarshallingContext;
import org.wildfly.clustering.marshalling.jboss.SimpleMarshallingConfigurationRepository; import org.wildfly.clustering.marshalling.jboss.SimpleMarshallingConfigurationRepository;
import org.wildfly.clustering.marshalling.jboss.SimpleMarshallingContextFactory; import org.wildfly.clustering.marshalling.jboss.SimpleMarshallingContextFactory;
import org.wildfly.clustering.marshalling.spi.IndexExternalizer;
import org.wildfly.clustering.server.group.JGroupsNodeFactory; import org.wildfly.clustering.server.group.JGroupsNodeFactory;
import org.wildfly.clustering.service.AsynchronousServiceBuilder; import org.wildfly.clustering.service.AsynchronousServiceBuilder;
import org.wildfly.clustering.service.Builder; import org.wildfly.clustering.service.Builder;
Expand All @@ -71,7 +70,7 @@ enum MarshallingVersion implements Function<MarshallingConfigurationContext, Mar
public MarshallingConfiguration apply(MarshallingConfigurationContext context) { public MarshallingConfiguration apply(MarshallingConfigurationContext context) {
MarshallingConfiguration config = new MarshallingConfiguration(); MarshallingConfiguration config = new MarshallingConfiguration();
config.setClassResolver(ModularClassResolver.getInstance(context.getModuleLoader())); config.setClassResolver(ModularClassResolver.getInstance(context.getModuleLoader()));
config.setClassTable(new DynamicClassTable(IndexExternalizer.UNSIGNED_BYTE, context.getModule().getClassLoader())); config.setClassTable(new DynamicClassTable(context.getModule().getClassLoader()));
return config; return config;
} }
}, },
Expand Down
Expand Up @@ -47,7 +47,6 @@
import org.wildfly.clustering.marshalling.jboss.SimpleMarshalledValueFactory; import org.wildfly.clustering.marshalling.jboss.SimpleMarshalledValueFactory;
import org.wildfly.clustering.marshalling.jboss.SimpleMarshallingConfigurationRepository; import org.wildfly.clustering.marshalling.jboss.SimpleMarshallingConfigurationRepository;
import org.wildfly.clustering.marshalling.jboss.SimpleMarshallingContextFactory; import org.wildfly.clustering.marshalling.jboss.SimpleMarshallingContextFactory;
import org.wildfly.clustering.marshalling.spi.IndexExternalizer;
import org.wildfly.clustering.marshalling.spi.MarshalledValueFactory; import org.wildfly.clustering.marshalling.spi.MarshalledValueFactory;
import org.wildfly.clustering.service.Builder; import org.wildfly.clustering.service.Builder;
import org.wildfly.clustering.web.session.SessionManagerFactoryBuilderProvider; import org.wildfly.clustering.web.session.SessionManagerFactoryBuilderProvider;
Expand Down Expand Up @@ -81,7 +80,7 @@ enum MarshallingVersion implements Function<Module, MarshallingConfiguration> {
public MarshallingConfiguration apply(Module module) { public MarshallingConfiguration apply(Module module) {
MarshallingConfiguration config = new MarshallingConfiguration(); MarshallingConfiguration config = new MarshallingConfiguration();
config.setClassResolver(ModularClassResolver.getInstance(module.getModuleLoader())); config.setClassResolver(ModularClassResolver.getInstance(module.getModuleLoader()));
config.setClassTable(new SimpleClassTable(IndexExternalizer.UNSIGNED_BYTE, Serializable.class, Externalizable.class)); config.setClassTable(new SimpleClassTable(Serializable.class, Externalizable.class));
return config; return config;
} }
}, },
Expand All @@ -90,7 +89,7 @@ public MarshallingConfiguration apply(Module module) {
public MarshallingConfiguration apply(Module module) { public MarshallingConfiguration apply(Module module) {
MarshallingConfiguration config = new MarshallingConfiguration(); MarshallingConfiguration config = new MarshallingConfiguration();
config.setClassResolver(ModularClassResolver.getInstance(module.getModuleLoader())); config.setClassResolver(ModularClassResolver.getInstance(module.getModuleLoader()));
config.setClassTable(new SimpleClassTable(IndexExternalizer.UNSIGNED_BYTE, Serializable.class, Externalizable.class)); config.setClassTable(new SimpleClassTable(Serializable.class, Externalizable.class));
config.setObjectTable(new ExternalizerObjectTable(module.getClassLoader())); config.setObjectTable(new ExternalizerObjectTable(module.getClassLoader()));
return config; return config;
} }
Expand Down
Expand Up @@ -43,7 +43,6 @@
import org.wildfly.clustering.marshalling.jboss.SimpleMarshalledValueFactory; import org.wildfly.clustering.marshalling.jboss.SimpleMarshalledValueFactory;
import org.wildfly.clustering.marshalling.jboss.SimpleMarshallingConfigurationRepository; import org.wildfly.clustering.marshalling.jboss.SimpleMarshallingConfigurationRepository;
import org.wildfly.clustering.marshalling.jboss.SimpleMarshallingContextFactory; import org.wildfly.clustering.marshalling.jboss.SimpleMarshallingContextFactory;
import org.wildfly.clustering.marshalling.spi.IndexExternalizer;
import org.wildfly.clustering.marshalling.spi.MarshalledValueFactory; import org.wildfly.clustering.marshalling.spi.MarshalledValueFactory;
import org.wildfly.clustering.service.Builder; import org.wildfly.clustering.service.Builder;
import org.wildfly.clustering.web.IdentifierFactory; import org.wildfly.clustering.web.IdentifierFactory;
Expand All @@ -67,7 +66,7 @@ enum MarshallingVersion implements Function<ModuleLoader, MarshallingConfigurati
public MarshallingConfiguration apply(ModuleLoader loader) { public MarshallingConfiguration apply(ModuleLoader loader) {
MarshallingConfiguration config = new MarshallingConfiguration(); MarshallingConfiguration config = new MarshallingConfiguration();
config.setClassResolver(ModularClassResolver.getInstance(loader)); config.setClassResolver(ModularClassResolver.getInstance(loader));
config.setClassTable(new SimpleClassTable(IndexExternalizer.UNSIGNED_BYTE, Serializable.class, Externalizable.class)); config.setClassTable(new SimpleClassTable(Serializable.class, Externalizable.class));
return config; return config;
} }
}, },
Expand Down
Expand Up @@ -44,7 +44,6 @@
import org.jboss.as.naming.ValueManagedReferenceFactory; import org.jboss.as.naming.ValueManagedReferenceFactory;
import org.jboss.ejb.client.SessionID; import org.jboss.ejb.client.SessionID;
import org.wildfly.clustering.marshalling.jboss.SimpleClassTable; import org.wildfly.clustering.marshalling.jboss.SimpleClassTable;
import org.wildfly.clustering.marshalling.spi.IndexExternalizer;


/** /**
* @author Paul Ferraro * @author Paul Ferraro
Expand Down Expand Up @@ -79,6 +78,6 @@ public class StatefulSessionBeanClassTable extends SimpleClassTable {
}; };


public StatefulSessionBeanClassTable() { public StatefulSessionBeanClassTable() {
super(IndexExternalizer.UNSIGNED_BYTE, classes); super(classes);
} }
} }

0 comments on commit f9ce3e2

Please sign in to comment.