Skip to content

Commit

Permalink
Fix TRAVERSE in batch, with newly created vertices and edges (in tx)
Browse files Browse the repository at this point in the history
Resolves: #9096
  • Loading branch information
luigidellaquila committed Dec 27, 2019
1 parent f0a7aa5 commit a2c4cc9
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.orientechnologies.orient.core.command.OCommandContext;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.db.record.ridbag.ORidBag;
import com.orientechnologies.orient.core.sql.parser.OInteger;
import com.orientechnologies.orient.core.sql.parser.OTraverseProjectionItem;
import com.orientechnologies.orient.core.sql.parser.OWhereClause;
Expand Down Expand Up @@ -57,7 +58,7 @@ private OResult toTraverseResult(OResult item) {
OTraverseResult res = null;
if (item instanceof OTraverseResult) {
res = (OTraverseResult) item;
} else if (item.isElement() && item.getElement().get().getIdentity().isPersistent()) {
} else if (item.isElement() && item.getElement().get().getIdentity().isValid()) {
res = new OTraverseResult();
res.setElement(item.getElement().get());
res.depth = 0;
Expand All @@ -72,7 +73,7 @@ private OResult toTraverseResult(OResult item) {
} else {
res = new OTraverseResult();
for (String key : item.getPropertyNames()) {
res.setProperty(key, item.getProperty(key));
res.setProperty(key, convert(item.getProperty(key)));
}
for (String md : item.getMetadataKeys()) {
res.setMetadata(md, item.getMetadata(md));
Expand All @@ -82,7 +83,16 @@ private OResult toTraverseResult(OResult item) {
return res;
}

@Override
public Object convert(Object value) {
if (value instanceof ORidBag) {
List result = new ArrayList();
((ORidBag) value).forEach(x -> result.add(x));
return result;
}
return value;
}

@Override
protected void fetchNextResults(OCommandContext ctx, int nRecords) {
if (!this.entryPoints.isEmpty()) {
OTraverseResult item = (OTraverseResult) this.entryPoints.remove(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.orientechnologies.orient.core.id.ORID;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

Expand All @@ -24,6 +25,8 @@ public class ORidSet implements Set<ORID> {
*/
protected long[][][] content = new long[8][][];

protected Set<ORID> negatives = new HashSet<>();

long size = 0;

protected int maxArraySize;
Expand All @@ -37,22 +40,22 @@ public ORidSet() {

/**
*
* @param bucketSize
* @param bucketSize
*/
public ORidSet(int bucketSize) {
maxArraySize = bucketSize;
}

@Override public int size() {
return size <= Integer.MAX_VALUE ? (int) size : Integer.MAX_VALUE;
return size + negatives.size() <= Integer.MAX_VALUE ? (int) size + negatives.size() : Integer.MAX_VALUE;
}

@Override public boolean isEmpty() {
return size == 0L;
}

@Override public boolean contains(Object o) {
if (size == 0L) {
if (size == 0L && negatives.size() == 0) {
return false;
}
if (!(o instanceof ORID)) {
Expand All @@ -65,7 +68,7 @@ public ORidSet(int bucketSize) {
int cluster = identifiable.getClusterId();
long position = identifiable.getClusterPosition();
if (cluster < 0 || position < 0) {
return false;
return negatives.contains(identifiable);
}
long positionByte = (position / 63);
int positionBit = (int) (position % 63);
Expand Down Expand Up @@ -113,7 +116,7 @@ public ORidSet(int bucketSize) {
int cluster = identifiable.getClusterId();
long position = identifiable.getClusterPosition();
if (cluster < 0 || position < 0) {
throw new IllegalArgumentException("negative RID");//TODO
return negatives.add(identifiable);
}
long positionByte = (position / 63);
int positionBit = (int) (position % 63);
Expand Down Expand Up @@ -195,7 +198,7 @@ private static long[] expandClusterArray(long[] original, int positionByteInt) {
int cluster = identifiable.getClusterId();
long position = identifiable.getClusterPosition();
if (cluster < 0 || position < 0) {
throw new IllegalArgumentException("negative RID");//TODO
return negatives.remove(o);
}
long positionByte = (position / 63);
int positionBit = (int) (position % 63);
Expand Down Expand Up @@ -258,6 +261,7 @@ private static long[] expandClusterArray(long[] original, int positionByteInt) {
@Override public void clear() {
content = new long[8][][];
size = 0;
this.negatives.clear();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,25 @@
*/
public class ORidSetIterator implements Iterator<ORID> {

private final Iterator<ORID> negativesIterator;
private ORidSet set;
int currentCluster = -1;
long currentId = -1;

ORidSetIterator(ORidSet set) {
this.set = set;
this.negativesIterator = set.negatives.iterator();
fetchNext();
}

@Override public boolean hasNext() {
return currentCluster >= 0;
return negativesIterator.hasNext() || currentCluster >= 0;
}

@Override public ORID next() {
if (negativesIterator.hasNext()) {
return negativesIterator.next();
}
if (!hasNext()) {
throw new IllegalStateException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.Collection;

/**
* @author Luigi Dell'Aquila (l.dellaquila-(at)-orientdb.com)
*/
Expand Down Expand Up @@ -165,4 +167,34 @@ public void testBreadthFirst() {
result.close();
}


@Test
public void testTraverseInBatchTx(){
String script = "";
script += "";

script += "drop class testTraverseInBatchTx_V if exists unsafe;";
script += "create class testTraverseInBatchTx_V extends V;";
script += "create property testTraverseInBatchTx_V.name STRING;";
script += "drop class testTraverseInBatchTx_E if exists unsafe;";
script += "create class testTraverseInBatchTx_E extends E;";


script += "begin;";
script += "insert into testTraverseInBatchTx_V(name) values ('a'), ('b'), ('c');";
script += "create edge testTraverseInBatchTx_E from (select from testTraverseInBatchTx_V where name = 'a') to (select from testTraverseInBatchTx_V where name = 'b');";
script += "create edge testTraverseInBatchTx_E from (select from testTraverseInBatchTx_V where name = 'b') to (select from testTraverseInBatchTx_V where name = 'c');";
script += "let top = (select * from (traverse in('testTraverseInBatchTx_E') from (select from testTraverseInBatchTx_V where name='c')) where in('testTraverseInBatchTx_E').size() == 0);";
script += "commit;";
script += "return $top";

OResultSet result = db.execute("sql", script);
Assert.assertTrue(result.hasNext());
OResult item = result.next();
Object val = item.getProperty("value");
Assert.assertTrue(val instanceof Collection);
Assert.assertEquals(1, ((Collection)val).size());
result.close();

}
}

0 comments on commit a2c4cc9

Please sign in to comment.