Skip to content

Commit

Permalink
Add registrationId index in InMemoryRegistrationStore
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Jun 7, 2018
1 parent 947e801 commit 4b7a6e2
Showing 1 changed file with 8 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public class InMemoryRegistrationStore implements CaliforniumRegistrationStore,
// Data structure
private final Map<String /* end-point */, Registration> regsByEp = new HashMap<>();
private final Map<InetSocketAddress, Registration> regsByAddr = new HashMap<>();
private final Map<String /* reg-id */, Registration> regsByRegId = new HashMap<>();
private Map<Token, org.eclipse.californium.core.observe.Observation> obsByToken = new HashMap<>();
private Map<String, Set<Token>> tokensByRegId = new HashMap<>();

Expand Down Expand Up @@ -101,12 +102,14 @@ public Deregistration addRegistration(Registration registration) {
lock.writeLock().lock();

Registration registrationRemoved = regsByEp.put(registration.getEndpoint(), registration);
regsByRegId.put(registration.getId(), registration);
// If a registration is already associated to this address we don't care as we only want to keep the most
// recent binding.
regsByAddr.put(registration.getSocketAddress(), registration);
if (registrationRemoved != null) {
Collection<Observation> observationsRemoved = unsafeRemoveAllObservations(registrationRemoved.getId());
removeFromMap(regsByAddr, registrationRemoved.getSocketAddress(), registrationRemoved);
removeFromMap(regsByRegId, registrationRemoved.getId(), registrationRemoved);
return new Deregistration(registrationRemoved, observationsRemoved);
}
} finally {
Expand All @@ -130,6 +133,9 @@ public UpdatedRegistration updateRegistration(RegistrationUpdate update) {
// recent binding.
regsByAddr.put(updatedRegistration.getSocketAddress(), updatedRegistration);
removeFromMap(regsByAddr, registration.getSocketAddress(), registration);

regsByRegId.put(updatedRegistration.getId(), updatedRegistration);

return new UpdatedRegistration(registration, updatedRegistration);
}
} finally {
Expand All @@ -141,15 +147,7 @@ public UpdatedRegistration updateRegistration(RegistrationUpdate update) {
public Registration getRegistration(String registrationId) {
try {
lock.readLock().lock();
// TODO we should create an index instead of iterate all over the collection
if (registrationId != null) {
for (Registration registration : regsByEp.values()) {
if (registrationId.equals(registration.getId())) {
return registration;
}
}
}
return null;
return regsByRegId.get(registrationId);
} finally {
lock.readLock().unlock();
}
Expand Down Expand Up @@ -195,6 +193,7 @@ public Deregistration removeRegistration(String registrationId) {
Collection<Observation> observationsRemoved = unsafeRemoveAllObservations(registration.getId());
regsByEp.remove(registration.getEndpoint());
removeFromMap(regsByAddr, registration.getSocketAddress(), registration);
removeFromMap(regsByRegId, registration.getId(), registration);
return new Deregistration(registration, observationsRemoved);
}
return null;
Expand Down

0 comments on commit 4b7a6e2

Please sign in to comment.