Skip to content

Commit 34192c7

Browse files
bogovicjcmhulbert
authored andcommitted
feat: add N5Writer.deleteBlock
* that deletes blocks or chunks
1 parent d03dc19 commit 34192c7

4 files changed

Lines changed: 68 additions & 5 deletions

File tree

src/main/java/org/janelia/saalfeldlab/n5/GsonKeyValueN5Writer.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,16 @@ default boolean remove(final String path) throws N5Exception {
319319
return true;
320320
}
321321

322+
@Override
323+
default boolean deleteBlock(
324+
final String path,
325+
final DatasetAttributes datasetAttributes,
326+
final long... gridPosition) throws N5Exception {
327+
328+
final PositionValueAccess posKva = PositionValueAccess.fromKva(getKeyValueAccess(), getURI(), N5URI.normalizeGroupPath(path), datasetAttributes);
329+
return posKva.remove(gridPosition);
330+
}
331+
322332
@Override
323333
default boolean deleteChunk(
324334
final String path,

src/main/java/org/janelia/saalfeldlab/n5/N5Writer.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,37 @@ default <T> void writeRegion(
384384
});
385385
}
386386
}
387+
388+
/**
389+
* Deletes the block at {@code gridPosition}.
390+
*
391+
* @param datasetPath dataset path
392+
* @param gridPosition position of block to be deleted
393+
* @throws N5Exception if the block exists but could not be deleted
394+
*
395+
* @return {@code true} if the block at {@code gridPosition} existed and was deleted.
396+
*/
397+
default boolean deleteBlock(
398+
final String datasetPath,
399+
final long... gridPosition) throws N5Exception {
400+
final DatasetAttributes datasetAttributes = getDatasetAttributes(datasetPath);
401+
return deleteBlock(datasetPath, datasetAttributes, gridPosition);
402+
}
403+
404+
/**
405+
* Deletes the block at {@code gridPosition}.
406+
*
407+
* @param datasetPath the dataset path
408+
* @param datasetAttributes the dataset attributes
409+
* @param gridPosition position of block to be deleted
410+
* @throws N5Exception if the block exists but could not be deleted
411+
*
412+
* @return {@code true} if the block at {@code gridPosition} existed and was deleted.
413+
*/
414+
boolean deleteBlock(
415+
String datasetPath,
416+
DatasetAttributes datasetAttributes,
417+
long... gridPosition) throws N5Exception;
387418

388419
/**
389420
* Deletes the chunk at {@code gridPosition}.

src/test/java/org/janelia/saalfeldlab/n5/AbstractN5Test.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,9 +1307,16 @@ public void testDelete() {
13071307
assertTrue(readBlock instanceof ByteArrayDataBlock);
13081308
assertArrayEquals(byteBlock, ((ByteArrayDataBlock)readBlock).getData());
13091309

1310-
assertTrue("deleting existing block should return true", n5.deleteChunk(datasetName, position1));
1311-
assertFalse("deleting non-existing block should return false", n5.deleteChunk(datasetName, position1));
1312-
assertFalse("deleting non-existing block should return false", n5.deleteChunk(datasetName, position2));
1310+
assertTrue("deleting existing chunk should return true", n5.deleteChunk(datasetName, position1));
1311+
assertFalse("deleting non-existing chunk should return false", n5.deleteChunk(datasetName, position1));
1312+
assertFalse("deleting non-existing chunk should return false", n5.deleteChunk(datasetName, position2));
1313+
1314+
// for an unsharded dataset, deleteChunk and deleteBlock behave identically
1315+
assertFalse("deleting non-existing block should return false", n5.deleteBlock(datasetName, position1));
1316+
assertFalse("deleting non-existing block should return false", n5.deleteBlock(datasetName, position2));
1317+
1318+
n5.writeChunk(datasetName, attributes, dataBlock);
1319+
assertTrue("deleting existing block should return true", n5.deleteBlock(datasetName, position1));
13131320

13141321
// no block should exist anymore
13151322
assertNull(n5.readChunk(datasetName, attributes, position1));

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858

5959
import static org.junit.Assert.assertArrayEquals;
6060
import static org.junit.Assert.assertEquals;
61+
import static org.junit.Assert.assertFalse;
6162
import static org.junit.Assert.assertNull;
6263
import static org.junit.Assert.assertTrue;
6364

@@ -425,10 +426,10 @@ public void writeReadShardTest() {
425426
final int[] shardSize = new int[] {4,4};
426427
final int shardN = 16;
427428

428-
final int[] blockSize = new int[] {2,2};
429+
final int[] chunkSize = new int[] {2,2};
429430

430431
final String dataset = "writeReadShard";
431-
DatasetAttributes attrs = getTestAttributes(DataType.INT32, new long[]{8, 8}, shardSize, blockSize);
432+
DatasetAttributes attrs = getTestAttributes(DataType.INT32, new long[]{8, 8}, shardSize, chunkSize);
432433

433434
final int[] shardData = range(shardN);
434435
IntArrayDataBlock shard = new IntArrayDataBlock(shardSize, new long[]{0, 0}, shardData);
@@ -475,6 +476,20 @@ public void writeReadShardTest() {
475476
Stream.of( new long[] {0,0}, new long[] {1,0}, new long[] {0,1}).collect(Collectors.toList()));
476477

477478
assertNull(n5.readBlock(dataset, attrs, 0, 0));
479+
480+
481+
// write the shard again
482+
n5.writeBlock(dataset, attrs, shard);
483+
484+
// delete the block
485+
// ensure it returns true because the block exists
486+
assertTrue(n5.deleteBlock(dataset, attrs, shard.getGridPosition()));
487+
488+
// ensure it returns false when the block does not exist
489+
assertFalse(n5.deleteBlock(dataset, attrs, shard.getGridPosition()));
490+
491+
// readBlock must return null for the deleted block
492+
assertNull(n5.readBlock(dataset, attrs, shard.getGridPosition()));
478493
}
479494
}
480495

0 commit comments

Comments
 (0)