@Bean public FileTrackerHistoryMemory fileTrackerHistoryMemory() { MultiMap<String, FileTrackerHistoryItem> historyMap = beanFactory .getBean(HazelcastInstance.class).getMultiMap( DistributedMapNames.MAP.FILE_TRACKER_HISTORY_MAP .toString()); IMap<String, FileTrackerHistoryItem> latestMap = beanFactory.getBean( HazelcastInstance.class).getMap( DistributedMapNames.MAP.FILE_TRACKER_HISTORY_LATEST_MAP .toString()); int threads = 10; return new FileTrackerHistoryMemoryImpl(historyMap, latestMap, threads); }
private boolean isSwitchExist(String switchId, MultiMap<ControllerModel, SwitchConnectModel> ControllerMappingRole) { for (ControllerModel controllerModel : ControllerMappingRole.keySet()) { Collection<SwitchConnectModel> switches = ControllerMappingRole .get(controllerModel); Iterator<SwitchConnectModel> it = switches.iterator();// 遍歷switch while (it.hasNext()) { SwitchConnectModel singleSwitch = it.next(); if (singleSwitch.getDpid().equals(switchId)) { return true; } } } return false; }
private Set<String> getAllSwitch( MultiMap<ControllerModel, SwitchConnectModel> ControllerMappingRole) { Set<String> allSwitch = new HashSet<String>(); for (ControllerModel controllerModel : ControllerMappingRole.keySet()) { Collection<SwitchConnectModel> switches = ControllerMappingRole .get(controllerModel); Iterator<SwitchConnectModel> it = switches.iterator();// 遍歷switch while (it.hasNext()) { SwitchConnectModel singleSwitch = it.next(); allSwitch.add(singleSwitch.getDpid()); } } return allSwitch; }
@Test public void get() throws InterruptedException { HazelcastClient hClient = getHazelcastClient(); MultiMap<String, Integer> multiMap = hClient.getMultiMap("get"); assertTrue(multiMap.put("a", 1)); assertTrue(multiMap.put("a", 2)); Map<Integer, CountDownLatch> map = new HashMap<Integer, CountDownLatch>(); map.put(1, new CountDownLatch(1)); map.put(2, new CountDownLatch(1)); Collection<Integer> collection = multiMap.get("a"); assertEquals(Values.class, collection.getClass()); assertEquals(2, collection.size()); for (Iterator<Integer> it = collection.iterator(); it.hasNext(); ) { Integer o = it.next(); map.get(o).countDown(); } assertTrue(map.get(1).await(10, TimeUnit.SECONDS)); assertTrue(map.get(2).await(10, TimeUnit.SECONDS)); }
@Test public void removeKey() throws InterruptedException { HazelcastClient hClient = getHazelcastClient(); MultiMap<String, Integer> multiMap = hClient.getMultiMap("removeKey"); assertTrue(multiMap.put("a", 1)); assertTrue(multiMap.put("a", 2)); Map<Integer, CountDownLatch> map = new HashMap<Integer, CountDownLatch>(); map.put(1, new CountDownLatch(1)); map.put(2, new CountDownLatch(1)); Collection<Integer> collection = multiMap.remove("a"); assertEquals(Values.class, collection.getClass()); assertEquals(2, collection.size()); for (Iterator<Integer> it = collection.iterator(); it.hasNext(); ) { Object o = it.next(); map.get((Integer) o).countDown(); } assertTrue(map.get(1).await(10, TimeUnit.SECONDS)); assertTrue(map.get(2).await(10, TimeUnit.SECONDS)); }
@Test public void keySet() throws InterruptedException { HazelcastClient hClient = getHazelcastClient(); MultiMap<String, String> multiMap = hClient.getMultiMap("keySet"); int count = 100; for (int i = 0; i < count; i++) { for (int j = 0; j <= i; j++) { multiMap.put(String.valueOf(i), String.valueOf(j)); } } assertEquals(count * (count + 1) / 2, multiMap.size()); Set<String> set = multiMap.keySet(); assertEquals(count, set.size()); Set<String> s = new HashSet<String>(); for (int i = 0; i < count; i++) { s.add(String.valueOf(i)); } assertEquals(s, set); }
@Test public void entrySet() throws InterruptedException { HazelcastClient hClient = getHazelcastClient(); MultiMap<String, String> multiMap = hClient.getMultiMap("entrySet"); Map<String, List<String>> keyValueListMap = new HashMap<String, List<String>>(); int count = 100; for (int i = 0; i < count; i++) { for (int j = 0; j <= i; j++) { String key = String.valueOf(i); String value = String.valueOf(j); multiMap.put(key, value); if (keyValueListMap.get(key) == null) { keyValueListMap.put(key, new ArrayList<String>()); } keyValueListMap.get(key).add(value); } } assertEquals(count * (count + 1) / 2, multiMap.size()); Set<Entry<String, String>> set = multiMap.entrySet(); assertEquals(count * (count + 1) / 2, set.size()); for (Iterator<Entry<String, String>> iterator = set.iterator(); iterator.hasNext(); ) { Entry<String, String> o = iterator.next(); assertTrue(Integer.valueOf(o.getValue()) < count); assertTrue(keyValueListMap.get(o.getKey()).contains(o.getValue())); } }
@Test public void testMultiMapPutAndGet() { HazelcastClient hClient = getHazelcastClient(); MultiMap<String, String> map = hClient.getMultiMap("testMultiMapPutAndGet"); map.put("Hello", "World"); Collection<String> values = map.get("Hello"); assertEquals("World", values.iterator().next()); map.put("Hello", "Europe"); map.put("Hello", "America"); map.put("Hello", "Asia"); map.put("Hello", "Africa"); map.put("Hello", "Antarctica"); map.put("Hello", "Australia"); values = map.get("Hello"); assertEquals(7, values.size()); assertTrue(map.containsKey("Hello")); assertFalse(map.containsKey("Hi")); }
@Test public void testMultiMapRemove() { HazelcastClient hClient = getHazelcastClient(); MultiMap<String, String> map = hClient.getMultiMap("testMultiMapRemove"); map.put("Hello", "World"); map.put("Hello", "Europe"); map.put("Hello", "America"); map.put("Hello", "Asia"); map.put("Hello", "Africa"); map.put("Hello", "Antarctica"); map.put("Hello", "Australia"); assertEquals(7, map.size()); assertEquals(1, map.keySet().size()); Collection<String> values = map.remove("Hello"); assertEquals(7, values.size()); assertEquals(0, map.size()); assertEquals(0, map.keySet().size()); map.put("Hello", "World"); assertEquals(1, map.size()); assertEquals(1, map.keySet().size()); }
@Test public void testMultiMapEntrySet() { HazelcastClient hClient = getHazelcastClient(); MultiMap<String, String> map = hClient.getMultiMap("testMultiMapEntrySet"); map.put("Hello", "World"); map.put("Hello", "Europe"); map.put("Hello", "America"); map.put("Hello", "Asia"); map.put("Hello", "Africa"); map.put("Hello", "Antarctica"); map.put("Hello", "Australia"); Set<Map.Entry<String, String>> entries = map.entrySet(); assertEquals(7, entries.size()); int itCount = 0; for (Map.Entry<String, String> entry : entries) { assertEquals("Hello", entry.getKey()); itCount++; } assertEquals(7, itCount); }
@Override public MapReduceTask<KeyIn, ValueIn, KeyOut, ValueOut> build( MultiMap<KeyIn, ValueIn> multiMap ) { try { ClientMultiMapProxy<KeyIn, ValueIn> proxy = (ClientMultiMapProxy<KeyIn, ValueIn>) multiMap; ClientContext context = (ClientContext) GET_CLIENTCONTEXT_METHOD.invoke( proxy ); return new MultiMapClientMapReduceTaskProxy<KeyIn, ValueIn, KeyOut, ValueOut>( proxy.getName(), context, hazelcastInstance ); } catch ( Throwable t ) { ExceptionUtil.rethrow( t ); } return null; }
@Override public MapReduceTask<KeyIn, ValueIn, KeyOut, ValueOut> build( MultiMap<KeyIn, ValueIn> multiMap ) { try { MultiMapProxySupport proxy = (MultiMapProxySupport) multiMap; NodeEngine nodeEngine = hazelcastInstance.node.nodeEngine; return new MultiMapNodeMapReduceTaskImpl<KeyIn, ValueIn, KeyOut, ValueOut>( proxy.getName(), nodeEngine, hazelcastInstance ); } catch ( Throwable t ) { ExceptionUtil.rethrow( t ); } return null; }
/** * @param map Multi map from hazelcast to use * @param map that will hold the latest status * @param threads number of threads to use to serve the async history put. */ public FileTrackerHistoryMemoryImpl( MultiMap<String, FileTrackerHistoryItem> map, IMap<String, FileTrackerHistoryItem> latestStatusMap, int threads) { this.map = map; this.latestStatusMap = latestStatusMap; service = Executors.newFixedThreadPool(threads); }
/** * Test latest collector status * * @throws InterruptedException * @throws ExecutionException * @throws TimeoutException */ @Test public void testDeleteAgentStatus() throws InterruptedException, ExecutionException, TimeoutException { MultiMap<String, FileTrackerHistoryItem> map = instance .getMultiMap("FileTrackerHistoryMemoryImplTest.testDeleteAgentStatusMap"); IMap<String, FileTrackerHistoryItem> map1 = instance .getMap("FileTrackerHistoryMemoryImplTest.testDeleteAgentStatusMap2"); FileTrackerHistoryMemoryImpl memory = new FileTrackerHistoryMemoryImpl( map, map1, 1); FileTrackerHistoryItem latestItem = null; for (int i = 0; i < 100; i++) { latestItem = new FileTrackerHistoryItem(new Date(), "test1", "collector1", FileTrackerHistoryItem.STATUS.OK); memory.addToHistory(latestItem); } Map<String, FileTrackerHistoryItem> agentStatus = memory .getLastestAgentStatus(); assertNotNull(agentStatus); assertNotNull(agentStatus.get(latestItem.getAgent())); Thread.sleep(500L); assertEquals(100, memory.getAgentHistoryCount(latestItem.getAgent())); memory.deleteAgentHistory(latestItem.getAgent()); agentStatus = memory.getLastestAgentStatus(); assertNull(agentStatus.get(latestItem.getAgent())); assertEquals(0, memory.getAgentHistoryCount(latestItem.getAgent())); }
/** * Test latest collector status * * @throws InterruptedException * @throws ExecutionException * @throws TimeoutException */ @Test public void testLatestCollectorStatus() throws InterruptedException, ExecutionException, TimeoutException { MultiMap<String, FileTrackerHistoryItem> map = instance .getMultiMap("FileTrackerHistoryMemoryImplTest.testLatestCollectorsAsyncMap"); IMap<String, FileTrackerHistoryItem> map1 = instance .getMap("FileTrackerHistoryMemoryImplTest.testLatestCollectorsAsyncMap2"); FileTrackerHistoryMemoryImpl memory = new FileTrackerHistoryMemoryImpl( map, map1, 1); FileTrackerHistoryItem latestItem = null; Collection<Future<?>> futures = new ArrayList<Future<?>>(); for (int i = 0; i < 100; i++) { latestItem = new FileTrackerHistoryItem(new Date(), "test1", "collector1", FileTrackerHistoryItem.STATUS.OK); futures.add(memory.addAsyncToHistory(latestItem)); } for (Future<?> future : futures) { future.get(10000L, TimeUnit.MILLISECONDS); } Map<String, Collection<FileTrackerHistoryItem>> collectorStatus = memory .getLastestCollectorStatus(); assertNotNull(collectorStatus); assertNotNull(collectorStatus.get(latestItem.getCollector())); }
/** * Tests that the latest agent status method returns the latest item. All * items are put using async. * * @throws TimeoutException * @throws ExecutionException * @throws InterruptedException */ @Test public void testLatestAgentAsyncStatus() throws InterruptedException, ExecutionException, TimeoutException { MultiMap<String, FileTrackerHistoryItem> map = instance .getMultiMap("FileTrackerHistoryMemoryImplTest.testLatestAsyncMap"); IMap<String, FileTrackerHistoryItem> map1 = instance .getMap("FileTrackerHistoryMemoryImplTest.testLatestAsyncMap2"); FileTrackerHistoryMemoryImpl memory = new FileTrackerHistoryMemoryImpl( map, map1, 1); FileTrackerHistoryItem latestItem = null; Collection<Future<?>> futures = new ArrayList<Future<?>>(); for (int i = 0; i < 100; i++) { latestItem = new FileTrackerHistoryItem(new Date(), "test1", "collector1", FileTrackerHistoryItem.STATUS.OK); futures.add(memory.addAsyncToHistory(latestItem)); } for (Future<?> future : futures) { future.get(10000L, TimeUnit.MILLISECONDS); } Map<String, FileTrackerHistoryItem> agentStatusMap = memory .getLastestAgentStatus(); assertNotNull(agentStatusMap); assertNotNull(agentStatusMap.get(latestItem.getAgent())); assertEquals(agentStatusMap.get(latestItem.getAgent()), latestItem); }
/** * Tests that the latest agent status method returns the latest item. */ @Test public void testLatestAgentStatus() { MultiMap<String, FileTrackerHistoryItem> map = instance .getMultiMap("FileTrackerHistoryMemoryImplTest.testLatestMap"); IMap<String, FileTrackerHistoryItem> map1 = instance .getMap("FileTrackerHistoryMemoryImplTest.testLatestMap2"); FileTrackerHistoryMemoryImpl memory = new FileTrackerHistoryMemoryImpl( map, map1, 1); FileTrackerHistoryItem latestItem = null; for (int i = 0; i < 100; i++) { latestItem = new FileTrackerHistoryItem(new Date(), "test1", "collector1", FileTrackerHistoryItem.STATUS.OK); memory.addToHistory(latestItem); } Map<String, FileTrackerHistoryItem> agentStatusMap = memory .getLastestAgentStatus(); assertNotNull(agentStatusMap); assertNotNull(agentStatusMap.get(latestItem.getAgent())); assertEquals(agentStatusMap.get(latestItem.getAgent()), latestItem); }
/** * Test non async put */ @Test public void testPut() { MultiMap<String, FileTrackerHistoryItem> map = instance .getMultiMap("FileTrackerHistoryMemoryImplTest.testPutSyncMap"); IMap<String, FileTrackerHistoryItem> map1 = instance .getMap("FileTrackerHistoryMemoryImplTest.testPutSyncMap2"); FileTrackerHistoryMemoryImpl memory = new FileTrackerHistoryMemoryImpl( map, map1, 1); FileTrackerHistoryItem item = new FileTrackerHistoryItem(new Date(), "test1", "collector1", FileTrackerHistoryItem.STATUS.OK); memory.addToHistory(item); Collection<FileTrackerHistoryItem> foundItems = memory.getAgentHistory( item.getAgent(), 0, 1); assertNotNull(foundItems); assertEquals(1, foundItems.size()); FileTrackerHistoryItem foundItem = foundItems.iterator().next(); assertEquals(item, foundItem); }
/** * Test put async to map * * @throws InterruptedException * @throws ExecutionException * @throws TimeoutException */ @Test public void testPutAsync() throws InterruptedException, ExecutionException, TimeoutException { MultiMap<String, FileTrackerHistoryItem> map = instance .getMultiMap("FileTrackerHistoryMemoryImplTest.testPutAsyncMap"); IMap<String, FileTrackerHistoryItem> map1 = instance .getMap("FileTrackerHistoryMemoryImplTest.testPutAsyncMap2"); FileTrackerHistoryMemoryImpl memory = new FileTrackerHistoryMemoryImpl( map, map1, 1); FileTrackerHistoryItem item = new FileTrackerHistoryItem(new Date(), "test1", "collector1", FileTrackerHistoryItem.STATUS.OK); Future<?> future = memory.addAsyncToHistory(item); future.get(10000L, TimeUnit.MILLISECONDS); Collection<FileTrackerHistoryItem> foundItems = memory.getAgentHistory( item.getAgent(), 0, 1); assertNotNull(foundItems); assertEquals(1, foundItems.size()); FileTrackerHistoryItem foundItem = foundItems.iterator().next(); assertEquals(item, foundItem); }
@Override @SuppressWarnings("unchecked") public <K, V> MultiMap<K, V> getIMultiMap(String name) { name = Objects.requireNonNull(name); final MultiMap<K, V> valu = getBeanSafely(name, MultiMap.class); if (null != valu) { return valu; } return hz().getMultiMap(name); }
public void store( String key, CacheEntry entry ) { logger.fine( "Store: " + key ); IMap<String, CacheEntry> cache = getCache(); cache.put( key, entry ); String[] tags = entry.getTags(); if( ( tags != null ) && ( tags.length > 0 ) ) { MultiMap<String, String> tagMap = getTagMap(); for( String tag : tags ) tagMap.put( tag, key ); } }
public void invalidate( String tag ) { MultiMap<String, String> tagMap = getTagMap(); Collection<String> tagged = tagMap.remove( tag ); if( tagged != null ) { IMap<String, CacheEntry> cache = getCache(); for( String key : tagged ) { logger.fine( "Invalidate " + tag + ": " + key ); cache.remove( key ); } } }
@Override public void map(Comparable key, Double value, Context<Feature, Double> context) { MultiMap<Comparable, FeatureConfidenceTuple> multiMap = hazelcastInstance.getMultiMap("traindata-"+this.mapKey); Collection<FeatureConfidenceTuple> values = multiMap.get(key); for (FeatureConfidenceTuple featureConfidenceTuple : values) { context.emit(featureConfidenceTuple.getFeature(), (featureConfidenceTuple.getConfidenceCoefficient() * value.doubleValue())); } }
@Override public void map(String key, Double value, Context<String, Double> context) { MultiMap<String, KeyValueTuple> multiMap = hazelcastInstance.getMultiMap("keyValueTuples"); Collection<KeyValueTuple> values = multiMap.get(key); for (KeyValueTuple keyValueTuple : values) { context.emit(keyValueTuple.getKey(), (keyValueTuple.getValue()*value.doubleValue()) ); } }
public void remove() { if (BaseManager.getInstanceType(name) == Instance.InstanceType.MULTIMAP) { if (operation == CONCURRENT_MAP_ITERATE_KEYS) { ((MultiMap) concurrentMapManager.node.factory.getOrCreateProxyByName(name)).remove(entry.getKey(), null); } else { ((MultiMap) concurrentMapManager.node.factory.getOrCreateProxyByName(name)).remove(entry.getKey(), entry.getValue()); } } else { ((IRemoveAwareProxy) concurrentMapManager.node.factory.getOrCreateProxyByName(name)).removeKey(entry.getKey()); } it.remove(); }
@Override public boolean equals(Object o) { if (o instanceof MultiMap) { return getName().equals(((MultiMap) o).getName()); } return false; }
@Test public void removeFromMultiMap() { HazelcastClient hClient = getHazelcastClient(); MultiMap<String, Integer> multiMap = hClient.getMultiMap("removeFromMultiMap"); assertTrue(multiMap.put("a", 1)); assertTrue(multiMap.remove("a", 1)); }
@Test public void containsKey() { HazelcastClient hClient = getHazelcastClient(); MultiMap<String, Integer> multiMap = hClient.getMultiMap("containsKey"); assertFalse(multiMap.containsKey("a")); assertTrue(multiMap.put("a", 1)); assertTrue(multiMap.containsKey("a")); }
@Test public void containsValue() { HazelcastClient hClient = getHazelcastClient(); MultiMap<String, Integer> multiMap = hClient.getMultiMap("containsValue"); assertFalse(multiMap.containsValue(1)); assertTrue(multiMap.put("a", 1)); assertTrue(multiMap.containsValue(1)); }
@Test public void containsEntry() { HazelcastClient hClient = getHazelcastClient(); MultiMap<String, Integer> multiMap = hClient.getMultiMap("containsEntry"); assertFalse(multiMap.containsEntry("a", 1)); assertTrue(multiMap.put("a", 1)); assertTrue(multiMap.containsEntry("a", 1)); assertFalse(multiMap.containsEntry("a", 2)); }
@Test public void size() { HazelcastClient hClient = getHazelcastClient(); MultiMap<String, Integer> multiMap = hClient.getMultiMap("size"); assertEquals(0, multiMap.size()); assertTrue(multiMap.put("a", 1)); assertEquals(1, multiMap.size()); assertTrue(multiMap.put("a", 2)); assertEquals(2, multiMap.size()); }
@Test public void testMultiMapGetNameAndType() { HazelcastClient hClient = getHazelcastClient(); MultiMap<String, String> map = hClient.getMultiMap("testMultiMapGetNameAndType"); assertEquals("testMultiMapGetNameAndType", map.getName()); assertTrue(map.getInstanceType().isMultiMap()); }
@Test public void testMultiMapClear() { HazelcastClient hClient = getHazelcastClient(); MultiMap<String, String> map = hClient.getMultiMap("testMultiMapClear"); map.put("Hello", "World"); assertEquals(1, map.size()); map.clear(); assertEquals(0, map.size()); }