diff options
Diffstat (limited to 'plugins/org.eclipse.emf.cdo.server.db4o/src/org/eclipse/emf/cdo/server/internal/db4o/DB4OStore.java')
-rw-r--r-- | plugins/org.eclipse.emf.cdo.server.db4o/src/org/eclipse/emf/cdo/server/internal/db4o/DB4OStore.java | 308 |
1 files changed, 308 insertions, 0 deletions
diff --git a/plugins/org.eclipse.emf.cdo.server.db4o/src/org/eclipse/emf/cdo/server/internal/db4o/DB4OStore.java b/plugins/org.eclipse.emf.cdo.server.db4o/src/org/eclipse/emf/cdo/server/internal/db4o/DB4OStore.java new file mode 100644 index 0000000000..003c7d37b0 --- /dev/null +++ b/plugins/org.eclipse.emf.cdo.server.db4o/src/org/eclipse/emf/cdo/server/internal/db4o/DB4OStore.java @@ -0,0 +1,308 @@ +/** + * Copyright (c) 2004 - 2010 Eike Stepper (Berlin, Germany) and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Victor Roldan Betancort - initial API and implementation + */ +package org.eclipse.emf.cdo.server.internal.db4o; + +import org.eclipse.emf.cdo.server.ISession; +import org.eclipse.emf.cdo.server.IStore; +import org.eclipse.emf.cdo.server.IStoreAccessor; +import org.eclipse.emf.cdo.server.ITransaction; +import org.eclipse.emf.cdo.server.IView; +import org.eclipse.emf.cdo.server.db4o.IDB4OStore; +import org.eclipse.emf.cdo.spi.server.LongIDStore; +import org.eclipse.emf.cdo.spi.server.StoreAccessorPool; + +import org.eclipse.net4j.util.ReflectUtil.ExcludeFromDump; + +import com.db4o.Db4o; +import com.db4o.ObjectContainer; +import com.db4o.ObjectServer; +import com.db4o.ObjectSet; +import com.db4o.config.Configuration; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +/** + * @author Victor Roldan Betancort + */ +public class DB4OStore extends LongIDStore implements IDB4OStore +{ + private transient String storeLocation; + + private transient int port; + + private transient ObjectServer server; + + private transient Configuration serverConfiguration; + + private ServerInfo serverInfo; + + private boolean requiredToSupportAudits; + + private boolean requiredToSupportBranches; + + @ExcludeFromDump + private transient final StoreAccessorPool readerPool = new StoreAccessorPool(this, null); + + @ExcludeFromDump + private transient final StoreAccessorPool writerPool = new StoreAccessorPool(this, null); + + public DB4OStore(String storeLocation, int port) + { + super(IDB4OStore.TYPE, set(ChangeFormat.REVISION), set(RevisionTemporality.NONE, RevisionTemporality.AUDITING), + set(RevisionParallelism.NONE, RevisionParallelism.BRANCHING)); + + this.storeLocation = storeLocation; + this.port = port; + } + + public DB4OStore(String storeLocation, int port, Configuration serverConfiguration) + { + this(storeLocation, port); + this.serverConfiguration = serverConfiguration; + } + + public String getStoreLocation() + { + return storeLocation; + } + + public int getPort() + { + return port; + } + + public long getCreationTime() + { + return getServerInfo().getCreationTime(); + } + + public boolean isFirstTime() + { + return getServerInfo().isFirstTime(); + } + + public Map<String, String> getPropertyValues(Set<String> names) + { + Map<String, String> result = new HashMap<String, String>(); + for (String key : names) + { + String value = getServerInfo().getProperties().get(key); + if (value != null) + { + result.put(key, value); + } + } + + return result; + } + + public void setPropertyValues(Map<String, String> properties) + { + ServerInfo serverInfo = getServerInfo(); + serverInfo.getProperties().putAll(properties); + commitServerInfo(null); + } + + public void removePropertyValues(Set<String> names) + { + ServerInfo serverInfo = getServerInfo(); + Map<String, String> properties = serverInfo.getProperties(); + for (String key : names) + { + properties.remove(key); + } + + commitServerInfo(null); + } + + public boolean isRequiredToSupportAudits() + { + return requiredToSupportAudits; + } + + public boolean isRequiredToSupportBranches() + { + return requiredToSupportBranches; + } + + public ObjectContainer openClient() + { + return server.openClient(); + } + + @Override + protected void doBeforeActivate() + { + requiredToSupportAudits = getRepository().isSupportingAudits(); + requiredToSupportBranches = getRepository().isSupportingBranches(); + } + + @Override + protected void doActivate() throws Exception + { + super.doActivate(); + Configuration configuration = serverConfiguration; + if (configuration == null) + { + configuration = createServerConfiguration(); + } + + server = Db4o.openServer(configuration, storeLocation, port); + getServerInfo(); + } + + private ServerInfo getServerInfo() + { + if (serverInfo == null) + { + initServerInfo(); + } + + return serverInfo; + } + + private void initServerInfo() + { + ObjectContainer container = openClient(); + + try + { + ObjectSet<ServerInfo> infos = container.query(ServerInfo.class); + if (infos.size() > 1) + { + throw new IllegalStateException("ServeInfo is stored in container more than once"); + } + + if (infos.isEmpty()) + { + serverInfo = new ServerInfo(); + serverInfo.setFirstTime(true); + serverInfo.setCreationTime(System.currentTimeMillis()); + commitServerInfo(container); + } + else + { + serverInfo = infos.get(0); + if (serverInfo.isFirstTime()) + { + serverInfo.setFirstTime(false); + commitServerInfo(container); + } + } + } + finally + { + container.close(); + } + } + + private void commitServerInfo(ObjectContainer container) + { + ObjectContainer usedContainer = container != null ? container : openClient(); + + try + { + usedContainer.store(serverInfo); + usedContainer.commit(); + } + finally + { + if (usedContainer != container) + { + usedContainer.close(); + } + } + } + + @Override + protected void doDeactivate() throws Exception + { + server.close(); + server = null; + super.doDeactivate(); + } + + protected Configuration createServerConfiguration() + { + return Db4o.newConfiguration(); + } + + @Override + protected IStoreAccessor createReader(ISession session) + { + return new DB4OStoreAccessor(this, session); + } + + @Override + protected IStoreAccessor createWriter(ITransaction transaction) + { + return new DB4OStoreAccessor(this, transaction); + } + + @Override + protected StoreAccessorPool getReaderPool(ISession session, boolean forReleasing) + { + return readerPool; + } + + @Override + protected StoreAccessorPool getWriterPool(IView view, boolean forReleasing) + { + return writerPool; + } + + /** + * Carries {@link IStore}-related information. + * + * @author Victor Roldan Betancort + */ + private static final class ServerInfo + { + private boolean isFirstTime; + + private long creationTime; + + private Map<String, String> properties = new HashMap<String, String>(); + + public boolean isFirstTime() + { + return isFirstTime; + } + + public void setFirstTime(boolean isFirstTime) + { + this.isFirstTime = isFirstTime; + } + + public void setCreationTime(long creationTime) + { + this.creationTime = creationTime; + } + + public long getCreationTime() + { + return creationTime; + } + + @SuppressWarnings("unused") + public void setProperties(Map<String, String> properties) + { + this.properties = properties; + } + + public Map<String, String> getProperties() + { + return properties; + } + } +} |