Skip to content

Commit

Permalink
Merge 2.2 into 2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
davidegrohmann committed May 26, 2015
2 parents 0827071 + 294e200 commit 4a50cef
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,20 @@ private void validateLabelIds( long[] labelIds,
{
engine.comparativeCheck( records.label( (int) labelId ), this );
}
sort( labelIds );
// This first loop, before sorting happens, verifies that labels are ordered like they are supposed to
boolean outOfOrder = false;
for ( int i = 1; i < labelIds.length; i++ )
{
if ( labelIds[i -1] > labelIds[i])
{
engine.report().labelsOutOfOrder( labelIds[i-1], labelIds[i] );
outOfOrder = true;
}
}
if ( outOfOrder )
{
sort( labelIds );
}
for ( int i = 1; i < labelIds.length; i++ )
{
if ( labelIds[i - 1] == labelIds[i] )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ interface NodeConsistencyReport extends PrimitiveConsistencyReport
@Documented
void labelDuplicate( long labelId );

/** The label id array is not ordered */
@Documented
void labelsOutOfOrder( long largest, long smallest );

/** The dynamic label record is not in use. */
@Documented
void dynamicLabelRecordNotInUse( DynamicRecord record );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@
*/
package org.neo4j.consistency.checking;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collection;

import org.junit.Test;

import org.neo4j.consistency.report.ConsistencyReport;
import org.neo4j.kernel.impl.store.DynamicArrayStore;
import org.neo4j.kernel.impl.store.DynamicNodeLabels;
import org.neo4j.kernel.impl.store.DynamicRecordAllocator;
import org.neo4j.kernel.impl.store.InlineNodeLabels;
import org.neo4j.kernel.impl.store.NodeStore;
import org.neo4j.kernel.impl.store.PreAllocatedRecords;
import org.neo4j.kernel.impl.store.record.DynamicRecord;
import org.neo4j.kernel.impl.store.record.LabelTokenRecord;
Expand All @@ -37,7 +39,6 @@
import org.neo4j.kernel.impl.store.record.RelationshipRecord;

import static java.util.Arrays.asList;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -311,6 +312,108 @@ public void shouldReportDuplicateDynamicLabels() throws Exception
verify( report ).labelDuplicate( 11 );
}

@Test
public void shouldReportOutOfOrderLabels() throws Exception
{
// given
final NodeRecord node = inUse( new NodeRecord( 42, false, NONE, NONE ) );
// We need to do this override so we can put the labels unsorted, since InlineNodeLabels always sorts on insert
new InlineNodeLabels( node.getLabelField(), node )
{
@Override
public Collection<DynamicRecord> put( long[] labelIds, NodeStore nodeStore, DynamicRecordAllocator
allocator )
{
return putSorted( node, labelIds, nodeStore, allocator );
}
}.put( new long[]{3, 1, 2}, null, null );
LabelTokenRecord label1 = inUse( new LabelTokenRecord( 1 ) );
LabelTokenRecord label2 = inUse( new LabelTokenRecord( 2 ) );
LabelTokenRecord label3 = inUse( new LabelTokenRecord( 3 ) );

add( label1 );
add( label2 );
add( label3 );
add( node );

// when
ConsistencyReport.NodeConsistencyReport report = check( node );

// then
verify( report ).labelsOutOfOrder( 3, 1 );
}

@Test
public void shouldProperlyReportOutOfOrderLabelsThatAreFarAway() throws Exception
{
// given
final NodeRecord node = inUse( new NodeRecord( 42, false, NONE, NONE ) );
// We need to do this override so we can put the labels unsorted, since InlineNodeLabels always sorts on insert
new InlineNodeLabels( node.getLabelField(), node )
{
@Override
public Collection<DynamicRecord> put( long[] labelIds, NodeStore nodeStore, DynamicRecordAllocator
allocator )
{
return putSorted( node, labelIds, nodeStore, allocator );
}
}.put( new long[]{1, 18, 13, 14, 15, 16, 12}, null, null );
LabelTokenRecord label1 = inUse( new LabelTokenRecord( 1 ) );
LabelTokenRecord label12 = inUse( new LabelTokenRecord( 12 ) );
LabelTokenRecord label13 = inUse( new LabelTokenRecord( 13 ) );
LabelTokenRecord label14 = inUse( new LabelTokenRecord( 14 ) );
LabelTokenRecord label15 = inUse( new LabelTokenRecord( 15 ) );
LabelTokenRecord label16 = inUse( new LabelTokenRecord( 16 ) );
LabelTokenRecord label18 = inUse( new LabelTokenRecord( 18 ) );

add( label1 );
add( label12 );
add( label13 );
add( label14 );
add( label15 );
add( label16 );
add( label18 );
add( node );

// when
ConsistencyReport.NodeConsistencyReport report = check( node );

// then
verify( report ).labelsOutOfOrder( 18, 13 );
verify( report ).labelsOutOfOrder( 16, 12 );
}

@Test
public void shouldReportOutOfOrderDynamicLabels() throws Exception
{
// given
long[] labelIds = createLabels( 100 );

NodeRecord node = inUse( new NodeRecord( 42, false, NONE, NONE ) );
add( node );

DynamicRecord labelsRecord1 = inUse( array( new DynamicRecord( 1 ) ) );
DynamicRecord labelsRecord2 = inUse( array( new DynamicRecord( 2 ) ) );
Collection<DynamicRecord> labelRecords = asList( labelsRecord1, labelsRecord2 );

long temp = labelIds[12];
labelIds[12] = labelIds[11];
labelIds[11] = temp;
DynamicArrayStore.allocateFromNumbers( new ArrayList<DynamicRecord>(), labelIds, labelRecords.iterator(),
new PreAllocatedRecords( 52 ) );
assertDynamicRecordChain( labelsRecord1, labelsRecord2 );
node.setLabelField( DynamicNodeLabels.dynamicPointer( labelRecords ), labelRecords );

addNodeDynamicLabels( labelsRecord1 );
addNodeDynamicLabels( labelsRecord2 );

// when
ConsistencyReport.NodeConsistencyReport report = check( node );

// then
verify( report ).labelsOutOfOrder( labelIds[11], labelIds[12] );
}

@Test
public void shouldDynamicLabelRecordsNotInUse() throws Exception
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public Collection<DynamicRecord> put( long[] labelIds, NodeStore nodeStore, Dyna
return putSorted( node, labelIds, nodeStore, allocator );
}

public static Collection<DynamicRecord> putSorted( NodeRecord node, long[] labelIds, NodeStore nodeStore,
DynamicRecordAllocator allocator )
public static Collection<DynamicRecord> putSorted( NodeRecord node, long[] labelIds,
NodeStore nodeStore, DynamicRecordAllocator allocator )
{
if ( tryInlineInNodeRecord( node, labelIds, node.getDynamicLabelRecords() ) )
{
Expand Down

0 comments on commit 4a50cef

Please sign in to comment.