From 438ab08ce854e83de7b4ef56ee19258520660b85 Mon Sep 17 00:00:00 2001
From: Stefan Winkler
Date: Tue, 20 Jul 2010 17:29:36 +0000
Subject: [305962] [DB] Provide a memory sensitive implementation of
IObjectTypeCache https://bugs.eclipse.org/bugs/show_bug.cgi?id=305962
---
.../rootfiles/configuration/cdo-server.xml | 8 +
.../emf/cdo/server/db/IObjectTypeCache.java | 64 ------
.../emf/cdo/server/db/IObjectTypeMapper.java | 49 +++++
.../cdo/server/db/mapping/IMappingStrategy.java | 14 ++
.../AbstractHorizontalMappingStrategy.java | 61 ++++--
.../horizontal/AbstractObjectTypeMapper.java | 69 ++++++
.../horizontal/DelegatingObjectTypeMapper.java | 133 ++++++++++++
.../HorizontalBranchingClassMapping.java | 2 +-
.../db/mapping/horizontal/ObjectTypeCache.java | 241 ++++-----------------
.../db/mapping/horizontal/ObjectTypeTable.java | 225 +++++++++++++++++++
10 files changed, 587 insertions(+), 279 deletions(-)
delete mode 100644 plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/db/IObjectTypeCache.java
create mode 100644 plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/db/IObjectTypeMapper.java
create mode 100644 plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/horizontal/AbstractObjectTypeMapper.java
create mode 100644 plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/horizontal/DelegatingObjectTypeMapper.java
create mode 100644 plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/horizontal/ObjectTypeTable.java
diff --git a/features/org.eclipse.emf.cdo.server.product-feature/rootfiles/configuration/cdo-server.xml b/features/org.eclipse.emf.cdo.server.product-feature/rootfiles/configuration/cdo-server.xml
index 56272de624..c28d860a58 100644
--- a/features/org.eclipse.emf.cdo.server.product-feature/rootfiles/configuration/cdo-server.xml
+++ b/features/org.eclipse.emf.cdo.server.product-feature/rootfiles/configuration/cdo-server.xml
@@ -27,6 +27,14 @@
+ *
+ * @since 4.0
+ */
+ public static final String PROP_OBJECT_TYPE_CACHE_SIZE = "objectTypeCacheSize"; //$NON-NLS-1$
+
/**
* @return the store, this MappingStrategy instance belongs to.
*/
diff --git a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/horizontal/AbstractHorizontalMappingStrategy.java b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/horizontal/AbstractHorizontalMappingStrategy.java
index 61d1569278..8c9ce196ff 100644
--- a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/horizontal/AbstractHorizontalMappingStrategy.java
+++ b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/horizontal/AbstractHorizontalMappingStrategy.java
@@ -22,7 +22,7 @@ import org.eclipse.emf.cdo.eresource.EresourcePackage;
import org.eclipse.emf.cdo.server.IStoreAccessor.QueryResourcesContext;
import org.eclipse.emf.cdo.server.IStoreAccessor.QueryXRefsContext;
import org.eclipse.emf.cdo.server.db.IDBStoreAccessor;
-import org.eclipse.emf.cdo.server.db.IObjectTypeCache;
+import org.eclipse.emf.cdo.server.db.IObjectTypeMapper;
import org.eclipse.emf.cdo.server.db.mapping.IClassMapping;
import org.eclipse.emf.cdo.server.db.mapping.IListMapping;
import org.eclipse.emf.cdo.server.internal.db.CDODBSchema;
@@ -62,24 +62,24 @@ public abstract class AbstractHorizontalMappingStrategy extends AbstractMappingS
private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG, AbstractHorizontalMappingStrategy.class);
/**
- * The associated object type cache.
+ * The associated object type mapper.
*/
- private IObjectTypeCache objectTypeCache;
+ private IObjectTypeMapper objectTypeMapper;
public CDOClassifierRef readObjectType(IDBStoreAccessor accessor, CDOID id)
{
- return objectTypeCache.getObjectType(accessor, id);
+ return objectTypeMapper.getObjectType(accessor, id);
}
public void putObjectType(IDBStoreAccessor accessor, long timeStamp, CDOID id, EClass type)
{
- objectTypeCache.putObjectType(accessor, timeStamp, id, type);
+ objectTypeMapper.putObjectType(accessor, timeStamp, id, type);
}
public long[] repairAfterCrash(IDBAdapter dbAdapter, Connection connection)
{
long minLocalID = getMinLocalID(connection);
- long maxID = objectTypeCache.getMaxID(connection);
+ long maxID = objectTypeMapper.getMaxID(connection);
long[] result = { minLocalID, maxID };
return result;
@@ -173,7 +173,7 @@ public abstract class AbstractHorizontalMappingStrategy extends AbstractMappingS
}
out.writeBoolean(false);
- objectTypeCache.rawExport(connection, out, fromCommitTime, toCommitTime);
+ objectTypeMapper.rawExport(connection, out, fromCommitTime, toCommitTime);
}
protected void rawExportList(CDODataOutput out, Connection connection, IListMapping listMapping, IDBTable attrTable,
@@ -211,7 +211,7 @@ public abstract class AbstractHorizontalMappingStrategy extends AbstractMappingS
}
}
- objectTypeCache.rawImport(connection, in);
+ objectTypeMapper.rawImport(connection, in);
}
protected void rawImportReviseOldRevisions(Connection connection, IDBTable table)
@@ -242,28 +242,59 @@ public abstract class AbstractHorizontalMappingStrategy extends AbstractMappingS
protected void doActivate() throws Exception
{
super.doActivate();
- if (objectTypeCache == null)
+ if (objectTypeMapper == null)
{
- objectTypeCache = createObjectTypeCache();
- LifecycleUtil.activate(objectTypeCache);
+ objectTypeMapper = createObjectTypeMapper();
+ LifecycleUtil.activate(objectTypeMapper);
}
}
@Override
protected void doDeactivate() throws Exception
{
- LifecycleUtil.deactivate(objectTypeCache);
- objectTypeCache = null;
+ LifecycleUtil.deactivate(objectTypeMapper);
+ objectTypeMapper = null;
super.doDeactivate();
}
- private IObjectTypeCache createObjectTypeCache()
+ private IObjectTypeMapper createObjectTypeMapper()
{
- ObjectTypeCache cache = new ObjectTypeCache();
+ ObjectTypeTable table = new ObjectTypeTable();
+ table.setMappingStrategy(this);
+
+ int cacheSize = getObjectTypeCacheSize();
+ if (cacheSize == 0)
+ {
+ return table;
+ }
+
+ ObjectTypeCache cache = new ObjectTypeCache(cacheSize);
cache.setMappingStrategy(this);
+ cache.setDelegate(table);
return cache;
}
+ private int getObjectTypeCacheSize()
+ {
+ int objectTypeCacheSize = ObjectTypeCache.DEFAULT_CACHE_CAPACITY;
+
+ Object value = getProperties().get(PROP_OBJECT_TYPE_CACHE_SIZE);
+ if (value != null)
+ {
+ try
+ {
+ int intValue = Integer.parseInt((String)value);
+ objectTypeCacheSize = intValue;
+ }
+ catch (NumberFormatException e)
+ {
+ OM.LOG.warn("Malformed configuration option for object type cache size. Using default.");
+ }
+ }
+
+ return objectTypeCacheSize;
+ }
+
/**
* This is an intermediate implementation. It should be changed after classmappings support a general way to implement
* queries ...
diff --git a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/horizontal/AbstractObjectTypeMapper.java b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/horizontal/AbstractObjectTypeMapper.java
new file mode 100644
index 0000000000..9e3b4d8090
--- /dev/null
+++ b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/horizontal/AbstractObjectTypeMapper.java
@@ -0,0 +1,69 @@
+/**
+ * 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:
+ * Eike Stepper - initial API and implementation
+ * Stefan Winkler - bug 259402
+ * Stefan Winkler - redesign (prepared statements)
+ * Stefan Winkler - bug 276926
+ */
+package org.eclipse.emf.cdo.server.internal.db.mapping.horizontal;
+
+import org.eclipse.emf.cdo.server.db.IMetaDataManager;
+import org.eclipse.emf.cdo.server.db.IObjectTypeMapper;
+import org.eclipse.emf.cdo.server.db.mapping.IMappingStrategy;
+
+import org.eclipse.net4j.util.lifecycle.Lifecycle;
+
+/**
+ * @author Eike Stepper
+ * @since 4.0
+ */
+public abstract class AbstractObjectTypeMapper extends Lifecycle implements IObjectTypeMapper
+{
+ private IMappingStrategy mappingStrategy;
+
+ private IMetaDataManager metaDataManager;
+
+ public AbstractObjectTypeMapper()
+ {
+ }
+
+ public IMappingStrategy getMappingStrategy()
+ {
+ return mappingStrategy;
+ }
+
+ public void setMappingStrategy(IMappingStrategy mappingStrategy)
+ {
+ this.mappingStrategy = mappingStrategy;
+ }
+
+ public IMetaDataManager getMetaDataManager()
+ {
+ return metaDataManager;
+ }
+
+ @Override
+ protected void doBeforeActivate() throws Exception
+ {
+ super.doBeforeActivate();
+ checkState(mappingStrategy, "mappingStrategy"); //$NON-NLS-1$
+ }
+
+ @Override
+ protected void doActivate() throws Exception
+ {
+ metaDataManager = getMappingStrategy().getStore().getMetaDataManager();
+ }
+
+ @Override
+ protected void doDeactivate() throws Exception
+ {
+ metaDataManager = null;
+ }
+}
diff --git a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/horizontal/DelegatingObjectTypeMapper.java b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/horizontal/DelegatingObjectTypeMapper.java
new file mode 100644
index 0000000000..14c09c96a8
--- /dev/null
+++ b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/horizontal/DelegatingObjectTypeMapper.java
@@ -0,0 +1,133 @@
+/**
+ * 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:
+ * Eike Stepper - initial API and implementation
+ */
+package org.eclipse.emf.cdo.server.internal.db.mapping.horizontal;
+
+import org.eclipse.emf.cdo.common.id.CDOID;
+import org.eclipse.emf.cdo.common.id.CDOIDUtil;
+import org.eclipse.emf.cdo.common.model.CDOClassifierRef;
+import org.eclipse.emf.cdo.common.protocol.CDODataInput;
+import org.eclipse.emf.cdo.common.protocol.CDODataOutput;
+import org.eclipse.emf.cdo.server.db.IDBStoreAccessor;
+import org.eclipse.emf.cdo.server.db.IObjectTypeMapper;
+
+import org.eclipse.net4j.util.lifecycle.LifecycleUtil;
+
+import org.eclipse.emf.ecore.EClass;
+
+import java.io.IOException;
+import java.sql.Connection;
+
+/**
+ * @author Eike Stepper
+ * @since 4.0
+ */
+public abstract class DelegatingObjectTypeMapper extends AbstractObjectTypeMapper
+{
+ public static final int DEFAULT_CACHE_CAPACITY = 10000000;
+
+ private IObjectTypeMapper delegate;
+
+ public DelegatingObjectTypeMapper()
+ {
+ }
+
+ public IObjectTypeMapper getDelegate()
+ {
+ return delegate;
+ }
+
+ public void setDelegate(IObjectTypeMapper delegate)
+ {
+ this.delegate = delegate;
+ }
+
+ public CDOClassifierRef getObjectType(IDBStoreAccessor accessor, CDOID id)
+ {
+ long longId = CDOIDUtil.getLong(id);
+ Long type = doGetObjectType(longId);
+ if (type != null)
+ {
+ long classID = type;
+ EClass eClass = (EClass)getMetaDataManager().getMetaInstance(classID);
+ return new CDOClassifierRef(eClass);
+ }
+
+ return delegate.getObjectType(accessor, id);
+ }
+
+ public void putObjectType(IDBStoreAccessor accessor, long timeStamp, CDOID id, EClass type)
+ {
+ long longId = CDOIDUtil.getLong(id);
+ long classID = getMetaDataManager().getMetaID(type);
+ doPutObjectType(longId, classID);
+
+ delegate.putObjectType(accessor, timeStamp, id, type);
+ }
+
+ public void removeObjectType(IDBStoreAccessor accessor, CDOID id)
+ {
+ long longId = CDOIDUtil.getLong(id);
+ doRemoveObjectType(longId);
+
+ delegate.removeObjectType(accessor, id);
+ }
+
+ public long getMaxID(Connection connection)
+ {
+ Long maxID = doGetMaxID();
+ if (maxID != null)
+ {
+ return maxID;
+ }
+
+ return delegate.getMaxID(connection);
+ }
+
+ public void rawExport(Connection connection, CDODataOutput out, long fromCommitTime, long toCommitTime)
+ throws IOException
+ {
+ delegate.rawExport(connection, out, fromCommitTime, toCommitTime);
+ }
+
+ public void rawImport(Connection connection, CDODataInput in) throws IOException
+ {
+ delegate.rawImport(connection, in);
+ }
+
+ @Override
+ protected void doBeforeActivate() throws Exception
+ {
+ super.doBeforeActivate();
+ checkState(delegate, "delegate");
+ }
+
+ @Override
+ protected void doActivate() throws Exception
+ {
+ super.doActivate();
+ LifecycleUtil.activate(delegate);
+ }
+
+ @Override
+ protected void doDeactivate() throws Exception
+ {
+ LifecycleUtil.deactivate(delegate);
+ super.doDeactivate();
+ }
+
+ protected abstract Long doGetObjectType(long id);
+
+ protected abstract void doPutObjectType(long id, long type);
+
+ protected abstract void doRemoveObjectType(long id);
+
+ protected abstract Long doGetMaxID();
+}
diff --git a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/horizontal/HorizontalBranchingClassMapping.java b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/horizontal/HorizontalBranchingClassMapping.java
index 8d19c3213f..8cd4d6a00e 100644
--- a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/horizontal/HorizontalBranchingClassMapping.java
+++ b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/horizontal/HorizontalBranchingClassMapping.java
@@ -590,7 +590,7 @@ public class HorizontalBranchingClassMapping extends AbstractHorizontalClassMapp
CDOID id = revision.getID();
if (accessor.isNewObject(id))
{
- // put new objects into objectTypeCache
+ // put new objects into objectTypeMapper
long timeStamp = revision.getTimeStamp();
HorizontalBranchingMappingStrategy mappingStrategy = (HorizontalBranchingMappingStrategy)getMappingStrategy();
mappingStrategy.putObjectType(accessor, timeStamp, id, getEClass());
diff --git a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/horizontal/ObjectTypeCache.java b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/horizontal/ObjectTypeCache.java
index e160bd376d..9b37195972 100644
--- a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/horizontal/ObjectTypeCache.java
+++ b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/horizontal/ObjectTypeCache.java
@@ -7,244 +7,87 @@
*
* Contributors:
* Eike Stepper - initial API and implementation
- * Stefan Winkler - bug 259402
- * Stefan Winkler - redesign (prepared statements)
- * Stefan Winkler - bug 276926
*/
package org.eclipse.emf.cdo.server.internal.db.mapping.horizontal;
-import org.eclipse.emf.cdo.common.id.CDOID;
-import org.eclipse.emf.cdo.common.id.CDOIDUtil;
-import org.eclipse.emf.cdo.common.model.CDOClassifierRef;
-import org.eclipse.emf.cdo.common.protocol.CDODataInput;
-import org.eclipse.emf.cdo.common.protocol.CDODataOutput;
-import org.eclipse.emf.cdo.server.db.IDBStoreAccessor;
-import org.eclipse.emf.cdo.server.db.IMetaDataManager;
-import org.eclipse.emf.cdo.server.db.IObjectTypeCache;
-import org.eclipse.emf.cdo.server.db.IPreparedStatementCache;
-import org.eclipse.emf.cdo.server.db.IPreparedStatementCache.ReuseProbability;
-import org.eclipse.emf.cdo.server.db.mapping.IMappingStrategy;
-import org.eclipse.emf.cdo.server.internal.db.CDODBSchema;
-
-import org.eclipse.net4j.db.DBException;
-import org.eclipse.net4j.db.DBType;
-import org.eclipse.net4j.db.DBUtil;
-import org.eclipse.net4j.db.IDBAdapter;
-import org.eclipse.net4j.db.ddl.IDBField;
-import org.eclipse.net4j.db.ddl.IDBIndex;
-import org.eclipse.net4j.db.ddl.IDBSchema;
-import org.eclipse.net4j.db.ddl.IDBTable;
-import org.eclipse.net4j.util.lifecycle.Lifecycle;
-import org.eclipse.net4j.util.lifecycle.LifecycleUtil;
-
-import org.eclipse.emf.ecore.EClass;
-
-import java.io.IOException;
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
/**
* @author Eike Stepper
- * @since 2.0
+ * @since 4.0
*/
-public class ObjectTypeCache extends Lifecycle implements IObjectTypeCache
+public class ObjectTypeCache extends DelegatingObjectTypeMapper
{
- private static final String SQL_STATE_UNIQUE_KEY_VIOLATION = "23001";
-
- private IMappingStrategy mappingStrategy;
-
- private IDBTable table;
-
- private IDBField idField;
-
- private IDBField typeField;
-
- private IDBField timeField;
-
- private String sqlDelete;
+ public static final int DEFAULT_CACHE_CAPACITY = 10000000;
- private String sqlInsert;
+ private Map