Skip to content

Commit

Permalink
Use iterator on AbstractTuple instead of RighTupleImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
lucamolteni committed Sep 4, 2023
1 parent 791d3fb commit 4bdfc45
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 236 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.drools.core.common.BetaConstraints;
import org.drools.core.common.ReteEvaluator;
import org.drools.core.common.TupleSets;
import org.drools.core.reteoo.AbstractTuple;
import org.drools.core.reteoo.BetaMemory;
import org.drools.core.reteoo.FromNode;
import org.drools.core.reteoo.FromNode.FromMemory;
Expand Down Expand Up @@ -145,7 +146,7 @@ public void doLeftUpdates(FromNode fromNode,

betaConstraints.updateFromTuple(context, reteEvaluator, leftTuple);

FastIterator<RightTupleImpl> rightIt = LinkedList.rightTupleFastIterator;
FastIterator<AbstractTuple> rightIt = LinkedList.fastIterator;
for (final java.util.Iterator<?> it = dataProvider.getResults(leftTuple, reteEvaluator, fm.providerContext); it.hasNext(); ) {
final Object object = it.next();
if ( (object == null) || !resultClass.isAssignableFrom( object.getClass() ) ) {
Expand All @@ -161,7 +162,7 @@ public void doLeftUpdates(FromNode fromNode,
// previous match, so reevaluate and propagate modify
if (rightIt.next(rightTuple) != null) {
// handle the odd case where more than one object has the same hashcode/equals value
previousMatches.put(object, rightIt.next(rightTuple));
previousMatches.put(object, (RightTupleImpl) rightIt.next(rightTuple));
rightTuple.setNext(null);
}
}
Expand All @@ -175,7 +176,7 @@ public void doLeftUpdates(FromNode fromNode,
}

for (RightTupleImpl rightTuple : previousMatches.values()) {
for (RightTupleImpl current = rightTuple; current != null; current = rightIt.next(current)) {
for (RightTupleImpl current = rightTuple; current != null; current = (RightTupleImpl) rightIt.next(current)) {
deleteChildLeftTuple(propagationContext, trgLeftTuples, stagedLeftTuples, current.getFirstChild());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,11 +368,11 @@ public void setRightInput( ObjectSource rightInput ) {
rightInputIsRiaNode = NodeTypeEnums.RightInputAdapterNode == rightInput.getType();
}

public FastIterator<RightTupleImpl> getRightIterator( TupleMemory memory ) {
public FastIterator<AbstractTuple> getRightIterator( TupleMemory memory ) {
if ( this.indexedUnificationJoin ) {
return memory.rightTupleFullFastIterator();
return memory.fullFastIterator();
} else {
return memory.rightTupleFastIterator();
return memory.fastIterator();
}
}

Expand All @@ -386,7 +386,7 @@ public RightTupleImpl getFirstRightTuple(final Tuple leftTuple,
}
}

public FastIterator<Tuple> getLeftIterator(TupleMemory memory) {
public FastIterator<AbstractTuple> getLeftIterator(TupleMemory memory) {
if (rightInputIsRiaNode) {
return FastIterator.NullFastIterator.INSTANCE;
} else {
Expand Down
18 changes: 3 additions & 15 deletions drools-core/src/main/java/org/drools/core/reteoo/TupleMemory.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,32 +54,20 @@ public boolean isComparison() {

Iterator<Tuple> iterator();

FastIterator<Tuple> fastIterator();

/**
* Avoid using the same iterator for Right due to type pollution
* @return
*/
FastIterator<RightTupleImpl> rightTupleFastIterator();
FastIterator<AbstractTuple> fastIterator();

/**
* Iterates the entire data structure, regardless of whether TupleMemory is hashed or not.
* @return
*/
FastIterator<Tuple> fullFastIterator();

/**
* Avoid using the same iterator for Right due to type pollution
* @return
*/
FastIterator<RightTupleImpl> rightTupleFullFastIterator();
FastIterator<AbstractTuple> fullFastIterator();

/**
* Iterator that resumes from the current RightTuple, regardless of whether the TupleMemory is hashed or not
* @param tuple
* @return
*/
FastIterator<Tuple> fullFastIterator( Tuple tuple );
FastIterator<AbstractTuple> fullFastIterator( AbstractTuple tuple );

Tuple[] toArray();

Expand Down
14 changes: 2 additions & 12 deletions drools-core/src/main/java/org/drools/core/util/LinkedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.drools.core.util;

import org.drools.core.reteoo.AbstractTuple;
import org.drools.core.reteoo.RightTuple;
import org.drools.core.reteoo.RightTupleImpl;
import org.drools.core.reteoo.Tuple;
Expand Down Expand Up @@ -54,8 +55,7 @@ public class LinkedList<T extends LinkedListNode<T>>

private int size;

public static final FastIterator<Tuple> fastIterator = new LinkedListFastIterator(); // contains no state, so ok to be static
public static final FastIterator<RightTupleImpl> rightTupleFastIterator = new RightTupleLinkedListFastIterator(); // contains no state, so ok to be static
public static final FastIterator<AbstractTuple> fastIterator = new LinkedListFastIterator(); // contains no state, so ok to be static

/**
* Construct an empty <code>LinkedList</code>
Expand Down Expand Up @@ -370,16 +370,6 @@ public java.util.Iterator<T> javaUtilIterator() {
return new JavaUtilIterator<>( this );
}

public static class RightTupleLinkedListFastIterator implements FastIterator<RightTupleImpl> {
public RightTupleImpl next(RightTupleImpl object) {
return (RightTupleImpl) object.getNext();
}

public boolean isFullIterator() {
return false;
}
}

/**
* Returns a list iterator
* @return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.drools.core.util.index;

import org.drools.base.util.FieldIndex;
import org.drools.core.reteoo.AbstractTuple;
import org.drools.core.reteoo.RightTupleImpl;
import org.drools.core.reteoo.Tuple;
import org.drools.core.reteoo.TupleMemory;
Expand All @@ -40,7 +41,6 @@ public class TupleIndexHashTable extends AbstractHashTable implements TupleMemor
private transient FieldIndexHashTableFullIterator tupleValueFullIterator;

private transient FullFastIterator fullFastIterator;
private transient RightTupleFullFastIterator rightTuplefullFastIterator;

private int factSize;

Expand Down Expand Up @@ -122,16 +122,11 @@ public Iterator<Tuple> iterator() {
return this.tupleValueFullIterator;
}

public FastIterator<Tuple> fastIterator() {
public FastIterator<AbstractTuple> fastIterator() {
return LinkedList.fastIterator;
}

@Override
public FastIterator<RightTupleImpl> rightTupleFastIterator() {
return LinkedList.rightTupleFastIterator;
}

public FastIterator<Tuple> fullFastIterator() {
public FastIterator<AbstractTuple> fullFastIterator() {
if ( fullFastIterator == null ) {
fullFastIterator = new FullFastIterator( this.table );
} else {
Expand All @@ -140,28 +135,13 @@ public FastIterator<Tuple> fullFastIterator() {
return fullFastIterator;
}

@Override
public FastIterator<RightTupleImpl> rightTupleFullFastIterator() {
if ( fullFastIterator == null ) {
rightTuplefullFastIterator = new RightTupleFullFastIterator( this.table );
} else {
rightTuplefullFastIterator.reset(this.table);
}
return rightTuplefullFastIterator;
}


public FastIterator<Tuple> fullFastIterator(Tuple tuple) {
public FastIterator<AbstractTuple> fullFastIterator(AbstractTuple tuple) {
fullFastIterator.resume(tuple.getMemory(), this.table);
return fullFastIterator;
}

public FastIterator<RightTupleImpl> rightTupleFullFastIterator(Tuple tuple) {
rightTuplefullFastIterator.resume(tuple.getMemory(), this.table);
return rightTuplefullFastIterator;
}

public static class FullFastIterator implements FastIterator<Tuple> {
public static class FullFastIterator implements FastIterator<AbstractTuple> {
private TupleList[] table;
private int row;

Expand All @@ -177,7 +157,7 @@ public void resume(TupleList target, TupleList[] table) {
row++; // row always points to the row after the current list
}

public Tuple next(Tuple tuple) {
public AbstractTuple next(AbstractTuple tuple) {
TupleList list = null;
if ( tuple != null ) {
list = tuple.getMemory(); // assumes you do not pass in a null RightTuple
Expand All @@ -199,7 +179,7 @@ public Tuple next(Tuple tuple) {

if ( list != null ) {
// we have a bucket so assign the frist LeftTuple and return
tuple = list.getFirst( );
tuple = (AbstractTuple) list.getFirst( );
return tuple;
}
}
Expand All @@ -213,7 +193,7 @@ public Tuple next(Tuple tuple) {
// try the next bucket if we have a shared array position
if ( list != null ) {
// if we have another bucket, assign the first LeftTuple and return
tuple = list.getFirst( );
tuple = (AbstractTuple) list.getFirst( );
return tuple;
}
}
Expand Down Expand Up @@ -251,77 +231,6 @@ public int getResizeHashcode(TupleList entry) {
return entry.hashCode();
}

public static class RightTupleFullFastIterator implements FastIterator<RightTupleImpl> {
private TupleList[] table;
private int row;

public RightTupleFullFastIterator(TupleList[] table) {
this.table = table;
this.row = 0;
}

public void resume(TupleList target, TupleList[] table) {
this.table = table;
row = indexOf( target.hashCode(),
this.table.length );
row++; // row always points to the row after the current list
}

public RightTupleImpl next(RightTupleImpl tuple) {
TupleList list = null;
if ( tuple != null ) {
list = tuple.getMemory(); // assumes you do not pass in a null RightTuple
}

int length = table.length;

while ( this.row <= length ) {
// check if there is a current bucket
while ( list == null ) {
if ( this.row < length ) {
// iterate while there is no current bucket, trying each array position
list = this.table[this.row];
this.row++;
} else {
// we've scanned the whole table and nothing is left, so return null
return null;
}

if ( list != null ) {
// we have a bucket so assign the frist LeftTuple and return
tuple = (RightTupleImpl)list.getFirst( );
return tuple;
}
}

tuple = (RightTupleImpl)tuple.getNext();
if ( tuple != null ) {
// we have a next tuple so return
return tuple;
} else {
list = list.getNext();
// try the next bucket if we have a shared array position
if ( list != null ) {
// if we have another bucket, assign the first LeftTuple and return
tuple = (RightTupleImpl)list.getFirst( );
return tuple;
}
}
}
return null;
}

public boolean isFullIterator() {
return true;
}

public void reset(TupleList[] table) {
this.table = table;
this.row = 0;
}

}

public static class FieldIndexHashTableFullIterator
implements
Iterator<Tuple> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.List;

import org.drools.base.util.index.ConstraintTypeOperator;
import org.drools.core.reteoo.AbstractTuple;
import org.drools.core.reteoo.RightTupleImpl;
import org.drools.core.reteoo.TupleMemory;
import org.drools.core.reteoo.Tuple;
Expand Down Expand Up @@ -124,31 +125,21 @@ public Iterator<Tuple> iterator() {
return new FastIterator.IteratorAdapter(fastIterator(), firstTuple);
}

public FastIterator<Tuple> fastIterator() {
public FastIterator<AbstractTuple> fastIterator() {
return new TupleFastIterator();
}

public FastIterator<Tuple> fullFastIterator() {
public FastIterator<AbstractTuple> fullFastIterator() {
return new TupleFastIterator();
}

@Override
public FastIterator<RightTupleImpl> rightTupleFastIterator() {
return new RightTupleFastIterator();
}

public FastIterator<Tuple> fullFastIterator(Tuple leftTuple) {
public FastIterator<AbstractTuple> fullFastIterator(AbstractTuple leftTuple) {
FastIterator fastIterator = fullFastIterator();
Comparable key = getLeftIndexedValue(leftTuple);
fastIterator.next(getNext(key, true));
return fastIterator;
}

@Override
public FastIterator<RightTupleImpl> rightTupleFullFastIterator() {
return new LinkedList.RightTupleLinkedListFastIterator();
}

private Tuple getNext(Comparable key, boolean first) {
return left ? getNextLeft( key, first ) : getNextRight( key, first );
}
Expand Down Expand Up @@ -212,18 +203,18 @@ public static Comparable coerceType(FieldIndex index, Comparable treeRootKey, Co
return key;
}

public class TupleFastIterator implements FastIterator<Tuple> {
public Tuple next(Tuple tuple) {
public class TupleFastIterator implements FastIterator<AbstractTuple> {
public AbstractTuple next(AbstractTuple tuple) {
if (tuple == null) {
Node<Comparable<Comparable>> firstNode = tree.first();
return firstNode == null ? null : firstNode.getFirst();
return firstNode == null ? null : (AbstractTuple) firstNode.getFirst();
}
Tuple next = tuple.getNext();
AbstractTuple next = tuple.getNext();
if (next != null) {
return next;
}
Comparable key = getLeftIndexedValue(tuple);
return getNext(key, false);
return (AbstractTuple) getNext(key, false);
}

public boolean isFullIterator() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,26 +201,15 @@ public IndexType getIndexType() {
return TupleMemory.IndexType.NONE;
}

public FastIterator<Tuple> fastIterator() {
public FastIterator<AbstractTuple> fastIterator() {
return LinkedList.fastIterator; // contains no state, so ok to be static
}

public FastIterator<Tuple> fullFastIterator() {
public FastIterator<AbstractTuple> fullFastIterator() {
return LinkedList.fastIterator; // contains no state, so ok to be static
}

@Override
public FastIterator<RightTupleImpl> rightTupleFastIterator() {
return LinkedList.rightTupleFastIterator;
}


@Override
public FastIterator<RightTupleImpl> rightTupleFullFastIterator() {
return LinkedList.rightTupleFastIterator;
}

public FastIterator<Tuple> fullFastIterator(Tuple tuple) {
public FastIterator<AbstractTuple> fullFastIterator(AbstractTuple tuple) {
return LinkedList.fastIterator; // contains no state, so ok to be static
}

Expand Down
Loading

0 comments on commit 4bdfc45

Please sign in to comment.