Skip to content

Commit

Permalink
Simplify the SwapperSet interface
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvest committed May 26, 2017
1 parent a58e022 commit ac6b4bd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 71 deletions.
Expand Up @@ -19,7 +19,6 @@
*/ */
package org.neo4j.io.pagecache.impl.muninn; package org.neo4j.io.pagecache.impl.muninn;


import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.function.IntConsumer; import java.util.function.IntConsumer;


Expand All @@ -36,7 +35,7 @@ final class SwapperSet
private final PrimitiveIntSet free = Primitive.intSet(); private final PrimitiveIntSet free = Primitive.intSet();
private final Object vacuumLock = new Object(); private final Object vacuumLock = new Object();


private static final class Allocation public static final class Allocation
{ {
public final int id; public final int id;
public final int filePageSize; public final int filePageSize;
Expand All @@ -50,28 +49,15 @@ private Allocation( int id, int filePageSize, PageSwapper swapper )
} }
} }


public interface ApplyCall public Allocation getAllocation( int id )
{
void apply( PageSwapper swapper, int filePageSize ) throws IOException;
}

public PageSwapper getSwapper( int id )
{
checkId( id );
return allocations[id].swapper;
}

public int getFilePageSize( int id )
{
checkId( id );
return allocations[id].filePageSize;
}

public void apply( int id, ApplyCall call ) throws IOException
{ {
checkId( id ); checkId( id );
Allocation allocation = allocations[id]; Allocation allocation = allocations[id];
call.apply( allocation.swapper, allocation.filePageSize ); if ( allocation == null )
{
throw noSuchId( id );
}
return allocation;
} }


private void checkId( int id ) private void checkId( int id )
Expand All @@ -82,6 +68,11 @@ private void checkId( int id )
} }
} }


private NullPointerException noSuchId( int id )
{
return new NullPointerException( "Swapper allocation by id " + id + " does not exist; it might have been freed" );
}

public synchronized int allocate( PageSwapper swapper, int filePageSize ) public synchronized int allocate( PageSwapper swapper, int filePageSize )
{ {
Allocation[] allocations = this.allocations; Allocation[] allocations = this.allocations;
Expand Down
Expand Up @@ -25,9 +25,6 @@
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;


import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

import org.neo4j.collection.primitive.Primitive; import org.neo4j.collection.primitive.Primitive;
import org.neo4j.collection.primitive.PrimitiveIntSet; import org.neo4j.collection.primitive.PrimitiveIntSet;
import org.neo4j.io.pagecache.PageSwapper; import org.neo4j.io.pagecache.PageSwapper;
Expand All @@ -36,7 +33,6 @@
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;


Expand All @@ -54,34 +50,27 @@ public void setUp()
} }


@Test @Test
public void mustReturnAllocatedSwapperAndFilePageSize() throws Exception public void mustReturnAllocationWithSwapperAndFilePageSize() throws Exception
{ {
DummyPageSwapper a = new DummyPageSwapper( "a" ); DummyPageSwapper a = new DummyPageSwapper( "a" );
DummyPageSwapper b = new DummyPageSwapper( "b" ); DummyPageSwapper b = new DummyPageSwapper( "b" );
int idA = set.allocate( a, 42 ); int idA = set.allocate( a, 42 );
int idB = set.allocate( b, 43 ); int idB = set.allocate( b, 43 );
assertThat( set.getSwapper( idA ), is( a ) ); SwapperSet.Allocation allocA = set.getAllocation( idA );
assertThat( set.getFilePageSize( idA ), is( 42 ) ); SwapperSet.Allocation allocB = set.getAllocation( idB );
assertThat( set.getSwapper( idB ), is( b ) ); assertThat( allocA.swapper, is( a ) );
assertThat( set.getFilePageSize( idB ), is( 43 ) ); assertThat( allocA.filePageSize, is( 42 ) );
} assertThat( allocB.swapper, is( b ) );

assertThat( allocB.filePageSize, is( 43 ) );
@Test
public void accessingPageSwapperOfFreedAllocationMustThrow() throws Exception
{
int id = set.allocate( new DummyPageSwapper( "a" ), 42 );
set.free( id );
exception.expect( NullPointerException.class );
set.getSwapper( id );
} }


@Test @Test
public void accessingFilePageSizeOfFreedAllocationMustThrow() throws Exception public void accessingFreedAllocationMustThrow() throws Exception
{ {
int id = set.allocate( new DummyPageSwapper( "a" ), 42 ); int id = set.allocate( new DummyPageSwapper( "a" ), 42 );
set.free( id ); set.free( id );
exception.expect( NullPointerException.class ); exception.expect( NullPointerException.class );
set.getSwapper( id ); set.getAllocation( id );
} }


@Test @Test
Expand Down Expand Up @@ -176,42 +165,16 @@ public void mustNotUseZeroAsSwapperId() throws Exception
} }


@Test @Test
public void freeOfIdZeroMustThrow() throws Exception public void gettingAllocationZeroMustThrow() throws Exception
{ {
exception.expect( IllegalArgumentException.class ); exception.expect( IllegalArgumentException.class );
set.free( 0 ); set.getAllocation( 0 );
} }


@Test @Test
public void gettingSwapperOfIdZeroMustThrow() throws Exception public void freeOfIdZeroMustThrow() throws Exception
{
exception.expect( IllegalArgumentException.class );
set.getSwapper( 0 );
}

@Test
public void gettingFilePageSizeOfIdZeroMustThrow() throws Exception
{
exception.expect( IllegalArgumentException.class );
set.getFilePageSize( 0 );
}

@Test
public void applyMustThrowForIdZero() throws Exception
{ {
exception.expect( IllegalArgumentException.class ); exception.expect( IllegalArgumentException.class );
set.apply( 0, (swapper,size) -> fail( "should not have been called" ) ); set.free( 0 );
}

@Test
public void applyMustReceiveArguments() throws Exception
{
AtomicReference<PageSwapper> gotSwapper = new AtomicReference<>();
AtomicInteger gotFilePageSize = new AtomicInteger();
PageSwapper swapper = new DummyPageSwapper( "a" );
int id = set.allocate( swapper, 3113 );
set.apply( id, (s,size) -> { gotSwapper.set( s ); gotFilePageSize.set( size ); } );
assertThat( gotSwapper.get(), is( sameInstance( swapper ) ) );
assertThat( gotFilePageSize.get(), is( 3113 ) );
} }
} }

0 comments on commit ac6b4bd

Please sign in to comment.