private void writeNodesForPath( NodeValue[] nodes ) throws IOException { nodeIndexes.reset( nodes.length ); for ( NodeValue node : nodes ) { nodeIndexes.putIfAbsent( node.id(), nodeIndexes.size() ); } int size = nodeIndexes.size(); packListHeader( size ); if ( size > 0 ) { NodeValue node = nodes[0]; for ( long id : nodeIndexes.keys() ) { int i = 1; while ( node.id() != id ) { node = nodes[i++]; } node.writeTo( this ); } } }
@Override public <E extends Exception> void writeTo( AnyValueWriter<E> writer ) throws E { writer.writeNode( id, labels(), properties() ); }
@Override public void writePath( NodeValue[] nodes, RelationshipValue[] relationships ) { if ( nodes.length == 0 ) { return; } //Path guarantees that nodes.length = edges.length = 1 nodes[0].writeTo( this ); for ( int i = 0; i < relationships.length; i++ ) { relationships[i].writeTo( this ); append( ">" ); nodes[i + 1].writeTo( this ); } }
@Test public void shouldBeAbleToPackAndUnpackMap() throws IOException { // Given PackedOutputArray output = new PackedOutputArray(); Neo4jPack.Packer packer = neo4jPack.newPacker( output ); packer.packMapHeader( ALICE.properties().size() ); ALICE.properties().foreach( ( s, value ) -> { try { packer.pack( s ); packer.pack( value ); } catch ( IOException e ) { throw new UncheckedIOException( e ); } } ); AnyValue unpacked = unpacked( output.bytes() ); // Then assertThat( unpacked, instanceOf( MapValue.class ) ); MapValue unpackedMap = (MapValue) unpacked; assertThat( unpackedMap, equalTo( ALICE.properties() ) ); }
@Test public void shouldBeAbleToPackAndUnpackList() throws IOException { // Given PackedOutputArray output = new PackedOutputArray(); Neo4jPack.Packer packer = neo4jPack.newPacker( output ); packer.packListHeader( ALICE.labels().length() ); List<String> expected = new ArrayList<>(); TextArray labels = ALICE.labels(); for ( int i = 0; i < labels.length(); i++ ) { String labelName = labels.stringValue( i ); packer.pack( labelName ); expected.add( labelName ); } AnyValue unpacked = unpacked( output.bytes() ); // Then assertThat( unpacked, instanceOf( ListValue.class ) ); ListValue unpackedList = (ListValue) unpacked; assertThat( unpackedList, equalTo( ValueUtils.asListValue( expected ) ) ); }
@Override public int computeHash() { NodeValue[] nodes = nodes(); RelationshipValue[] relationships = relationships(); int result = nodes[0].hashCode(); for ( int i = 1; i < nodes.length; i++ ) { result += 31 * (result + relationships[i - 1].hashCode()); result += 31 * (result + nodes[i].hashCode()); } return result; }
@Override public int compareTo( VirtualValue other, Comparator<AnyValue> comparator ) { if ( other == null || !(other instanceof PathValue) ) { throw new IllegalArgumentException( "Cannot compare different virtual values" ); } PathValue otherPath = (PathValue) other; NodeValue[] nodes = nodes(); RelationshipValue[] relationships = relationships(); NodeValue[] otherNodes = otherPath.nodes(); RelationshipValue[] otherRelationships = otherPath.relationships(); int x = nodes[0].compareTo( otherNodes[0], comparator ); if ( x == 0 ) { int i = 0; int length = Math.min( relationships.length, otherRelationships.length ); while ( x == 0 && i < length ) { x = relationships[i].compareTo( otherRelationships[i], comparator ); ++i; } if ( x == 0 ) { x = Integer.compare( relationships.length, otherRelationships.length ); } } return x; }
@Test public void shouldHandleNodes() { // Given NodeValue nodeValue = nodeValue( 42L, stringArray( "L" ), EMPTY_MAP ); // When nodeValue.writeTo( converter ); // Then assertThat( converter.value(), equalTo( VirtualValues.node( 42L ) ) ); }
return ((NodeValue) object).properties().get( key );
@Override public int computeHash() { NodeValue[] nodes = nodes(); RelationshipValue[] relationships = relationships(); int result = nodes[0].hashCode(); for ( int i = 1; i < nodes.length; i++ ) { result += 31 * (result + relationships[i - 1].hashCode()); result += 31 * (result + nodes[i].hashCode()); } return result; }
@Override public int compareTo( VirtualValue other, Comparator<AnyValue> comparator ) { if ( other == null || !(other instanceof PathValue) ) { throw new IllegalArgumentException( "Cannot compare different virtual values" ); } PathValue otherPath = (PathValue) other; NodeValue[] nodes = nodes(); RelationshipValue[] relationships = relationships(); NodeValue[] otherNodes = otherPath.nodes(); RelationshipValue[] otherRelationships = otherPath.relationships(); int x = nodes[0].compareTo( otherNodes[0], comparator ); if ( x == 0 ) { int i = 0; int length = Math.min( relationships.length, otherRelationships.length ); while ( x == 0 && i < length ) { x = relationships[i].compareTo( otherRelationships[i], comparator ); ++i; } if ( x == 0 ) { x = Integer.compare( relationships.length, otherRelationships.length ); } } return x; }
public static boolean hasLabel( AnyValue entity, int labelToken, DbAccess access ) { if ( entity instanceof NodeValue ) { return access.isLabelSetOnNode( labelToken, ((NodeValue) entity).id() ); } else { throw new ParameterWrongTypeException( "Expected a Node, got: " + entity, null ); } }
private void writeNodesForPath( NodeValue[] nodes ) throws IOException { nodeIndexes.reset( nodes.length ); for ( NodeValue node : nodes ) { nodeIndexes.putIfAbsent( node.id(), nodeIndexes.size() ); } int size = nodeIndexes.size(); packListHeader( size ); if ( size > 0 ) { NodeValue node = nodes[0]; for ( long id : nodeIndexes.keys() ) { int i = 1; while ( node.id() != id ) { node = nodes[i++]; } node.writeTo( this ); } } }
@Test void shouldHandleNodeValueWithoutProperties() { // Given NodeValue node = VirtualValues.nodeValue( 42L, Values.stringArray( "L1", "L2", "L3" ), EMPTY_MAP ); PrettyPrinter printer = new PrettyPrinter(); // When node.writeTo( printer ); // Then assertThat( printer.value(), equalTo( "(id=42 :L1:L2:L3)" ) ); }
@Override public <E extends Exception> void writeTo( AnyValueWriter<E> writer ) throws E { writer.writeNode( id, labels(), properties() ); }
return ((NodeValue) object).properties().get( key );
public static ListValue labels( AnyValue item, DbAccess access ) { if ( item instanceof NodeValue ) { return access.getLabelsForNode( ((NodeValue) item).id() ); } else { throw new ParameterWrongTypeException( "Expected a Node, got: " + item, null ); } }
@Test void shouldHandleNodeValueWithoutLabelsNorProperties() { // Given NodeValue node = VirtualValues.nodeValue( 42L, Values.stringArray(), EMPTY_MAP ); PrettyPrinter printer = new PrettyPrinter(); // When node.writeTo( printer ); // Then assertThat( printer.value(), equalTo( "(id=42)" ) ); }