public static ListValue asListOfEdges( Iterable<Relationship> rels ) { return VirtualValues.list( StreamSupport.stream( rels.spliterator(), false ) .map( ValueUtils::fromRelationshipProxy ).toArray( RelationshipValue[]::new ) ); }
@Override public Pair<String, String> evaluate( String script ) { if ( StringUtils.EMPTY.equals( script.trim() ) ) { return Pair.of( StringUtils.EMPTY, null ); } String resultString; try { TransactionalContext tc = cypherExecutor.createTransactionContext( script, emptyMap(), request ); ExecutionEngine engine = cypherExecutor.getExecutionEngine(); Result result = engine.executeQuery( script, emptyMap(), tc ); resultString = result.resultAsString(); } catch ( SyntaxException error ) { resultString = error.getMessage(); } catch ( Exception exception ) { log.error( "Unknown error executing cypher query", exception ); resultString = "Error: " + exception.getClass().getSimpleName() + " - " + exception.getMessage(); } return Pair.of( resultString, null ); } }
return VirtualValues.fromList( (List<AnyValue>) (((List) anyValue).stream() .map( v -> materializeAnyResult( proxySpi, v ) ).collect( Collectors.toList() ) ) ); return VirtualValues.fromList( ((PrimitiveNodeStream) anyValue).longStream() .mapToObj( id -> (AnyValue) ValueUtils.fromNodeProxy( proxySpi.newNodeProxy( id ) ) ) .collect( Collectors.toList() ) ); return VirtualValues.fromList( ((PrimitiveRelationshipStream) anyValue).longStream() .mapToObj( id -> (AnyValue) ValueUtils.fromRelationshipProxy( proxySpi.newRelationshipProxy( id ) ) ) .collect( Collectors.toList() ) ); return VirtualValues.fromList( ((IntStream) anyValue).mapToObj( i -> Values.booleanValue( i != 0 ) ).collect( Collectors.toList() ) ); return VirtualValues.list( copy );
@Override public ListValue split( String separator ) { assert separator != null; String asString = value(); //Cypher has different semantics for the case where the separator //is exactly the value, in cypher we expect two empty arrays //where as java returns an empty array if ( separator.equals( asString ) ) { return EMPTY_SPLIT; } else if ( separator.isEmpty() ) { return VirtualValues.fromArray( Values.charArray( asString.toCharArray() ) ); } List<AnyValue> split = splitNonRegex( asString, separator ); return VirtualValues.fromList( split ); }
private static RelationshipValue newRelationshipValue() { NodeValue startNode = nodeValue( 24, stringArray( "Person" ), EMPTY_MAP ); NodeValue endNode = nodeValue( 42, stringArray( "Person" ), EMPTY_MAP ); return relationshipValue( 42, startNode, endNode, stringValue( "KNOWS" ), EMPTY_MAP ); } }
VirtualValues.list( booleanValue( false ), byteArray( new byte[]{3, 4, 5} ), ), shouldWrite( VirtualValues.map( new String[]{"foo", "bar"}, new AnyValue[]{intValue( 100 ), charValue( 'c' )} ), shouldWrite( VirtualValues.node( 1L ), writeNodeReference( 1L ) ), shouldWrite( relationship( 2L ), writeRelationshipReference( 2L ) ), shouldWrite( VirtualValues.path( new NodeValue[]{nodeValue( 20L, stringArray( "L" ), emptyMap() ), nodeValue( 40L, stringArray( "L" ), emptyMap() )}, new RelationshipValue[]{ relationshipValue( 100L, nodeValue( 40L, stringArray( "L" ), emptyMap() ), nodeValue( 20L, stringArray( "L" ), emptyMap() ), stringValue( "T" ), emptyMap() )} ), writePath( new NodeValue[]{nodeValue( 20L, stringArray( "L" ), emptyMap() ),
private static Stream<AnyValue> parameters() NodeValue node1 = nodeValue( 1, stringArray(), emptyMap() ); NodeValue node2 = nodeValue( 2, stringArray(), emptyMap() ); NodeValue node3 = nodeValue( 3, stringArray(), emptyMap() ); RelationshipValue relationship1 = relationshipValue( 100, node1, node2, stringValue( "ONE" ), emptyMap() ); RelationshipValue relationship2 = relationshipValue( 200, node2, node2, stringValue( "TWO" ), emptyMap() ); return Stream.of( node1, relationship1, path( new NodeValue[] {node1, node2, node3}, new RelationshipValue[]{relationship1, relationship2} ), map( new String[] {"alpha", "beta"}, new AnyValue[] {stringValue( "one" ), numberValue( 2 )} ), NO_VALUE, list( numberValue( 1 ), stringValue( "fine" ), node2 ), stringValue( "hello world" ), stringArray( "hello", "brave", "new", "world" ),
@Test void shouldHandleEdgeValue() { // Given NodeValue startNode = VirtualValues.nodeValue( 1L, Values.stringArray( "L" ), EMPTY_MAP ); NodeValue endNode = VirtualValues.nodeValue( 2L, Values.stringArray( "L" ), EMPTY_MAP ); RelationshipValue rel = VirtualValues.relationshipValue( 42L, startNode, endNode, stringValue( "R" ), props( "foo", intValue( 42 ), "bar", list( intValue( 1337 ), stringValue( "baz" ) ) ) ); PrettyPrinter printer = new PrettyPrinter(); // When rel.writeTo( printer ); // Then assertThat( printer.value(), equalTo( "-[id=42 :R {bar: [1337, \"baz\"], foo: 42}]-" ) ); }
@Test public void shouldHandleMapsWithLists() { // Given MapValue map = map( new String[]{"foo", "bar"}, new AnyValue[]{longValue( 42L ), list( stringValue( "baz" ) )} ); // When map.writeTo( converter ); // Then Map<?,?> value = (Map<?,?>) converter.value(); assertThat( value.get( "foo" ), equalTo( 42L ) ); assertThat( value.get( "bar" ), equalTo( singletonList( "baz" ) ) ); assertThat( value.size(), equalTo( 2 ) ); } }
@Test void shouldHandleNodeValue() { // Given NodeValue node = VirtualValues.nodeValue( 42L, Values.stringArray( "L1", "L2", "L3" ), props( "foo", intValue( 42 ), "bar", list( intValue( 1337 ), stringValue( "baz" ) ) ) ); PrettyPrinter printer = new PrettyPrinter(); // When node.writeTo( printer ); // Then assertThat( printer.value(), equalTo( "(id=42 :L1:L2:L3 {bar: [1337, \"baz\"], foo: 42})" ) ); }
@Test public void shouldSerializeRelationship() throws Throwable { RelationshipValue rel = relationshipValue( 12L, nodeValue( 1L, stringArray(), VirtualValues.EMPTY_MAP ), nodeValue( 2L, stringArray(), VirtualValues.EMPTY_MAP ), stringValue( "KNOWS" ), VirtualValues.map( new String[]{"name", "age"}, new AnyValue[]{stringValue( "Bob" ), intValue( 14 )} ) ); assertThat( serialized( rel ), equalTo( "B1 71 91 B5 52 0C 01 02 85 4B 4E 4F 57 53 A2 84" + lineSeparator() + "6E 61 6D 65 83 42 6F 62 83 61 67 65 0E" ) ); }
@Test void shouldHandleMultipleListConcatenation() { // Given ListValue inner1 = list( stringValue( "foo" ), longValue( 42 ), booleanValue( true ) ); ListValue inner2 = list( list(stringValue("bar"), intValue(42)) ); ListValue inner3 = list( map("foo", 1337L, "bar", 42 ), stringValue("baz") ); // When ListValue concat = concat( inner1, inner2, inner3 ); // Then ListValue expected = list( stringValue( "foo" ), longValue( 42 ), booleanValue( true ), list( stringValue( "bar" ), intValue( 42 ) ), map( "foo", 1337L, "bar", 42 ), stringValue( "baz" ) ); assertEquals(expected, concat); assertEquals(expected.hashCode(), concat.hashCode()); assertArrayEquals(expected.asArray(), concat.asArray()); } }
private MapValue mapValue( Object... kv ) { assert kv.length % 2 == 0; String[] keys = new String[kv.length / 2]; AnyValue[] values = new AnyValue[kv.length / 2]; for ( int i = 0; i < kv.length; i += 2 ) { keys[i / 2] = (String) kv[i]; values[i / 2] = (AnyValue) kv[i + 1]; } return map( keys, values ); } }
@Test void shouldHandlePaths() { // Given NodeValue startNode = VirtualValues.nodeValue( 1L, Values.stringArray( "L" ), EMPTY_MAP ); NodeValue endNode = VirtualValues.nodeValue( 2L, Values.stringArray( "L" ), EMPTY_MAP ); RelationshipValue rel = VirtualValues.relationshipValue( 42L, startNode, endNode, stringValue( "R" ), EMPTY_MAP ); PathValue path = VirtualValues.path( new NodeValue[]{startNode, endNode}, new RelationshipValue[]{rel} ); PrettyPrinter printer = new PrettyPrinter(); // When path.writeTo( printer ); // Then assertThat( printer.value(), equalTo( "(id=1 :L)-[id=42 :R]->(id=2 :L)" ) ); }
@Test public void shouldHandleRelationships() { // Given RelationshipValue relValue = relationshipValue( 1L, nodeValue( 42L, stringArray( "L" ), EMPTY_MAP ), nodeValue( 42L, stringArray( "L" ), EMPTY_MAP ), stringValue( "R" ), EMPTY_MAP ); // When relValue.writeTo( converter ); // Then assertThat( converter.value(), equalTo( VirtualValues.relationship( 1L ) ) ); }
public static ListValue asListValue( Iterable<?> collection ) { ArrayList<AnyValue> values = new ArrayList<>(); for ( Object o : collection ) { values.add( ValueUtils.of( o ) ); } return VirtualValues.fromList( values ); }
public static AnyValue convert( Iterable<Notification> notifications ) { List<AnyValue> out = new ArrayList<>(); for ( Notification notification : notifications ) { InputPosition pos = notification.getPosition(); // position is optional boolean includePosition = !pos.equals( InputPosition.empty ); int size = includePosition ? 5 : 4; MapValueBuilder builder = new MapValueBuilder( size ); builder.add( "code", stringValue( notification.getCode() ) ); builder.add( "title", stringValue( notification.getTitle() ) ); builder.add( "description", stringValue( notification.getDescription() ) ); builder.add( "severity", stringValue( notification.getSeverity().toString() ) ); if ( includePosition ) { // only add the position if it is not empty builder.add( "position", VirtualValues.map( new String[]{"offset", "line", "column"}, new AnyValue[]{ intValue( pos.getOffset() ), intValue( pos.getLine() ), intValue( pos.getColumn() ) } ) ); } out.add( builder.build() ); } return VirtualValues.fromList( out ); } }
public static RelationshipValue rel( long id, long start, long end ) { return relationshipValue( id, node( start ), node( end ), stringValue( "T" ), emptyMap() ); }