Java源码示例:org.ehcache.spi.serialization.Serializer
示例1
@Test
public void parseServiceCreationConfiguration() throws SAXException, JAXBException, ParserConfigurationException, IOException, ClassNotFoundException {
Configuration xmlConfig = new XmlConfiguration(getClass().getResource("/configs/default-serializer.xml"));
assertThat(xmlConfig.getServiceCreationConfigurations()).hasSize(1);
ServiceCreationConfiguration<?, ?> configuration = xmlConfig.getServiceCreationConfigurations().iterator().next();
assertThat(configuration).isExactlyInstanceOf(DefaultSerializationProviderConfiguration.class);
DefaultSerializationProviderConfiguration factoryConfiguration = (DefaultSerializationProviderConfiguration) configuration;
Map<Class<?>, Class<? extends Serializer<?>>> defaultSerializers = factoryConfiguration.getDefaultSerializers();
assertThat(defaultSerializers).hasSize(4);
assertThat(defaultSerializers.get(CharSequence.class)).isEqualTo(TestSerializer.class);
assertThat(defaultSerializers.get(Number.class)).isEqualTo(TestSerializer2.class);
assertThat(defaultSerializers.get(Long.class)).isEqualTo(TestSerializer3.class);
assertThat(defaultSerializers.get(Integer.class)).isEqualTo(TestSerializer4.class);
}
示例2
@Test @SuppressWarnings("unchecked")
public void unparseServiceCreationConfiguration() {
DefaultSerializationProviderConfiguration providerConfig = new DefaultSerializationProviderConfiguration();
providerConfig.addSerializerFor(Description.class, (Class) TestSerializer3.class);
providerConfig.addSerializerFor(Person.class, (Class) TestSerializer4.class);
Configuration config = ConfigurationBuilder.newConfigurationBuilder().withService(providerConfig).build();
ConfigType configType = new DefaultSerializationProviderConfigurationParser().unparseServiceCreationConfiguration(config, new ConfigType());
List<SerializerType.Serializer> serializers = configType.getDefaultSerializers().getSerializer();
assertThat(serializers).hasSize(2);
serializers.forEach(serializer -> {
if (serializer.getType().equals(Description.class.getName())) {
assertThat(serializer.getValue()).isEqualTo(TestSerializer3.class.getName());
} else if (serializer.getType().equals(Person.class.getName())) {
assertThat(serializer.getValue()).isEqualTo(TestSerializer4.class.getName());
} else {
throw new AssertionError("Not expected");
}
});
}
示例3
/**
* Here we need to encode two objects of unknown size: the key and the value.
* Encoding should be done in such a way that the key and value can be read
* separately while decoding the bytes.
* So the way it is done here is by writing the size of the payload along with
* the payload. That is, the size of the key payload is written before the key
* itself. The value payload is written after that.
*
* While decoding, the size is read first and then reading the same number of
* bytes will get you the key payload. Whatever that is left is the value payload.
*/
@Override
public ByteBuffer encode(final Serializer<K> keySerializer, final Serializer<V> valueSerializer) {
ByteBuffer keyBuf = keySerializer.serialize(key);
ByteBuffer valueBuf = valueHolder.encode(valueSerializer);
int size = BYTE_SIZE_BYTES + // Operation type
INT_SIZE_BYTES + // Size of the key payload
LONG_SIZE_BYTES + // Size of expiration time stamp
keyBuf.remaining() + // the key payload itself
valueBuf.remaining(); // the value payload
ByteBuffer buffer = ByteBuffer.allocate(size);
buffer.put(getOpCode().getValue());
buffer.putLong(this.timeStamp);
buffer.putInt(keyBuf.remaining());
buffer.put(keyBuf);
buffer.put(valueBuf);
buffer.flip();
return buffer;
}
示例4
BaseKeyValueOperation(ByteBuffer buffer, Serializer<K> keySerializer, Serializer<V> valueSerializer) {
OperationCode opCode = OperationCode.valueOf(buffer.get());
if (opCode != getOpCode()) {
throw new IllegalArgumentException("Invalid operation: " + opCode);
}
this.timeStamp = buffer.getLong();
int keySize = buffer.getInt();
int maxLimit = buffer.limit();
buffer.limit(buffer.position() + keySize);
ByteBuffer keyBlob = buffer.slice();
buffer.position(buffer.limit());
buffer.limit(maxLimit);
try {
this.key = keySerializer.read(keyBlob);
} catch (ClassNotFoundException e) {
throw new CodecException(e);
}
this.valueHolder = new LazyValueHolder<>(buffer.slice(), valueSerializer);
}
示例5
private EhcacheConcurrentOffHeapClockCache<K, OffHeapValueHolder<V>> createBackingMap(long size, Serializer<K> keySerializer, Serializer<V> valueSerializer, SwitchableEvictionAdvisor<K, OffHeapValueHolder<V>> evictionAdvisor) {
HeuristicConfiguration config = new HeuristicConfiguration(size);
PageSource source = new UpfrontAllocatingPageSource(getBufferSource(), config.getMaximumSize(), config.getMaximumChunkSize(), config.getMinimumChunkSize());
Portability<K> keyPortability = new SerializerPortability<>(keySerializer);
Portability<OffHeapValueHolder<V>> valuePortability = createValuePortability(valueSerializer);
Factory<OffHeapBufferStorageEngine<K, OffHeapValueHolder<V>>> storageEngineFactory = OffHeapBufferStorageEngine.createFactory(PointerSize.INT, source, config
.getSegmentDataPageSize(), keyPortability, valuePortability, false, true);
Factory<? extends PinnableSegment<K, OffHeapValueHolder<V>>> segmentFactory = new EhcacheSegmentFactory<>(
source,
storageEngineFactory,
config.getInitialSegmentTableSize(),
evictionAdvisor,
mapEvictionListener);
return new EhcacheConcurrentOffHeapClockCache<>(evictionAdvisor, segmentFactory, config.getConcurrency());
}
示例6
private EhcachePersistentConcurrentOffHeapClockCache<K, OffHeapValueHolder<V>> createBackingMap(long size, Serializer<K> keySerializer, Serializer<V> valueSerializer, SwitchableEvictionAdvisor<K, OffHeapValueHolder<V>> evictionAdvisor) throws IOException {
File metadataFile = getMetadataFile();
try (FileOutputStream fos = new FileOutputStream(metadataFile)) {
Properties properties = new Properties();
properties.put(KEY_TYPE_PROPERTY_NAME, keyType.getName());
properties.put(VALUE_TYPE_PROPERTY_NAME, valueType.getName());
properties.store(fos, "Key and value types");
}
MappedPageSource source = new MappedPageSource(getDataFile(), size);
PersistentPortability<K> keyPortability = persistent(new SerializerPortability<>(keySerializer));
PersistentPortability<OffHeapValueHolder<V>> valuePortability = persistent(createValuePortability(valueSerializer));
DiskWriteThreadPool writeWorkers = new DiskWriteThreadPool(executionService, threadPoolAlias, writerConcurrency);
Factory<FileBackedStorageEngine<K, OffHeapValueHolder<V>>> storageEngineFactory = FileBackedStorageEngine.createFactory(source,
max((size / diskSegments) / 10, 1024), BYTES, keyPortability, valuePortability, writeWorkers, true);
EhcachePersistentSegmentFactory<K, OffHeapValueHolder<V>> factory = new EhcachePersistentSegmentFactory<>(
source,
storageEngineFactory,
64,
evictionAdvisor,
mapEvictionListener, true);
return new EhcachePersistentConcurrentOffHeapClockCache<>(evictionAdvisor, factory, diskSegments);
}
示例7
/**
* Adds a new {@link Serializer} mapping for the class {@code serializableClass}
*
* @param serializableClass the {@code Class} to add the mapping for
* @param serializerClass the {@link Serializer} type to use
* @param overwrite indicates if an existing mapping is to be overwritten
* @param <T> the type of instances to be serialized / deserialized
* @return this configuration object
*
* @throws NullPointerException if any argument is null
* @throws IllegalArgumentException if a mapping for {@code serializableClass} already exists and {@code overwrite} is {@code false}
*/
public <T> DefaultSerializationProviderConfiguration addSerializerFor(Class<T> serializableClass, Class<? extends Serializer<T>> serializerClass, boolean overwrite) {
if (serializableClass == null) {
throw new NullPointerException("Serializable class cannot be null");
}
if (serializerClass == null) {
throw new NullPointerException("Serializer class cannot be null");
}
if(!isConstructorPresent(serializerClass, ClassLoader.class)) {
throw new IllegalArgumentException("The serializer: " + serializerClass.getName() + " does not have a constructor that takes in a ClassLoader.");
}
if (isConstructorPresent(serializerClass, ClassLoader.class, FileBasedPersistenceContext.class)) {
LOGGER.warn(serializerClass.getName() + " class has a constructor that takes in a FileBasedPersistenceContext. " +
"Support for this constructor has been removed since version 3.2. Consider removing it.");
}
if (defaultSerializers.containsKey(serializableClass) && !overwrite) {
throw new IllegalArgumentException("Duplicate serializer for class : " + serializableClass.getName());
} else {
defaultSerializers.put(serializableClass, serializerClass);
}
return this;
}
示例8
@Override
public ByteBuffer encode(final Serializer<K> keySerializer, final Serializer<V> valueSerializer) {
ByteBuffer keyBuf = keySerializer.serialize(key);
ByteBuffer oldValueBuf = oldValueHolder.encode(valueSerializer);
ByteBuffer valueBuf = newValueHolder.encode(valueSerializer);
ByteBuffer buffer = ByteBuffer.allocate(BYTE_SIZE_BYTES + // Operation type
INT_SIZE_BYTES + // Size of the key payload
LONG_SIZE_BYTES + // Size of expiration time stamp
keyBuf.remaining() + // the key payload itself
INT_SIZE_BYTES + // Size of the old value payload
oldValueBuf.remaining() + // The old value payload itself
valueBuf.remaining()); // The value payload itself
buffer.put(getOpCode().getValue());
buffer.putLong(this.timeStamp);
buffer.putInt(keyBuf.remaining());
buffer.put(keyBuf);
buffer.putInt(oldValueBuf.remaining());
buffer.put(oldValueBuf);
buffer.put(valueBuf);
buffer.flip();
return buffer;
}
示例9
@Test
public void testTransientMinimalStatefulSerializer() throws Exception {
DefaultSerializationProvider provider = new DefaultSerializationProvider(null);
provider.start(providerContaining());
MinimalStatefulSerializer.baseConstructorInvoked = false;
@SuppressWarnings("unchecked")
Class<Serializer<Object>> serializerClass = (Class) MinimalStatefulSerializer.class;
DefaultSerializerConfiguration<Object> configuration = new DefaultSerializerConfiguration<>(serializerClass, DefaultSerializerConfiguration.Type.VALUE);
Serializer<Object> valueSerializer =
provider.createValueSerializer(Object.class, ClassLoader.getSystemClassLoader(), configuration);
assertThat(valueSerializer, instanceOf(MinimalStatefulSerializer.class));
assertThat(MinimalStatefulSerializer.baseConstructorInvoked, is(true));
}
示例10
@Override
public ByteBuffer encode(final Serializer<K> keySerializer, final Serializer<V> valueSerializer) {
ByteBuffer keyBuf = keySerializer.serialize(key);
int size = BYTE_SIZE_BYTES + // Operation type
LONG_SIZE_BYTES + // Size of expiration time stamp
keyBuf.remaining(); // the key payload itself
ByteBuffer buffer = ByteBuffer.allocate(size);
buffer.put(getOpCode().getValue());
buffer.putLong(this.timeStamp);
buffer.put(keyBuf);
buffer.flip();
return buffer;
}
示例11
@Test
public void testCreateTransientSerializerWithoutConstructor() throws Exception {
expectedException.expect(RuntimeException.class);
expectedException.expectMessage("does not have a constructor that takes in a ClassLoader.");
DefaultSerializationProvider provider = new DefaultSerializationProvider(null);
provider.start(providerContaining());
@SuppressWarnings("unchecked")
Class<Serializer<Object>> serializerClass = (Class) BaseSerializer.class;
DefaultSerializerConfiguration<Object> configuration = new DefaultSerializerConfiguration<>(serializerClass, DefaultSerializerConfiguration.Type.VALUE);
provider.createValueSerializer(Object.class, ClassLoader.getSystemClassLoader(), configuration);
}
示例12
private void testSerializerWithByValueHeapCache(Serializer<Long> serializer) throws Exception {
CacheManagerBuilder<CacheManager> cmBuilder =
newCacheManagerBuilder()
.withCache("heapByValueCache",
newCacheConfigurationBuilder(Long.class, Person.class, newResourcePoolsBuilder().heap(10, EntryUnit.ENTRIES))
.withKeyCopier(SerializingCopier.<Long>asCopierClass())
.withKeySerializer(serializer)
);
cmBuilder.build(true);
}
示例13
private void testSerializerWithOffheapCache(Serializer<Long> serializer) throws Exception {
CacheManagerBuilder<CacheManager> cmBuilder =
newCacheManagerBuilder()
.withCache("offheapCache",
newCacheConfigurationBuilder(Long.class, Person.class, newResourcePoolsBuilder().offheap(2, MemoryUnit.MB))
.withKeySerializer(serializer)
);
cmBuilder.build(true);
}
示例14
private void testSerializerWithHeapOffheapCache(Serializer<Long> serializer) throws Exception {
CacheManagerBuilder<CacheManager> cmBuilder =
newCacheManagerBuilder()
.withCache("heapOffheapCache",
newCacheConfigurationBuilder(Long.class, Person.class, newResourcePoolsBuilder().heap(10, EntryUnit.ENTRIES).offheap(2, MemoryUnit.MB))
.withKeySerializer(serializer)
);
cmBuilder.build(true);
}
示例15
private void testSerializerWithHeapDiskCache(Serializer<Long> serializer) throws Exception {
CacheManagerBuilder<PersistentCacheManager> cmBuilder =
newCacheManagerBuilder()
.with(persistence(temporaryFolder.newFolder().getAbsolutePath()))
.withCache("heapDiskCache",
newCacheConfigurationBuilder(Long.class, Person.class, newResourcePoolsBuilder().heap(10, EntryUnit.ENTRIES).disk(8, MemoryUnit.MB, true))
.withKeySerializer(serializer)
);
cmBuilder.build(true);
}
示例16
protected SerializableEntry(Entry<K> entry, Serializer<K> keySerializer) {
this.state = entry.state;
this.heuristic = entry.heuristic;
this.serializedKeys = new ArrayList<>();
for (K key : entry.keys) {
ByteBuffer byteBuffer = keySerializer.serialize(key);
byte[] bytes = new byte[byteBuffer.remaining()];
byteBuffer.get(bytes);
this.serializedKeys.add(bytes);
}
}
示例17
protected Collection<K> deserializeKeys(Serializer<K> keySerializer) throws ClassNotFoundException {
Collection<K> result = new ArrayList<>();
for (byte[] serializedKey : serializedKeys) {
K key = keySerializer.read(ByteBuffer.wrap(serializedKey));
result.add(key);
}
return result;
}
示例18
public PersistentJournal(File directory, Serializer<K> keySerializer) {
if (directory == null) {
throw new NullPointerException("directory must not be null");
}
if (keySerializer == null) {
throw new NullPointerException("keySerializer must not be null");
}
this.directory = directory;
this.keySerializer = keySerializer;
}
示例19
@Test
public void testTransientLegacySerializer() throws Exception {
expectedException.expect(RuntimeException.class);
expectedException.expectMessage("does not have a constructor that takes in a ClassLoader.");
DefaultSerializationProvider provider = new DefaultSerializationProvider(null);
provider.start(providerContaining());
@SuppressWarnings("unchecked")
Class<Serializer<Object>> serializerClass = (Class) LegacySerializer.class;
DefaultSerializerConfiguration<Object> configuration = new DefaultSerializerConfiguration<>(serializerClass, DefaultSerializerConfiguration.Type.VALUE);
provider.createValueSerializer(Object.class, ClassLoader.getSystemClassLoader(), configuration);
}
示例20
protected SoftLock<V> copyAfterDeserialization(Serializer<V> valueSerializer, SoftLock<V> serializedSoftLock) throws ClassNotFoundException {
V oldValue = null;
if (serializedSoftLock.oldValueSerialized != null) {
oldValue = valueSerializer.read(ByteBuffer.wrap(serializedSoftLock.oldValueSerialized));
}
XAValueHolder<V> newValueHolder = null;
if (this.newValueHolder != null) {
newValueHolder = this.newValueHolder.copyAfterDeserialization(valueSerializer);
}
return new SoftLock<>(transactionId, oldValue, newValueHolder);
}
示例21
@Override
protected OffHeapDiskStore<String, byte[]> createAndInitStore(TimeSource timeSource, ExpiryPolicy<? super String, ? super byte[]> expiry, EvictionAdvisor<? super String, ? super byte[]> evictionAdvisor) {
try {
SerializationProvider serializationProvider = new DefaultSerializationProvider(null);
serializationProvider.start(providerContaining(diskResourceService));
ClassLoader classLoader = getClass().getClassLoader();
Serializer<String> keySerializer = serializationProvider.createKeySerializer(String.class, classLoader);
Serializer<byte[]> valueSerializer = serializationProvider.createValueSerializer(byte[].class, classLoader);
StoreConfigurationImpl<String, byte[]> storeConfiguration = new StoreConfigurationImpl<>(String.class, byte[].class,
evictionAdvisor, getClass().getClassLoader(), expiry, null, 0, true, keySerializer, valueSerializer, null, false);
OffHeapDiskStore<String, byte[]> offHeapStore = new OffHeapDiskStore<String, byte[]>(
getPersistenceContext(),
new OnDemandExecutionService(), null, DEFAULT_WRITER_CONCURRENCY, DEFAULT_DISK_SEGMENTS,
storeConfiguration, timeSource,
new TestStoreEventDispatcher<>(),
MB.toBytes(1), new DefaultStatisticsService()) {
@Override
protected OffHeapValueHolderPortability<byte[]> createValuePortability(Serializer<byte[]> serializer) {
return new AssertingOffHeapValueHolderPortability<>(serializer);
}
};
OffHeapDiskStore.Provider.init(offHeapStore);
return offHeapStore;
} catch (UnsupportedTypeException e) {
throw new AssertionError(e);
}
}
示例22
@Test
public void testDefaultCharSerializer() throws Exception {
DefaultSerializationProvider provider = getStartedProvider();
Serializer<Character> keySerializer = provider.createKeySerializer(Character.class, getSystemClassLoader());
assertThat(keySerializer, instanceOf(CharSerializer.class));
keySerializer = provider.createKeySerializer(Character.class, getSystemClassLoader(), getPersistenceSpaceIdentifierMock());
assertThat(keySerializer, instanceOf(CharSerializer.class));
}
示例23
@Test
public void testDefaultFloatSerializer() throws Exception {
DefaultSerializationProvider provider = getStartedProvider();
Serializer<Float> keySerializer = provider.createKeySerializer(Float.class, getSystemClassLoader());
assertThat(keySerializer, instanceOf(FloatSerializer.class));
keySerializer = provider.createKeySerializer(Float.class, getSystemClassLoader(), getPersistenceSpaceIdentifierMock());
assertThat(keySerializer, instanceOf(FloatSerializer.class));
}
示例24
public DefaultSerializationProvider(DefaultSerializationProviderConfiguration configuration) {
if (configuration != null) {
this.serializers = new LinkedHashMap<>(configuration.getDefaultSerializers());
} else {
this.serializers = new LinkedHashMap<>(Collections.<Class<?>, Class<? extends Serializer<?>>>emptyMap());
}
}
示例25
@Test
public void testUserProvidedCloseableCopierInstanceDoesNotCloseOnRelease() throws Exception {
DefaultCopyProvider copyProvider = new DefaultCopyProvider(null);
TestCloseableCopier<Long> testCloseableCopier = new TestCloseableCopier<>();
DefaultCopierConfiguration<Long> config = new DefaultCopierConfiguration<>(testCloseableCopier, DefaultCopierConfiguration.Type.KEY);
@SuppressWarnings("unchecked")
Serializer<Long> serializer = mock(Serializer.class);
assertThat(copyProvider.createKeyCopier(Long.class, serializer, config), sameInstance((Copier)testCloseableCopier));
copyProvider.releaseCopier(testCloseableCopier);
assertFalse(testCloseableCopier.isInvoked());
}
示例26
@Override
public <T> Serializer<T> createValueSerializer(Class<T> clazz, ClassLoader classLoader, ServiceConfiguration<?, ?>... configs) throws UnsupportedTypeException {
DefaultSerializerConfiguration<T> configuration = find(DefaultSerializerConfiguration.Type.VALUE, configs);
Serializer<T> serializer = getUserProvidedSerializer(configuration);
if (serializer == null) {
serializer = createSerializer(clazz, classLoader, configuration, configs);
instantiated.add(serializer);
}
updateProvidedInstanceCounts(serializer);
return serializer;
}
示例27
@Test
public void testDefaultStringSerializer() throws Exception {
DefaultSerializationProvider provider = getStartedProvider();
Serializer<String> keySerializer = provider.createKeySerializer(String.class, getSystemClassLoader());
assertThat(keySerializer, instanceOf(StringSerializer.class));
keySerializer = provider.createKeySerializer(String.class, getSystemClassLoader(), getPersistenceSpaceIdentifierMock());
assertThat(keySerializer, instanceOf(StringSerializer.class));
}
示例28
private static <T> Serializer<T> getUserProvidedSerializer(DefaultSerializerConfiguration<T> conf) {
if(conf != null) {
Serializer<T> instance = conf.getInstance();
if(instance != null) {
return instance;
}
}
return null;
}
示例29
private EhcachePersistentSegmentFactory.EhcachePersistentSegment<String, String> createTestSegmentWithAdvisorAndListener(final EvictionAdvisor<? super String, ? super String> evictionPredicate, EvictionListener<String, String> evictionListener) throws IOException {
try {
HeuristicConfiguration configuration = new HeuristicConfiguration(1024 * 1024);
SerializationProvider serializationProvider = new DefaultSerializationProvider(null);
serializationProvider.start(providerContaining());
MappedPageSource pageSource = new MappedPageSource(folder.newFile(), true, configuration.getMaximumSize());
Serializer<String> keySerializer = serializationProvider.createKeySerializer(String.class, EhcachePersistentSegmentTest.class.getClassLoader());
Serializer<String> valueSerializer = serializationProvider.createValueSerializer(String.class, EhcachePersistentSegmentTest.class.getClassLoader());
PersistentPortability<String> keyPortability = persistent(new SerializerPortability<>(keySerializer));
PersistentPortability<String> elementPortability = persistent(new SerializerPortability<>(valueSerializer));
Factory<FileBackedStorageEngine<String, String>> storageEngineFactory = FileBackedStorageEngine.createFactory(pageSource, configuration.getMaximumSize() / 10, BYTES, keyPortability, elementPortability);
SwitchableEvictionAdvisor<String, String> wrappedEvictionAdvisor = new SwitchableEvictionAdvisor<String, String>() {
private volatile boolean enabled = true;
@Override
public boolean adviseAgainstEviction(String key, String value) {
return evictionPredicate.adviseAgainstEviction(key, value);
}
@Override
public boolean isSwitchedOn() {
return enabled;
}
@Override
public void setSwitchedOn(boolean switchedOn) {
this.enabled = switchedOn;
}
};
return new EhcachePersistentSegmentFactory.EhcachePersistentSegment<>(pageSource, storageEngineFactory.newInstance(), 1, true, wrappedEvictionAdvisor, evictionListener);
} catch (UnsupportedTypeException e) {
throw new AssertionError(e);
}
}
示例30
/**
* Creates a new copier that will using the provided {@link Serializer}.
*
* @param serializer the serializer to use
*/
public SerializingCopier(Serializer<T> serializer) {
if (serializer == null) {
throw new NullPointerException("A " + SerializingCopier.class.getName() + " instance requires a "
+ Serializer.class.getName() + " instance to copy!");
}
this.serializer = serializer;
}