|
| 1 | +package se.liu.ida.hefquin.engine.federation.access.impl; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.Date; |
| 5 | +import java.util.concurrent.CompletableFuture; |
| 6 | +import java.util.concurrent.ExecutorService; |
| 7 | + |
| 8 | +import se.liu.ida.hefquin.base.datastructures.impl.cache.CacheEntry; |
| 9 | +import se.liu.ida.hefquin.base.datastructures.impl.cache.CacheEntryFactory; |
| 10 | +import se.liu.ida.hefquin.base.datastructures.impl.cache.CacheInvalidationPolicy; |
| 11 | +import se.liu.ida.hefquin.base.datastructures.impl.cache.CacheInvalidationPolicyTimeToLive; |
| 12 | +import se.liu.ida.hefquin.base.datastructures.impl.cache.CachePolicies; |
| 13 | +import se.liu.ida.hefquin.base.datastructures.impl.cache.CacheReplacementPolicy; |
| 14 | +import se.liu.ida.hefquin.base.datastructures.impl.cache.CacheReplacementPolicyFactory; |
| 15 | +import se.liu.ida.hefquin.base.datastructures.impl.cache.CacheReplacementPolicyLRU; |
| 16 | +import se.liu.ida.hefquin.engine.federation.BRTPFServer; |
| 17 | +import se.liu.ida.hefquin.engine.federation.SPARQLEndpoint; |
| 18 | +import se.liu.ida.hefquin.engine.federation.TPFServer; |
| 19 | +import se.liu.ida.hefquin.engine.federation.access.BRTPFRequest; |
| 20 | +import se.liu.ida.hefquin.engine.federation.access.CardinalityResponse; |
| 21 | +import se.liu.ida.hefquin.engine.federation.access.DataRetrievalResponse; |
| 22 | +import se.liu.ida.hefquin.engine.federation.access.FederationAccessException; |
| 23 | +import se.liu.ida.hefquin.engine.federation.access.FederationAccessManager; |
| 24 | +import se.liu.ida.hefquin.engine.federation.access.SPARQLRequest; |
| 25 | +import se.liu.ida.hefquin.engine.federation.access.TPFRequest; |
| 26 | +import se.liu.ida.hefquin.engine.federation.access.impl.cache.CardinalityCacheEntry; |
| 27 | +import se.liu.ida.hefquin.engine.federation.access.impl.cache.CardinalityCacheEntryFactory; |
| 28 | +import se.liu.ida.hefquin.engine.federation.access.impl.cache.CardinalityCacheKey; |
| 29 | +import se.liu.ida.hefquin.engine.federation.access.impl.cache.ChronicleMapCardinalityCache; |
| 30 | +import se.liu.ida.hefquin.engine.federation.access.impl.response.CachedCardinalityResponseImpl; |
| 31 | + |
| 32 | +/** |
| 33 | + * A FederationAccessManager implementation that incorporates persistent disk |
| 34 | + * caching of cardinality requests. |
| 35 | + */ |
| 36 | +public class FederationAccessManagerWithChronicleMapCache extends FederationAccessManagerWithCache |
| 37 | +{ |
| 38 | + protected final ChronicleMapCardinalityCache cardinalityCache; |
| 39 | + protected final static int defaultCacheCapacity = 1000; |
| 40 | + |
| 41 | + public FederationAccessManagerWithChronicleMapCache( final FederationAccessManager fedAccMan, |
| 42 | + final int cacheCapacity, |
| 43 | + final CachePolicies<Key, CompletableFuture<? extends DataRetrievalResponse>, ? extends CacheEntry<CompletableFuture<? extends DataRetrievalResponse>>> cachePolicies, |
| 44 | + final CachePolicies<CardinalityCacheKey, Integer, CardinalityCacheEntry> cardinalityCachePolicies ) |
| 45 | + throws IOException |
| 46 | + { |
| 47 | + super( fedAccMan, cacheCapacity, cachePolicies ); |
| 48 | + cardinalityCache = new ChronicleMapCardinalityCache( cardinalityCachePolicies, cacheCapacity ); |
| 49 | + } |
| 50 | + |
| 51 | + public FederationAccessManagerWithChronicleMapCache( final FederationAccessManager fedAccMan, |
| 52 | + final int cacheCapacity, |
| 53 | + final long timeToLive ) |
| 54 | + throws IOException |
| 55 | + { |
| 56 | + this( fedAccMan, |
| 57 | + cacheCapacity, |
| 58 | + new MyDefaultCachePolicies(), |
| 59 | + new MyDefaultCardinalityCachePolicies( timeToLive ) ); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Creates a {@link FederationAccessManagerWithChronicleMapCache} with the default configuration. |
| 64 | + */ |
| 65 | + public FederationAccessManagerWithChronicleMapCache( final ExecutorService execService ) throws IOException |
| 66 | + { |
| 67 | + this( new AsyncFederationAccessManagerImpl( execService ), |
| 68 | + defaultCacheCapacity, |
| 69 | + new MyDefaultCachePolicies(), |
| 70 | + new MyDefaultCardinalityCachePolicies() ); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public CompletableFuture<CardinalityResponse> issueCardinalityRequest( final SPARQLRequest req, |
| 75 | + final SPARQLEndpoint fm ) |
| 76 | + throws FederationAccessException |
| 77 | + { |
| 78 | + final CardinalityCacheKey key = new CardinalityCacheKey( req, fm ); |
| 79 | + final Date requestStartTime = new Date(); |
| 80 | + final CardinalityCacheEntry cachedEntry = cardinalityCache.get( key ); |
| 81 | + final Date requestEndTime = new Date(); |
| 82 | + if ( cachedEntry != null ) { |
| 83 | + cacheHitsSPARQLCardinality++; |
| 84 | + final CardinalityResponse cr = new CachedCardinalityResponseImpl( fm, |
| 85 | + req, |
| 86 | + cachedEntry.getObject(), |
| 87 | + requestStartTime, |
| 88 | + requestEndTime ); |
| 89 | + return CompletableFuture.completedFuture( cr ); |
| 90 | + } |
| 91 | + |
| 92 | + final CompletableFuture<CardinalityResponse> newResponse = fedAccMan.issueCardinalityRequest( req, fm ); |
| 93 | + newResponse.thenAccept( value -> { |
| 94 | + cardinalityCache.put( key, value.getCardinality() ); |
| 95 | + } ); |
| 96 | + return newResponse; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public CompletableFuture<CardinalityResponse> issueCardinalityRequest( final TPFRequest req, |
| 101 | + final TPFServer fm ) |
| 102 | + throws FederationAccessException |
| 103 | + { |
| 104 | + final CardinalityCacheKey key = new CardinalityCacheKey( req, fm ); |
| 105 | + final Date requestStartTime = new Date(); |
| 106 | + final CardinalityCacheEntry cachedEntry = cardinalityCache.get( key ); |
| 107 | + final Date requestEndTime = new Date(); |
| 108 | + if ( cachedEntry != null ) { |
| 109 | + cacheHitsTPFCardinality++; |
| 110 | + final CardinalityResponse cr = new CachedCardinalityResponseImpl( fm, |
| 111 | + req, |
| 112 | + cachedEntry.getObject(), |
| 113 | + requestStartTime, |
| 114 | + requestEndTime ); |
| 115 | + return CompletableFuture.completedFuture( cr ); |
| 116 | + } |
| 117 | + |
| 118 | + final CompletableFuture<CardinalityResponse> newResponse = fedAccMan.issueCardinalityRequest( req, fm ); |
| 119 | + newResponse.thenAccept( value -> { |
| 120 | + cardinalityCache.put( key, value.getCardinality() ); |
| 121 | + } ); |
| 122 | + return newResponse; |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public CompletableFuture<CardinalityResponse> issueCardinalityRequest( final TPFRequest req, |
| 127 | + final BRTPFServer fm ) |
| 128 | + throws FederationAccessException |
| 129 | + { |
| 130 | + final CardinalityCacheKey key = new CardinalityCacheKey( req, fm ); |
| 131 | + final Date requestStartTime = new Date(); |
| 132 | + final CardinalityCacheEntry cachedEntry = cardinalityCache.get( key ); |
| 133 | + final Date requestEndTime = new Date(); |
| 134 | + if ( cachedEntry != null ) { |
| 135 | + cacheHitsTPFCardinality++; |
| 136 | + final CardinalityResponse cr = new CachedCardinalityResponseImpl( fm, |
| 137 | + req, |
| 138 | + cachedEntry.getObject(), |
| 139 | + requestStartTime, |
| 140 | + requestEndTime ); |
| 141 | + return CompletableFuture.completedFuture( cr ); |
| 142 | + } |
| 143 | + |
| 144 | + final CompletableFuture<CardinalityResponse> newResponse = fedAccMan.issueCardinalityRequest( req, fm ); |
| 145 | + newResponse.thenAccept( value -> { |
| 146 | + cardinalityCache.put( key, value.getCardinality() ); |
| 147 | + } ); |
| 148 | + return newResponse; |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public CompletableFuture<CardinalityResponse> issueCardinalityRequest( final BRTPFRequest req, |
| 153 | + final BRTPFServer fm ) |
| 154 | + throws FederationAccessException |
| 155 | + { |
| 156 | + final CardinalityCacheKey key = new CardinalityCacheKey( req, fm ); |
| 157 | + final Date requestStartTime = new Date(); |
| 158 | + final CardinalityCacheEntry cachedEntry = cardinalityCache.get( key ); |
| 159 | + final Date requestEndTime = new Date(); |
| 160 | + if ( cachedEntry != null ) { |
| 161 | + cacheHitsTPFCardinality++; |
| 162 | + final CardinalityResponse cr = new CachedCardinalityResponseImpl( fm, |
| 163 | + req, |
| 164 | + cachedEntry.getObject(), |
| 165 | + requestStartTime, |
| 166 | + requestEndTime ); |
| 167 | + return CompletableFuture.completedFuture( cr ); |
| 168 | + } |
| 169 | + |
| 170 | + final CompletableFuture<CardinalityResponse> newResponse = fedAccMan.issueCardinalityRequest( req, fm ); |
| 171 | + newResponse.thenAccept( value -> { |
| 172 | + cardinalityCache.put( key, value.getCardinality() ); |
| 173 | + } ); |
| 174 | + return newResponse; |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Clears the persisted cardinality cache map. |
| 179 | + */ |
| 180 | + public void clearCardinalityCache() { |
| 181 | + cardinalityCache.clear(); |
| 182 | + } |
| 183 | + |
| 184 | + protected static class MyDefaultCardinalityCachePolicies implements CachePolicies<CardinalityCacheKey, Integer, CardinalityCacheEntry> |
| 185 | + { |
| 186 | + protected final long timeToLive; |
| 187 | + protected final static long defaultTimeToLive = 300_000; // 5 minutes |
| 188 | + |
| 189 | + public MyDefaultCardinalityCachePolicies() { |
| 190 | + this( defaultTimeToLive ); |
| 191 | + } |
| 192 | + |
| 193 | + public MyDefaultCardinalityCachePolicies( final long timeToLive ) { |
| 194 | + this.timeToLive = timeToLive; |
| 195 | + } |
| 196 | + |
| 197 | + @Override |
| 198 | + public CacheEntryFactory<CardinalityCacheEntry, Integer> getEntryFactory() { |
| 199 | + return new CardinalityCacheEntryFactory(); |
| 200 | + } |
| 201 | + |
| 202 | + @Override |
| 203 | + public CacheReplacementPolicyFactory<CardinalityCacheKey, Integer, CardinalityCacheEntry> getReplacementPolicyFactory() { |
| 204 | + return new CacheReplacementPolicyFactory<>() { |
| 205 | + @Override |
| 206 | + public CacheReplacementPolicy<CardinalityCacheKey, Integer, CardinalityCacheEntry> create() { |
| 207 | + return new CacheReplacementPolicyLRU<>(); |
| 208 | + } |
| 209 | + }; |
| 210 | + } |
| 211 | + |
| 212 | + @Override |
| 213 | + public CacheInvalidationPolicy<CardinalityCacheEntry, Integer> getInvalidationPolicy() { |
| 214 | + return new CacheInvalidationPolicyTimeToLive<>( timeToLive ); |
| 215 | + } |
| 216 | + } // end of MyDefaultCachePolicies |
| 217 | +} |
0 commit comments