package org.mbari.m3.vars.annotation.services;

/**
 * @author Brian Schlining
 * @since 2019-06-04T16:03:00
 */
public class CachedReferenceNumberService {
    //private final AnnotationService annotationService;

}

@@ -41,16 +41,468 @@
 vices;%0A%0A
+import com.github.benmanes.caffeine.cache.AsyncLoadingCache;%0Aimport com.github.benmanes.caffeine.cache.Caffeine;%0Aimport org.mbari.m3.vars.annotation.model.Association;%0Aimport org.mbari.m3.vars.annotation.model.Media;%0Aimport org.mbari.m3.vars.annotation.util.AsyncUtils;%0A%0Aimport java.time.Duration;%0Aimport java.util.*;%0Aimport java.util.concurrent.CompletableFuture;%0Aimport java.util.concurrent.CopyOnWriteArrayList;%0Aimport java.util.stream.Collectors;%0A%0A
 /**%0A * @
@@ -606,64 +606,2680 @@
 e %7B%0A
+%0A
     
-//private final AnnotationService annotationService;%0A
+private static class NewNumberKey %7B%0A        final Media media;%0A    %7D%0A%0A    private final AnnotationService annotationService;%0A    private final MediaService mediaService;%0A    private final AsyncLoadingCache%3CMedia, List%3CMedia%3E%3E loadedMediaCache;%0A    private final AsyncLoadingCache%3CString, List%3CAssociation%3E%3E oldNumberCache;%0A    private final List%3CAssociation%3E oldNumbers = new CopyOnWriteArrayList%3C%3E();%0A%0A    public CachedReferenceNumberService(AnnotationService annotationService,%0A                                        MediaService mediaService) %7B%0A        this.annotationService = annotationService;%0A        this.mediaService = mediaService;%0A%0A        oldNumberCache = Caffeine.newBuilder()%0A                .expireAfterAccess(Duration.ofMinutes(60));%0A%0A        loadedMediaCache = Caffeine.newBuilder()%0A                .expireAfterWrite(Duration.ofMinutes(120))%0A                .buildAsync((key, executor) -%3E mediaService.findByVideoSequenceName(key.getVideoSequenceName());%0A    %7D%0A%0A    public synchronized void clear() %7B%0A%0A    %7D%0A%0A    public CompletableFuture%3CList%3CMedia%3E%3E findRelatedMedia(Media media) %7B%0A        return loadedMediaCache.get(media);%0A    %7D%0A%0A    private CompletableFuture%3CList%3CAssociation%3E%3E loadNewNumbers(Media media, String associationKey) %7B%0A%0A%0A%0A        return mediaService.findByVideoSequenceName(media.getVideoSequenceName())%0A                .thenCompose(medias -%3E AsyncUtils.collectAll(medias, m -%3E%0A                        annotationService.findByVideoReferenceAndLinkName(m.getVideoReferenceUuid(), associationKey)))%0A                .thenApply(associationLists -%3E%0A                     associationLists.stream()%0A                            .flatMap(List::stream)%0A                            .collect(Collectors.toList()));%0A%0A    %7D%0A%0A    private CompletableFuture%3CMap%3CString, String%3E%3E loadOldNumbers(Media media,%0A                                                                  String associationKey,%0A                                                                  String concept) %7B%0A        mediaService.findByVideoSequenceName(media.getVideoSequenceName())%0A                .thenCompose(medias -%3E %7B%0A                    medias.forEach(m -%3E %7B%0A                        annotationService.findByVideoReferenceAndLinkNameAndConcept(m.getVideoReferenceUuid(), associationKey, concept)%0A                                .thenAccept(associations -%3E %7B%0A                                    Map%3CString, List%3CAssociation%3E%3E map = new HashMap%3C%3E();%0A                                    map.put(associationKey, )%0A%0A                                    oldNumberCache.synchronous().put(media, )%0A                                %7D)%0A                    %7D);%0A                %7D);%0A    %7D
 %0A%7D%0A

check point. NON COMPILING