Skip to content

Commit

Permalink
feat: add ShardIndex.isEmpty
Browse files Browse the repository at this point in the history
* use it to return early for getBlocks
  • Loading branch information
bogovicj committed Jan 21, 2025
1 parent 9224215 commit a74d343
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/main/java/org/janelia/saalfeldlab/n5/shard/ShardIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.util.Arrays;
import java.util.stream.IntStream;

public class ShardIndex extends LongArrayDataBlock {

Expand Down Expand Up @@ -66,6 +67,16 @@ public boolean exists(int blockNum) {
data[blockNum * 2 + 1] != EMPTY_INDEX_NBYTES;
}

public int getNumBlocks() {

return Arrays.stream(getSize()).reduce(1, (x, y) -> x * y);
}

public boolean isEmpty() {

return !IntStream.range(0, getNumBlocks()).anyMatch(i -> exists(i));
}

public IndexLocation getLocation() {

return location;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ public List<DataBlock<T>> getBlocks() {
public List<DataBlock<T>> getBlocks(final int[] blockIndexes) {

// will not contain nulls

final ShardIndex index = getIndex();
// TODO if the index is completely empty, can return right away

final ArrayList<DataBlock<T>> blocks = new ArrayList<>();

if (index.isEmpty())
return blocks;

// sort index offsets
// and keep track of relevant positions
final long[] indexData = index.getData();
Expand Down

0 comments on commit a74d343

Please sign in to comment.