From a314f76c0eb33222809fd686d4a41a271e62fcff Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Sat, 23 Sep 2006 00:05:31 +0000 Subject: Added notJoinedFrom method. --- .../com/amazon/carbonado/filter/AndFilter.java | 15 +++ .../java/com/amazon/carbonado/filter/Filter.java | 135 +++++++++++++++++++++ .../java/com/amazon/carbonado/filter/OrFilter.java | 32 +++++ .../amazon/carbonado/filter/PropertyFilter.java | 31 +++++ 4 files changed, 213 insertions(+) (limited to 'src/main/java/com/amazon') diff --git a/src/main/java/com/amazon/carbonado/filter/AndFilter.java b/src/main/java/com/amazon/carbonado/filter/AndFilter.java index 2b5ff0f..a1fd664 100644 --- a/src/main/java/com/amazon/carbonado/filter/AndFilter.java +++ b/src/main/java/com/amazon/carbonado/filter/AndFilter.java @@ -66,6 +66,21 @@ public class AndFilter extends BinaryOpFilter { return mLeft.asJoinedFrom(joinProperty).and(mRight.asJoinedFrom(joinProperty)); } + @Override + NotJoined notJoinedFrom(ChainedProperty joinProperty, + Class joinPropertyType) + { + NotJoined left = mLeft.notJoinedFrom(joinProperty, joinPropertyType); + NotJoined right = mRight.notJoinedFrom(joinProperty, joinPropertyType); + + // Remove wildcards to shut the compiler up. + Filter leftNotJoined = left.getNotJoinedFilter(); + Filter rightNotJoined = right.getNotJoinedFilter(); + + return new NotJoined(leftNotJoined.and(rightNotJoined), + left.getRemainderFilter().and(right.getRemainderFilter())); + } + Filter buildDisjunctiveNormalForm() { Filter left = mLeft.reduce().dnf(); Filter right = mRight.reduce().dnf(); diff --git a/src/main/java/com/amazon/carbonado/filter/Filter.java b/src/main/java/com/amazon/carbonado/filter/Filter.java index 55e0b93..05cf47d 100644 --- a/src/main/java/com/amazon/carbonado/filter/Filter.java +++ b/src/main/java/com/amazon/carbonado/filter/Filter.java @@ -480,6 +480,91 @@ public abstract class Filter implements Appender { */ public abstract Filter asJoinedFrom(ChainedProperty joinProperty); + /** + * Removes a join property prefix from all applicable properties of this + * filter. For example, consider two Storable types, Person and + * Address. Person has a property "homeAddress" which joins to Address. A + * Person filter might be "homeAddress.city = ? & lastName = ?". When not + * joined from "homeAddress", it becomes "city = ?" on Address with a + * remainder of "lastName = ?" on Person. + * + *

The resulting remainder filter (if any) is always logically and'd to + * the not joined filter. In order to achieve this, the original filter is + * first converted to conjunctive normal form. And as a side affect, both + * the remainder and not joined filters are {@link #bind bound}. + * + * @param joinProperty property to not join from + * @return not join result + * @throws IllegalArgumentException if property does not exist or if + * property does not refer to a Storable + */ + public final NotJoined notJoinedFrom(String joinProperty) { + return notJoinedFrom + (ChainedProperty.parse(StorableIntrospector.examine(mType), joinProperty)); + } + + /** + * Removes a join property prefix from all applicable properties of this + * filter. For example, consider two Storable types, Person and + * Address. Person has a property "homeAddress" which joins to Address. A + * Person filter might be "homeAddress.city = ? & lastName = ?". When not + * joined from "homeAddress", it becomes "city = ?" on Address with a + * remainder of "lastName = ?" on Person. + * + *

The resulting remainder filter (if any) is always logically and'd to + * the not joined filter. In order to achieve this, the original filter is + * first converted to conjunctive normal form. And as a side affect, both + * the remainder and not joined filters are {@link #bind bound}. + * + * @param joinProperty property to not join from + * @return not join result + * @throws IllegalArgumentException if property does not refer to a Storable + */ + public final NotJoined notJoinedFrom(ChainedProperty joinProperty) { + Class type = joinProperty.getType(); + if (!Storable.class.isAssignableFrom(type)) { + throw new IllegalArgumentException + ("Join property type is not a Storable: " + joinProperty); + } + + Filter cnf = conjunctiveNormalForm(); + NotJoined nj = cnf.notJoinedFrom(joinProperty, (Class) type); + + if (nj.getNotJoinedFilter() instanceof OpenFilter) { + // Remainder filter should be same as original, but it might have + // expanded with conjunctive normal form. If so, restore to + // original, but still bind it to ensure consistent side-effects. + if (nj.getRemainderFilter() != this) { + nj = new NotJoined(nj.getNotJoinedFilter(), bind()); + } + } + + if (isDisjunctiveNormalForm()) { + // Try to return filters which look similar to the original. The + // conversion from disjunctive normal form to conjunctive normal + // form may make major changes. If original was dnf, restore the + // result filters to dnf. + + if (!(nj.getNotJoinedFilter().isDisjunctiveNormalForm()) || + !(nj.getRemainderFilter().isDisjunctiveNormalForm())) + { + nj = new NotJoined(nj.getNotJoinedFilter().disjunctiveNormalForm(), + nj.getRemainderFilter().disjunctiveNormalForm()); + } + } + + return nj; + } + + /** + * Should only be called on a filter in conjunctive normal form. + */ + NotJoined notJoinedFrom(ChainedProperty joinProperty, + Class joinPropertyType) + { + return new NotJoined(getOpenFilter(joinPropertyType), this); + } + abstract Filter buildDisjunctiveNormalForm(); abstract Filter buildConjunctiveNormalForm(); @@ -541,4 +626,54 @@ public abstract class Filter implements Appender { } abstract void dumpTree(Appendable app, int indentLevel) throws IOException; + + /** + * Result from calling {@link Filter#notJoinedFrom}. + */ + public class NotJoined { + private final Filter mNotJoined; + private final Filter mRemainder; + + NotJoined(Filter notJoined, Filter remainder) { + mNotJoined = notJoined; + mRemainder = remainder; + } + + /** + * Returns the filter which is no longer as from a join. + * + * @return not joined filter or open filter if none + */ + public Filter getNotJoinedFilter() { + return mNotJoined; + } + + /** + * Returns the filter which could not be separated. + * + * @return remainder filter or open filter if none + */ + public Filter getRemainderFilter() { + return mRemainder; + } + + public int hashCode() { + return mNotJoined.hashCode() * 31 + mRemainder.hashCode(); + } + + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj instanceof Filter.NotJoined) { + NotJoined other = (NotJoined) obj; + return mNotJoined.equals(other.mNotJoined) && mRemainder.equals(other.mRemainder); + } + return false; + } + + public String toString() { + return "not joined: " + mNotJoined + ", remainder: " + mRemainder; + } + } } diff --git a/src/main/java/com/amazon/carbonado/filter/OrFilter.java b/src/main/java/com/amazon/carbonado/filter/OrFilter.java index e1d4f6e..c7ff64a 100644 --- a/src/main/java/com/amazon/carbonado/filter/OrFilter.java +++ b/src/main/java/com/amazon/carbonado/filter/OrFilter.java @@ -66,6 +66,38 @@ public class OrFilter extends BinaryOpFilter { return mLeft.asJoinedFrom(joinProperty).or(mRight.asJoinedFrom(joinProperty)); } + @Override + NotJoined notJoinedFrom(ChainedProperty joinProperty, + Class joinPropertyType) + { + NotJoined left = mLeft.notJoinedFrom(joinProperty, joinPropertyType); + NotJoined right = mRight.notJoinedFrom(joinProperty, joinPropertyType); + + // Assert that our child nodes are only OrFilter or PropertyFilter. + if (!isConjunctiveNormalForm()) { + throw new IllegalStateException(); + } + + // If child nodes have any remainder, then everything must go to the + // remainder. As per the contract of notJoinedFrom, the not-joined and + // remainder filters are logically and'd together to reform the + // original filter. If the remainder was broken up, then the not-joined + // and remainder filters would need to logically or'd together to + // reform the original filter, breaking the notJoinedFrom contract. + + if (!(left.getRemainderFilter() instanceof OpenFilter) || + !(right.getRemainderFilter() instanceof OpenFilter)) + { + return super.notJoinedFrom(joinProperty, joinPropertyType); + } + + // Remove wildcards to shut the compiler up. + Filter leftNotJoined = left.getNotJoinedFilter(); + Filter rightNotJoined = right.getNotJoinedFilter(); + + return new NotJoined(leftNotJoined.or(rightNotJoined), getOpenFilter(getStorableType())); + } + Filter buildDisjunctiveNormalForm() { return mLeft.dnf().or(mRight.dnf()).reduce(); } diff --git a/src/main/java/com/amazon/carbonado/filter/PropertyFilter.java b/src/main/java/com/amazon/carbonado/filter/PropertyFilter.java index b66ad22..64d1d94 100644 --- a/src/main/java/com/amazon/carbonado/filter/PropertyFilter.java +++ b/src/main/java/com/amazon/carbonado/filter/PropertyFilter.java @@ -184,6 +184,37 @@ public class PropertyFilter extends Filter { } } + @Override + NotJoined notJoinedFrom(ChainedProperty joinProperty, + Class joinPropertyType) + { + ChainedProperty notJoinedProp = getChainedProperty(); + ChainedProperty jp = joinProperty; + + while (notJoinedProp.getPrimeProperty().equals(jp.getPrimeProperty())) { + notJoinedProp = notJoinedProp.tail(); + if (jp.getChainCount() == 0) { + jp = null; + break; + } + jp = jp.tail(); + } + + if (jp != null || notJoinedProp.equals(getChainedProperty())) { + return super.notJoinedFrom(joinProperty, joinPropertyType); + } + + PropertyFilter notJoinedFilter; + + if (isConstant()) { + notJoinedFilter = getCanonical(notJoinedProp, mOp, mConstant); + } else { + notJoinedFilter = getCanonical(notJoinedProp, mOp, mBindID); + } + + return new NotJoined(notJoinedFilter, getOpenFilter(getStorableType())); + } + /** * Returns another PropertyFilter instance which is bound to the given constant value. * -- cgit v1.2.3