Skip to content

Commit 730d9f6

Browse files
committed
test/fix: N5Writer in ShardTest tracks, does not serialize
* refactor ShardedN5Writer to TrackingN5Writer * a special N5Writer class is not needed to * rather, particular DatasetAttribute instances can be used to test sharding
1 parent 3d33e99 commit 730d9f6

1 file changed

Lines changed: 13 additions & 36 deletions

File tree

src/test/java/org/janelia/saalfeldlab/n5/shard/ShardTest.java

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,9 @@
3939
import org.janelia.saalfeldlab.n5.N5FSTest;
4040
import org.janelia.saalfeldlab.n5.N5KeyValueWriter;
4141
import org.janelia.saalfeldlab.n5.N5Writer;
42-
import org.janelia.saalfeldlab.n5.NameConfigAdapter;
4342
import org.janelia.saalfeldlab.n5.RawCompression;
4443
import org.janelia.saalfeldlab.n5.N5Exception.N5NoSuchKeyException;
4544
import org.janelia.saalfeldlab.n5.GsonKeyValueN5Writer;
46-
import org.janelia.saalfeldlab.n5.codec.CodecInfo;
4745
import org.janelia.saalfeldlab.n5.codec.DataCodecInfo;
4846
import org.janelia.saalfeldlab.n5.codec.N5BlockCodecInfo;
4947
import org.janelia.saalfeldlab.n5.codec.RawBlockCodecInfo;
@@ -55,9 +53,10 @@
5553
import org.junit.runner.RunWith;
5654
import org.junit.runners.Parameterized;
5755

58-
import com.google.gson.Gson;
5956
import com.google.gson.GsonBuilder;
6057

58+
import static org.junit.Assert.assertTrue;
59+
6160
import java.io.File;
6261
import java.io.IOException;
6362
import java.io.UncheckedIOException;
@@ -88,15 +87,15 @@ public class ShardTest {
8887
@Override public N5Writer createTempN5Writer() {
8988

9089
if (LOCAL_DEBUG) {
91-
final N5Writer writer = new ShardedN5Writer("src/test/resources/test.n5");
90+
final N5Writer writer = new TrackingN5Writer("src/test/resources/test.n5");
9291
writer.remove(""); // Clear old when starting new test
9392
return writer;
9493
}
9594

9695
final String basePath = new File(tempN5PathName()).toURI().normalize().getPath();
9796
try {
9897
String uri = new URI("file", null, basePath, null).toString();
99-
return new ShardedN5Writer(uri);
98+
return new TrackingN5Writer(uri);
10099
} catch (URISyntaxException e) {
101100
e.printStackTrace();
102101
}
@@ -117,10 +116,6 @@ private String tempN5PathName() {
117116
}
118117
};
119118

120-
public static GsonBuilder gsonBuilder() {
121-
return new GsonBuilder();
122-
}
123-
124119
@Parameterized.Parameters(name = "IndexLocation({0}), Index ByteOrder({1})")
125120
public static Collection<Object[]> data() {
126121

@@ -313,15 +308,17 @@ public void writeShardDataSizeTest() {
313308

314309
int numBlocksPerShard = 16;
315310
final int n5HeaderSizeBytes = 12; // 2 + 2 + 4*2
316-
final DatasetAttributes datasetAttributes = getTestAttributes(
311+
final DatasetAttributes attrs = getTestAttributes(
317312
new long[]{24, 24},
318313
new int[]{8, 8},
319314
new int[]{2, 2}
320315
);
321316

322317
final String dataset = "writeBlocksShardSize";
323318
writer.remove(dataset);
324-
writer.createDataset(dataset, datasetAttributes);
319+
final DatasetAttributes datasetAttributes = writer.createDataset(dataset, attrs);
320+
assertTrue(datasetAttributes.isSharded());
321+
325322
final KeyValueAccess kva = ((N5KeyValueWriter)writer).getKeyValueAccess();
326323

327324
final int[] blockSize = datasetAttributes.getBlockSize();
@@ -438,7 +435,7 @@ public void writeReadBlockTest() {
438435
*/
439436
public void numReadsTest() {
440437

441-
final ShardedN5Writer writer = (ShardedN5Writer)tempN5Factory.createTempN5Writer();
438+
final TrackingN5Writer writer = (TrackingN5Writer)tempN5Factory.createTempN5Writer();
442439

443440
final DatasetAttributes datasetAttributes = getTestAttributes(
444441
new long[]{24, 24},
@@ -492,43 +489,23 @@ public void numReadsTest() {
492489
}
493490

494491
/**
495-
* An N5Writer that serializing the sharding codecs, enabling testing of
496-
* shard functionality, despite the fact that the N5 format does not support
497-
* sharding.
492+
* An N5Writer that tracks the number of materialize calls performed by
493+
* its underlying key value access.
498494
*/
499-
public static class ShardedN5Writer extends N5KeyValueWriter {
500-
501-
Gson gson;
502-
503-
public ShardedN5Writer(String basePath) {
495+
public static class TrackingN5Writer extends N5KeyValueWriter {
496+
public TrackingN5Writer(String basePath) {
504497

505498
super( new TrackingFileSystemKeyValueAccess(FileSystems.getDefault()),
506499
basePath, new GsonBuilder(), false);
507500
}
508501

509-
public ShardedN5Writer(String basePath, GsonBuilder gsonBuilder) {
510-
511-
this(basePath);
512-
gsonBuilder.registerTypeAdapter(DataType.class, new DataType.JsonAdapter());
513-
gsonBuilder.registerTypeHierarchyAdapter(CodecInfo.class, NameConfigAdapter.getJsonAdapter(CodecInfo.class));
514-
gsonBuilder.registerTypeHierarchyAdapter(ByteOrder.class, RawBlockCodecInfo.byteOrderAdapter);
515-
gsonBuilder.disableHtmlEscaping();
516-
gson = gsonBuilder.create();
517-
}
518-
519502
public void resetNumMaterializeCalls() {
520503
((TrackingFileSystemKeyValueAccess)super.getKeyValueAccess()).numMaterializeCalls = 0;
521504
}
522505

523506
public int getNumMaterializeCalls() {
524507
return ((TrackingFileSystemKeyValueAccess)super.getKeyValueAccess()).numMaterializeCalls;
525508
}
526-
527-
@Override
528-
public Gson getGson() {
529-
// the super constructor needs the gson instance, unfortunately
530-
return gson == null ? super.gson : gson;
531-
}
532509
}
533510

534511
private static class TrackingFileSystemKeyValueAccess extends FileSystemKeyValueAccess {

0 commit comments

Comments
 (0)