Skip to content

Commit

Permalink
Use Set instead of List for TokenToReg index
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Mar 1, 2018
1 parent b7c0257 commit 2112e13
Showing 1 changed file with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -64,7 +66,7 @@ public class InMemoryRegistrationStore implements CaliforniumRegistrationStore,
// Data structure
private final Map<String /* end-point */, Registration> regsByEp = new HashMap<>();
private Map<Token, org.eclipse.californium.core.observe.Observation> obsByToken = new HashMap<>();
private Map<String, List<Token>> tokensByRegId = new HashMap<>();
private Map<String, Set<Token>> tokensByRegId = new HashMap<>();

private final ReadWriteLock lock = new ReentrantReadWriteLock();

Expand Down Expand Up @@ -305,7 +307,7 @@ private org.eclipse.californium.core.observe.Observation add(Token token, org.ec
previousObservation = obsByToken.put(token, obs);
}
if (!tokensByRegId.containsKey(registrationId)) {
tokensByRegId.put(registrationId, new ArrayList<Token>());
tokensByRegId.put(registrationId, new HashSet<Token>());
}
tokensByRegId.get(registrationId).add(token);

Expand Down Expand Up @@ -367,7 +369,7 @@ private void unsafeRemoveObservation(Token observationId) {

if (removed != null) {
String registrationId = ObserveUtil.extractRegistrationId(removed);
List<Token> tokens = tokensByRegId.get(registrationId);
Set<Token> tokens = tokensByRegId.get(registrationId);
tokens.remove(observationId);
if (tokens.isEmpty()) {
tokensByRegId.remove(registrationId);
Expand All @@ -377,7 +379,7 @@ private void unsafeRemoveObservation(Token observationId) {

private Collection<Observation> unsafeRemoveAllObservations(String registrationId) {
Collection<Observation> removed = new ArrayList<>();
List<Token> tokens = tokensByRegId.get(registrationId);
Set<Token> tokens = tokensByRegId.get(registrationId);
if (tokens != null) {
for (Token token : tokens) {
Observation observationRemoved = build(obsByToken.remove(token));
Expand All @@ -392,7 +394,7 @@ private Collection<Observation> unsafeRemoveAllObservations(String registrationI

private Collection<Observation> unsafeGetObservations(String registrationId) {
Collection<Observation> result = new ArrayList<>();
List<Token> tokens = tokensByRegId.get(registrationId);
Set<Token> tokens = tokensByRegId.get(registrationId);
if (tokens != null) {
for (Token token : tokens) {
Observation obs = build(unsafeGetObservation(token));
Expand Down

0 comments on commit 2112e13

Please sign in to comment.