diff --git a/core/src/main/java/com/orientechnologies/orient/core/sql/executor/CreateRecordStep.java b/core/src/main/java/com/orientechnologies/orient/core/sql/executor/CreateRecordStep.java index 82b6b196cc2..2a8ad1c313d 100644 --- a/core/src/main/java/com/orientechnologies/orient/core/sql/executor/CreateRecordStep.java +++ b/core/src/main/java/com/orientechnologies/orient/core/sql/executor/CreateRecordStep.java @@ -4,15 +4,19 @@ import com.orientechnologies.orient.core.command.OCommandContext; import com.orientechnologies.orient.core.record.impl.ODocument; import com.orientechnologies.orient.core.sql.executor.resultset.OExecutionStream; +import java.util.Optional; /** @author Luigi Dell'Aquila (l.dellaquila-(at)-orientdb.com) */ public class CreateRecordStep extends AbstractExecutionStep { private int total = 0; + private Optional cl; - public CreateRecordStep(OCommandContext ctx, int total, boolean profilingEnabled) { + public CreateRecordStep( + OCommandContext ctx, int total, boolean profilingEnabled, Optional cl) { super(ctx, profilingEnabled); this.total = total; + this.cl = cl; } @Override @@ -22,6 +26,9 @@ public OExecutionStream internalStart(OCommandContext ctx) throws OTimeoutExcept } private OResult produce(OCommandContext ctx) { + if (cl.isPresent()) { + return new OUpdatableResult((ODocument) ctx.getDatabase().newInstance(cl.get())); + } return new OUpdatableResult((ODocument) ctx.getDatabase().newInstance()); } diff --git a/core/src/main/java/com/orientechnologies/orient/core/sql/executor/OInsertExecutionPlanner.java b/core/src/main/java/com/orientechnologies/orient/core/sql/executor/OInsertExecutionPlanner.java index 9d68d376461..3de7bbb5cc6 100755 --- a/core/src/main/java/com/orientechnologies/orient/core/sql/executor/OInsertExecutionPlanner.java +++ b/core/src/main/java/com/orientechnologies/orient/core/sql/executor/OInsertExecutionPlanner.java @@ -3,6 +3,7 @@ import com.orientechnologies.orient.core.command.OCommandContext; import com.orientechnologies.orient.core.db.ODatabaseSession; import com.orientechnologies.orient.core.index.OIndexAbstract; +import com.orientechnologies.orient.core.metadata.OMetadataInternal; import com.orientechnologies.orient.core.metadata.schema.OClass; import com.orientechnologies.orient.core.metadata.schema.OSchema; import com.orientechnologies.orient.core.sql.parser.OCluster; @@ -18,6 +19,7 @@ import com.orientechnologies.orient.core.sql.parser.OUpdateItem; import java.util.ArrayList; import java.util.List; +import java.util.Optional; /** Created by luigidellaquila on 08/08/16. */ public class OInsertExecutionPlanner { @@ -57,10 +59,10 @@ public OInsertExecutionPlan createExecutionPlan(OCommandContext ctx, boolean ena } else { if (selectStatement != null) { handleInsertSelect(result, this.selectStatement, ctx, enableProfiling); + handleTargetClass(result, ctx, enableProfiling); } else { handleCreateRecord(result, this.insertBody, ctx, enableProfiling); } - handleTargetClass(result, ctx, enableProfiling); handleSetFields(result, insertBody, ctx, enableProfiling); ODatabaseSession database = ctx.getDatabase(); if (targetCluster != null) { @@ -134,7 +136,14 @@ private void handleSetFields( private void handleTargetClass( OInsertExecutionPlan result, OCommandContext ctx, boolean profilingEnabled) { ODatabaseSession database = ctx.getDatabase(); - OSchema schema = database.getMetadata().getSchema(); + Optional tc = resolveTargetClass(database); + if (tc.isPresent()) { + result.chain(new SetDocumentClassStep(tc.get(), ctx, profilingEnabled)); + } + } + + protected Optional resolveTargetClass(ODatabaseSession database) { + OSchema schema = ((OMetadataInternal) database.getMetadata()).getImmutableSchemaSnapshot(); OIdentifier tc = null; if (targetClass != null) { tc = targetClass; @@ -157,7 +166,9 @@ private void handleTargetClass( } } if (tc != null) { - result.chain(new SetDocumentClassStep(tc, ctx, profilingEnabled)); + return Optional.of(tc.getStringValue()); + } else { + return Optional.empty(); } } @@ -185,7 +196,8 @@ private void handleCreateRecord( tot = body.getContent().size(); } } - result.chain(new CreateRecordStep(ctx, tot, profilingEnabled)); + Optional cl = resolveTargetClass(ctx.getDatabase()); + result.chain(new CreateRecordStep(ctx, tot, profilingEnabled, cl)); } private void handleInsertSelect( diff --git a/core/src/main/java/com/orientechnologies/orient/core/sql/executor/SetDocumentClassStep.java b/core/src/main/java/com/orientechnologies/orient/core/sql/executor/SetDocumentClassStep.java index 83447a0d791..2a5de954508 100644 --- a/core/src/main/java/com/orientechnologies/orient/core/sql/executor/SetDocumentClassStep.java +++ b/core/src/main/java/com/orientechnologies/orient/core/sql/executor/SetDocumentClassStep.java @@ -5,7 +5,6 @@ import com.orientechnologies.orient.core.db.record.OIdentifiable; import com.orientechnologies.orient.core.record.impl.ODocument; import com.orientechnologies.orient.core.sql.executor.resultset.OExecutionStream; -import com.orientechnologies.orient.core.sql.parser.OIdentifier; /** * Assigns a class to documents coming from upstream @@ -15,10 +14,9 @@ public class SetDocumentClassStep extends AbstractExecutionStep { private final String targetClass; - public SetDocumentClassStep( - OIdentifier targetClass, OCommandContext ctx, boolean profilingEnabled) { + public SetDocumentClassStep(String targetClass, OCommandContext ctx, boolean profilingEnabled) { super(ctx, profilingEnabled); - this.targetClass = targetClass.getStringValue(); + this.targetClass = targetClass; } @Override diff --git a/server/src/test/java/com/orientechnologies/orient/core/db/ODatabasePoolRemoteTest.java b/server/src/test/java/com/orientechnologies/orient/core/db/ODatabasePoolRemoteTest.java index f1984833a1f..eaf940af691 100644 --- a/server/src/test/java/com/orientechnologies/orient/core/db/ODatabasePoolRemoteTest.java +++ b/server/src/test/java/com/orientechnologies/orient/core/db/ODatabasePoolRemoteTest.java @@ -1,12 +1,15 @@ package com.orientechnologies.orient.core.db; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import com.orientechnologies.common.io.OFileUtils; import com.orientechnologies.orient.core.Orient; import com.orientechnologies.orient.core.config.OGlobalConfiguration; import com.orientechnologies.orient.core.db.document.ODatabaseDocument; +import com.orientechnologies.orient.core.record.OVertex; import com.orientechnologies.orient.core.record.impl.ODocument; +import com.orientechnologies.orient.core.sql.executor.OResultSet; import com.orientechnologies.orient.server.OServer; import java.io.File; import org.junit.After; @@ -76,6 +79,59 @@ public void testPoolDoubleClose() { orientDb.close(); } + @Test + public void testPoolCorrectVertex() { + OrientDB orientDb = + new OrientDB( + "remote:localhost:", + "root", + "root", + OrientDBConfig.builder().addConfig(OGlobalConfiguration.DB_POOL_MAX, 1).build()); + + if (!orientDb.exists("test")) { + orientDb.execute( + "create database test memory users (admin identified by 'admin' role admin)"); + } + + ODatabasePool pool = new ODatabasePool(orientDb, "test", "admin", "admin"); + ODatabaseDocument db = pool.acquire(); + db.command("create vertex v set name='abc'").close(); + OResultSet res = db.query("select * from V "); + assertTrue(res.next().getElement().get() instanceof OVertex); + db.close(); + pool.close(); + orientDb.close(); + } + + @Test + public void testPoolCorrectVertexCloseOpen() { + OrientDB orientDb = + new OrientDB( + "remote:localhost:", + "root", + "root", + OrientDBConfig.builder().addConfig(OGlobalConfiguration.DB_POOL_MAX, 1).build()); + + if (!orientDb.exists("test")) { + orientDb.execute( + "create database test memory users (admin identified by 'admin' role admin)"); + } + + ODatabasePool pool = new ODatabasePool(orientDb, "test", "admin", "admin"); + ODatabaseDocument db = pool.acquire(); + db.command("create vertex v set name='abc'").close(); + OResultSet res = db.query("select * from V "); + assertTrue(res.next().getElement().get() instanceof OVertex); + db.close(); + + db = pool.acquire(); + res = db.query("select * from V "); + assertTrue(res.next().getElement().get() instanceof OVertex); + + pool.close(); + orientDb.close(); + } + @After public void after() {