public void removeCoolDown(final int sessionId, final int streamId) { final StreamInterest streamInterest = streamInterestByIdMap.get(streamId); if (null != streamInterest) { final SessionInterest sessionInterest = streamInterest.sessionInterestByIdMap.get(sessionId); if (null != sessionInterest && ON_COOL_DOWN == sessionInterest.state) { streamInterest.sessionInterestByIdMap.remove(sessionId); } } }
public void addSubscription(final int streamId) { final StreamInterest streamInterest = streamInterestByIdMap.get(streamId); if (null == streamInterest) { streamInterestByIdMap.put(streamId, new StreamInterest(true)); } else if (!streamInterest.isForAllSessions) { streamInterest.isForAllSessions = true; for (final int sessionId : streamInterest.sessionInterestByIdMap.keySet()) { final SessionInterest sessionInterest = streamInterest.sessionInterestByIdMap.get(sessionId); if (NO_INTEREST == sessionInterest.state) { streamInterest.sessionInterestByIdMap.remove(sessionId); } } } }
private void updateMemberEndpoints(final String memberEndpoints, final int leaderMemberId) final Int2ObjectHashMap<MemberEndpoint> tempMap = new Int2ObjectHashMap<>(); tempMap.put(memberId, new MemberEndpoint(memberId, endpoint.substring(i + 1))); final MemberEndpoint existingLeaderEndpoint = endpointByMemberIdMap.get(leaderMemberId); final MemberEndpoint leaderEndpoint = tempMap.get(leaderMemberId); endpointByMemberIdMap.values().forEach(MemberEndpoint::disconnect); endpointByMemberIdMap = tempMap;
private BufferBuilder getBufferBuilder(final int sessionId) { BufferBuilder bufferBuilder = builderBySessionIdMap.get(sessionId); if (null == bufferBuilder) { bufferBuilder = new BufferBuilder(initialBufferLength, isDirectByteBuffer); builderBySessionIdMap.put(sessionId, bufferBuilder); } return bufferBuilder; } }
final Int2ObjectHashMap<String> map = new Int2ObjectHashMap<String>() map.put(0, null); map.put(1, "one"); assertThat(map.get(0), nullValue()); assertThat(map.get(1), is("one")); assertThat(map.get(-1), nullValue()); assertThat(map.containsKey(0), is(true)); assertThat(map.containsKey(1), is(true)); assertThat(map.containsKey(-1), is(false)); assertThat(map.values(), containsInAnyOrder(null, "one")); assertThat(map.keySet(), containsInAnyOrder(0, 1)); assertThat(map.size(), is(2)); map.remove(0); assertThat(map.get(0), nullValue()); assertThat(map.get(1), is("one")); assertThat(map.get(-1), nullValue()); assertThat(map.containsKey(0), is(false)); assertThat(map.containsKey(1), is(true)); assertThat(map.containsKey(-1), is(false)); assertThat(map.size(), is(1));
public PartitionTestClient partitionClient(final int partitionId) { if (!testPartitionClients.containsKey(partitionId)) { testPartitionClients.put(partitionId, new PartitionTestClient(this, partitionId)); } return testPartitionClients.get(partitionId); }
public void addSubscription(final int streamId, final int sessionId) { StreamInterest streamInterest = streamInterestByIdMap.get(streamId); if (null == streamInterest) { streamInterest = new StreamInterest(false); streamInterestByIdMap.put(streamId, streamInterest); } streamInterest.subscribedSessionIds.add(sessionId); final SessionInterest sessionInterest = streamInterest.sessionInterestByIdMap.get(sessionId); if (null != sessionInterest && NO_INTEREST == sessionInterest.state) { streamInterest.sessionInterestByIdMap.remove(sessionId); } }
/** * Get the {@link SystemCounterDescriptor} for a given id. * * @param id for the descriptor. * @return the descriptor if found otherwise null. */ public static SystemCounterDescriptor get(final int id) { return DESCRIPTOR_BY_ID_MAP.get(id); }
@Test public void shouldClearCollection() { for (int i = 0; i < 15; i++) { intToObjectMap.put(i, Integer.toString(i)); } assertThat(intToObjectMap.size(), is(15)); assertThat(intToObjectMap.get(1), is("1")); intToObjectMap.clear(); assertThat(intToObjectMap.size(), is(0)); Assert.assertNull(intToObjectMap.get(1)); }
public void removeSubscription(final int streamId) { final StreamInterest streamInterest = streamInterestByIdMap.get(streamId); if (null == streamInterest) { throw new UnknownSubscriptionException("No subscription registered on stream " + streamId); } for (final int sessionId : streamInterest.sessionInterestByIdMap.keySet()) { final SessionInterest sessionInterest = streamInterest.sessionInterestByIdMap.get(sessionId); if (!streamInterest.subscribedSessionIds.contains(sessionId)) { if (null != sessionInterest.image) { sessionInterest.image.ifActiveGoInactive(); } streamInterest.sessionInterestByIdMap.remove(sessionId); } } streamInterest.isForAllSessions = false; if (streamInterest.subscribedSessionIds.isEmpty()) { streamInterestByIdMap.remove(streamId); } }
@Test public void shouldReplaceExistingValueForTheSameKey() { final int key = 7; final String value = "Seven"; intToObjectMap.put(key, value); final String newValue = "New Seven"; final String oldValue = intToObjectMap.put(key, newValue); assertThat(intToObjectMap.get(key), is(newValue)); assertThat(oldValue, is(value)); assertThat(intToObjectMap.size(), is(1)); }
@Test public void shouldRemoveEntry() { final int key = 7; final String value = "Seven"; intToObjectMap.put(key, value); Assert.assertTrue(intToObjectMap.containsKey(key)); intToObjectMap.remove(key); Assert.assertFalse(intToObjectMap.containsKey(key)); }
@Test public void shouldHandleCollisionAndThenLinearProbe() { final float loadFactor = 0.5f; final int initialCapacity = 32; final Int2ObjectHashMap<String> map = newMap(loadFactor, initialCapacity); final int key = 7; final String value = "Seven"; map.put(key, value); final int collisionKey = key + map.capacity(); final String collisionValue = Integer.toString(collisionKey); map.put(collisionKey, collisionValue); assertThat(map.get(key), is(value)); assertThat(map.get(collisionKey), is(collisionValue)); assertThat((double)loadFactor, closeTo(map.loadFactor(), 0.0f)); }
/** * {@inheritDoc} */ public V put(final Integer key, final V value) { return put(key.intValue(), value); }
@Test public void shouldCompactCollection() { final int totalItems = 50; for (int i = 0; i < totalItems; i++) { intToObjectMap.put(i, Integer.toString(i)); } for (int i = 0, limit = totalItems - 4; i < limit; i++) { intToObjectMap.remove(i); } final int capacityBeforeCompaction = intToObjectMap.capacity(); intToObjectMap.compact(); assertThat(intToObjectMap.capacity(), lessThan(capacityBeforeCompaction)); }
/** * Free an existing session buffer to reduce memory pressure when an image goes inactive or no more * large messages are expected. * * @param sessionId to have its buffer freed * @return true if a buffer has been freed otherwise false. */ public boolean freeSessionBuffer(final int sessionId) { return null != builderBySessionIdMap.remove(sessionId); }
@Test public void shouldRemoveEntryAndCompactCollisionChain() { final int key = 12; final String value = "12"; intToObjectMap.put(key, value); intToObjectMap.put(13, "13"); final int collisionKey = key + intToObjectMap.capacity(); final String collisionValue = Integer.toString(collisionKey); intToObjectMap.put(collisionKey, collisionValue); intToObjectMap.put(14, "14"); assertThat(intToObjectMap.remove(key), is(value)); }
for (final MemberEndpoint member : endpointByMemberIdMap.values()) for (final MemberEndpoint member : endpointByMemberIdMap.values()) final long clusterSessionId = openSession(deadlineNs, poller, encodedCredentials); endpointByMemberIdMap.get(leaderMemberId).publication = null; endpointByMemberIdMap.values().forEach(MemberEndpoint::disconnect);
public Map<Integer, Integer> create(final Object... entries) { final Int2ObjectHashMap<Integer> map = new Int2ObjectHashMap<>( entries.length * 2, Hashing.DEFAULT_LOAD_FACTOR, false); for (final Object o : entries) { @SuppressWarnings("unchecked") final Map.Entry<Integer, Integer> e = (Map.Entry<Integer, Integer>)o; map.put(e.getKey(), e.getValue()); } return map; }