[jboss-svn-commits] JBL Code SVN: r6906 - labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Wed Oct 18 20:19:50 EDT 2006
Author: mark.proctor at jboss.com
Date: 2006-10-18 20:19:48 -0400 (Wed, 18 Oct 2006)
New Revision: 6906
Removed:
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/FieldIndexHashTable.java
Modified:
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/CompositeFieldIndexHashTable.java
Log:
JBRULES-526 Implement a CompositeIndex for Facts
-tidy ups
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/CompositeFieldIndexHashTable.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/CompositeFieldIndexHashTable.java 2006-10-18 23:55:15 UTC (rev 6905)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/CompositeFieldIndexHashTable.java 2006-10-19 00:19:48 UTC (rev 6906)
@@ -8,7 +8,6 @@
import org.drools.reteoo.ReteTuple;
import org.drools.rule.Declaration;
import org.drools.spi.FieldExtractor;
-import org.drools.util.AbstractHashTable.ObjectComparator;
import org.drools.util.ObjectHashMap.ObjectEntry;
public class CompositeFieldIndexHashTable extends AbstractHashTable
@@ -345,12 +344,6 @@
public int hashCodeOf(Object object);
- // public void setCachedValue(Object object);
- //
- // public void setCachedValue(ReteTuple tuple);
-
- // public boolean equal(Object object);
-
public boolean equal(Object object1,
Object object2);
@@ -395,19 +388,6 @@
return this.comparator.rehash( hashCode );
}
- // public void setCachedValue(Object object) {
- // this.cachedValue = extractor.getValue( object );
- // }
- //
- // public void setCachedValue(ReteTuple tuple) {
- // this.cachedValue = declaration.getValue( tuple.get( declaration ).getObject() );
- // }
- //
- // public boolean equal(Object object) {
- // return this.comparator.equal( this.cachedValue,
- // this.extractor.getValue( object ) );
- // }
-
public boolean equal(Object object1,
ReteTuple tuple) {
Object value1 = this.extractor.getValue( object1 );
@@ -433,15 +413,11 @@
Index {
private FieldIndex index0;
private FieldIndex index1;
-
-
+
private int startResult;
private ObjectComparator comparator;
- // private Object cachedValue0;
- // private Object cachedValue1;
-
public DoubleCompositeIndex(FieldIndex[] indexes,
int startResult,
ObjectComparator comparator) {
@@ -477,23 +453,6 @@
return this.comparator.rehash( hashCode );
}
- // public void setCachedValue(Object object) {
- // this.cachedValue0 = index0.getExtractor().getValue( object );
- // this.cachedValue1 = index1.getExtractor().getValue( object );
- // }
- //
- // public void setCachedValue(ReteTuple tuple) {
- // this.cachedValue0 = index0.getDeclaration().getValue( tuple.get( index0.getDeclaration() ).getObject() );
- // this.cachedValue1 = index1.getDeclaration().getValue( tuple.get( index1.getDeclaration() ).getObject() );
- // }
- //
- // public boolean equal(Object object) {
- //
- // return this.comparator.equal( cachedValue0,
- // index0.getExtractor().getValue( object ) ) && this.comparator.equal( cachedValue1,
- // index1.getExtractor().getValue( object ) );
- // }
-
public boolean equal(Object object1,
ReteTuple tuple) {
Object value1 = index0.extractor.getValue( object1 );
@@ -548,10 +507,6 @@
private ObjectComparator comparator;
- // private Object cachedValue0;
- // private Object cachedValue1;
- // private Object cachedValue2;
-
public TripleCompositeIndex(FieldIndex[] indexes,
int startResult,
ObjectComparator comparator) {
@@ -567,13 +522,13 @@
public int hashCodeOf(Object object) {
int hashCode = startResult;
- Object value = index0.getExtractor().getValue( object );
+ Object value = index0.extractor.getValue( object );
hashCode += CompositeFieldIndexHashTable.PRIME * hashCode + ((value == null) ? 0 : value.hashCode());
- value = index1.getExtractor().getValue( object );
+ value = index1.extractor.getValue( object );
hashCode += CompositeFieldIndexHashTable.PRIME * hashCode + ((value == null) ? 0 : value.hashCode());
- value = index2.getExtractor().getValue( object );
+ value = index2.extractor.getValue( object );
hashCode += CompositeFieldIndexHashTable.PRIME * hashCode + ((value == null) ? 0 : value.hashCode());
return this.comparator.rehash( hashCode );
@@ -582,37 +537,18 @@
public int hashCodeOf(ReteTuple tuple) {
int hashCode = startResult;
- Object value = index0.getDeclaration().getValue( tuple.get( index0.getDeclaration() ).getObject() );
+ Object value = index0.declaration.getValue( tuple.get( index0.declaration ).getObject() );
hashCode += CompositeFieldIndexHashTable.PRIME * hashCode + ((value == null) ? 0 : value.hashCode());
- value = index1.getDeclaration().getValue( tuple.get( index1.getDeclaration() ).getObject() );
+ value = index1.declaration.getValue( tuple.get( index1.declaration ).getObject() );
hashCode += CompositeFieldIndexHashTable.PRIME * hashCode + ((value == null) ? 0 : value.hashCode());
- value = index2.getDeclaration().getValue( tuple.get( index2.getDeclaration() ).getObject() );
+ value = index2.declaration.getValue( tuple.get( index2.declaration ).getObject() );
hashCode += CompositeFieldIndexHashTable.PRIME * hashCode + ((value == null) ? 0 : value.hashCode());
return this.comparator.rehash( hashCode );
}
- // public void setCachedValue(Object object) {
- // this.cachedValue0 = index0.getExtractor().getValue( object );
- // this.cachedValue1 = index1.getExtractor().getValue( object );
- // this.cachedValue2 = index2.getExtractor().getValue( object );
- // }
- //
- // public void setCachedValue(ReteTuple tuple) {
- // this.cachedValue0 = index0.getDeclaration().getValue( tuple.get( index0.getDeclaration() ).getObject() );
- // this.cachedValue1 = index1.getDeclaration().getValue( tuple.get( index1.getDeclaration() ).getObject() );
- // this.cachedValue2 = index2.getDeclaration().getValue( tuple.get( index2.getDeclaration() ).getObject() );
- // }
- //
- // public boolean equal(Object object) {
- // return this.comparator.equal( cachedValue0,
- // index0.getExtractor().getValue( object ) ) && this.comparator.equal( cachedValue1,
- // index1.getExtractor().getValue( object ) ) && this.comparator.equal( cachedValue2,
- // index2.getExtractor().getValue( object ) );
- // }
-
public boolean equal(Object object1,
ReteTuple tuple) {
Object value1 = index0.extractor.getValue( object1 );
@@ -672,66 +608,6 @@
}
}
- private static class DefaultCompositeIndex
- implements
- Index {
- private FieldIndex[] indexes;
- private int startResult;
- private ObjectComparator comparator;
-
- public DefaultCompositeIndex(FieldIndex[] fieldIndexes,
- int startResult,
- ObjectComparator comparator) {
- this.startResult = startResult;
- this.indexes = fieldIndexes;
- this.comparator = comparator;
- }
-
- public int hashCodeOf(Object object) {
- int hashCode = startResult;
- for ( int i = 0, length = this.indexes.length; i < length; i++ ) {
- Object value = indexes[i].getExtractor().getValue( object );
- hashCode += CompositeFieldIndexHashTable.PRIME * hashCode + ((value == null) ? 0 : value.hashCode() );
- }
- return this.comparator.rehash( hashCode );
- }
-
- public int hashCodeOf(ReteTuple tuple) {
- int hashCode = startResult;
- for ( int i = 0, length = this.indexes.length; i < length; i++ ) {
- Object value = indexes[i].declaration.getValue( tuple.get( indexes[i].declaration ).getObject() );
- hashCode += CompositeFieldIndexHashTable.PRIME * hashCode + ((value == null) ? 0 : value.hashCode() );
- }
- return this.comparator.rehash( hashCode );
- }
-
- public boolean equal(Object object1,
- ReteTuple tuple) {
- for ( int i = 0, length = this.indexes.length; i < length; i++ ) {
- Object value1 = indexes[i].extractor.getValue( object1 );
- Object value2 = indexes[i].declaration.getValue( tuple.get( indexes[i].declaration ).getObject() );
- if ( !this.comparator.equal( value1,
- value2 ) ) {
- return false;
- }
- }
- return true;
- }
-
- public boolean equal(Object object1,
- Object object2) {
- for ( int i = 0, length = this.indexes.length; i < length; i++ ) {
- Object value1 = indexes[i].getExtractor().getValue( object1 );
- Object value2 = indexes[i].getExtractor().getValue( object2 );
- if ( !this.comparator.equal( value1,
- value2 ) ) {
- return false;
- }
- }
- return true;
- }
- }
-
public static class FieldIndex {
private FieldExtractor extractor;
private Declaration declaration;
Deleted: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/FieldIndexHashTable.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/FieldIndexHashTable.java 2006-10-18 23:55:15 UTC (rev 6905)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/FieldIndexHashTable.java 2006-10-19 00:19:48 UTC (rev 6906)
@@ -1,322 +0,0 @@
-/**
- *
- */
-package org.drools.util;
-
-import org.drools.common.InternalFactHandle;
-import org.drools.reteoo.ObjectHashTable;
-import org.drools.reteoo.ReteTuple;
-import org.drools.rule.Declaration;
-import org.drools.spi.FieldExtractor;
-import org.drools.util.ObjectHashMap.ObjectEntry;
-
-public class FieldIndexHashTable extends AbstractHashTable
- implements
- ObjectHashTable {
- private int fieldIndex;
-
- private FieldExtractor extractor;
-
- private Declaration declaration;
-
- private final int PRIME = 31;
-
- private final int startResult;
-
- private FieldIndexHashTableIterator tupleValueIterator;
-
- private int factSize;
-
- public FieldIndexHashTable(FieldExtractor extractor,
- Declaration declaration) {
- this( 16,
- 0.75f,
- extractor,
- declaration );
- }
-
- public FieldIndexHashTable(int capacity,
- float loadFactor,
- FieldExtractor extractor,
- Declaration declaration) {
- super( capacity,
- loadFactor );
- this.fieldIndex = extractor.getIndex();
-
- this.extractor = extractor;
-
- this.declaration = declaration;
-
- this.startResult = PRIME + this.fieldIndex;
- }
-
- public Iterator iterator() {
- throw new UnsupportedOperationException( "FieldIndexHashTable does not support iterator()" );
- }
-
- public Iterator iterator(ReteTuple tuple) {
- if ( this.tupleValueIterator == null ) {
- this.tupleValueIterator = new FieldIndexHashTableIterator();
- }
- FieldIndexEntry entry = ( FieldIndexEntry ) get( tuple );
- this.tupleValueIterator.reset( (entry != null) ? entry.first : null );
- return this.tupleValueIterator;
- }
-
- public boolean isIndexed() {
- return true;
- }
-
- public Entry getBucket(Object object) {
- InternalFactHandle handle = ( InternalFactHandle ) object;
- Object value = this.extractor.getValue( handle.getObject() );
- int hashCode = PRIME * startResult + ((value == null) ? 0 : this.comparator.hashCodeOf( value ) );
- int index = indexOf( hashCode,
- table.length );
-
- return (ObjectEntry) this.table[index];
- }
-
- /**
- * Fast re-usable iterator
- *
- */
- public static class FieldIndexHashTableIterator
- implements
- Iterator {
- private Entry entry;
-
- public FieldIndexHashTableIterator() {
-
- }
-
- /* (non-Javadoc)
- * @see org.drools.util.Iterator#next()
- */
- public Entry next() {
- Entry current = this.entry;
- this.entry = ( this.entry != null ) ? this.entry.getNext() : null;
- return current;
- }
-
- /* (non-Javadoc)
- * @see org.drools.util.Iterator#reset()
- */
- public void reset(Entry entry) {
- this.entry = entry;
- }
- }
-
- public boolean add(InternalFactHandle handle) {
- FieldIndexEntry entry = getOrCreate( this.extractor.getValue( handle.getObject() ) );
- entry.add( handle );
- this.factSize++;
- return true;
- }
-
- public boolean add(InternalFactHandle handle,
- boolean checkExists) {
- throw new UnsupportedOperationException( "FieldIndexHashTable does not support add(InternalFactHandle handle, boolean checkExists)" );
- }
-
- public boolean remove(InternalFactHandle handle) {
- Object value = this.extractor.getValue( handle.getObject() );
- int hashCode = PRIME * startResult + ((value == null) ? 0 : this.comparator.hashCodeOf( value ) );
-
- int index = indexOf( hashCode,
- table.length );
-
- // search the table for the Entry, we need to track previous and next, so if the
- // Entry is empty after its had the FactEntry removed, we must remove it from the table
- FieldIndexEntry previous = (FieldIndexEntry) this.table[index];
- FieldIndexEntry current = previous;
- while ( current != null ) {
- FieldIndexEntry next = (FieldIndexEntry) current.next;
- if ( hashCode == current.hashCode && value.equals( current.getValue() ) ) {
- current.remove( handle );
- this.factSize--;
- // If the FactEntryIndex is empty, then remove it from the hash map
- if ( current.first == null ) {
- if ( previous == current ) {
- this.table[index] = next;
- previous.next = next;
- }
- current.next = null;
- this.size--;
- }
- return true;
- }
- previous = current;
- current = next;
- }
- return false;
- }
-
- public boolean contains(InternalFactHandle handle) {
- Object value = this.extractor.getValue( handle.getObject() );
- int hashCode = PRIME * startResult + ((value == null) ? 0 : this.comparator.hashCodeOf( value ) );
-
- int index = indexOf( hashCode,
- table.length );
-
- FieldIndexEntry current = (FieldIndexEntry) this.table[index];
- while ( current != null ) {
- if ( hashCode == current.hashCode && this.comparator.equal( value, current.getValue() ) ) {
- return true;
- }
- current = (FieldIndexEntry) current.next;
- }
- return false;
- }
-
- public FieldIndexEntry get(ReteTuple tuple) {
- Object value = this.declaration.getValue( tuple.get( this.declaration ).getObject() );
- int hashCode = PRIME * startResult + ((value == null) ? 0 : this.comparator.hashCodeOf( value ) );
-
- int index = indexOf( hashCode,
- table.length );
- FieldIndexEntry entry = (FieldIndexEntry) this.table[index];
-
- while ( entry != null ) {
- if ( hashCode == entry.hashCode && this.comparator.equal( value, entry.getValue() ) ) {
- return entry;
- }
- entry = (FieldIndexEntry) entry.getNext();
- }
-
- return entry;
- }
-
- /**
- * We use this method to aviod to table lookups for the same hashcode; which is what we would have to do if we did
- * a get and then a create if the value is null.
- *
- * @param value
- * @return
- */
- private FieldIndexEntry getOrCreate(Object value) {
- int hashCode = PRIME * startResult + ((value == null) ? 0 : this.comparator.hashCodeOf( value ) );
- int index = indexOf( hashCode,
- table.length );
- FieldIndexEntry entry = (FieldIndexEntry) this.table[index];
-
- while ( entry != null ) {
-
- if ( hashCode == entry.hashCode && this.comparator.equal( value, entry.getValue() ) ) {
- return entry;
- }
- entry = (FieldIndexEntry) entry.next;
- }
-
- if ( entry == null ) {
- entry = new FieldIndexEntry( this.extractor,
- fieldIndex,
- hashCode );
- entry.next = this.table[index];
- this.table[index] = entry;
-
- if ( this.size++ >= this.threshold ) {
- resize( 2 * this.table.length );
- }
- }
- return entry;
- }
-
- public int size() {
- return this.factSize;
- }
-
- public static class FieldIndexEntry
- implements
- Entry {
- private Entry next;
- private FactEntry first;
- private final int hashCode;
- private final int fieldIndex;
- private final FieldExtractor extractor;
-
- public FieldIndexEntry(FieldExtractor extractor,
- int fieldIndex,
- int hashCode) {
- this.extractor = extractor;
- this.fieldIndex = fieldIndex;
- this.hashCode = hashCode;
- }
-
- public Entry getNext() {
- return next;
- }
-
- public void setNext(Entry next) {
- this.next = next;
- }
-
- public FactEntry getFirst() {
- return this.first;
- }
-
- public void add(InternalFactHandle handle) {
- FactEntry entry = new FactEntry( handle );
- entry.next = this.first;
- this.first = entry;
- }
-
- public FactEntry get(InternalFactHandle handle) {
- long id = handle.getId();
- FactEntry current = first;
- while ( current != null ) {
- if ( current.handle.getId() == id ) {
- return current;
- }
- current = (FactEntry) current.next;
- }
- return null;
- }
-
- public FactEntry remove(InternalFactHandle handle) {
- long id = handle.getId();
-
- FactEntry previous = this.first;
- FactEntry current = previous;
- while ( current != null ) {
- FactEntry next = (FactEntry) current.next;
- if ( current.handle.getId() == id ) {
- if ( this.first == current ) {
- this.first = next;
- } else {
- previous.next = next;
- }
- current.next = null;
- return current;
- }
- previous = current;
- current = next;
- }
- return current;
- }
-
- public String toString() {
- return this.extractor.toString();
- }
-
- public int fieldIndex() {
- return this.fieldIndex;
- }
-
- public Object getValue() {
- if ( this.first == null ) {
- return null;
- }
- return this.extractor.getValue( this.first.getFactHandle().getObject() );
- }
-
- public int hashCode() {
- return this.hashCode;
- }
-
- public boolean equals(Object object) {
- FieldIndexEntry other = (FieldIndexEntry) object;
- return this.hashCode == other.hashCode && this.fieldIndex == other.fieldIndex;
- }
- }
-}
\ No newline at end of file
More information about the jboss-svn-commits
mailing list