Skip to content

Commit

Permalink
feat: support source state removal by exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
cmhulbert committed Aug 25, 2023
1 parent 74e1fab commit c6a6f47
Showing 1 changed file with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import org.janelia.saalfeldlab.paintera.serialization.sourcestate.SourceStateSerialization;
import org.janelia.saalfeldlab.paintera.state.SourceInfo;
import org.janelia.saalfeldlab.paintera.state.SourceState;
import org.janelia.saalfeldlab.paintera.state.raw.ConnectomicsRawState;
import org.janelia.saalfeldlab.util.n5.N5Helpers;
import org.scijava.plugin.Plugin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -157,9 +157,11 @@ public static void populate(
}

final SourceState<?, ?>[] sourceStates = new SourceState[numStates];
final boolean[] removeState = new boolean[numStates];

for (int i = 0; i < numStates && Arrays.stream(sourceStates).anyMatch(Objects::isNull); ++i) {
for (int k = 0; k < numStates; ++k) {
if (removeState[k]) continue;
if (sourceStates[k] == null) {
final SourceState<?, ?>[] dependencies = IntStream
.of(dependsOn[k].toArray())
Expand All @@ -180,18 +182,36 @@ public static void populate(
@SuppressWarnings("unchecked") final Class<? extends SourceState<?, ?>> clazz = (Class<? extends SourceState<?, ?>>)Class
.forName(state.get(STATE_TYPE_KEY).getAsString());
LOG.debug("Deserializing state={}, class={}", state, clazz);
sourceStates[k] = gson.fromJson(state.get(STATE_KEY), clazz);
try {
sourceStates[k] = gson.fromJson(state.get(STATE_KEY), clazz);
} catch (Exception e) {
//noinspection ConstantValue
if (e instanceof N5Helpers.RemoveSourceException) {
LOG.info("Removing source: {}", stateName);
sourceStates[k] = null;
removeState[k] = true;
continue;
} else {
throw e;
}
}
logSourceForDependencies.accept(k, sourceStates[k]);
}
}
}
}

if (Arrays.stream(sourceStates).anyMatch(Objects::isNull)) {
throw new RuntimeException("Unable to deserialize all source states");
final ArrayList<SourceState<?, ?>> states = new ArrayList<>();
for (int i = 0; i < sourceStates.length; i++) {
final SourceState<?, ?> sourceState = sourceStates[i];
if (sourceState == null && !removeState[i])
throw new RuntimeException("Unable to deserialize all source states");
else if (sourceState != null)
states.add(sourceState);

}

return sourceStates;
return states.toArray(SourceState[]::new);

}

Expand Down

0 comments on commit c6a6f47

Please sign in to comment.