Skip to content

Commit

Permalink
feat!: support read-only ID services
Browse files Browse the repository at this point in the history
  • Loading branch information
cmhulbert committed Aug 9, 2024
1 parent a5d1051 commit 849e004
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
package org.janelia.saalfeldlab.paintera.id;

import org.janelia.saalfeldlab.n5.N5Reader;
import org.janelia.saalfeldlab.n5.N5Writer;


import java.util.stream.LongStream;

public class N5IdService implements IdService {

private final N5Writer n5;
private final N5Reader n5;

private final String dataset;

private long next;
private long nextTemp = IdService.FIRST_TEMPORARY_ID;

public N5IdService(final N5Writer n5, final String dataset, final long next) {
public N5IdService(final N5Reader n5, final String dataset, final long next) {

super();
this.n5 = n5;
this.dataset = dataset;
this.next = next;
}

public N5Writer getWriter() {

return n5;
}

public String getDataset() {

return dataset;
Expand Down Expand Up @@ -74,8 +70,9 @@ public synchronized long[] next(final int n) {
}

private void serializeMaxId() {

n5.setAttribute(dataset, "maxId", next);
if (n5 instanceof N5Writer) {
((N5Writer)n5).setAttribute(dataset, "maxId", next);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ public N5FragmentSegmentAssignmentPersister(N5Writer writer, String dataset) {
LOG.debug("Creating {} with writer {} and dataset {}", getClass().getName(), this.writer, this.dataset);
}

public N5Writer getWriter() {

return this.writer;
}

public String getDataset() {

return this.dataset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class N5BackendPainteraDataset<D, T>(

//FIXME Caleb: same as above with idService
override fun createIdService(source: DataSource<D, T>): IdService {
return metadataState.writer?.let {
return (metadataState.writer ?: metadataState.reader)?.let {
N5Helpers.idService(it, dataset)
} ?: IdService.IdServiceNotProvided()
}
Expand Down
9 changes: 6 additions & 3 deletions src/main/kotlin/org/janelia/saalfeldlab/util/n5/N5Helpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -413,12 +413,15 @@ object N5Helpers {
*/
@JvmStatic
@Throws(MaxIDNotSpecified::class, IOException::class)
fun idService(n5: N5Writer, dataset: String?): IdService {
fun idService(n5: N5Reader, dataset: String?): IdService {
LOG.debug { "Requesting id service for $n5:$dataset" }
val maxId = n5.getAttribute(dataset, "maxId", Long::class.java)
LOG.debug { "Found maxId=$maxId" }
if (maxId == null) throw MaxIDNotSpecified("Required attribute `maxId' not specified for dataset `$dataset' in container `$n5'.")
return N5IdService(n5, dataset, maxId)
return when {
maxId == null && n5 is N5Writer -> throw MaxIDNotSpecified("Required attribute `maxId' not specified for dataset `$dataset' in container `$n5'.")
maxId == null -> N5IdService(n5, dataset, 1)
else -> N5IdService(n5, dataset, maxId)
}
}

/**
Expand Down

0 comments on commit 849e004

Please sign in to comment.