Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check for dirty indexes before add it to a collection #143

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
*.iml
target
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public class CollectionQueryEngine<O> implements QueryEngineInternal<O> {
// 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<O, ? extends Comparable> persistence;
private volatile ObjectStore<O> objectStore;

Expand Down Expand Up @@ -171,6 +173,10 @@ else if (index instanceof AttributeIndex) {
* @param <A> The type of objects indexed
*/
<A> void addAttributeIndex(AttributeIndex<A, O> attributeIndex, QueryOptions queryOptions) {
Object dirtyCheck = queryOptions.get(DISABLE_DIRTY_CHECK_OPTION);
if (dirtyCheck != null && (Boolean) dirtyCheck) {
attributeIndex.checkDirty();
}
Attribute<O, A> attribute = attributeIndex.getAttribute();
Set<Index<O>> indexesOnThisAttribute = attributeIndexes.get(attribute);
if (indexesOnThisAttribute == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@ public interface AttributeIndex<A, O> extends Index<O> {
* @return The attribute indexed by this index
*/
Attribute<O, A> getAttribute();

/**
* Checks if this index is alredy being used by another collection.
*/
void checkDirty();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -50,6 +47,7 @@ public class SQLiteIdentityIndex<A extends Comparable<A>, O> implements Identity
final Class<O> objectType;
final SimpleAttribute<O, A> primaryKeyAttribute;
final SimpleAttribute<A, O> foreignKeyAttribute;
private boolean dirty = false;

public SQLiteIdentityIndex(final SimpleAttribute<O, A> primaryKeyAttribute) {
this.sqLiteIndex = new SQLiteIndex<A, O, byte[]>(
Expand Down Expand Up @@ -80,6 +78,14 @@ public Attribute<O, A> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public abstract class SimplifiedSQLiteIndex<A extends Comparable<A>, O, K extend
final Attribute<O, A> attribute;
final String tableNameSuffix;
volatile SQLiteIndex<A, O, K> backingIndex;
private boolean dirty = false;

protected SimplifiedSQLiteIndex(Class<? extends SQLitePersistence<O, A>> persistenceType, Attribute<O, A> attribute, String tableNameSuffix) {
this.persistenceType = persistenceType;
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public abstract class AbstractAttributeIndex<A, O> implements AttributeIndex<A,

protected final Attribute<O, A> attribute;

private boolean dirty = false;

/**
* Protected constructor, called by subclasses.
*
Expand All @@ -60,6 +62,19 @@ public Attribute<O, A> 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}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public abstract class PartialIndex<A, O, I extends AttributeIndex<A, O>> impleme
protected final Query<O> filterQuery;
protected final Attribute<O, A> attribute;
protected volatile I backingIndex;
private boolean dirty = false;

/**
* Protected constructor, called by subclasses.
Expand All @@ -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<O, A> getAttribute() {
return backingIndex().getAttribute();
Expand Down