Skip to content

Commit

Permalink
Disallows the reserved MINUS_ONE id when generating node ids in import
Browse files Browse the repository at this point in the history
  • Loading branch information
tinwelint committed Apr 4, 2016
1 parent d9139a1 commit e7b52ce
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
Expand Up @@ -19,7 +19,9 @@
*/ */
package org.neo4j.unsafe.impl.batchimport.cache.idmapping; package org.neo4j.unsafe.impl.batchimport.cache.idmapping;


import org.neo4j.kernel.impl.store.id.IdSequence;
import org.neo4j.unsafe.impl.batchimport.input.InputNode; import org.neo4j.unsafe.impl.batchimport.input.InputNode;
import org.neo4j.unsafe.impl.batchimport.store.BatchingIdSequence;


/** /**
* Common {@link IdGenerator} implementations. * Common {@link IdGenerator} implementations.
Expand Down Expand Up @@ -71,12 +73,12 @@ public static IdGenerator startingFrom( final long startingId )
{ {
return new IdGenerator() return new IdGenerator()
{ {
private long id = startingId; private final IdSequence ids = new BatchingIdSequence( startingId );


@Override @Override
public long generate( Object inputId ) public long generate( Object inputId )
{ {
return id++; return ids.nextId();
} }


@Override @Override
Expand Down
Expand Up @@ -29,6 +29,16 @@ public class BatchingIdSequence implements IdSequence
{ {
private long nextId = 0; private long nextId = 0;


public BatchingIdSequence()
{
this( 0 );
}

public BatchingIdSequence( long startingId )
{
nextId = startingId;
}

@Override @Override
public long nextId() public long nextId()
{ {
Expand Down
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.unsafe.impl.batchimport.cache.idmapping;

import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.neo4j.kernel.impl.store.id.IdGeneratorImpl;

public class IdGeneratorsTest
{
@Test
public void shouldNotUseReservedMinusOneId() throws Exception
{
// GIVEN
int idsBefore = 100;
IdGenerator generator = IdGenerators.startingFrom( IdGeneratorImpl.INTEGER_MINUS_ONE - idsBefore );

// WHEN/THEN
long previous = 0;
for ( int i = 0; i < idsBefore; i++ )
{
long current = generator.generate( null ); // This generator doesn't care about the input argument anyway.
assertTrue( previous < current );
assertNotEquals( current, IdGeneratorImpl.INTEGER_MINUS_ONE );
previous = current;
}
}
}

0 comments on commit e7b52ce

Please sign in to comment.