/** {@inheritDoc} */ @Override public Cache.Entry<K, V> next() { try { List<?> row = iter.next(); return new CacheEntryImpl<>((K)row.get(0), (V)row.get(1)); } catch (CacheException e) { throw e; } catch (Exception e) { throw new CacheException(e); } }
/** {@inheritDoc} */ @Override public <T> T unwrap(Class<T> cls) { if (cls != null && cls.isAssignableFrom(getClass())) return cls.cast(this); if (cls.isAssignableFrom(CacheEntryImpl.class)) return (T)new CacheEntryImpl<>(getKey(), getValue()); if (cls.isAssignableFrom(CacheEntry.class)) return (T)new CacheEntryImplEx<>(getKey(), getValue(), null); throw new IllegalArgumentException("Unwrapping to class is not supported: " + cls); } }
/** {@inheritDoc} */ @Override public Iterator<Cache.Entry<?, ?>> query(@Nullable String cacheName, Collection<Object> params, @Nullable IndexingQueryFilter filters) { return idx.containsKey(GridIoPolicy.QUERY_POOL) ? Collections.<Cache.Entry<?, ?>>singletonList( new CacheEntryImpl<>(GridIoPolicy.QUERY_POOL, GridIoPolicy.QUERY_POOL)).iterator() : Collections.<Cache.Entry<?, ?>>emptyList().iterator(); }
/** * @param key Key. * @return Previous value. * @throws IgniteCheckedException If failed. */ protected V getAndRemove0(final K key) throws IgniteCheckedException { final boolean keepBinary = ctx.keepBinary(); return syncOp(new SyncOp<V>(true) { @Override public V op(GridNearTxLocal tx) throws IgniteCheckedException { K key0 = keepBinary ? (K)ctx.toCacheKeyObject(key) : key; IgniteInternalFuture<GridCacheReturn> fut = tx.removeAllAsync(ctx, null, Collections.singletonList(key0), /*retval*/true, null, /*singleRmv*/false); V ret = fut.get().value(); if (ctx.config().getInterceptor() != null) { K key = keepBinary ? (K)ctx.unwrapBinaryIfNeeded(key0, true, false) : key0; return (V)ctx.config().getInterceptor().onBeforeRemove(new CacheEntryImpl(key, ret)).get2(); } return ret; } @Override public String toString() { return S.toString("remove", "key", key, true); } }); }
@Override public void run() { try { while (running.get()) { for (int i = 0; i < CACHE_SIZE; i++) { store.write(new CacheEntryImpl<>(i, "val-0")); store.write(new CacheEntryImpl<>(i, "val" + i)); } } } catch (Exception e) { error("Unexpected exception in put thread", e); assert false; } } }, 1, "put");
@Override public void run() { try { while (running.get()) { for (int i = 0; i < CACHE_SIZE; i++) { store.write(new CacheEntryImpl<>(i, "val-0")); actualPutCnt.incrementAndGet(); store.write(new CacheEntryImpl<>(i, "val" + i)); actualPutCnt.incrementAndGet(); } } } catch (Exception e) { error("Unexpected exception in put thread", e); assert false; } } }, 1, "put");
/** {@inheritDoc} */ @Override public Iterator<Cache.Entry<?, ?>> query(@Nullable String cacheName, Collection<Object> params, @Nullable IndexingQueryFilter filters) throws IgniteSpiException { if (params.size() < 2) throw new IgniteSpiException("Range parameters required."); Iterator<Object> paramsIt = params.iterator(); Object from = paramsIt.next(); Object to = paramsIt.next(); from = from instanceof BinaryObject ? ((BinaryObject)from).deserialize() : from; to = to instanceof BinaryObject ? ((BinaryObject)to).deserialize() : to; SortedMap<Object, Object> map = idx.subMap(from, to); Collection<Cache.Entry<?, ?>> res = new ArrayList<>(map.size()); for (Map.Entry<Object, Object> entry : map.entrySet()) res.add(new CacheEntryImpl<>(entry.getKey(), entry.getValue())); return res.iterator(); }
/** * @param entry Internal entry. * @param deserializeBinary Deserialize binary flag. * @return Public API entry. * @throws IgniteCheckedException If failed. * @throws GridCacheEntryRemovedException If entry removed. */ @Nullable private Cache.Entry<K, V> toCacheEntry(GridCacheEntryEx entry, boolean deserializeBinary) throws IgniteCheckedException, GridCacheEntryRemovedException { CacheObject val = entry.innerGet( /*ver*/null, /*tx*/null, /*readThrough*/false, /*metrics*/false, /*evt*/false, /*subjId*/null, /*transformClo*/null, /*taskName*/null, /*expiryPlc*/null, !deserializeBinary); if (val == null) return null; KeyCacheObject key = entry.key(); Object key0 = ctx.unwrapBinaryIfNeeded(key, !deserializeBinary, true); Object val0 = ctx.unwrapBinaryIfNeeded(val, !deserializeBinary, true); return new CacheEntryImpl<>((K)key0, (V)val0, entry.version()); }
store.write(new CacheEntryImpl<>(key, "val" + key)); set.add(key); store.write(new CacheEntryImpl<>(key, "broken")); store.write(new CacheEntryImpl<>(key, "val" + key));
/** {@inheritDoc} */ @Override public Iterable<Entry<K, V>> call() throws Exception { Collection<Entry<K, V>> res = new ArrayList<>(); for (Entry<K, V> e : cache().localEntries(peekModes)) res.add(new CacheEntryImpl<>(e.getKey(), e.getValue())); return res; } }
@Override public void run() { try { delegate.setShouldFail(true); initStore(2, writeCoalescing); try { store.write(new CacheEntryImpl<>(1, "val1")); store.write(new CacheEntryImpl<>(2, "val2")); } finally { shutdownStore(); delegate.setShouldFail(false); } } catch (Exception e) { err.set(e); } } }, 1).get();
store.write(new CacheEntryImpl<>(i, "val" + i));
/** * @throws Exception If failed. */ @Test public void testTimestamp() throws Exception { Timestamp k = new Timestamp(System.currentTimeMillis()); ses.newSession(null); Integer v = 5; store.write(new CacheEntryImpl<>(k, v)); assertEquals(v, store.load(k)); store.delete(k); assertNull(store.load(k)); }
/** * Checks that write behind cache flush frequency was correctly adjusted to nanos expecting putAllCnt to be * less or equal than elapsed time divided by flush frequency. * * @throws Exception If failed. */ @Test public void testSimpleStoreFlushFrequencyWithoutCoalescing() throws Exception { initStore(1, false); long writeBehindFlushFreqNanos = FLUSH_FREQUENCY * 1000 * 1000; int threshold = store.getWriteBehindStoreBatchSize() / 10; try { long start = System.nanoTime(); for (int i = 0; i < threshold / 2; i++) store.write(new CacheEntryImpl<>(i, "v" + i)); U.sleep(FLUSH_FREQUENCY + 300); for (int i = threshold / 2; i < threshold; i++) store.write(new CacheEntryImpl<>(i, "v" + i)); long elapsed = System.nanoTime() - start; U.sleep(FLUSH_FREQUENCY + 300); int expFlushOps = (int)(1 + elapsed / writeBehindFlushFreqNanos); assertTrue(delegate.getPutAllCount() <= expFlushOps); } finally { shutdownStore(); } }
/** {@inheritDoc} */ @Override public <K, V> Cache.Entry<K, V> wrap() { try { IgniteInternalTx tx = cctx.tm().userTx(); CacheObject val; if (tx != null) { GridTuple<CacheObject> peek = tx.peek(cctx, false, key); val = peek == null ? rawGet() : peek.get(); } else val = rawGet(); return new CacheEntryImpl<>(key.<K>value(cctx.cacheObjectContext(), false), CU.<V>value(val, cctx, false), ver); } catch (GridCacheFilterFailedException ignored) { throw new IgniteException("Should never happen."); } }
store.write(new CacheEntryImpl<>(wrap(k1), wrap(v1)));
/** * Simple store test. * * @param writeCoalescing Write coalescing flag. * @throws Exception If failed. */ private void testSimpleStore(boolean writeCoalescing) throws Exception { initStore(2, writeCoalescing); try { store.write(new CacheEntryImpl<>(1, "v1")); store.write(new CacheEntryImpl<>(2, "v2")); assertEquals("v1", store.load(1)); assertEquals("v2", store.load(2)); assertNull(store.load(3)); assertEquals(store.loadAll(Arrays.asList(3, 4, 5)).size(), 0); store.delete(1); assertNull(store.load(1)); assertEquals(store.loadAll(Arrays.asList(1)).size(), 0); assertEquals("v2", store.load(2)); assertNull(store.load(3)); assertEquals(store.loadAll(Arrays.asList(3)).size(), 0); } finally { shutdownStore(); } }
store.write(new CacheEntryImpl<>(i, "reloaded-" + i));
/** * @throws Exception If failed. */ @Test public void testStore() throws Exception { // Create dummy transaction Transaction tx = new DummyTx(); ses.newSession(tx); store.write(new CacheEntryImpl<>("k1", "v1")); store.write(new CacheEntryImpl<>("k2", "v2")); store.sessionEnd(true); ses.newSession(null); assertEquals("v1", store.load("k1")); assertEquals("v2", store.load("k2")); assertNull(store.load("k3")); ses.newSession(tx); store.delete("k1"); store.sessionEnd(true); ses.newSession(null); assertNull(store.load("k1")); assertEquals("v2", store.load("k2")); assertNull(store.load("k3")); }
/** * Check that all values written to the store will be in underlying store after timeout or due to size limits. * * @param writeCoalescing Write coalescing flag * @throws Exception If failed. */ private void testValuePropagation(boolean writeCoalescing) throws Exception { // Need to test size-based write. initStore(1, writeCoalescing); try { for (int i = 0; i < CACHE_SIZE * 2; i++) store.write(new CacheEntryImpl<>(i, "val" + i)); U.sleep(200); for (int i = 0; i < CACHE_SIZE; i++) { String val = delegate.load(i); assertNotNull("Value for [key= " + i + "] was not written in store", val); assertEquals("Invalid value [key=" + i + "]", "val" + i, val); } U.sleep(FLUSH_FREQUENCY + 300); for (int i = CACHE_SIZE; i < CACHE_SIZE * 2; i++) { String val = delegate.load(i); assertNotNull("Value for [key= " + i + "] was not written in store", val); assertEquals("Invalid value [key=" + i + "]", "val" + i, val); } } finally { shutdownStore(); } }