private Set<String> getSessions(Flag... flags) { Locality locality = new CacheLocality(this.cache); try (Stream<Key<String>> keys = this.cache.getAdvancedCache().withFlags(flags).keySet().stream()) { return keys.filter(this.filter.and(key -> locality.isLocal(key))).map(key -> key.getValue()).collect(Collectors.toSet()); } }
private void schedule(Locality oldLocality, Locality newLocality) { SessionMetaDataFactory<InfinispanSessionMetaData<L>, L> metaDataFactory = this.factory.getMetaDataFactory(); // Iterate over sessions in memory try (CloseableIterator<Key<String>> keys = this.cache.getAdvancedCache().withFlags(Flag.CACHE_MODE_LOCAL, Flag.SKIP_CACHE_LOAD).keySet().iterator()) { while (keys.hasNext()) { if (Thread.currentThread().isInterrupted()) break; Key<String> key = keys.next(); // If we are the new primary owner of this session then schedule expiration of this session locally if (this.filter.test(key) && !oldLocality.isLocal(key) && newLocality.isLocal(key)) { String id = key.getValue(); try (Batch batch = this.batcher.createBatch()) { try { // We need to lookup the session to obtain its meta data InfinispanSessionMetaData<L> value = metaDataFactory.tryValue(id); if (value != null) { this.scheduler.schedule(id, metaDataFactory.createImmutableSessionMetaData(id, value)); } return; } catch (CacheException e) { batch.discard(); } } } } } } }
/** * {@inheritDoc} */ public Set<CacheKey> keySet() { return parentCache.keySet(); }
@Override public Set<K> keySet() { return this.cacheStore.with(this.classloader).keySet(); } }
/** * {@inheritDoc} */ public Set<String> getAllGroupOfNodesQuota(String repositoryName, String workspaceName) { Set<String> pathes = new HashSet<String>(); String workspaceUniqueName = composeWorkspaceUniqueName(repositoryName, workspaceName); for (Serializable cacheKey : cache.keySet()) { if (cacheKey instanceof GroupOfNodesQuotaKey) { if (workspaceUniqueName.equals(((WorkspaceBasedKey)cacheKey).getWorkspaceUniqueName())) { pathes.add(((PathBasedKey)cacheKey).getPath()); } } } return pathes; }
/** * {@inheritDoc} */ public Set<String> getAllNodeQuota(String repositoryName, String workspaceName) { Set<String> pathes = new HashSet<String>(); String workspaceUniqueName = composeWorkspaceUniqueName(repositoryName, workspaceName); for (Serializable cacheKey : cache.keySet()) { if (cacheKey instanceof NodeQuotaKey) { if (workspaceUniqueName.equals(((WorkspaceBasedKey)cacheKey).getWorkspaceUniqueName())) { pathes.add(((PathBasedKey)cacheKey).getPath()); } } } return pathes; }
/** * {@inheritDoc} */ public Set<String> getAllTrackedNodes(String repositoryName, String workspaceName) { Set<String> pathes = new HashSet<String>(); String workspaceUniqueName = composeWorkspaceUniqueName(repositoryName, workspaceName); for (Serializable cacheKey : cache.keySet()) { if (cacheKey instanceof NodeDataSizeKey) { if (workspaceUniqueName.equals(((WorkspaceBasedKey)cacheKey).getWorkspaceUniqueName())) { pathes.add(((PathBasedKey)cacheKey).getPath()); } } } return pathes; }
/** * {@inheritDoc} */ public void clearWorkspaceData(String repositoryName, String workspaceName) throws BackupException { String workspaceUniqueName = composeWorkspaceUniqueName(repositoryName, workspaceName); for (Serializable cacheKey : cache.keySet()) { if (cacheKey instanceof WorkspaceBasedKey) { if (workspaceUniqueName.equals(((WorkspaceBasedKey)cacheKey).getWorkspaceUniqueName())) { cache.remove(cacheKey); } } } }
Set<Class<?>> keys() { if (indexedEntities != null) { return indexedEntities; } startInternalCache(); Set<Class<?>> result = new HashSet<>(); Transaction tx = transactionHelper.suspendTxIfExists(); try { for (KeyValuePair<String, Class<?>> key : knownClassesCache.keySet()) { if (key.getKey().equals(cacheName)) { result.add(key.getValue()); } } return result; } finally { transactionHelper.resume(tx); } }
Set<Class<?>> keys() { if (indexedEntities != null) { return indexedEntities; } startInternalCache(); Set<Class<?>> result = new HashSet<>(); Transaction tx = transactionHelper.suspendTxIfExists(); try { for (KeyValuePair<String, Class<?>> key : knownClassesCache.keySet()) { if (key.getKey().equals(cacheName)) { result.add(key.getValue()); } } return result; } finally { transactionHelper.resume(tx); } }
public static void removeAll(AdvancedCache cache) { CloseableIterator it = cache.keySet().iterator(); try { while (it.hasNext()) { // Cannot use it.next(); it.remove() due to ISPN-5653 cache.remove(it.next()); } } finally { it.close(); } }
/** * Brings all data from the distributed cache into our local cache. */ private void populateLocalCache() { CloseableIterator iterator = cache.keySet().iterator(); try { while (iterator.hasNext()) { get(null, iterator.next()); } } finally { iterator.close(); } }
private void bulkGetKeysInternal(HotRodHeader header, AdvancedCache<byte[], byte[]> cache, int scope) { try { if (trace) { log.tracef("About to create bulk get keys response scope = %d", scope); } writeResponse(header, header.encoder().bulkGetKeysResponse(header, server, channel.alloc(), cache.keySet().iterator())); } catch (Throwable t) { writeException(header, t); } }
@Test public void testKeySetRemove() { Map<Object, String> values = putValuesInCache(); final Cache<Object, Object> cache = cache(0, CACHE_NAME); cache.getAdvancedCache().addInterceptor(new BaseCustomInterceptor() { @Override public Object visitRemoveCommand(InvocationContext ctx, RemoveCommand command) throws Throwable { assertTrue(command.hasAnyFlag(FlagBitSets.SKIP_CACHE_STORE)); return super.visitRemoveCommand(ctx, command); } }, 0); for (Iterator<Object> it = cache(0, CACHE_NAME).getAdvancedCache().withFlags(Flag.SKIP_CACHE_STORE).keySet().iterator(); it.hasNext();) { assertTrue(values.containsKey(it.next())); it.remove(); } assertEquals(0, cache.size()); }
public void testKeySetWithEvictedEntriesAndFlags() { final int numKeys = 300; for (int i = 0; i < numKeys; i++) { cache.put(i, i); } AdvancedCache<Object, Object> flagCache = cache.getAdvancedCache().withFlags(Flag.SKIP_CACHE_LOAD); DataContainer dc = flagCache.getDataContainer(); assertFalse("Data Container should not have all keys", numKeys == dc.size()); Set<Object> keySet = flagCache.keySet(); assertEquals(dc.size(), keySet.size()); for (Object key : dc.keySet()) { assertTrue("Key: " + key + " was not found!", keySet.contains(key)); } }
@Test public void testKeySetStreamRemove() { Map<Object, String> values = putValuesInCache(); final Cache<Object, Object> cache = cache(0, CACHE_NAME); cache.getAdvancedCache().addInterceptor(new BaseCustomInterceptor() { @Override public Object visitRemoveCommand(InvocationContext ctx, RemoveCommand command) throws Throwable { assertTrue(command.hasAnyFlag(FlagBitSets.SKIP_CACHE_STORE)); return super.visitRemoveCommand(ctx, command); } }, 0); Iterator<Object> it = cache(0, CACHE_NAME).getAdvancedCache().withFlags(Flag.SKIP_CACHE_STORE) .keySet() .stream() .iterator(); assertTrue(it.hasNext()); assertTrue(values.containsKey(it.next())); // We don't support remove on stream iterator Exceptions.expectException(UnsupportedOperationException.class, it::remove); }
Set keys = cacheWithIgnoredOwnership.keySet(); for (Object key : keys) assertTrue(expKeys.remove(key));
@Test public void testNoTenancy() throws Exception { final Item item = new Item("my item", "description" ); withTxSession(s -> s.persist(item)); for (int i = 0; i < 5; ++i) { // make sure we get something cached withTxSession(s -> { Item item2 = s.get(Item.class, item.getId()); assertNotNull(item2); assertEquals(item.getName(), item2.getName()); }); } InfinispanBaseRegion region = TEST_SESSION_ACCESS.getRegion(sessionFactory(), Item.class.getName()); AdvancedCache localCache = region.getCache().withFlags(Flag.CACHE_MODE_LOCAL); assertEquals(1, localCache.size()); try (CloseableIterator iterator = localCache.keySet().iterator()) { assertEquals(sessionFactory().getClassMetadata(Item.class).getIdentifierType().getReturnedClass(), iterator.next().getClass()); } } }
@Before public void insertAndClearCache() throws Exception { region = TEST_SESSION_ACCESS.getRegion(sessionFactory(), Item.class.getName()); entityCache = region.getCache(); timeout = TestRegionFactoryProvider.load().findRegionFactory(sessionFactory().getCache()) .getPendingPutsCacheConfiguration().expiration().maxIdle(); Item item = new Item("my item", "Original item"); withTxSession(s -> s.persist(item)); entityCache.clear(); assertEquals("Cache is not empty", Collections.EMPTY_SET, entityCache.keySet()); itemId = item.getId(); log.info("Insert and clear finished"); }
AdvancedCache localCache = region.getCache().withFlags(Flag.CACHE_MODE_LOCAL); assertEquals(1, localCache.size()); try (CloseableIterator iterator = localCache.keySet().iterator()) { assertEquals("CacheKeyImplementation", iterator.next().getClass().getSimpleName());