@Test public void testComputeFunctionReturnsDifferentNoPin() throws Exception { EhcacheOffHeapBackingMap<String, String> segment = createTestSegment(); try { segment.put("key", "value"); String value = segment.compute("key", (s, s2) -> "otherValue", false); assertThat(value, is("otherValue")); assertThat(isPinned("key", segment), is(false)); } finally { destroySegment(segment); } }
@Test public void testComputeIfPinnedClearsMappingOnNullReturnWithPinningFalse() throws Exception { EhcacheOffHeapBackingMap<String, String> segment = createTestSegment(); final String value = "value"; try { putPinned("key", value, segment); boolean result = segment.computeIfPinned("key", (s, s2) -> { assertThat(s2, is(value)); return null; }, s -> { assertThat(s, is(value)); return false; }); assertThat(segment.containsKey("key"), is(false)); assertThat(result, is(true)); } finally { destroySegment(segment); } }
@Override public void invalidate(final K key) throws StoreAccessException { invalidateObserver.begin(); final AtomicBoolean removed = new AtomicBoolean(false); try { backingMap().computeIfPresent(key, (k, present) -> { removed.set(true); notifyInvalidation(key, present); return null; }); if (removed.get()) { invalidateObserver.end(LowerCachingTierOperationsOutcome.InvalidateOutcome.REMOVED); } else { invalidateObserver.end(LowerCachingTierOperationsOutcome.InvalidateOutcome.MISS); } } catch (RuntimeException re) { throw handleException(re); } }
@Test public void testComputeIfPresentReturnsDifferentValue() throws Exception { EhcacheOffHeapBackingMap<String, String> segment = createTestSegment(); try { segment.put("key", "value"); String value = segment.computeIfPresent("key", (s, s2) -> "newValue"); assertThat(segment.get("key"), is(value)); } finally { destroySegment(segment); } }
@Test public void testComputeIfPresentReturnsNullRemovesMapping() throws Exception { EhcacheOffHeapBackingMap<String, String> segment = createTestSegment(); try { segment.put("key", "value"); String value = segment.computeIfPresent("key", (s, s2) -> null); assertThat(segment.containsKey("key"), is(false)); } finally { destroySegment(segment); } }
@Test public void testComputeIfPresentAndPinReplacesAndPins() throws Exception { EhcacheOffHeapBackingMap<String, String> segment = createTestSegment(); final String value = "value"; final String newValue = "newValue"; segment.put("key", value); segment.computeIfPresentAndPin("key", (s, s2) -> { assertThat(s2, is(value)); return newValue; }); assertThat(isPinned("key", segment), is(true)); assertThat(segment.get("key"), is(newValue)); }
private OffHeapValueHolder<V> computeWithRetry(K key, BiFunction<K, OffHeapValueHolder<V>, OffHeapValueHolder<V>> computeFunction, boolean fault) throws StoreAccessException { OffHeapValueHolder<V> computeResult; try { computeResult = backingMap().compute(key, computeFunction, fault); } catch (OversizeMappingException ex) { try { evictionAdvisor().setSwitchedOn(false); invokeValve(); computeResult = backingMap().compute(key, computeFunction, fault); } catch (OversizeMappingException e) { throw new StoreAccessException("The element with key '" + key + "' is too large to be stored" + " in this offheap store.", e); } catch (RuntimeException e) { throw handleException(e); } finally { evictionAdvisor().setSwitchedOn(true); } } catch (RuntimeException re) { throw handleException(re); } return computeResult; }
@Test public void testComputeIfPresentAndPinNoOpNoMapping() throws Exception { EhcacheOffHeapBackingMap<String, String> segment = createTestSegment(); segment.computeIfPresentAndPin("key", (s, s2) -> { fail("Function should not be invoked"); return null; }); }
@Test public void testComputeIfPinnedClearsPinWithoutChangingValue() throws Exception { EhcacheOffHeapBackingMap<String, String> segment = createTestSegment(); final String value = "value"; try { putPinned("key", value, segment); boolean result = segment.computeIfPinned("key", (s, s2) -> { assertThat(s2, is(value)); return s2; }, s -> { assertThat(s, is(value)); return true; }); assertThat(isPinned("key", segment), is(false)); assertThat(result, is(true)); } finally { destroySegment(segment); } }
@Test public void testComputeIfPinnedReplacesValueUnpinnedWhenUnpinFunctionTrue() throws Exception { EhcacheOffHeapBackingMap<String, String> segment = createTestSegment(); final String value = "value"; final String newValue = "newValue"; try { putPinned("key", value, segment); boolean result = segment.computeIfPinned("key", (s, s2) -> { assertThat(s2, is(value)); return newValue; }, s -> { assertThat(s, is(value)); return true; }); assertThat(segment.get("key"), is(newValue)); assertThat(isPinned("key", segment), is(false)); assertThat(result, is(false)); } finally { destroySegment(segment); } }
@Test public void testComputeIfPresentAndPinDoesPin() throws Exception { EhcacheOffHeapBackingMap<String, String> segment = createTestSegment(); final String value = "value"; segment.put("key", value); segment.computeIfPresentAndPin("key", (s, s2) -> { assertThat(s2, is(value)); return value; }); assertThat(isPinned("key", segment), is(true)); }
@Test public void testComputeFunctionCalledWhenNoMapping() throws Exception { EhcacheOffHeapBackingMap<String, String> segment = createTestSegment(); try { String value = segment.compute("key", (s, s2) -> "value", false); assertThat(value, is("value")); assertThat(segment.get("key"), is(value)); } finally { destroySegment(segment); } }
@Test public void testComputeFunctionReturnsNullRemoves() throws Exception { EhcacheOffHeapBackingMap<String, String> segment = createTestSegment(); try { putPinned("key", "value", segment); String value = segment.compute("key", (s, s2) -> null, false); assertThat(value, nullValue()); assertThat(segment.containsKey("key"), is(false)); } finally { destroySegment(segment); } }
@Test public void testComputeIfPinnedNoOpUnpinned() throws Exception { EhcacheOffHeapBackingMap<String, String> segment = createTestSegment(); try { segment.put("key", "value"); boolean result = segment.computeIfPinned("key", (s, s2) -> { fail("Method should not be invoked"); return null; }, s -> { fail("Method should not be invoked"); return false; }); assertThat(isPinned("key", segment), is(false)); assertThat(result, is(false)); } finally { destroySegment(segment); } }
private OffHeapValueHolder<V> newCreateValueHolder(K key, V value, long now, StoreEventSink<K, V> eventSink) { Objects.requireNonNull(value); Duration duration = ExpiryUtils.getExpiryForCreation(key, value, expiry); if(duration.isZero()) { return null; } eventSink.created(key, value); long expirationTime = isExpiryDurationInfinite(duration) ? ValueHolder.NO_EXPIRE : ExpiryUtils.getExpirationMillis(now, duration); return new BasicOffHeapValueHolder<>(backingMap().nextIdFor(key), value, now, expirationTime); }
private void flushToLowerTier(OffHeapStore<Object, ?> offheapStore) { StoreAccessException lastFailure = null; int failureCount = 0; for (Object key : offheapStore.backingMap().keySet()) { try { offheapStore.invalidate(key); } catch (StoreAccessException cae) { lastFailure = cae; failureCount++; LOGGER.warn("Error flushing '{}' to lower tier", key, cae); } } if (lastFailure != null) { throw new RuntimeException("Failed to flush some mappings to lower tier, " + failureCount + " could not be flushed. This error represents the last failure.", lastFailure); } }
@Override public void clear() throws StoreAccessException { try { backingMap().clear(); } catch (RuntimeException re) { throw handleException(re); } }
@Override public void invalidateAllWithHash(long hash) { invalidateAllWithHashObserver.begin(); int intHash = HashUtils.longHashToInt(hash); Map<K, OffHeapValueHolder<V>> removed = backingMap().removeAllWithHash(intHash); for (Map.Entry<K, OffHeapValueHolder<V>> entry : removed.entrySet()) { notifyInvalidation(entry.getKey(), entry.getValue()); } invalidateAllWithHashObserver.end(LowerCachingTierOperationsOutcome.InvalidateAllWithHashOutcome.SUCCESS); }
backingMap().compute(key, computeFunction, false); ValueHolder<V> result = valueHolderAtomicReference.get(); if (result == null) {
@Test public void testComputeIfPresentReturnsSameValue() throws Exception { EhcacheOffHeapBackingMap<String, String> segment = createTestSegment(); try { segment.put("key", "value"); String value = segment.computeIfPresent("key", (s, s2) -> s2); assertThat(segment.get("key"), is(value)); } finally { destroySegment(segment); } }