Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #153 from mbari-media-management/feature/swingtable
Feature/swingtable
- Loading branch information
Showing
48 changed files
with
1,639 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip | ||
networkTimeout=10000 | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
185 changes: 185 additions & 0 deletions
185
org.mbari.vars.services/src/main/java/org/mbari/vars/services/CachedMediaService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
package org.mbari.vars.services; | ||
|
||
import com.github.benmanes.caffeine.cache.AsyncLoadingCache; | ||
import com.github.benmanes.caffeine.cache.Caffeine; | ||
import org.mbari.vars.services.model.LastUpdate; | ||
import org.mbari.vars.services.model.Media; | ||
|
||
import java.net.URI; | ||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class CachedMediaService implements MediaService { | ||
|
||
private final MediaService mediaService; | ||
private final AsyncLoadingCache<UUID, Optional<Media>> mediaCache; | ||
|
||
public CachedMediaService(MediaService mediaService) { | ||
this.mediaService = mediaService; | ||
mediaCache = Caffeine.newBuilder() | ||
.expireAfterWrite(15, TimeUnit.MINUTES) | ||
.maximumSize(100) | ||
.buildAsync((key, executor) -> mediaService.findByUuid(key).thenApply(Optional::ofNullable)); | ||
} | ||
|
||
public synchronized void clear() { | ||
mediaCache.synchronous().invalidateAll(); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Media> create(Media media) { | ||
return cacheSingle(mediaService.create(media)); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Media> create(String videoSequenceName, String cameraId, String videoName, URI uri, Instant startTimestamp) { | ||
return cacheSingle(mediaService.create(videoSequenceName, cameraId, videoName, uri, startTimestamp)); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Media> update(UUID videoReferenceUuid, Instant startTimestamp, Duration duration) { | ||
return cacheSingle(mediaService.update(videoReferenceUuid, startTimestamp, duration)); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Media> update(Media media) { | ||
return cacheSingle(mediaService.update(media)); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Boolean> delete(UUID videoReferenceUuid) { | ||
return mediaService.delete(videoReferenceUuid) | ||
.thenApply(ok -> { | ||
if (ok) { | ||
mediaCache.synchronous().invalidate(videoReferenceUuid); | ||
} | ||
return ok; | ||
}); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Media> findByUuid(UUID uuid) { | ||
return mediaCache.get(uuid).thenApply(m -> m.orElse(null)); | ||
} | ||
|
||
|
||
@Override | ||
public CompletableFuture<Media> findBySha512(byte[] sha512) { | ||
return cacheSingle(mediaService.findBySha512(sha512)); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Media> findByUri(URI uri) { | ||
return cacheSingle(mediaService.findByUri(uri)); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<List<Media>> findByVideoSequenceName(String videoSequenceName) { | ||
return cacheMany(mediaService.findByVideoSequenceName(videoSequenceName)); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<List<Media>> findByVideoName(String videoName) { | ||
return cacheMany(mediaService.findByVideoName(videoName)); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<List<String>> findAllVideoSequenceNames() { | ||
return mediaService.findAllVideoSequenceNames(); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<List<Media>> findByCameraIdAndTimestamp(String cameraId, Instant timestamp) { | ||
return cacheMany(mediaService.findByCameraIdAndTimestamp(cameraId, timestamp)); | ||
|
||
} | ||
|
||
@Override | ||
public CompletableFuture<List<Media>> findByCameraIdAndDate(String cameraId, Instant startTimestamp, Instant endTimestamp) { | ||
return cacheMany(mediaService.findByCameraIdAndDate(cameraId, startTimestamp, endTimestamp)); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<List<Media>> findByVideoSequenceNameAndTimestamp(String videoSequenceName, Instant timestamp) { | ||
return cacheMany(mediaService.findByVideoSequenceNameAndTimestamp(videoSequenceName, timestamp)); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<List<String>> findAllCameraIds() { | ||
return mediaService.findAllCameraIds(); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<List<URI>> findAllURIs() { | ||
return mediaService.findAllURIs(); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<List<Media>> findConcurrentByVideoReferenceUuid(UUID uuid) { | ||
return cacheMany(mediaService.findConcurrentByVideoReferenceUuid(uuid)); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<List<Media>> findByFilename(String filename) { | ||
return cacheMany(mediaService.findByFilename(filename)); | ||
|
||
} | ||
|
||
@Override | ||
public CompletableFuture<LastUpdate> findLastVideoSequenceUpdate(UUID uuid) { | ||
return mediaService.findLastVideoSequenceUpdate(uuid); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<LastUpdate> findLastVideoUpdate(UUID uuid) { | ||
return mediaService.findLastVideoUpdate(uuid); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<LastUpdate> findLastVideoReferenceUpdate(UUID uuid) { | ||
return mediaService.findLastVideoReferenceUpdate(uuid); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<List<String>> findVideoSequenceNamesByCameraId(String cameraId) { | ||
return mediaService.findVideoSequenceNamesByCameraId(cameraId); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<List<String>> findVideoNamesByVideoSequenceName(String videoSequenceName) { | ||
return mediaService.findVideoNamesByVideoSequenceName(videoSequenceName); | ||
} | ||
|
||
private CompletableFuture<List<Media>> cacheMany(CompletableFuture<List<Media>> future) { | ||
return future.thenApply(xs -> { | ||
if (xs != null) { | ||
var cache = mediaCache.synchronous(); | ||
xs.forEach(m -> cache.put(m.getVideoReferenceUuid(), Optional.of(m))); | ||
} | ||
return xs; | ||
}); | ||
} | ||
|
||
private CompletableFuture<Media> cacheSingle(CompletableFuture<Media> future) { | ||
return future.thenApply(m -> { | ||
if (m != null) { | ||
mediaCache.synchronous().put(m.getVideoReferenceUuid(), Optional.of(m)); | ||
} | ||
return m; | ||
}); | ||
} | ||
|
||
// private CompletableFuture<Media> invalidateSingle(CompletableFuture<Media> future) { | ||
// return future.thenApply(m -> { | ||
// if (m != null) { | ||
// mediaCache.synchronous().invalidate(m.getVideoReferenceUuid()); | ||
// } | ||
// return m; | ||
// }); | ||
// } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
org.mbari.vars.services/src/test/java/org/mbari/vars/services/model/MediaTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.mbari.vars.services.model; | ||
|
||
import org.junit.Test; | ||
import static org.junit.Assert.*; | ||
import org.mbari.vcr4j.VideoIndex; | ||
|
||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.util.UUID; | ||
|
||
public class MediaTest { | ||
|
||
@Test | ||
public void testToMediaElaspsedTime1() { | ||
var timestamp = Instant.parse("2000-01-01T00:00:00Z"); | ||
var elapsedTime = Duration.ofMillis(1000); | ||
var recordedTimestamp = timestamp.plus(elapsedTime); | ||
|
||
var media = new Media(); | ||
media.setVideoReferenceUuid(UUID.randomUUID()); | ||
media.setStartTimestamp(timestamp); | ||
var a0 = new Annotation("Media UUID matches", "brian", new VideoIndex(elapsedTime), media.getVideoReferenceUuid()); | ||
var a1 = new Annotation("Media UUID does not match", "brian", new VideoIndex(Duration.ZERO, recordedTimestamp), UUID.randomUUID()); | ||
|
||
// Same videoReferenceUUID should return annos elapsedTime as is | ||
var opt1 = media.toMediaElapsedTime(a0); | ||
assertTrue(opt1.isPresent()); | ||
assertEquals(elapsedTime, opt1.get()); | ||
|
||
// Different videoReferenceUuid should munge the annos elapsedTime relative to the media using | ||
// the annos recordedTimestamp. Annos elapsedTime = 9, but it'sr ecorded date is 1 seconds from | ||
// the start of the video | ||
var opt2 = media.toMediaElapsedTime(a1); | ||
assertTrue(opt2.isPresent()); | ||
assertEquals(elapsedTime, opt2.get()); | ||
|
||
} | ||
} |
Oops, something went wrong.