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

one.microstream.persistence.binary.util.ObjectCopier Maven / Gradle / Ivy

There is a newer version: 08.01.02-MS-GA
Show newest version
package one.microstream.persistence.binary.util;

/*-
 * #%L
 * microstream-persistence-binary
 * %%
 * Copyright (C) 2019 - 2021 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
 * http://www.eclipse.org/legal/epl-2.0.
 * 
 * This Source Code may also be made available under the following Secondary
 * Licenses when the conditions for such availability set forth in the Eclipse
 * Public License, v. 2.0 are satisfied: GNU General Public License, version 2
 * with the GNU Classpath Exception which is
 * available at https://www.gnu.org/software/classpath/license.html.
 * 
 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
 * #L%
 */

import static one.microstream.X.notNull;

import java.io.Closeable;

import one.microstream.X;
import one.microstream.collections.types.XGettingCollection;
import one.microstream.persistence.binary.types.Binary;
import one.microstream.persistence.binary.types.BinaryPersistence;
import one.microstream.persistence.binary.types.BinaryPersistenceFoundation;
import one.microstream.persistence.exceptions.PersistenceExceptionTransfer;
import one.microstream.persistence.types.PersistenceContextDispatcher;
import one.microstream.persistence.types.PersistenceIdSet;
import one.microstream.persistence.types.PersistenceManager;
import one.microstream.persistence.types.PersistenceSource;
import one.microstream.persistence.types.PersistenceTarget;
import one.microstream.persistence.types.PersistenceTypeDictionaryManager;
import one.microstream.reference.Reference;

public interface ObjectCopier extends Closeable
{
	public  T copy(T source);
	
	@Override
	public void close();
	
	
	public static ObjectCopier New()
	{
		return new Default(BinaryPersistence.Foundation());
	}
	
	public static ObjectCopier New(final BinaryPersistenceFoundation foundation)
	{
		return new Default(
			notNull(foundation)
		);
	}
	
	
	public static class Default implements ObjectCopier
	{
		private final BinaryPersistenceFoundation foundation        ;
		private PersistenceManager           persistenceManager;
				
		Default(final BinaryPersistenceFoundation foundation)
		{
			super();
			this.foundation = foundation;
		}

		@SuppressWarnings("unchecked")
		@Override
		public synchronized  T copy(final T source)
		{
			this.lazyInit();
			
			this.persistenceManager.store(source);
			return (T)this.persistenceManager.get();
		}
		
		@Override
		public synchronized void close()
		{
			if(this.persistenceManager != null)
			{
				this.persistenceManager.objectRegistry().clearAll();
				this.persistenceManager.close();
				this.persistenceManager = null;
			}
		}
		
		private void lazyInit()
		{
			if(this.persistenceManager == null)
			{
				final Reference buffer = X.Reference(null);
				final CopySource        source = ()   -> X.Constant(buffer.get());
				final CopyTarget        target = data -> buffer.set(data);
				
				final BinaryPersistenceFoundation foundation = this.foundation
					.setPersistenceSource(source)
					.setPersistenceTarget(target)
					.setContextDispatcher(PersistenceContextDispatcher.LocalObjectRegistration())
				;
				
				foundation.setTypeDictionaryManager(
					PersistenceTypeDictionaryManager.Transient(
						foundation.getTypeDictionaryCreator()
					)
				);
				
				this.persistenceManager = foundation.createPersistenceManager();
			}
			else
			{
				this.persistenceManager.objectRegistry().truncateAll();
			}
		}
		
		
		static interface CopySource extends PersistenceSource
		{
			@Override
			default XGettingCollection readByObjectIds(final PersistenceIdSet[] oids)
				throws PersistenceExceptionTransfer
			{
				return null;
			}
		}
		
		
		static interface CopyTarget extends PersistenceTarget
		{
			@Override
			default boolean isWritable()
			{
				return true;
			}
		}
		
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy