Skip to content

Commit

Permalink
Changed metadata cache key
Browse files Browse the repository at this point in the history
  • Loading branch information
Rylern committed Apr 9, 2024
1 parent a8b4e23 commit 61e9322
Showing 1 changed file with 58 additions and 58 deletions.
116 changes: 58 additions & 58 deletions src/main/java/qupath/ext/omero/core/imageserver/OmeroImageServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class OmeroImageServer extends AbstractTileableImageServer implements Pat
private static final Logger logger = LoggerFactory.getLogger(OmeroImageServer.class);
private static final String PIXEL_API_ARGUMENT = "--pixelAPI";
private static final int METADATA_CACHE_SIZE = 50;
private static final Cache<URI, CompletableFuture<Optional<ImageServerMetadata>>> metadataCache = CacheBuilder.newBuilder()
private static final Cache<Long, CompletableFuture<Optional<ImageServerMetadata>>> metadataCache = CacheBuilder.newBuilder()
.maximumSize(METADATA_CACHE_SIZE)
.build();
private final URI uri;
Expand Down Expand Up @@ -87,6 +87,8 @@ static Optional<OmeroImageServer> create(URI uri, WebClient client, String... ar
}

if (originalMetadata.isPresent()) {
System.err.println(originalMetadata.get().getName());

PixelAPI pixelAPI;
var pixelAPIFromArgs = getPixelAPIFromArgs(client, args);

Expand Down Expand Up @@ -278,65 +280,63 @@ public long getId() {
* @return a CompletableFuture with the metadata, or an empty Optional if the request failed
*/
public static CompletableFuture<Optional<ImageServerMetadata>> getOriginalMetadata(URI uri, WebClient client) {
try {
CompletableFuture<Optional<ImageServerMetadata>> request = metadataCache.get(
uri,
() -> {
OptionalLong id = WebUtilities.parseEntityId(uri);

if (id.isPresent()) {
return client.getApisHandler().getImageMetadata(id.getAsLong()).thenApply(imageMetadataResponse -> {
if (imageMetadataResponse.isPresent()) {
ImageServerMetadata.Builder builder = new ImageServerMetadata.Builder(
OmeroImageServer.class,
uri.toString(),
imageMetadataResponse.get().getSizeX(),
imageMetadataResponse.get().getSizeY()
)
.name(imageMetadataResponse.get().getImageName())
.sizeT(imageMetadataResponse.get().getSizeT())
.sizeZ(imageMetadataResponse.get().getSizeZ())
.preferredTileSize(imageMetadataResponse.get().getTileSizeX(), imageMetadataResponse.get().getTileSizeY())
.levels(imageMetadataResponse.get().getLevels())
.pixelType(imageMetadataResponse.get().getPixelType())
.channels(imageMetadataResponse.get().getChannels())
.rgb(imageMetadataResponse.get().isRGB());

if (imageMetadataResponse.get().getMagnification().isPresent()) {
builder.magnification(imageMetadataResponse.get().getMagnification().get());
}

if (imageMetadataResponse.get().getPixelWidthMicrons().isPresent() && imageMetadataResponse.get().getPixelHeightMicrons().isPresent()) {
builder.pixelSizeMicrons(
imageMetadataResponse.get().getPixelWidthMicrons().get(),
imageMetadataResponse.get().getPixelHeightMicrons().get()
);
}

if (imageMetadataResponse.get().getZSpacingMicrons().isPresent() && imageMetadataResponse.get().getZSpacingMicrons().get() > 0) {
builder.zSpacingMicrons(imageMetadataResponse.get().getZSpacingMicrons().get());
}

return Optional.of(builder.build());
} else {
return Optional.empty();
OptionalLong id = WebUtilities.parseEntityId(uri);

if (id.isPresent()) {
try {
CompletableFuture<Optional<ImageServerMetadata>> request = metadataCache.get(
id.getAsLong(),
() -> client.getApisHandler().getImageMetadata(id.getAsLong()).thenApply(imageMetadataResponse -> {
if (imageMetadataResponse.isPresent()) {
ImageServerMetadata.Builder builder = new ImageServerMetadata.Builder(
OmeroImageServer.class,
uri.toString(),
imageMetadataResponse.get().getSizeX(),
imageMetadataResponse.get().getSizeY()
)
.name(imageMetadataResponse.get().getImageName())
.sizeT(imageMetadataResponse.get().getSizeT())
.sizeZ(imageMetadataResponse.get().getSizeZ())
.preferredTileSize(imageMetadataResponse.get().getTileSizeX(), imageMetadataResponse.get().getTileSizeY())
.levels(imageMetadataResponse.get().getLevels())
.pixelType(imageMetadataResponse.get().getPixelType())
.channels(imageMetadataResponse.get().getChannels())
.rgb(imageMetadataResponse.get().isRGB());

if (imageMetadataResponse.get().getMagnification().isPresent()) {
builder.magnification(imageMetadataResponse.get().getMagnification().get());
}
});
} else {
logger.warn("Could not get image ID from " + uri);
return CompletableFuture.completedFuture(Optional.empty());
}
}
);

request.thenAccept(response -> {
if (response.isEmpty()) {
metadataCache.invalidate(uri);
}
});
return request;
} catch (ExecutionException e) {
logger.error("Error when retrieving metadata", e);
if (imageMetadataResponse.get().getPixelWidthMicrons().isPresent() && imageMetadataResponse.get().getPixelHeightMicrons().isPresent()) {
builder.pixelSizeMicrons(
imageMetadataResponse.get().getPixelWidthMicrons().get(),
imageMetadataResponse.get().getPixelHeightMicrons().get()
);
}

if (imageMetadataResponse.get().getZSpacingMicrons().isPresent() && imageMetadataResponse.get().getZSpacingMicrons().get() > 0) {
builder.zSpacingMicrons(imageMetadataResponse.get().getZSpacingMicrons().get());
}

return Optional.of(builder.build());
} else {
return Optional.empty();
}
})
);

request.thenAccept(response -> {
if (response.isEmpty()) {
metadataCache.invalidate(id.getAsLong());
}
});
return request;
} catch (ExecutionException e) {
logger.error("Error when retrieving metadata", e);
return CompletableFuture.completedFuture(Optional.empty());
}
} else {
logger.warn("Could not get image ID from " + uri);
return CompletableFuture.completedFuture(Optional.empty());
}
}
Expand Down

0 comments on commit 61e9322

Please sign in to comment.