Skip to content

Commit

Permalink
Move reverse to ListValue
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed May 27, 2018
1 parent 5175a1d commit 052dfcb
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 16 deletions.
Expand Up @@ -24,14 +24,14 @@ import org.neo4j.cypher.internal.runtime.interpreted.pipes.QueryState
import org.opencypher.v9_0.util.CypherTypeException import org.opencypher.v9_0.util.CypherTypeException
import org.neo4j.values.AnyValue import org.neo4j.values.AnyValue
import org.neo4j.values.storable.{TextValue, Values} import org.neo4j.values.storable.{TextValue, Values}
import org.neo4j.values.virtual.{ListValue, VirtualValues} import org.neo4j.values.virtual.ListValue


case class ReverseFunction(argument: Expression) extends NullInNullOutExpression(argument) { case class ReverseFunction(argument: Expression) extends NullInNullOutExpression(argument) {
override def compute(value: AnyValue, m: ExecutionContext, state: QueryState): AnyValue = { override def compute(value: AnyValue, m: ExecutionContext, state: QueryState): AnyValue = {
argument(m, state) match { argument(m, state) match {
case x if x == Values.NO_VALUE => Values.NO_VALUE case x if x == Values.NO_VALUE => Values.NO_VALUE
case string: TextValue => string.reverse case string: TextValue => string.reverse
case seq: ListValue => VirtualValues.reverse(seq) case seq: ListValue => seq.reverse()
case a => throw new CypherTypeException( case a => throw new CypherTypeException(
"Expected a string or a list; consider converting it to a string with toString() or creating a list." "Expected a string or a list; consider converting it to a string with toString() or creating a list."
.format(toString(), a.toString)) .format(toString(), a.toString))
Expand Down
Expand Up @@ -22,8 +22,7 @@ package org.neo4j.cypher.internal.runtime.interpreted.pipes
import org.neo4j.cypher.internal.runtime.QueryContext import org.neo4j.cypher.internal.runtime.QueryContext
import org.neo4j.cypher.internal.runtime.interpreted.{ExecutionContext, ListSupport} import org.neo4j.cypher.internal.runtime.interpreted.{ExecutionContext, ListSupport}
import org.opencypher.v9_0.util.attribution.Id import org.opencypher.v9_0.util.attribution.Id
import org.neo4j.values.virtual.VirtualValues.reverse import org.neo4j.values.virtual.{ListValue, NodeValue, RelationshipReference, RelationshipValue}
import org.neo4j.values.virtual.{RelationshipReference, RelationshipValue, ListValue, NodeValue}


case class ProjectEndpointsPipe(source: Pipe, relName: String, case class ProjectEndpointsPipe(source: Pipe, relName: String,
start: String, startInScope: Boolean, start: String, startInScope: Boolean,
Expand All @@ -46,7 +45,7 @@ case class ProjectEndpointsPipe(source: Pipe, relName: String,
case Some((NotInScope(startNode, endNode), rels)) if !directed => case Some((NotInScope(startNode, endNode), rels)) if !directed =>
Iterator( Iterator(
executionContextFactory.copyWith(context, start, startNode, end, endNode), executionContextFactory.copyWith(context, start, startNode, end, endNode),
executionContextFactory.copyWith(context, start, endNode, end, startNode, relName, reverse(rels)) executionContextFactory.copyWith(context, start, endNode, end, startNode, relName, rels.reverse())
) )
case Some((startAndEnd, rels)) => case Some((startAndEnd, rels)) =>
Iterator(context.set(start, startAndEnd.start, end, startAndEnd.end)) Iterator(context.set(start, startAndEnd.start, end, startAndEnd.end))
Expand Down
Expand Up @@ -924,4 +924,9 @@ public ListValue take( int n )
int end = Math.max( 0, Math.min( n, size() ) ); int end = Math.max( 0, Math.min( n, size() ) );
return new ListSlice( this, 0, end ); return new ListSlice( this, 0, end );
} }

public ListValue reverse()
{
return new ReversedList( this );
}
} }
Expand Up @@ -75,11 +75,6 @@ public static ListValue fromArray( ArrayValue arrayValue )
*/ */


public static ListValue reverse( ListValue list )
{
return new ListValue.ReversedList( list );
}

public static ListValue concat( ListValue... lists ) public static ListValue concat( ListValue... lists )
{ {
return new ListValue.ConcatList( lists ); return new ListValue.ConcatList( lists );
Expand Down
Expand Up @@ -228,7 +228,7 @@ public void shouldRecurseAndCoerce()
list( -2L, 1L, 4L, 7L, 10L ).slice( 1, 4 ), list( -2L, 1L, 4L, 7L, 10L ).slice( 1, 4 ),
list( -2L, 1L, 4L, 7L ).drop( 1 ), list( -2L, 1L, 4L, 7L ).drop( 1 ),
list( 1L, 4L, 7L, 10L, 13L ).take( 3 ), list( 1L, 4L, 7L, 10L, 13L ).take( 3 ),
VirtualValues.reverse( list( 7L, 4L, 1L ) ), list( 7L, 4L, 1L ).reverse(),
VirtualValues.concat( list( 1L, 4L ), list( 7L ) ) VirtualValues.concat( list( 1L, 4L ), list( 7L ) )
}; };


Expand All @@ -246,7 +246,7 @@ public void shouldRecurseAndCoerce()
list( -2L, 1L, 5L, 8L, 11L ).slice( 1, 4 ), list( -2L, 1L, 5L, 8L, 11L ).slice( 1, 4 ),
list( -2L, 6L, 9L, 12L ).drop( 1 ), list( -2L, 6L, 9L, 12L ).drop( 1 ),
list( 7L, 10L, 13L, 10L, 13L ).take( 3 ), list( 7L, 10L, 13L, 10L, 13L ).take( 3 ),
VirtualValues.reverse( list( 15L, 12L, 9L ) ), list( 15L, 12L, 9L ).reverse(),
VirtualValues.concat( list( 10L, 13L ), list( 16L ) ) VirtualValues.concat( list( 10L, 13L ), list( 16L ) )
}; };


Expand Down
Expand Up @@ -26,7 +26,6 @@
import static org.neo4j.values.storable.Values.longValue; import static org.neo4j.values.storable.Values.longValue;
import static org.neo4j.values.virtual.VirtualValues.EMPTY_LIST; import static org.neo4j.values.virtual.VirtualValues.EMPTY_LIST;
import static org.neo4j.values.virtual.VirtualValues.list; import static org.neo4j.values.virtual.VirtualValues.list;
import static org.neo4j.values.virtual.VirtualValues.reverse;


public class ReversedListTest public class ReversedListTest
{ {
Expand All @@ -37,7 +36,7 @@ public void shouldHandleEmptyList()
// Given // Given
ListValue inner = EMPTY_LIST; ListValue inner = EMPTY_LIST;
// When // When
ListValue reverse = reverse( inner ); ListValue reverse = inner.reverse();


// Then // Then
assertEquals( inner, reverse ); assertEquals( inner, reverse );
Expand All @@ -52,7 +51,7 @@ public void shouldHandleSingleItemList()
ListValue inner = list( longValue( 5L ) ); ListValue inner = list( longValue( 5L ) );


// When // When
ListValue reverse = reverse( inner ); ListValue reverse = inner.reverse();


// Then // Then
assertEquals( inner, reverse ); assertEquals( inner, reverse );
Expand All @@ -67,7 +66,7 @@ public void shouldReverseList()
ListValue inner = list( longValue( 5L ), longValue( 6L ), longValue( 7L ) ); ListValue inner = list( longValue( 5L ), longValue( 6L ), longValue( 7L ) );


// When // When
ListValue reverse = reverse( inner ); ListValue reverse = inner.reverse();


// Then // Then
ListValue expected = list( longValue( 7L ), longValue( 6L ), longValue( 5L ) ); ListValue expected = list( longValue( 7L ), longValue( 6L ), longValue( 5L ) );
Expand Down

0 comments on commit 052dfcb

Please sign in to comment.