Skip to content

Commit

Permalink
Do not use subtraction in compareTo
Browse files Browse the repository at this point in the history
  • Loading branch information
MishaDemianenko committed Jul 12, 2018
1 parent bc77d6c commit 7b89b19
Show file tree
Hide file tree
Showing 11 changed files with 18 additions and 43 deletions.
Expand Up @@ -78,6 +78,7 @@
*/ */
public class TransactionRecordState implements RecordState public class TransactionRecordState implements RecordState
{ {
private static final CommandComparator COMMAND_SORTER = new CommandComparator();
private static final Command[] EMPTY_COMMANDS = new Command[0]; private static final Command[] EMPTY_COMMANDS = new Command[0];


private final NeoStores neoStores; private final NeoStores neoStores;
Expand Down Expand Up @@ -464,43 +465,17 @@ void createRelationshipTypeToken( String name, long id )
creator.createToken( name, id, recordChangeSet.getRelationshipTypeTokenChanges() ); creator.createToken( name, id, recordChangeSet.getRelationshipTypeTokenChanges() );
} }


private static class CommandSorter implements Comparator<Command> private static class CommandComparator implements Comparator<Command>
{ {
@Override @Override
public int compare( Command o1, Command o2 ) public int compare( Command o1, Command o2 )
{ {
long id1 = o1.getKey(); long id1 = o1.getKey();
long id2 = o2.getKey(); long id2 = o2.getKey();
long diff = id1 - id2; return Long.compare( id1, id2 );
if ( diff > Integer.MAX_VALUE )
{
return Integer.MAX_VALUE;
}
else if ( diff < Integer.MIN_VALUE )
{
return Integer.MIN_VALUE;
}
else
{
return (int) diff;
}
}

@Override
public boolean equals( Object o )
{
return o instanceof CommandSorter;
}

@Override
public int hashCode()
{
return 3217;
} }
} }


private static final CommandSorter COMMAND_SORTER = new CommandSorter();

private RecordProxy<NeoStoreRecord, Void> getOrLoadNeoStoreRecord() private RecordProxy<NeoStoreRecord, Void> getOrLoadNeoStoreRecord()
{ {
// TODO Move this neo store record thingie into RecordAccessSet // TODO Move this neo store record thingie into RecordAccessSet
Expand Down
Expand Up @@ -74,6 +74,6 @@ public int compareTo( CountsKey other )
{ {
return Long.compare( indexId, ((IndexKey) other).indexId() ); return Long.compare( indexId, ((IndexKey) other).indexId() );
} }
return recordType().ordinal() - other.recordType().ordinal(); return recordType().compareTo( other.recordType() );
} }
} }
Expand Up @@ -41,6 +41,6 @@ public int compareTo( CountsKey other )
{ {
return super.compareTo( other ); return super.compareTo( other );
} }
return recordType().ordinal() - other.recordType().ordinal(); return recordType().compareTo( other.recordType() );
} }
} }
Expand Up @@ -41,6 +41,6 @@ public int compareTo( CountsKey other )
{ {
return super.compareTo( other ); return super.compareTo( other );
} }
return recordType().ordinal() - other.recordType().ordinal(); return recordType().compareTo( other.recordType() );
} }
} }
Expand Up @@ -77,8 +77,8 @@ public int compareTo( CountsKey other )
{ {
org.neo4j.kernel.impl.store.counts.keys.NodeKey that = org.neo4j.kernel.impl.store.counts.keys.NodeKey that =
(org.neo4j.kernel.impl.store.counts.keys.NodeKey) other; (org.neo4j.kernel.impl.store.counts.keys.NodeKey) other;
return this.labelId - that.labelId; return Integer.compare( this.labelId, that.labelId );
} }
return recordType().ordinal() - other.recordType().ordinal(); return recordType().compareTo( other.recordType() );
} }
} }
Expand Up @@ -104,14 +104,14 @@ public int compareTo( CountsKey other )
RelationshipKey that = (RelationshipKey) other; RelationshipKey that = (RelationshipKey) other;
if ( this.typeId != that.typeId ) if ( this.typeId != that.typeId )
{ {
return this.typeId - that.typeId; return Integer.compare( this.typeId, that.typeId );
} }
if ( this.startLabelId != that.startLabelId ) if ( this.startLabelId != that.startLabelId )
{ {
return this.startLabelId - that.startLabelId; return Integer.compare( this.startLabelId, that.startLabelId );
} }
return this.endLabelId - that.endLabelId; return Integer.compare( this.endLabelId, that.endLabelId );
} }
return recordType().ordinal() - other.recordType().ordinal(); return recordType().compareTo( other.recordType() );
} }
} }
Expand Up @@ -124,7 +124,7 @@ public boolean eq( Object other )
@Override @Override
public int compareTo( PointValue other ) public int compareTo( PointValue other )
{ {
int cmpCRS = this.crs.getCode() - other.crs.getCode(); int cmpCRS = Integer.compare( this.crs.getCode(), other.crs.getCode() );
if ( cmpCRS != 0 ) if ( cmpCRS != 0 )
{ {
return cmpCRS; return cmpCRS;
Expand Down
Expand Up @@ -54,7 +54,7 @@ public InstanceId( int serverId )
@Override @Override
public int compareTo( InstanceId o ) public int compareTo( InstanceId o )
{ {
return serverId - o.serverId; return Integer.compare( serverId, o.serverId );
} }


@Override @Override
Expand Down
Expand Up @@ -61,7 +61,7 @@ public long getId()
@Override @Override
public int compareTo( InstanceId o ) public int compareTo( InstanceId o )
{ {
return (int) (id - o.getId()); return Long.compare( id, o.getId() );
} }


@Override @Override
Expand Down
Expand Up @@ -117,7 +117,7 @@ public int compareTo( LockUnit o )


// Then shared/exclusive locks are compared by resourceTypeId and then by resourceId // Then shared/exclusive locks are compared by resourceTypeId and then by resourceId
return resourceType.typeId() == o.resourceType.typeId() ? Long.compare( resourceId, o.resourceId ) return resourceType.typeId() == o.resourceType.typeId() ? Long.compare( resourceId, o.resourceId )
: resourceType.typeId() - o.resourceType.typeId(); : Integer.compare( resourceType.typeId(), o.resourceType.typeId() );
} }


@Override @Override
Expand Down
Expand Up @@ -55,7 +55,7 @@ public int compareTo( ElectionCredentials o )
// Smaller id means higher priority // Smaller id means higher priority
if ( this.currentWinner == other.currentWinner ) if ( this.currentWinner == other.currentWinner )
{ {
return Integer.signum( other.serverId - this.serverId ); return Integer.compare( other.serverId, this.serverId );
} }
else else
{ {
Expand All @@ -64,7 +64,7 @@ public int compareTo( ElectionCredentials o )
} }
else else
{ {
return Long.signum( this.latestTxId - other.latestTxId ); return Long.compare( this.latestTxId, other.latestTxId );
} }
} }


Expand Down

0 comments on commit 7b89b19

Please sign in to comment.