Skip to content

Commit

Permalink
(De-)serialize dirty blocks
Browse files Browse the repository at this point in the history
Addresses serialization bullet point of #1
  • Loading branch information
hanslovsky committed May 31, 2018
1 parent 8127d50 commit cb704c0
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.function.BiConsumer;
Expand Down Expand Up @@ -673,7 +674,7 @@ public static < T extends IntegerType< T > > void downsample(

public TLongSet getModifiedBlocks( final int level, final long id )
{
LOG.warn( "Getting modified blocks for level={} and id={}", level, id );
LOG.debug( "Getting modified blocks for level={} and id={}", level, id );
return Optional.ofNullable( this.affectedBlocksByLabel[ level ].get( id ) ).map( TLongHashSet::new ).orElseGet( TLongHashSet::new );
}

Expand Down Expand Up @@ -1084,4 +1085,51 @@ public CellGrid getCellGrid( final int t, final int level )
return ( ( AbstractCellImg< ?, ?, ?, ? > ) underlyingSource().getSource( t, level ) ).getCellGrid();
}

public long[] getAffectedBlocks()
{
return this.affectedBlocks.toArray();
}

Map< Long, long[] >[] getAffectedBlocksById()
{
@SuppressWarnings( "unchecked" )
final Map< Long, long[] >[] maps = new HashMap[ this.affectedBlocksByLabel.length ];

for ( int level = 0; level < maps.length; ++level )
{
maps[ level ] = new HashMap<>();
final Map< Long, long[] > map = maps[ level ];
for ( final Entry< Long, TLongHashSet > entry : this.affectedBlocksByLabel[ level ].entrySet() )
{
map.put( entry.getKey(), entry.getValue().toArray() );
}
}

LOG.debug( "Retruning affected blocks by id for {} levels: {}", maps.length, maps );
return maps;
}

void affectBlocks(
final long[] blocks,
final Map< Long, long[] >[] blocksById )
{
assert this.affectedBlocksByLabel.length == blocksById.length;

LOG.debug( "Affected blocks: {} to add: {}", this.affectedBlocks, blocks );
this.affectedBlocks.addAll( blocks );
LOG.debug( "Affected blocks: {}", this.affectedBlocks );

LOG.debug( "Affected blocks by id: {} to add: {}", this.affectedBlocksByLabel, blocksById );
for ( int level = 0; level < blocksById.length; ++level )
{
final Map< Long, TLongHashSet > map = this.affectedBlocksByLabel[ level ];
for ( final Entry< Long, long[] > entry : blocksById[ level ].entrySet() )
{
map.computeIfAbsent( entry.getKey(), key -> new TLongHashSet() ).addAll( entry.getValue() );
}
}
LOG.debug( "Affected blocks by id: {}", this.affectedBlocksByLabel, null );

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Type;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.concurrent.ExecutorService;
import java.util.function.BiConsumer;
import java.util.function.IntFunction;
Expand All @@ -15,6 +16,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.reflect.TypeToken;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
Expand All @@ -29,15 +31,15 @@ public class MaskedSourceDeserializer implements JsonDeserializer< MaskedSource<

private static final Logger LOG = LoggerFactory.getLogger( MethodHandles.lookup().lookupClass() );

private static final String UNDERLYING_SOURCE_CLASS_KEY = "sourceClass";
private static final String UNDERLYING_SOURCE_CLASS_KEY = MaskedSourceSerializer.UNDERLYING_SOURCE_CLASS_KEY;

private static final String UNDERLYING_SOURCE_KEY = "source";
private static final String UNDERLYING_SOURCE_KEY = MaskedSourceSerializer.UNDERLYING_SOURCE_KEY;

private static final String CURRENT_CACHE_DIR_KEY = "cacheDir";
private static final String CURRENT_CACHE_DIR_KEY = MaskedSourceSerializer.CURRENT_CACHE_DIR_KEY;

private static final String PERSIST_CANVAS_CLASS_KEY = "persistCanvasClass";
private static final String PERSIST_CANVAS_CLASS_KEY = MaskedSourceSerializer.PERSIST_CANVAS_CLASS_KEY;

private static final String PERSIST_CANVAS_KEY = "persistCanvas";
private static final String PERSIST_CANVAS_KEY = MaskedSourceSerializer.PERSIST_CANVAS_KEY;

private final Supplier< String > currentProjectDirectory;

Expand Down Expand Up @@ -71,9 +73,20 @@ public MaskedSourceDeserializer( final Supplier< String > currentProjectDirector
final String initialCanvasPath = map.get( CURRENT_CACHE_DIR_KEY ).getAsString();

final DataSource< ?, ? > masked = Masks.mask( source, initialCanvasPath, canvasCacheDirUpdate, mergeCanvasIntoBackground, propagationExecutor );
return masked instanceof MaskedSource< ?, ? >
MaskedSource< ?, ? > returnVal = masked instanceof MaskedSource< ?, ? >
? ( MaskedSource< ?, ? > ) masked
: null;

if ( returnVal != null )
{
Type mapType = new TypeToken< HashMap< Long, long[] >[] >(){}.getType();
long[] blocks = context.deserialize( map.get( MaskedSourceSerializer.DIRTY_BLOCKS_KEY ), long[].class );
HashMap< Long, long []>[] blocksById = context.deserialize( map.get( MaskedSourceSerializer.DIRTY_BLOCKS_BY_ID_KEY ), mapType );
returnVal.affectBlocks( blocks, blocksById );
}

return returnVal;

}
catch ( final ClassNotFoundException e )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,21 @@
public class MaskedSourceSerializer implements JsonSerializer< MaskedSource< ?, ? > >
{

private static final Logger LOG = LoggerFactory.getLogger( MethodHandles.lookup().lookupClass() );
public static final Logger LOG = LoggerFactory.getLogger( MethodHandles.lookup().lookupClass() );

private static final String UNDERLYING_SOURCE_CLASS_KEY = "sourceClass";
public static final String UNDERLYING_SOURCE_CLASS_KEY = "sourceClass";

private static final String UNDERLYING_SOURCE_KEY = "source";
public static final String UNDERLYING_SOURCE_KEY = "source";

private static final String CURRENT_CACHE_DIR_KEY = "cacheDir";
public static final String CURRENT_CACHE_DIR_KEY = "cacheDir";

private static final String PERSIST_CANVAS_CLASS_KEY = "persistCanvasClass";
public static final String PERSIST_CANVAS_CLASS_KEY = "persistCanvasClass";

private static final String PERSIST_CANVAS_KEY = "persistCanvas";
public static final String PERSIST_CANVAS_KEY = "persistCanvas";

public static final String DIRTY_BLOCKS_KEY = "dirtyBlocks";

public static final String DIRTY_BLOCKS_BY_ID_KEY = "dirtyBlocksById";

@Override
public JsonElement serialize( final MaskedSource< ?, ? > src, final Type type, final JsonSerializationContext context )
Expand All @@ -38,6 +42,8 @@ public JsonElement serialize( final MaskedSource< ?, ? > src, final Type type, f
// map.addProperty( CURRENT_CACHE_DIR_KEY, Paths.get( currentProjectDirectory.get() ).relativize( Paths.get( src.currentCanvasDirectory() ) ).toString() );
map.addProperty( PERSIST_CANVAS_CLASS_KEY, src.getPersister().getClass().getName() );
map.add( PERSIST_CANVAS_KEY, context.serialize( src.getPersister(), src.getPersister().getClass() ) );
map.add( DIRTY_BLOCKS_KEY, context.serialize( src.getAffectedBlocks() ) );
map.add( DIRTY_BLOCKS_BY_ID_KEY, context.serialize( src.getAffectedBlocksById() ) );
return map;
}

Expand Down

0 comments on commit cb704c0

Please sign in to comment.