Skip to content

Commit

Permalink
Fix live queries with ridbags
Browse files Browse the repository at this point in the history
Resolves: #8594
  • Loading branch information
luigidellaquila committed Oct 11, 2018
1 parent 8892a7b commit 3ff1d93
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import com.orientechnologies.orient.core.config.OGlobalConfiguration;
import com.orientechnologies.orient.core.db.ODatabaseInternal;
import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.db.record.ORecordOperation;
import com.orientechnologies.orient.core.db.record.ridbag.ORidBag;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.executor.OResult;
import com.orientechnologies.orient.core.sql.executor.OResultInternal;
Expand Down Expand Up @@ -214,7 +216,7 @@ private static OLiveQueryOp prevousUpdate(List<OLiveQueryOp> list, ODocument doc
private static OResultInternal calculateBefore(ODocument iDocument) {
OResultInternal result = new OResultInternal();
for (String prop : iDocument.getPropertyNames()) {
result.setProperty(prop, iDocument.getProperty(prop));
result.setProperty(prop, unboxRidbags(iDocument.getProperty(prop)));
}
result.setProperty("@rid", iDocument.getIdentity());
result.setProperty("@class", iDocument.getClassName());
Expand All @@ -228,11 +230,23 @@ private static OResultInternal calculateBefore(ODocument iDocument) {
private static OResultInternal calculateAfter(ODocument iDocument) {
OResultInternal result = new OResultInternal();
for (String prop : iDocument.getPropertyNames()) {
result.setProperty(prop, iDocument.getProperty(prop));
result.setProperty(prop, unboxRidbags(iDocument.getProperty(prop)));
}
result.setProperty("@rid", iDocument.getIdentity());
result.setProperty("@class", iDocument.getClassName());
result.setProperty("@version", iDocument.getVersion() + 1);
return result;
}

private static Object unboxRidbags(Object value) {
if (value instanceof ORidBag) {
List<OIdentifiable> result = new ArrayList<>(((ORidBag) value).size());
for (OIdentifiable oIdentifiable : (ORidBag) value) {
result.add(oIdentifiable);
}
return result;
}
return value;
}

}
148 changes: 148 additions & 0 deletions tests/src/test/java/com/orientechnologies/OrientDB3Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package com.orientechnologies;

import com.orientechnologies.orient.core.db.ODatabaseSession;
import com.orientechnologies.orient.core.db.ODatabaseType;
import com.orientechnologies.orient.core.db.OrientDB;
import com.orientechnologies.orient.core.db.OrientDBConfig;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.metadata.schema.OType;
import com.orientechnologies.orient.core.record.OEdge;
import com.orientechnologies.orient.core.record.OVertex;
import com.orientechnologies.orient.core.sql.executor.OResult;
import com.orientechnologies.orient.core.sql.executor.OResultSet;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class OrientDB3Test {
final String dbUrl = "remote:localhost";

final String dbName = "demo";

OrientDB orientDB;

final String teacher = "Teacher";

final String student = "Student";

final String teach = "teach";

final String name = "name";

OClass edge;

@Before
public void createDatabase() {
this.orientDB = new OrientDB(dbUrl, "root", "root", OrientDBConfig.defaultConfig());
orientDB.createIfNotExists(dbName, ODatabaseType.PLOCAL);
createSchema();
}

private void createSchema() {
ODatabaseSession db = getSession();

OClass t = db.getClass(teacher);
if (t == null) {
System.out.println("Create teacher class");
t = db.createVertexClass(teacher);
}
if (t.getProperty(name) == null) {
System.out.println("Create name property for teacher");
t.createProperty(name, OType.STRING);
}

OClass s = db.getClass(student);
if (s == null) {
System.out.println("Create student class");
s = db.createVertexClass(student);
}
if (s.getProperty(name) == null) {
System.out.println("Create name property for student");
s.createProperty(name, OType.STRING);
}

if ((edge = db.getClass(teach)) == null) {
System.out.println("Create edge for teacher and student");
edge = db.createEdgeClass(teach);
}
db.close();
}

ODatabaseSession getSession() {
return orientDB.open(dbName, "admin", "admin");
}

@Test
public void testMixJavaApiAndSqlWithTraverseInTransaction() {
try (ODatabaseSession db = getSession()) {
String teacherName = "teacher1";
String studentName = "student1";
String query1 = "TRAVERSE in,out from " + teacher + " while name = ?";
String query2 = "select from (traverse out('" + teach + "') from (select from Teacher where name=?)) where @class='" + student + "'";

db.begin();
OVertex teacherV = db.newVertex(teacher);
teacherV.setProperty(name, teacherName);

OVertex studentV = db.newVertex(student);
studentV.setProperty(name, studentName);

OEdge teachE = db.newEdge(teacherV, studentV, edge);
// teacherV.save();
// studentV.save();
teachE.save();

System.out.println("----------------------");
System.out.println("After saving and before committing, the query1 results:");

OResultSet rs = db.query(query1, teacherName);
while (rs.hasNext()) {
OResult row = rs.next();
System.out.println(row);
}
rs.close();
System.out.println("----------------------\n\n");

System.out.println("----------------------");
System.out.println("After saving and before committing, the query2 results:");
rs = db.query(query2, teacherName);
while (rs.hasNext()) {
OResult row = rs.next();
System.out.println(row);
}
rs.close();
System.out.println("----------------------\n\n");

db.commit();

System.out.println("----------------------");
System.out.println("After committing, the query1 results:");

rs = db.query(query1, teacherName);
while (rs.hasNext()) {
OResult row = rs.next();
System.out.println(row);
}
rs.close();
System.out.println("----------------------\n\n");

System.out.println("----------------------");
System.out.println("After committing, the query2 results:");
rs = db.query(query2, teacherName);
while (rs.hasNext()) {
OResult row = rs.next();
System.out.println(row);
}
rs.close();
System.out.println("----------------------\n\n");

}
}

@After
public void releaseResource() {
if (orientDB != null) {
orientDB.close();
}
}
}

0 comments on commit 3ff1d93

Please sign in to comment.