I took a look at the h2 code and found out why I was having trouble
creating indexes on local temporary tables on multiple connections.
The index creation code didn't have any logic to create session local
indexes for local temporary tables. This is a very rough patch to add
the notion of session local indexes that are created when an index is
created on a local temporary table. Let me know if I should post this
somewhere else.
Matt
Index: src/main/org/h2/table/TableData.java
===================================================================
--- src/main/org/h2/table/TableData.java (revision 928)
+++ src/main/org/h2/table/TableData.java (working copy)
@@ -219,7 +219,11 @@
index.setTemporary(temporary);
if (index.getCreateSQL() != null) {
index.setComment(indexComment);
- database.addSchemaObject(session, index);
+ if (temporary && !getGlobalTemporary()) {
+ session.addLocalTempTableIndex(index);
+ } else {
+ database.addSchemaObject(session, index);
+ }
// Need to update, because maybe the index is rebuilt at
startup,
// and so the head pos may have changed, which needs to
be stored now.
// addSchemaObject doesn't update the sys table at
startup
Index: src/main/org/h2/table/Table.java
===================================================================
--- src/main/org/h2/table/Table.java (revision 928)
+++ src/main/org/h2/table/Table.java (working copy)
@@ -382,6 +382,13 @@
constraints.remove(0);
database.removeSchemaObject(session, constraint);
}
+ if(getTemporary()&&!getGlobalTemporary()) {
+ while (getIndexes() != null && getIndexes().size() > 0) {
+ Index index = (Index) getIndexes().get(0);
+ getIndexes().remove(0);
+ database.removeSchemaObject(session, index);
+ }
+ }
ObjectArray rights = database.getAllRights();
for (int i = 0; i < rights.size(); i++) {
Right right = (Right) rights.get(i);
Index: src/main/org/h2/engine/Database.java
===================================================================
--- src/main/org/h2/engine/Database.java (revision 928)
+++ src/main/org/h2/engine/Database.java (working copy)
@@ -21,6 +21,7 @@
import org.h2.command.dml.SetTypes;
import org.h2.constant.ErrorCode;
import org.h2.constant.SysProperties;
+import org.h2.engine.DbObject;
import org.h2.index.Cursor;
import org.h2.index.Index;
import org.h2.index.IndexType;
@@ -1565,6 +1566,13 @@
return;
}
}
+ if (obj.getType() == DbObject.INDEX) {
+ Index index = (Index) obj;
+ if (index.getTable().getTemporary() && !
index.getTable().getGlobalTemporary()) {
+ session.removeLocalTempTableIndex(index);
+ return;
+ }
+ }
checkWritingAllowed();
Comment comment = findComment(obj);
if (comment != null) {
Index: src/main/org/h2/engine/Session.java
===================================================================
--- src/main/org/h2/engine/Session.java (revision 928)
+++ src/main/org/h2/engine/Session.java (working copy)
@@ -20,6 +20,7 @@
import org.h2.command.dml.SetTypes;
import org.h2.constant.ErrorCode;
import org.h2.constant.SysProperties;
+import org.h2.index.Index;
import org.h2.jdbc.JdbcConnection;
import org.h2.log.InDoubtTransaction;
import org.h2.log.LogSystem;
@@ -65,6 +66,7 @@
private HashMap savepoints;
private Exception stackTrace = new Exception();
private HashMap localTempTables;
+ private HashMap localTempTablesIndexes;
private int throttle;
private long lastThrottle;
private Command currentCommand;
@@ -204,6 +206,59 @@
table.removeChildrenAndResources(this);
}
+ /**
+ * Get the local temporary index if one exists with that name, or
null if not.
+ *
+ * @param name
+ * the table name
+ * @return the table, or null
+ */
+ public Index findLocalTempTableIndex(String name) {
+ Index t = null;
+ if (localTempTablesIndexes != null) {
+ t = (Index) localTempTablesIndexes.get(name);
+ }
+ return t;
+ }
+
+ public ObjectArray getLocalTempTablesIndexes() {
+ if (localTempTablesIndexes == null) {
+ return new ObjectArray();
+ }
+ ObjectArray list = new
ObjectArray(localTempTablesIndexes.values());
+ return list;
+ }
+
+ /**
+ * Add a local temporary index to this session.
+ *
+ * @param index
+ * the index to add
+ * @throws SQLException
+ * if a index with this name already exists
+ */
+ public void addLocalTempTableIndex(Index index) throws
SQLException {
+ if (localTempTablesIndexes == null) {
+ localTempTablesIndexes = new HashMap();
+ }
+ if (localTempTablesIndexes.get(index.getName()) != null) {
+ throw
Message.getSQLException(ErrorCode.INDEX_ALREADY_EXISTS_1,
index.getSQL());
+ }
+ localTempTablesIndexes.put(index.getName(), index);
+ }
+
+ /**
+ * Drop and remove the given local temporary index from this
session.
+ *
+ * @param index
+ * the index
+ */
+ public void removeLocalTempTableIndex(Index index) throws
SQLException {
+ localTempTablesIndexes.remove(index.getName());
+ index.removeChildrenAndResources(this);
+ }
+
+ @Override
protected void finalize() {
if (!SysProperties.runFinalize) {
return;
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "H2
Database" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/h2-database?hl=en
-~----------~----~----~----~------~----~------~--~---