@SPITest @SuppressWarnings("unchecked") public void iterableContainsValuesInAnyOrder() throws StoreAccessException, IllegalAccessException, InstantiationException { K key1 = factory.createKey(1L); K key2 = factory.createKey(2L); K key3 = factory.createKey(3L); V value1 = factory.createValue(1L); V value2 = factory.createValue(2L); V value3 = factory.createValue(3L); kvStore.put(key1, value1); kvStore.put(key2, value2); kvStore.put(key3, value3); Store.Iterator<Cache.Entry<K, Store.ValueHolder<V>>> iterator = kvStore.iterator(); List<K> keys = new ArrayList<>(); List<V> values = new ArrayList<>(); while (iterator.hasNext()) { Cache.Entry<K, Store.ValueHolder<V>> nextEntry = iterator.next(); keys.add(nextEntry.getKey()); values.add(nextEntry.getValue().get()); } assertThat(keys, containsInAnyOrder(equalTo(key1), equalTo(key2), equalTo(key3))); assertThat(values, containsInAnyOrder(equalTo(value1), equalTo(value2), equalTo(value3))); } }
@SPITest public void nextReturnsNextElement() throws IllegalAccessException, InstantiationException, StoreAccessException, LegalSPITesterException { kvStore = factory.newStore(); K key = factory.createKey(1); V value = factory.createValue(1); kvStore.put(key, value); Store.Iterator<Cache.Entry<K, Store.ValueHolder<V>>> iterator = kvStore.iterator(); try { Cache.Entry<K, Store.ValueHolder<V>> entry = iterator.next(); assertThat(entry.getKey(), is(equalTo(key))); assertThat(entry.getValue().get(), is(equalTo(value))); } catch (StoreAccessException e) { throw new LegalSPITesterException("Warning, an exception is thrown due to the SPI test"); } }
private static void assertEntry(Entry<String, ValueHolder<String>> entry, String key, String value) { assertThat(entry.getKey(), equalTo(key)); assertThat(entry.getValue().get(), equalTo(value)); }
@Override public Cache.Entry<K, V> next() { try { Cache.Entry<K, Store.ValueHolder<V>> next = iterator.next(); final K nextKey = next.getKey(); Store.ValueHolder<V> nextValueHolder = next.getValue(); // call Cache.get() here to check for expiry *and* account for a get in the stats, without using the loader if (cache.getNoLoader(nextKey) == null) { current = null; return null; } current = next; final V nextValue = nextValueHolder.get(); return new Cache.Entry<K, V>() { @Override public K getKey() { return nextKey; } @Override public V getValue() { return nextValue; } }; } catch (StoreAccessException sae) { current = null; return null; } }
public List<V> values() { if (cacheManager == null || null == cacheManager.getCache(cacheName)) { return new ArrayList<>(); } Iterator it = cacheManager.getCache(cacheName).iterator(); List<V> list = new ArrayList<>(); while (it.hasNext()) { Cache.Entry<K, V> entry = (Cache.Entry<K, V>) it.next(); V t = entry.getValue(); list.add(t); } return list; }
@Override public V getValue() { return ehEntry.getValue(); }
@Override public V getValue() { return storeEntry.getValue().get(); }
while (iterator.hasNext()) { Cache.Entry<Long, String> next = iterator.next(); result.put(next.getKey(), next.getValue()); iterator.remove(); result.put(next.getKey(), next.getValue()); Cache.Entry<Long, String> next = iterator.next(); iterator.remove(); result.put(next.getKey(), next.getValue()); result.put(next.getKey(), next.getValue());
@Override public Cache.Entry<K, V> next() { try { Cache.Entry<K, Store.ValueHolder<V>> next = iterator.next(); final K nextKey = next.getKey(); Store.ValueHolder<V> nextValueHolder = next.getValue(); // call Cache.get() here to check for expiry *and* account for a get in the stats, without using the loader if (cache.getNoLoader(nextKey) == null) { current = null; return null; } current = next; final V nextValue = nextValueHolder.get(); return new Cache.Entry<K, V>() { @Override public K getKey() { return nextKey; } @Override public V getValue() { return nextValue; } }; } catch (StoreAccessException sae) { current = null; return null; } }
@Override public void run() { try { transactionManager.begin(); Map<Long, String> result = new HashMap<>(); for (Cache.Entry<Long, String> next : txCache1) { result.put(next.getKey(), next.getValue()); } assertThat(result.size(), equalTo(0)); transactionManager.commit(); } catch (Throwable t) { throwableRef.set(t); } } };
/** * Tests fetching all entries via an {@link Ehcache#iterator()} on a non-empty cache. */ @Test public void testIteratorNonEmptyAll() throws Exception { final Map<String, String> testStoreEntries = this.getTestStoreEntries(); this.store = new FakeStore(testStoreEntries); final InternalCache<String, String> ehcache = this.getEhcache(); for (Cache.Entry<String, String> cacheEntry : ehcache) { final String cacheEntryKey = cacheEntry.getKey(); assertThat(testStoreEntries, hasEntry(equalTo(cacheEntryKey), equalTo(cacheEntry.getValue()))); testStoreEntries.remove(cacheEntryKey); } assertThat("Iterator did not return all values", testStoreEntries.isEmpty(), is(true)); }
@Override public V getValue() { return storeEntry.getValue().get(); }
@Override public V getValue() { return ehEntry.getValue(); }
private static Map<String, String> observe(Iterator<Entry<String, ValueHolder<String>>> iter) throws StoreAccessException { Map<String, String> map = new HashMap<>(); while (iter.hasNext()) { Entry<String, ValueHolder<String>> entry = iter.next(); map.put(entry.getKey(), entry.getValue().get()); } return map; }
@Test public void testIterator() throws StoreAccessException { store.put(KEY, VALUE); Store.Iterator<Cache.Entry<Long, Store.ValueHolder<Value>>> iterator = store.iterator(); assertThat(iterator.hasNext(), is(true)); while (iterator.hasNext()) { Cache.Entry<Long, Store.ValueHolder<Value>> entry = iterator.next(); compareValues(entry.getValue().get(), VALUE); } }
private static Map<String, Long> observeAccessTimes(Iterator<Entry<String, ValueHolder<String>>> iter) throws StoreAccessException { Map<String, Long> map = new HashMap<>(); while (iter.hasNext()) { Entry<String, ValueHolder<String>> entry = iter.next(); map.put(entry.getKey(), entry.getValue().lastAccessTime()); } return map; }