diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..e5aa5b3c6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea +*.iml +target \ No newline at end of file diff --git a/code/src/main/java/com/googlecode/cqengine/engine/CollectionQueryEngine.java b/code/src/main/java/com/googlecode/cqengine/engine/CollectionQueryEngine.java index 3ea5d404b..b793fa883 100644 --- a/code/src/main/java/com/googlecode/cqengine/engine/CollectionQueryEngine.java +++ b/code/src/main/java/com/googlecode/cqengine/engine/CollectionQueryEngine.java @@ -82,6 +82,8 @@ public class CollectionQueryEngine implements QueryEngineInternal { // A key used to store the root query in the QueryOptions, so it may be accessed by partial indexes... public static final String ROOT_QUERY = "ROOT_QUERY"; + public static final String DISABLE_DIRTY_CHECK_OPTION = "DISABLE_DIRTY_CHECK"; + private volatile Persistence persistence; private volatile ObjectStore objectStore; @@ -171,6 +173,10 @@ else if (index instanceof AttributeIndex) { * @param The type of objects indexed */ void addAttributeIndex(AttributeIndex attributeIndex, QueryOptions queryOptions) { + Object dirtyCheck = queryOptions.get(DISABLE_DIRTY_CHECK_OPTION); + if (dirtyCheck != null && (Boolean) dirtyCheck) { + attributeIndex.checkDirty(); + } Attribute attribute = attributeIndex.getAttribute(); Set> indexesOnThisAttribute = attributeIndexes.get(attribute); if (indexesOnThisAttribute == null) { diff --git a/code/src/main/java/com/googlecode/cqengine/index/AttributeIndex.java b/code/src/main/java/com/googlecode/cqengine/index/AttributeIndex.java index b4c8c5d75..d6cc90e56 100644 --- a/code/src/main/java/com/googlecode/cqengine/index/AttributeIndex.java +++ b/code/src/main/java/com/googlecode/cqengine/index/AttributeIndex.java @@ -31,4 +31,9 @@ public interface AttributeIndex extends Index { * @return The attribute indexed by this index */ Attribute getAttribute(); + + /** + * Checks if this index is alredy being used by another collection. + */ + void checkDirty(); } diff --git a/code/src/main/java/com/googlecode/cqengine/index/sqlite/SQLiteIdentityIndex.java b/code/src/main/java/com/googlecode/cqengine/index/sqlite/SQLiteIdentityIndex.java index 4fb26e3a4..623af05af 100644 --- a/code/src/main/java/com/googlecode/cqengine/index/sqlite/SQLiteIdentityIndex.java +++ b/code/src/main/java/com/googlecode/cqengine/index/sqlite/SQLiteIdentityIndex.java @@ -20,10 +20,7 @@ import com.googlecode.cqengine.attribute.SimpleAttribute; import com.googlecode.cqengine.index.Index; import com.googlecode.cqengine.index.sqlite.support.PojoSerializer; -import com.googlecode.cqengine.index.support.CloseableIterable; -import com.googlecode.cqengine.index.support.KeyStatistics; -import com.googlecode.cqengine.index.support.KeyValue; -import com.googlecode.cqengine.index.support.SortedKeyStatisticsAttributeIndex; +import com.googlecode.cqengine.index.support.*; import com.googlecode.cqengine.index.support.indextype.NonHeapTypeIndex; import com.googlecode.cqengine.persistence.support.ObjectSet; import com.googlecode.cqengine.persistence.support.ObjectStore; @@ -50,6 +47,7 @@ public class SQLiteIdentityIndex, O> implements Identity final Class objectType; final SimpleAttribute primaryKeyAttribute; final SimpleAttribute foreignKeyAttribute; + private boolean dirty = false; public SQLiteIdentityIndex(final SimpleAttribute primaryKeyAttribute) { this.sqLiteIndex = new SQLiteIndex( @@ -80,6 +78,14 @@ public Attribute getAttribute() { return sqLiteIndex.getAttribute(); } + @Override + public void checkDirty() { + if (dirty) { + throw new AbstractAttributeIndex.DirtyIndexException("Index of attribute: " + primaryKeyAttribute.getAttributeName() + " - is in use."); + } + dirty = true; + } + @Override public boolean isMutable() { return sqLiteIndex.isMutable(); diff --git a/code/src/main/java/com/googlecode/cqengine/index/sqlite/SimplifiedSQLiteIndex.java b/code/src/main/java/com/googlecode/cqengine/index/sqlite/SimplifiedSQLiteIndex.java index e5b7e88d5..3494a9816 100644 --- a/code/src/main/java/com/googlecode/cqengine/index/sqlite/SimplifiedSQLiteIndex.java +++ b/code/src/main/java/com/googlecode/cqengine/index/sqlite/SimplifiedSQLiteIndex.java @@ -44,6 +44,7 @@ public abstract class SimplifiedSQLiteIndex, O, K extend final Attribute attribute; final String tableNameSuffix; volatile SQLiteIndex backingIndex; + private boolean dirty = false; protected SimplifiedSQLiteIndex(Class> persistenceType, Attribute attribute, String tableNameSuffix) { this.persistenceType = persistenceType; @@ -248,4 +249,12 @@ public int hashCode() { result = 31 * result + attribute.hashCode(); return result; } + + @Override + public void checkDirty() { + if (dirty) { + throw new AbstractAttributeIndex.DirtyIndexException("Index of attribute: " + attribute.getAttributeName() + " - is in use."); + } + dirty = true; + } } diff --git a/code/src/main/java/com/googlecode/cqengine/index/support/AbstractAttributeIndex.java b/code/src/main/java/com/googlecode/cqengine/index/support/AbstractAttributeIndex.java index 5f7559048..09765ca67 100644 --- a/code/src/main/java/com/googlecode/cqengine/index/support/AbstractAttributeIndex.java +++ b/code/src/main/java/com/googlecode/cqengine/index/support/AbstractAttributeIndex.java @@ -38,6 +38,8 @@ public abstract class AbstractAttributeIndex implements AttributeIndex attribute; + private boolean dirty = false; + /** * Protected constructor, called by subclasses. * @@ -60,6 +62,19 @@ public Attribute getAttribute() { return attribute; } + public static class DirtyIndexException extends RuntimeException { + public DirtyIndexException(String message) { + super(message); + } + } + + public void checkDirty() { + if (dirty) { + throw new DirtyIndexException("Index of attribute: " + attribute.getAttributeName() + " - is in use."); + } + dirty = true; + } + /** * {@inheritDoc} */ diff --git a/code/src/main/java/com/googlecode/cqengine/index/support/PartialIndex.java b/code/src/main/java/com/googlecode/cqengine/index/support/PartialIndex.java index daf38ea43..da71e3147 100644 --- a/code/src/main/java/com/googlecode/cqengine/index/support/PartialIndex.java +++ b/code/src/main/java/com/googlecode/cqengine/index/support/PartialIndex.java @@ -90,6 +90,7 @@ public abstract class PartialIndex> impleme protected final Query filterQuery; protected final Attribute attribute; protected volatile I backingIndex; + private boolean dirty = false; /** * Protected constructor, called by subclasses. @@ -113,6 +114,13 @@ protected I backingIndex() { return backingIndex; } + @Override + public void checkDirty() { + if (dirty) { + throw new AbstractAttributeIndex.DirtyIndexException("Index of attribute: " + attribute.getAttributeName() + " - is in use."); + } + dirty = true; + } public Attribute getAttribute() { return backingIndex().getAttribute();