All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.store.storage.restadapter.types.ViewerBinaryTypeHandlerManager Maven / Gradle / Ivy

package org.eclipse.store.storage.restadapter.types;

import java.util.function.BiConsumer;

/*-
 * #%L
 * EclipseStore Storage REST Adapter
 * %%
 * Copyright (C) 2023 MicroStream Software
 * %%
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 * 
 * SPDX-License-Identifier: EPL-2.0
 * #L%
 */

import java.util.function.Consumer;

import org.eclipse.serializer.collections.EqHashTable;
import org.eclipse.serializer.collections.types.XGettingEnum;
import org.eclipse.serializer.collections.types.XGettingSequence;
import org.eclipse.serializer.collections.types.XGettingTable;
import org.eclipse.serializer.persistence.binary.types.Binary;
import org.eclipse.serializer.persistence.binary.types.BinaryPersistence;
import org.eclipse.serializer.persistence.exceptions.PersistenceExceptionConsistency;
import org.eclipse.serializer.persistence.types.PersistenceLegacyTypeHandler;
import org.eclipse.serializer.persistence.types.PersistenceManager;
import org.eclipse.serializer.persistence.types.PersistenceStoring;
import org.eclipse.serializer.persistence.types.PersistenceTypeDefinition;
import org.eclipse.serializer.persistence.types.PersistenceTypeDictionary;
import org.eclipse.serializer.persistence.types.PersistenceTypeHandler;
import org.eclipse.serializer.persistence.types.PersistenceTypeHandlerManager;
import org.eclipse.serializer.persistence.types.PersistenceTypeLink;
import org.eclipse.serializer.reference.Referencing;
import org.eclipse.serializer.reflect.XReflect;
import org.eclipse.serializer.typing.KeyValue;

public class ViewerBinaryTypeHandlerManager implements PersistenceTypeHandlerManager, Referencing>
{
	///////////////////////////////////////////////////////////////////////////
	// instance fields //
	////////////////////

	private final PersistenceTypeDictionary typeDictionary;
	private final EqHashTable> viewerTypeHandlers = EqHashTable.New();
	private final XGettingSequence> nativeHandlers;

	///////////////////////////////////////////////////////////////////////////
	// constructors //
	/////////////////

	public ViewerBinaryTypeHandlerManager(final PersistenceManager persistenceManager)
	{
		super();

		this.typeDictionary = persistenceManager.typeDictionary();
		this.nativeHandlers = BinaryPersistence.createNativeHandlersValueTypes(this, null, null);

		//initialize generic handlers
		for (final PersistenceTypeHandler persistenceTypeHandler : this.nativeHandlers)
		{
			final PersistenceTypeDefinition typeDefinition = this.typeDictionary
				.lookupTypeByName(persistenceTypeHandler.typeName());
			if(typeDefinition != null)
			{
				persistenceTypeHandler.initialize(typeDefinition.typeId());
			}
		}

		this.buildTypeHandlerDictionary();
	}

	///////////////////////////////////////////////////////////////////////////
	// methods //
	////////////

	private void buildTypeHandlerDictionary()
	{
		final XGettingTable orginialTypes = this.typeDictionary.allTypeDefinitions();

		for (final KeyValue keyValue : orginialTypes)
		{
			this.viewerTypeHandlers.add(keyValue.key(), this.deriveTypeHandler(keyValue.key()));
		}
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	private PersistenceTypeHandler deriveTypeHandler(final long typeId)
	{
		final PersistenceTypeDefinition persistenceTypeDef = this.typeDictionary.lookupTypeById(typeId);
		final PersistenceTypeHandler nativeHandler = this.nativeHandlers.search(t->t.typeId() == typeId );

		final ViewerBinaryTypeHandlerGeneric genericHandler = new ViewerBinaryTypeHandlerGeneric(persistenceTypeDef);

		if(nativeHandler != null)
		{
			if(persistenceTypeDef.type().isArray())
			{
				if(persistenceTypeDef.type().getComponentType().isPrimitive())
				{
					return new ViewerBinaryTypeHandlerNativeArray(nativeHandler);
				}
			}

			return new ViewerBinaryTypeHandlerBasic(nativeHandler, genericHandler);
		}

		return genericHandler;
	}

	private PersistenceTypeHandler createTypeHandler(final long typeId)
	{
		this.viewerTypeHandlers.add(typeId, this.deriveTypeHandler(typeId));
		return this.viewerTypeHandlers.get(typeId);
	}

	@Override
	public long currentTypeId()
	{
		throw new UnsupportedOperationException();
	}

	@Override
	public void updateCurrentHighestTypeId(final long highestTypeId)
	{
		throw new UnsupportedOperationException();
	}

	@Override
	public boolean registerType(final long typeId, final Class type) throws PersistenceExceptionConsistency
	{
		throw new UnsupportedOperationException();
	}

	@Override
	public boolean registerTypes(final Iterable types) throws PersistenceExceptionConsistency
	{
		throw new UnsupportedOperationException();
	}

	@Override
	public long lookupTypeId(final Class type)
	{
		throw new UnsupportedOperationException();
	}

	@Override
	public  Class lookupType(final long typeId)
	{
		throw new UnsupportedOperationException();
	}

	@Override
	public boolean validateTypeMapping(final long typeId, final Class type) throws PersistenceExceptionConsistency
	{
		throw new UnsupportedOperationException();
	}

	@Override
	public boolean validateTypeMappings(final Iterable mappings)
			throws PersistenceExceptionConsistency
	{
		throw new UnsupportedOperationException();
	}
	
	@Override
	public  boolean registerTypeHandler(final Class type, final PersistenceTypeHandler typeHandler)
	{
		throw new UnsupportedOperationException();
	}

	@Override
	public  boolean registerTypeHandler(final PersistenceTypeHandler typeHandler)
	{
		throw new UnsupportedOperationException();
	}

	@Override
	public long registerTypeHandlers(final Iterable> typeHandlers)
	{
		throw new UnsupportedOperationException();
	}

	@Override
	public boolean registerLegacyTypeHandler(final PersistenceLegacyTypeHandler legacyTypeHandler)
	{
		throw new UnsupportedOperationException();
	}

	@Override
	public >> C iterateTypeHandlers(final C iterator)
	{
		throw new UnsupportedOperationException();
	}

	@Override
	public >> C iterateLegacyTypeHandlers(final C iterator)
	{
		throw new UnsupportedOperationException();
	}

	@Override
	public  PersistenceTypeHandler lookupTypeHandler(final T instance)
	{
		return this.lookupTypeHandler(XReflect.getClass(instance));
	}

	@SuppressWarnings("unchecked")
	@Override
	public  PersistenceTypeHandler lookupTypeHandler(final Class type)
	{
		return (PersistenceTypeHandler)this.viewerTypeHandlers.values().search(
			v -> v.typeName().equals(type.getName())
		);
	}

	@Override
	public PersistenceTypeHandler lookupTypeHandler(final long typeId)
	{
		PersistenceTypeHandler handler = this.viewerTypeHandlers.get(typeId);
		
		if(handler==null)
		{
			handler = this.createTypeHandler(typeId);
		}
		return handler;
	}

	@Override
	public  PersistenceTypeHandler ensureTypeHandler(final T instance)
	{
		return this.lookupTypeHandler(instance);
	}

	@Override
	public  PersistenceTypeHandler ensureTypeHandler(final Class type)
	{
		return this.lookupTypeHandler(type);
	}

	@Override
	public  PersistenceTypeHandler ensureTypeHandler(final PersistenceTypeDefinition typeDefinition)
	{
		throw new UnsupportedOperationException();
	}

	@Override
	public void ensureTypeHandlers(final XGettingEnum typeDefinitions)
	{
		throw new UnsupportedOperationException();
	}

	@Override
	public void ensureTypeHandlersByTypeIds(final XGettingEnum typeIds)
	{
		throw new UnsupportedOperationException();
	}

	@Override
	public PersistenceTypeHandlerManager initialize()
	{
		throw new UnsupportedOperationException();
	}

	@Override
	public void update(final PersistenceTypeDictionary typeDictionary, final long highestTypeId)
	{
		throw new UnsupportedOperationException();
	}

	@Override
	public PersistenceTypeDictionary typeDictionary()
	{
		return this.typeDictionary;
	}

	@Override
	public long ensureTypeId(final Class type)
	{
		throw new UnsupportedOperationException();
	}

	@Override
	public Class ensureType(final long typeId)
	{
		throw new UnsupportedOperationException();
	}

	@Override
	public void validateTypeHandler(final PersistenceTypeHandler typeHandler)
	{
		throw new UnsupportedOperationException();
	}

	@Override
	public void checkForPendingRootInstances()
	{
		throw new UnsupportedOperationException();
	}

	@Override
	public void checkForPendingRootsStoring(final PersistenceStoring storingCallback)
	{
		throw new UnsupportedOperationException();
	}

	@Override
	public void clearStorePendingRoots()
	{
		throw new UnsupportedOperationException();
	}

	@Override
	public PersistenceTypeHandlerManager get()
	{
		return this;
	}

	@Override
	public  PersistenceLegacyTypeHandler ensureLegacyTypeHandler
	(
			final PersistenceTypeDefinition legacyTypeDefinition,
			final PersistenceTypeHandler currentTypeHandler
	)
	{
		throw new UnsupportedOperationException();
	}

	@Override
	public void iteratePerIds(final BiConsumer> consumer)
	{
		throw new UnsupportedOperationException();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy