Skip to content

Commit

Permalink
Don't clear an empty table
Browse files Browse the repository at this point in the history
  • Loading branch information
tinwelint committed Dec 21, 2015
1 parent d4de728 commit b509ce1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
Expand Up @@ -163,7 +163,10 @@ protected int index( int index )
@Override
public void clear()
{
clearTable();
if ( !isEmpty() )
{
clearTable();
}
super.clear();
}
}
Expand Up @@ -39,13 +39,21 @@ protected UnsafeTable( int capacity, int bytesPerKey, VALUE valueMarker )
this.valueMarker = valueMarker;
this.dataSize = (long)this.capacity*bytesPerEntry;
this.address = UnsafeUtil.allocateMemory( dataSize );
clear();
clearMemory();
}

@Override
public void clear()
{
if ( !isEmpty() )
{
clearMemory();
}
super.clear();
}

private void clearMemory()
{
UnsafeUtil.setMemory( address, dataSize, (byte)-1 );
}

Expand Down
Expand Up @@ -192,9 +192,9 @@ public BasicTableTest( TableFactory factory )
@Test
public void shouldSetAndGetSmallKey() throws Exception
{
// GIVEN
try ( Table table = factory.newTable( Primitive.DEFAULT_HEAP_CAPACITY ) )
{
// GIVEN
long nullKey = table.nullKey();
assertEquals( nullKey, table.key( 0 ) );

Expand All @@ -215,10 +215,10 @@ public void shouldSetAndGetSmallKey() throws Exception
@Test
public void shouldSetAndGetBigKey() throws Exception
{
// GIVEN
assumeTrue( factory.supportsLongs() );
try ( Table table = factory.newTable( Primitive.DEFAULT_HEAP_CAPACITY ) )
{
// GIVEN
long nullKey = table.nullKey();
assertEquals( nullKey, table.key( 0 ) );

Expand All @@ -235,10 +235,10 @@ public void shouldSetAndGetBigKey() throws Exception
@Test
public void shouldRemoveBigKey() throws Exception
{
// GIVEN
assumeTrue( factory.supportsLongs() );
try ( Table table = factory.newTable( Primitive.DEFAULT_HEAP_CAPACITY ) )
{
// GIVEN
long nullKey = table.nullKey();
long key = 0x24F1FF3FEL;
int index = 5;
Expand All @@ -256,9 +256,9 @@ public void shouldRemoveBigKey() throws Exception
@Test
public void shouldSetHopBits() throws Exception
{
// GIVEN
try ( Table<?> table = factory.newTable( Primitive.DEFAULT_HEAP_CAPACITY ) )
{
// GIVEN
int index = 10;
long hopBits = table.hopBits( index );
assertEquals( 0L, hopBits );
Expand All @@ -275,9 +275,9 @@ public void shouldSetHopBits() throws Exception
@Test
public void shouldMoveHopBit() throws Exception
{
// GIVEN
try ( Table<?> table = factory.newTable( Primitive.DEFAULT_HEAP_CAPACITY ) )
{
// GIVEN
int index = 10;
table.putHopBit( index, 2 );
table.putHopBit( index, 11 );
Expand All @@ -290,6 +290,26 @@ public void shouldMoveHopBit() throws Exception
}
}

@Test
public void shouldClearTable() throws Exception
{
try ( Table table = factory.newTable( Primitive.DEFAULT_HEAP_CAPACITY ) )
{
// GIVEN
int index = 3;
long key = 123L;
Object value = factory.sampleValue();
table.put( index, key, value );
assertEquals( key, table.key( index ) );

// WHEN
table.clear();

// THEN
assertEquals( table.nullKey(), table.key( index ) );
}
}

private interface TableFactory
{
Table newTable( int capacity );
Expand Down

0 comments on commit b509ce1

Please sign in to comment.