Skip to content

Commit

Permalink
Remove LabelValue
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed Jun 19, 2017
1 parent be0f1c1 commit 601fd47
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 189 deletions.
6 changes: 0 additions & 6 deletions community/bolt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,6 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-values</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-kernel</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@
*/
package org.neo4j.values;

import java.io.IOException;

import org.neo4j.values.virtual.CoordinateReferenceSystem;
import org.neo4j.values.virtual.EdgeValue;
import org.neo4j.values.virtual.LabelSet;
import org.neo4j.values.virtual.LabelValue;
import org.neo4j.values.virtual.MapValue;
import org.neo4j.values.virtual.NodeValue;

Expand All @@ -40,8 +37,6 @@ public interface AnyValueWriter<E extends Exception> extends ValueWriter<E>

void beginLabels( int numberOfLabels ) throws E;

void writeLabel( LabelValue labelValue ) throws E;

void endLabels() throws E;

void writeEdgeReference( long edgeId ) throws E;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@
*/
package org.neo4j.values.virtual;

import java.util.Comparator;
import java.util.Iterator;
import java.util.NoSuchElementException;

import org.neo4j.values.AnyValue;
import org.neo4j.values.AnyValues;
import org.neo4j.values.Value;
import org.neo4j.values.Values;
import org.neo4j.values.VirtualValue;

/**
* This class is way too similar to org.neo4j.collection.primitive.PrimitiveArrays.
*
* <p>
* Should we introduce dependency on primitive collections?
*/
final class ArrayHelpers
Expand All @@ -47,11 +51,23 @@ static boolean isSortedSet( int[] keys )
return true;
}

static boolean isSortedSet( VirtualValue[] keys )
static boolean isSortedSet( VirtualValue[] keys, Comparator<AnyValue> comparator )
{
for ( int i = 0; i < keys.length - 1; i++ )
{
if ( comparator.compare( keys[i], keys[i + 1] ) >= 0 )
{
return false;
}
}
return true;
}

static boolean isSortedSet( Value[] keys, Comparator<AnyValue> comparator )
{
for ( int i = 0; i < keys.length - 1; i++ )
{
if ( AnyValues.COMPARATOR.compare( keys[i], keys[i + 1] ) >= 0 )
if ( comparator.compare( keys[i], keys[i + 1] ) >= 0 )
{
return false;
}
Expand All @@ -70,4 +86,29 @@ static boolean hasNullOrNoValue( AnyValue[] values )
}
return false;
}

static <T> Iterator<T> asIterator( T[] array )
{
assert array != null;
return new Iterator<T>()
{
private int index;

@Override
public boolean hasNext()
{
return index < array.length;
}

@Override
public T next()
{
if ( !hasNext() )
{
throw new NoSuchElementException();
}
return array[index++];
}
};
}
}
124 changes: 89 additions & 35 deletions community/values/src/main/java/org/neo4j/values/virtual/LabelSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,59 +21,54 @@

import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;

import org.neo4j.values.AnyValue;
import org.neo4j.values.AnyValueWriter;
import org.neo4j.values.AnyValues;
import org.neo4j.values.TextValue;
import org.neo4j.values.VirtualValue;

import static org.neo4j.values.virtual.ArrayHelpers.asIterator;
import static org.neo4j.values.virtual.ArrayHelpers.isSortedSet;
import static org.neo4j.values.virtual.VirtualValueGroup.LABEL_SET;

public abstract class LabelSet extends VirtualValue
public abstract class LabelSet extends VirtualValue implements Iterable<TextValue>
{
public abstract int size();

public abstract int getLabelId( int offset );

/**
* This implementation is assuming that the label set is fairly
* small, maintains a sorted set of labels.
*/
static class ArrayBasedLabelSet extends LabelSet
{
private final LabelValue[] labelIds;
private final TextValue[] labels;

ArrayBasedLabelSet( LabelValue[] labels )
ArrayBasedLabelSet( TextValue[] labels )
{
assert labels != null;
assert isSortedSet( labels );

this.labelIds = labels;
}

@Override
public int size()
{
return labelIds.length;
}

@Override
public int getLabelId( int offset )
{
return labelIds[offset].id();
this.labels = new TextValue[labels.length];
System.arraycopy( labels, 0, this.labels, 0, this.labels.length );
Arrays.sort( this.labels, AnyValues.COMPARATOR );
assert isSortedSet( this.labels, AnyValues.COMPARATOR );
}

@Override
public <E extends Exception> void writeTo( AnyValueWriter<E> writer ) throws E
{
writer.beginLabels( labelIds.length );
for ( LabelValue label : labelIds )
writer.beginLabels( labels.length );
for ( TextValue label : labels )
{
writer.writeLabel( label );
label.writeTo( writer );
}
writer.endLabels();
}

@Override
public int hash()
{
return Arrays.hashCode( labelIds );
return Arrays.hashCode( labels );
}

@Override
Expand All @@ -83,19 +78,40 @@ public boolean equals( VirtualValue other )
{
return false;
}
LabelSet that = (LabelSet) other;
if ( labelIds.length != that.size() )
if ( labels.length != ((LabelSet) other).size() )
{
return false;
}
for ( int i = 0; i < labelIds.length; i++ )
{
if ( labelIds[i].id() != that.getLabelId( i ) )

if ( other instanceof ArrayBasedLabelSet )
{ //fast route
ArrayBasedLabelSet that = (ArrayBasedLabelSet) other;

for ( int i = 0; i < labels.length; i++ )
{
if ( !labels[i].equals( that.labels[i] ) )
{
return false;
}
}
return true;
}
else
{ //slow route
LabelSet that = (LabelSet) other;
Iterator<TextValue> thisIterator = iterator();
Iterator<TextValue> thatIterator = that.iterator();
while ( thisIterator.hasNext() && thatIterator.hasNext() )
{
return false;
TextValue label1 = thisIterator.next();
TextValue label2 = thatIterator.next();
if ( !label1.equals( label2 ) )
{
return false;
}
}
return true;
}
return true;
}

@Override
Expand All @@ -117,17 +133,55 @@ public int compareTo( VirtualValue other, Comparator<AnyValue> comparator )

if ( x == 0 )
{
for ( int i = 0; i < size(); i++ )
if ( otherSet instanceof ArrayBasedLabelSet )
{
ArrayBasedLabelSet otherArraySet = (ArrayBasedLabelSet) otherSet;
for ( int i = 0; i < size(); i++ )
{
x = comparator.compare( labels[i], otherArraySet.labels[i] );
if ( x != 0 )
{
return x;
}
}
}
else
{
x = Integer.compare( this.labelIds[i].id(), otherSet.getLabelId( i ) );
if ( x != 0 )
Iterator<TextValue> thisIterator = iterator();
Iterator<TextValue> thatIterator = otherSet.iterator();
while ( thisIterator.hasNext() && thatIterator.hasNext() )
{
return x;
TextValue label1 = thisIterator.next();
TextValue label2 = thatIterator.next();
x = comparator.compare( label1, label2 );
if ( x != 0 )
{
return x;
}
}
}
}

return x;
}

@Override
public String toString()
{
return Arrays.toString( labels );
}

@Override
public int size()
{
return labels.length;
}

@SuppressWarnings( "NullableProblems" )
@Override
public Iterator<TextValue> iterator()
{
return asIterator( labels );
}
}
}

This file was deleted.

0 comments on commit 601fd47

Please sign in to comment.