Skip to content

Commit

Permalink
Add support for string prefix, suffix and contains to OperationsFacad…
Browse files Browse the repository at this point in the history
…e.indexQuery
  • Loading branch information
chrisvest committed Feb 22, 2017
1 parent bf17c3f commit 756aeaf
Show file tree
Hide file tree
Showing 7 changed files with 316 additions and 141 deletions.
Expand Up @@ -204,7 +204,7 @@ final class TransactionBoundQueryContext(val transactionalContext: Transactional
}

private def indexSeekByPrefixRange(index: IndexDescriptor, prefix: String): scala.Iterator[Node] = {
val indexedNodes = transactionalContext.statement.readOperations().nodesGetFromIndexRangeSeekByPrefix(index, prefix)
val indexedNodes = transactionalContext.statement.readOperations().indexQuery(index, IndexQuery.stringPrefix(index.property, prefix))
JavaConversionSupport.mapToScalaENFXSafe(indexedNodes)(nodeOps.getById)
}

Expand Down Expand Up @@ -264,13 +264,13 @@ final class TransactionBoundQueryContext(val transactionalContext: Transactional
}

override def indexScan(index: IndexDescriptor) =
mapToScalaENFXSafe(transactionalContext.statement.readOperations().nodesGetFromIndexScan(index))(nodeOps.getById)
mapToScalaENFXSafe(transactionalContext.statement.readOperations().indexQuery(index, IndexQuery.exists(index.property)))(nodeOps.getById)

override def indexScanByContains(index: IndexDescriptor, value: String) =
mapToScalaENFXSafe(transactionalContext.statement.readOperations().nodesGetFromIndexContainsScan(index, value))(nodeOps.getById)
mapToScalaENFXSafe(transactionalContext.statement.readOperations().indexQuery(index, IndexQuery.stringContains(index.property, value)))(nodeOps.getById)

override def indexScanByEndsWith(index: IndexDescriptor, value: String) =
mapToScalaENFXSafe(transactionalContext.statement.readOperations().nodesGetFromIndexEndsWithScan(index, value))(nodeOps.getById)
mapToScalaENFXSafe(transactionalContext.statement.readOperations().indexQuery(index, IndexQuery.stringSuffix(index.property, value)))(nodeOps.getById)

override def lockingUniqueIndexSeek(index: IndexDescriptor, value: Any): Option[Node] = {
indexSearchMonitor.lockingUniqueIndexSeek(index, value)
Expand Down
Expand Up @@ -19,6 +19,11 @@
*/
package org.neo4j.kernel.api.schema_new;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

public abstract class IndexQuery
{
public static ExistsPredicate exists( int propertyKeyId )
Expand All @@ -43,51 +48,89 @@ public static StringRangePredicate range( int propertyKeyId, String from, boolea
return new StringRangePredicate( propertyKeyId, from, fromInclusive, to, toInclusive );
}

public static StringPrefixPredicate stringPrefix( int propertyKeyId, String prefix )
{
return new StringPrefixPredicate( propertyKeyId, prefix );
}

public static StringContainsPredicate stringContains( int propertyKeyId, String contains )
{
return new StringContainsPredicate( propertyKeyId, contains );
}

public static StringSuffixPredicate stringSuffix( int propertyKeyId, String suffix )
{
return new StringSuffixPredicate( propertyKeyId, suffix );
}

private final int propertyKeyId;

protected IndexQuery( int propertyKeyId )
{
this.propertyKeyId = propertyKeyId;
}

public abstract IndexQueryType type();

public static final class ExistsPredicate extends IndexQuery
@SuppressWarnings( "EqualsWhichDoesntCheckParameterClass" )
@Override
public final boolean equals( Object other )
{
private final int propertyKeyId;
// equals() and hashcode() are only used for testing so we don't care that they are a bit slow.
return EqualsBuilder.reflectionEquals( this, other );
}

public ExistsPredicate( int propertyKeyId )
{
this.propertyKeyId = propertyKeyId;
}
@Override
public final int hashCode()
{
// equals() and hashcode() are only used for testing so we don't care that they are a bit slow.
return HashCodeBuilder.reflectionHashCode( this, false );
}

@Override
public IndexQueryType type()
{
return IndexQueryType.exists;
}
@Override
public final String toString()
{
// Only used to debugging, it's okay to be a bit slow.
return ToStringBuilder.reflectionToString( this, ToStringStyle.SHORT_PREFIX_STYLE );
}

@Override
public boolean equals( Object o )
{
if ( this == o )
{ return true; }
if ( o == null || getClass() != o.getClass() )
{ return false; }
public final int propertyKeyId()
{
return propertyKeyId;
}

ExistsPredicate that = (ExistsPredicate) o;
public enum IndexQueryType
{
exists,
exact,
rangeString,
rangeNumeric,
stringPrefix,
stringSuffix,
stringContains
}

return propertyKeyId == that.propertyKeyId;
public static final class ExistsPredicate extends IndexQuery
{
public ExistsPredicate( int propertyKeyId )
{
super( propertyKeyId );
}

@Override
public int hashCode()
public IndexQueryType type()
{
return propertyKeyId;
return IndexQueryType.exists;
}
}

public static final class ExactPredicate extends IndexQuery
{
private final int propertyKeyId;
private final Object value;

public ExactPredicate( int propertyKeyId, Object value )
{
this.propertyKeyId = propertyKeyId;
super( propertyKeyId );
this.value = value;
}

Expand All @@ -101,34 +144,10 @@ public Object value()
{
return value;
}

@Override
public boolean equals( Object o )
{
if ( this == o )
{ return true; }
if ( o == null || getClass() != o.getClass() )
{ return false; }

ExactPredicate that = (ExactPredicate) o;

if ( propertyKeyId != that.propertyKeyId )
{ return false; }
return value.equals( that.value );
}

@Override
public int hashCode()
{
int result = propertyKeyId;
result = 31 * result + value.hashCode();
return result;
}
}

public static final class NumberRangePredicate extends IndexQuery
{
private final int propertyKeyId;
private final Number from;
private final boolean fromInclusive;
private final Number to;
Expand All @@ -137,7 +156,7 @@ public static final class NumberRangePredicate extends IndexQuery
public NumberRangePredicate( int propertyKeyId, Number from,
boolean fromInclusive, Number to, boolean toInclusive )
{
this.propertyKeyId = propertyKeyId;
super( propertyKeyId );
this.from = from;
this.fromInclusive = fromInclusive;
this.to = to;
Expand All @@ -150,62 +169,29 @@ public IndexQueryType type()
return IndexQueryType.rangeNumeric;
}

public Number getFrom()
public Number from()
{
return from;
}

public Number getTo()
public Number to()
{
return to;
}

public boolean isFromInclusive()
public boolean fromInclusive()
{
return fromInclusive;
}

public boolean isToInclusive()
public boolean toInclusive()
{
return toInclusive;
}

@Override
public boolean equals( Object o )
{
if ( this == o )
{ return true; }
if ( o == null || getClass() != o.getClass() )
{ return false; }

NumberRangePredicate that = (NumberRangePredicate) o;

if ( propertyKeyId != that.propertyKeyId )
{ return false; }
if ( fromInclusive != that.fromInclusive )
{ return false; }
if ( toInclusive != that.toInclusive )
{ return false; }
if ( !from.equals( that.from ) )
{ return false; }
return to.equals( that.to );
}

@Override
public int hashCode()
{
int result = propertyKeyId;
result = 31 * result + from.hashCode();
result = 31 * result + (fromInclusive ? 1 : 0);
result = 31 * result + to.hashCode();
result = 31 * result + (toInclusive ? 1 : 0);
return result;
}
}

public static final class StringRangePredicate extends IndexQuery
{
private final int propertyKeyId;
private final String from;
private final boolean fromInclusive;
private final String to;
Expand All @@ -215,7 +201,7 @@ public StringRangePredicate( int propertyKeyId, String from,
boolean fromInclusive, String to,
boolean toInclusive )
{
this.propertyKeyId = propertyKeyId;
super( propertyKeyId );
this.from = from;
this.fromInclusive = fromInclusive;
this.to = to;
Expand All @@ -228,64 +214,90 @@ public IndexQueryType type()
return IndexQueryType.rangeString;
}

public String getFrom()
public String from()
{
return from;
}

public boolean isFromInclusive()
public boolean fromInclusive()
{
return fromInclusive;
}

public String getTo()
public String to()
{
return to;
}

public boolean isToInclusive()
public boolean toInclusive()
{
return toInclusive;
}
}

public static final class StringPrefixPredicate extends IndexQuery
{
private final String prefix;

public StringPrefixPredicate( int propertyKeyId, String prefix )
{
super( propertyKeyId );
this.prefix = prefix;
}

@Override
public boolean equals( Object o )
public IndexQueryType type()
{
return IndexQueryType.stringPrefix;
}

public String prefix()
{
return prefix;
}
}

public static final class StringContainsPredicate extends IndexQuery
{
private final String contains;

public StringContainsPredicate( int propertyKeyId, String contains )
{
if ( this == o )
{ return true; }
if ( o == null || getClass() != o.getClass() )
{ return false; }

StringRangePredicate that = (StringRangePredicate) o;

if ( propertyKeyId != that.propertyKeyId )
{ return false; }
if ( fromInclusive != that.fromInclusive )
{ return false; }
if ( toInclusive != that.toInclusive )
{ return false; }
if ( !from.equals( that.from ) )
{ return false; }
return to.equals( that.to );
super( propertyKeyId );
this.contains = contains;
}

@Override
public int hashCode()
public IndexQueryType type()
{
int result = propertyKeyId;
result = 31 * result + from.hashCode();
result = 31 * result + (fromInclusive ? 1 : 0);
result = 31 * result + to.hashCode();
result = 31 * result + (toInclusive ? 1 : 0);
return result;
return IndexQueryType.stringContains;
}

public String contains()
{
return contains;
}
}

public enum IndexQueryType
public static final class StringSuffixPredicate extends IndexQuery
{
exists,
exact,
rangeString,
rangeNumeric
private final String suffix;

public StringSuffixPredicate( int propertyKeyId, String suffix )
{
super( propertyKeyId );
this.suffix = suffix;
}

@Override
public IndexQueryType type()
{
return IndexQueryType.stringSuffix;
}

public String suffix()
{
return suffix;
}
}
}

0 comments on commit 756aeaf

Please sign in to comment.