private VersionCorrection getResolvedVersionCorrection() { VersionCorrection versionCorrection = getExecutionOptions().getVersionCorrection(); if (!versionCorrection.containsLatest()) { return versionCorrection; } return versionCorrection.withLatestFixed(Instant.now()); }
@SuppressWarnings("unchecked") @Override public Collection<V> get( final ExternalIdBundle bundle, final VersionCorrection versionCorrection) { ArgumentChecker.notNull(bundle, "bundle"); ArgumentChecker.notNull(versionCorrection, "versionCorrection"); if (versionCorrection.containsLatest()) { Collection<V> results = getUnderlying().get(bundle, versionCorrection); cacheItems(results); return results; } final Pair<ExternalIdBundle, VersionCorrection> key = Pairs.of(bundle, versionCorrection); final Element e = _eidToUidCache.get(key); if (e != null) { if (e.getObjectValue() instanceof Collection) { final Collection<UniqueId> identifiers = (Collection<UniqueId>) e.getObjectValue(); if (identifiers.isEmpty()) { return Collections.emptySet(); } else { return get(identifiers).values(); } } } final Collection<V> result = getUnderlying().get(bundle, versionCorrection); if (result.isEmpty()) { cacheIdentifiers(Collections.<UniqueId>emptyList(), key); } else { final List<UniqueId> uids = new ArrayList<UniqueId>(result.size()); for (final V item : result) { uids.add(item.getUniqueId()); } Collections.sort(uids); cacheIdentifiers(uids, key); cacheItems(result); } return result; }
@Override @SuppressWarnings("unchecked") public Map<ExternalIdBundle, V> getSingle( final Collection<ExternalIdBundle> bundles, final VersionCorrection versionCorrection) { ArgumentChecker.notNull(bundles, "bundles"); ArgumentChecker.notNull(versionCorrection, "versionCorrection"); if (versionCorrection.containsLatest()) { return getUnderlying().getSingle(bundles, versionCorrection); } final Map<ExternalIdBundle, V> results = Maps.newHashMapWithExpectedSize(bundles.size()); final Collection<ExternalIdBundle> misses = new ArrayList<ExternalIdBundle>(bundles.size()); final Map<ExternalIdBundle, Collection<UniqueId>> hits = Maps.newHashMapWithExpectedSize(bundles.size()); final Set<UniqueId> lookup = Sets.newHashSetWithExpectedSize(bundles.size()); for (ExternalIdBundle bundle : bundles) { final Pair<ExternalIdBundle, VersionCorrection> key = Pairs.of(bundle, versionCorrection); final Element e = _eidToUidCache.get(key); if (e != null) { if (e.getObjectValue() instanceof List) { final List<UniqueId> identifiers = (List<UniqueId>) e.getObjectValue(); lookup.addAll(identifiers); hits.put(bundle, identifiers); continue; } else if (e.getObjectValue() instanceof UniqueId) { final UniqueId identifier = (UniqueId) e.getObjectValue(); lookup.add(identifier); hits.put(bundle, Collections.singleton(identifier)); continue; } } misses.add(bundle); } if (!lookup.isEmpty()) { final Map<UniqueId, V> underlying = getUnderlying().get(lookup); for (Map.Entry<ExternalIdBundle, Collection<UniqueId>> hit : hits.entrySet()) { final ExternalIdBundle bundle = hit.getKey(); for (UniqueId uid : hit.getValue()) { final V result = underlying.get(uid); if (result != null) { results.put(bundle, result); break; } } } } if (!misses.isEmpty()) { final Map<ExternalIdBundle, ? extends V> underlying = getUnderlying().getSingle(misses, versionCorrection); for (ExternalIdBundle miss : misses) { final Pair<ExternalIdBundle, VersionCorrection> key = Pairs.of(miss, versionCorrection); final V result = underlying.get(miss); if (result == null) { cacheIdentifiers(Collections.<UniqueId>emptyList(), key); } else { cacheIdentifiers(result.getUniqueId(), key); cacheItem(result); results.put(miss, result); } } } return results; }
@SuppressWarnings("unchecked") @Override public Map<ExternalIdBundle, Collection<V>> getAll( final Collection<ExternalIdBundle> bundles, VersionCorrection versionCorrection) { ArgumentChecker.notNull(bundles, "bundles"); ArgumentChecker.notNull(versionCorrection, "versionCorrection"); if (versionCorrection.containsLatest()) { return getUnderlying().getAll(bundles, versionCorrection); } final Map<ExternalIdBundle, Collection<V>> results = Maps.newHashMapWithExpectedSize(bundles.size()); final Collection<ExternalIdBundle> misses = new ArrayList<ExternalIdBundle>(bundles.size()); final Map<ExternalIdBundle, Collection<UniqueId>> lookupBundles = Maps.newHashMapWithExpectedSize(bundles.size()); final Set<UniqueId> lookupIds = Sets.newHashSetWithExpectedSize(bundles.size()); for (ExternalIdBundle bundle : bundles) { final Pair<ExternalIdBundle, VersionCorrection> key = Pairs.of(bundle, versionCorrection); final Element e = _eidToUidCache.get(key); if (e != null) { if (e.getObjectValue() instanceof Collection) { final Collection<UniqueId> identifiers = (Collection<UniqueId>) e.getObjectValue(); if (identifiers.isEmpty()) { results.put(bundle, Collections.<V>emptySet()); } else { lookupBundles.put(bundle, identifiers); lookupIds.addAll(identifiers); } continue; } } misses.add(bundle); } if (!lookupIds.isEmpty()) { final Map<UniqueId, V> underlying = get(lookupIds); for (Map.Entry<ExternalIdBundle, Collection<UniqueId>> lookupBundle : lookupBundles.entrySet()) { final ArrayList<V> resultCollection = new ArrayList<V>(lookupBundle.getValue().size()); for (UniqueId uid : lookupBundle.getValue()) { final V resultValue = underlying.get(uid); if (resultValue != null) { resultCollection.add(resultValue); } } resultCollection.trimToSize(); results.put(lookupBundle.getKey(), resultCollection); } } if (!misses.isEmpty()) { final Map<ExternalIdBundle, Collection<V>> underlying = getUnderlying().getAll(misses, versionCorrection); for (ExternalIdBundle miss : misses) { final Pair<ExternalIdBundle, VersionCorrection> key = Pairs.of(miss, versionCorrection); final Collection<V> result = underlying.get(miss); if ((result == null) || result.isEmpty()) { cacheIdentifiers(Collections.<UniqueId>emptyList(), key); } else { final List<UniqueId> uids = new ArrayList<>(result.size()); for (final V item : result) { uids.add(item.getUniqueId()); } Collections.sort(uids); cacheIdentifiers(uids, key); cacheItems(result); results.put(miss, result); } } } return results; }