summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Stepper2008-01-07 04:15:14 (EST)
committerEike Stepper2008-01-07 04:15:14 (EST)
commit404477aba55888996f82e78e6a4f5624696916f0 (patch)
treeec775ebef241688a49eadb7d091b188988b9f0ee
parentd166a816f13c894bb98050f3670b242301a9de2e (diff)
downloadcdo-404477aba55888996f82e78e6a4f5624696916f0.zip
cdo-404477aba55888996f82e78e6a4f5624696916f0.tar.gz
cdo-404477aba55888996f82e78e6a4f5624696916f0.tar.bz2
[214437] Provide efficient type lookup service in storage framework
https://bugs.eclipse.org/bugs/show_bug.cgi?id=214437
-rw-r--r--plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/db/IObjectTypeCache.java7
-rw-r--r--plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/CDODBSchema.java5
-rw-r--r--plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/HorizontalClassMapping.java5
-rw-r--r--plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/HorizontalMappingStrategy.java11
-rw-r--r--plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/ObjectTypeCache.java136
5 files changed, 149 insertions, 15 deletions
diff --git a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/db/IObjectTypeCache.java b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/db/IObjectTypeCache.java
index 4eff0b8..a869f63 100644
--- a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/db/IObjectTypeCache.java
+++ b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/db/IObjectTypeCache.java
@@ -11,16 +11,15 @@
package org.eclipse.emf.cdo.server.db;
import org.eclipse.emf.cdo.protocol.CDOID;
+import org.eclipse.emf.cdo.protocol.model.CDOClass;
import org.eclipse.emf.cdo.protocol.model.CDOClassRef;
-import java.sql.Connection;
-
/**
* @author Eike Stepper
*/
public interface IObjectTypeCache
{
- public CDOClassRef getObjectType(Connection connection, CDOID id);
+ public CDOClassRef getObjectType(IDBStoreReader storeReader, CDOID id);
- public void putObjectType(Connection connection, CDOID id, CDOClassRef type);
+ public void putObjectType(IDBStoreWriter storeWriter, CDOID id, CDOClass type);
}
diff --git a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/CDODBSchema.java b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/CDODBSchema.java
index c7c699e..a967e0f 100644
--- a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/CDODBSchema.java
+++ b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/CDODBSchema.java
@@ -160,6 +160,11 @@ public class CDODBSchema extends DBSchema
FEATURES.addIndex(IDBIndex.Type.PRIMARY_KEY, FEATURES_ID);
/**
+ * Name of object table
+ */
+ public static final String CDO_OBJECTS = "cdo_objects";
+
+ /**
* Field names of attribute tables
*/
public static final String ATTRIBUTES_ID = "cdo_id";
diff --git a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/HorizontalClassMapping.java b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/HorizontalClassMapping.java
index f90c96e..eac046b 100644
--- a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/HorizontalClassMapping.java
+++ b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/HorizontalClassMapping.java
@@ -10,7 +10,6 @@
**************************************************************************/
package org.eclipse.emf.cdo.server.internal.db;
-import org.eclipse.emf.cdo.internal.protocol.model.CDOClassRefImpl;
import org.eclipse.emf.cdo.internal.protocol.revision.CDORevisionImpl;
import org.eclipse.emf.cdo.protocol.CDOID;
import org.eclipse.emf.cdo.protocol.model.CDOClass;
@@ -39,8 +38,8 @@ public class HorizontalClassMapping extends ClassMapping
if (revision.getVersion() == 1)
{
CDOID id = revision.getID();
- CDOClassRefImpl type = revision.getCDOClass().createClassRef();
- getMappingStrategy().getObjectTypeCache().putObjectType(storeWriter.getConnection(), id, type);
+ CDOClass type = revision.getCDOClass();
+ getMappingStrategy().getObjectTypeCache().putObjectType(storeWriter, id, type);
}
}
diff --git a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/HorizontalMappingStrategy.java b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/HorizontalMappingStrategy.java
index 142021c..3359e32 100644
--- a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/HorizontalMappingStrategy.java
+++ b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/HorizontalMappingStrategy.java
@@ -57,11 +57,12 @@ public class HorizontalMappingStrategy extends MappingStrategy
public CDOClassRef readObjectType(IDBStoreReader storeReader, CDOID id)
{
- CDOClassRef type = objectTypeCache.getObjectType(storeReader.getConnection(), id);
+ CDOClassRef type = objectTypeCache.getObjectType(storeReader, id);
if (type == null)
{
- type = readObjectTypeFromClassesWithObjectInfo(storeReader, id);
- objectTypeCache.putObjectType(storeReader.getConnection(), id, type);
+ throw new DBException("Type of " + id + " not found");
+ // type = readObjectTypeFromClassesWithObjectInfo(storeReader, id);
+ // objectTypeCache.putObjectType(storeReader.getStatement(), id, type);
}
return type;
@@ -155,6 +156,8 @@ public class HorizontalMappingStrategy extends MappingStrategy
protected IObjectTypeCache createObjectTypeCache(IDBStore store)
{
- return new ObjectTypeCache();
+ ObjectTypeCache cache = new ObjectTypeCache();
+ cache.setMappingStrategy(this);
+ return cache;
}
}
diff --git a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/ObjectTypeCache.java b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/ObjectTypeCache.java
index 12246ee..139e987 100644
--- a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/ObjectTypeCache.java
+++ b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/ObjectTypeCache.java
@@ -11,28 +11,156 @@
package org.eclipse.emf.cdo.server.internal.db;
import org.eclipse.emf.cdo.protocol.CDOID;
+import org.eclipse.emf.cdo.protocol.model.CDOClass;
import org.eclipse.emf.cdo.protocol.model.CDOClassRef;
+import org.eclipse.emf.cdo.server.db.IDBStoreReader;
+import org.eclipse.emf.cdo.server.db.IDBStoreWriter;
import org.eclipse.emf.cdo.server.db.IObjectTypeCache;
+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.IDBField;
+import org.eclipse.net4j.db.IDBIndex;
+import org.eclipse.net4j.db.IDBSchema;
+import org.eclipse.net4j.db.IDBTable;
import org.eclipse.net4j.internal.util.lifecycle.Lifecycle;
-import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
/**
* @author Eike Stepper
*/
public class ObjectTypeCache extends Lifecycle implements IObjectTypeCache
{
+ private MappingStrategy mappingStrategy;
+
+ private IDBTable table;
+
+ private IDBField idField;
+
+ private IDBField typeField;
+
+ private transient Object initializeLock = new Object();
+
public ObjectTypeCache()
{
}
- public CDOClassRef getObjectType(Connection connection, CDOID id)
+ public MappingStrategy getMappingStrategy()
+ {
+ return mappingStrategy;
+ }
+
+ public void setMappingStrategy(MappingStrategy mappingStrategy)
+ {
+ this.mappingStrategy = mappingStrategy;
+ }
+
+ public CDOClassRef getObjectType(IDBStoreReader storeReader, CDOID id)
+ {
+ Statement statement = storeReader.getStatement();
+ initialize(statement);
+
+ StringBuilder builder = new StringBuilder();
+ builder.append("SELECT ");
+ builder.append(typeField);
+ builder.append(" FROM ");
+ builder.append(table);
+ builder.append(" WHERE ");
+ builder.append(idField);
+ builder.append("=");
+ builder.append(id.getValue());
+ String sql = builder.toString();
+ DBUtil.trace(sql);
+
+ ResultSet resultSet = null;
+
+ try
+ {
+ resultSet = statement.executeQuery(sql);
+ if (!resultSet.next())
+ {
+ return null;
+ }
+
+ int classID = resultSet.getInt(1);
+ return mappingStrategy.getClassRef(storeReader, classID);
+ }
+ catch (SQLException ex)
+ {
+ throw new DBException(ex);
+ }
+ finally
+ {
+ DBUtil.close(resultSet);
+ }
+ }
+
+ public void putObjectType(IDBStoreWriter storeWriter, CDOID id, CDOClass type)
+ {
+ Statement statement = storeWriter.getStatement();
+ initialize(statement);
+
+ StringBuilder builder = new StringBuilder();
+ builder.append("INSERT INTO ");
+ builder.append(table);
+ builder.append(" VALUES (");
+ builder.append(id.getValue());
+ builder.append(", ");
+ builder.append(ClassServerInfo.getDBID(type));
+ builder.append(")");
+ String sql = builder.toString();
+ DBUtil.trace(sql);
+
+ try
+ {
+ statement.execute(sql);
+ if (statement.getUpdateCount() != 1)
+ {
+ throw new DBException("Object type not inserted: " + id + " -> " + type);
+ }
+ }
+ catch (SQLException ex)
+ {
+ throw new DBException(ex);
+ }
+ }
+
+ private void initialize(Statement statement)
+ {
+ synchronized (initializeLock)
+ {
+ if (table == null)
+ {
+ IDBSchema schema = mappingStrategy.getStore().getSchema();
+ table = schema.addTable(CDODBSchema.CDO_OBJECTS);
+ idField = table.addField(CDODBSchema.ATTRIBUTES_ID, DBType.BIGINT);
+ typeField = table.addField(CDODBSchema.ATTRIBUTES_CLASS, DBType.INTEGER);
+ table.addIndex(IDBIndex.Type.PRIMARY_KEY, idField);
+
+ IDBAdapter dbAdapter = mappingStrategy.getStore().getDBAdapter();
+ dbAdapter.createTable(table, statement);
+ }
+ }
+ }
+
+ @Override
+ protected void doBeforeActivate() throws Exception
{
- return null;
+ super.doBeforeActivate();
+ checkState(mappingStrategy, "mappingStrategy");
}
- public void putObjectType(Connection connection, CDOID id, CDOClassRef type)
+ @Override
+ protected void doDeactivate() throws Exception
{
+ table = null;
+ idField = null;
+ typeField = null;
+ super.doDeactivate();
}
}