From d1e7699f07a6c60bfb41d63331219c5e6515dc52 Mon Sep 17 00:00:00 2001 From: angelico Date: Thu, 11 May 2017 22:33:45 +0200 Subject: [PATCH 1/3] added simple gitignore file --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .gitignore 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 From 583f2fb5de59ed6b9cbf00462b620c717518100b Mon Sep 17 00:00:00 2001 From: angelico Date: Thu, 11 May 2017 22:40:15 +0200 Subject: [PATCH 2/3] New feature/Constraint: When a collection with indexes is filled with data, you can have that indexes into separate variables or static fields. Then you could add this indexes to a new collection (p.ex the same collection but re-instantiated from a persistence serialization system), but this indexes can have data and can associate with the new collection silent and dirty. This checks that the index was used and is dirty and throws an exception to warn the user. Added new QueryOption DISABLE_DIRTY_CHECK_OPTION boolean that handles the deactivation of the newfeature/constraint. This check is enabled by default. --- .../cqengine/engine/CollectionQueryEngine.java | 6 ++++++ .../googlecode/cqengine/index/AttributeIndex.java | 5 +++++ .../index/sqlite/SQLiteIdentityIndex.java | 14 ++++++++++---- .../index/sqlite/SimplifiedSQLiteIndex.java | 9 +++++++++ .../index/support/AbstractAttributeIndex.java | 15 +++++++++++++++ .../cqengine/index/support/PartialIndex.java | 8 ++++++++ 6 files changed, 53 insertions(+), 4 deletions(-) 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..386fcfd9a 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(); From 46292465ef39c38a2351bdd6cdc034fa6e6115ab Mon Sep 17 00:00:00 2001 From: angelico Date: Thu, 11 May 2017 22:46:31 +0200 Subject: [PATCH 3/3] now the behavior is disabled by default --- .../com/googlecode/cqengine/engine/CollectionQueryEngine.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 386fcfd9a..b793fa883 100644 --- a/code/src/main/java/com/googlecode/cqengine/engine/CollectionQueryEngine.java +++ b/code/src/main/java/com/googlecode/cqengine/engine/CollectionQueryEngine.java @@ -174,7 +174,7 @@ else if (index instanceof AttributeIndex) { */ void addAttributeIndex(AttributeIndex attributeIndex, QueryOptions queryOptions) { Object dirtyCheck = queryOptions.get(DISABLE_DIRTY_CHECK_OPTION); - if (dirtyCheck == null || !(Boolean) dirtyCheck) { + if (dirtyCheck != null && (Boolean) dirtyCheck) { attributeIndex.checkDirty(); } Attribute attribute = attributeIndex.getAttribute();