Jonathan Hart

Pulled internal event classes out of EventuallyConsistentMap class

for clarity.

Also switched to using Pair instead of my own Entry class for returning
entrySet().

Change-Id: I78d84f9fe931257d4ffe1d48c9d0de25f18c638f
......@@ -15,9 +15,8 @@
*/
package org.onosproject.store.impl;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.onlab.util.KryoNamespace;
import org.onosproject.cluster.ClusterService;
import org.onosproject.cluster.ControllerNode;
......@@ -38,7 +37,6 @@ import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
......@@ -351,7 +349,7 @@ public class EventuallyConsistentMapImpl<K, V>
checkState(!destroyed, mapName + ERROR_DESTROYED);
return items.entrySet().stream()
.map(e -> new Entry(e.getKey(), e.getValue().value()))
.map(e -> Pair.of(e.getKey(), e.getValue().value()))
.collect(Collectors.toSet());
}
......@@ -415,49 +413,6 @@ public class EventuallyConsistentMapImpl<K, V>
clusterCommunicator.unicast(message, peer);
}
private final class Entry implements Map.Entry<K, V> {
private final K key;
private final V value;
public Entry(K key, V value) {
this.key = key;
this.value = value;
}
@Override
public K getKey() {
return key;
}
@Override
public V getValue() {
return value;
}
@Override
public V setValue(V value) {
throw new UnsupportedOperationException();
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Map.Entry)) {
return false;
}
Map.Entry that = (Map.Entry) o;
return Objects.equals(this.key, that.getKey()) &&
Objects.equals(this.value, that.getValue());
}
@Override
public int hashCode() {
return Objects.hash(key, value);
}
}
private final class SendAdvertisementTask implements Runnable {
@Override
public void run() {
......@@ -754,112 +709,4 @@ public class EventuallyConsistentMapImpl<K, V>
}
}
static final class InternalPutEvent<K, V> {
private final List<PutEntry<K, V>> entries;
public InternalPutEvent(K key, V value, Timestamp timestamp) {
entries = ImmutableList.of(new PutEntry<>(key, value, timestamp));
}
public InternalPutEvent(List<PutEntry<K, V>> entries) {
this.entries = checkNotNull(entries);
}
// Needed for serialization.
@SuppressWarnings("unused")
private InternalPutEvent() {
entries = null;
}
public List<PutEntry<K, V>> entries() {
return entries;
}
}
static final class PutEntry<K, V> {
private final K key;
private final V value;
private final Timestamp timestamp;
public PutEntry(K key, V value, Timestamp timestamp) {
this.key = checkNotNull(key);
this.value = checkNotNull(value);
this.timestamp = checkNotNull(timestamp);
}
// Needed for serialization.
@SuppressWarnings("unused")
private PutEntry() {
this.key = null;
this.value = null;
this.timestamp = null;
}
public K key() {
return key;
}
public V value() {
return value;
}
public Timestamp timestamp() {
return timestamp;
}
public String toString() {
return MoreObjects.toStringHelper(getClass())
.add("key", key)
.add("value", value)
.add("timestamp", timestamp)
.toString();
}
}
static final class InternalRemoveEvent<K> {
private final List<RemoveEntry<K>> entries;
public InternalRemoveEvent(K key, Timestamp timestamp) {
entries = ImmutableList.of(new RemoveEntry<>(key, timestamp));
}
public InternalRemoveEvent(List<RemoveEntry<K>> entries) {
this.entries = checkNotNull(entries);
}
// Needed for serialization.
@SuppressWarnings("unused")
private InternalRemoveEvent() {
entries = null;
}
public List<RemoveEntry<K>> entries() {
return entries;
}
}
static final class RemoveEntry<K> {
private final K key;
private final Timestamp timestamp;
public RemoveEntry(K key, Timestamp timestamp) {
this.key = checkNotNull(key);
this.timestamp = checkNotNull(timestamp);
}
// Needed for serialization.
@SuppressWarnings("unused")
private RemoveEntry() {
this.key = null;
this.timestamp = null;
}
public K key() {
return key;
}
public Timestamp timestamp() {
return timestamp;
}
}
}
......
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.store.impl;
import com.google.common.collect.ImmutableList;
import org.onosproject.store.Timestamp;
import java.util.List;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Internal inter-instance event used by EventuallyConsistentMap for PUT events.
*/
final class InternalPutEvent<K, V> {
private final List<PutEntry<K, V>> entries;
/**
* Creates a put event for a single key.
*
* @param key key the event concerns
* @param value value of the key
* @param timestamp timestamp of the event
*/
public InternalPutEvent(K key, V value, Timestamp timestamp) {
entries = ImmutableList.of(new PutEntry<>(key, value, timestamp));
}
/**
* Creates a put event for multiple keys.
*
* @param entries list of put entries to send an event for
*/
public InternalPutEvent(List<PutEntry<K, V>> entries) {
this.entries = checkNotNull(entries);
}
// Needed for serialization.
@SuppressWarnings("unused")
private InternalPutEvent() {
entries = null;
}
/**
* Returns the list of put entries this event concerns.
*
* @return list of put entries
*/
public List<PutEntry<K, V>> entries() {
return entries;
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.store.impl;
import com.google.common.collect.ImmutableList;
import org.onosproject.store.Timestamp;
import java.util.List;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Internal inter-instance event used by EventuallyConsistentMap for REMOVE
* events.
*/
final class InternalRemoveEvent<K> {
private final List<RemoveEntry<K>> entries;
/**
* Creates a remove event for a single key.
*
* @param key key the event concerns
* @param timestamp timestamp of the event
*/
public InternalRemoveEvent(K key, Timestamp timestamp) {
entries = ImmutableList.of(new RemoveEntry<>(key, timestamp));
}
/**
* Creates a remove event for multiple keys.
*
* @param entries list of remove entries to send an event for
*/
public InternalRemoveEvent(List<RemoveEntry<K>> entries) {
this.entries = checkNotNull(entries);
}
// Needed for serialization.
@SuppressWarnings("unused")
private InternalRemoveEvent() {
entries = null;
}
/**
* Returns the list of remove entries this event concerns.
*
* @return list of remove entries
*/
public List<RemoveEntry<K>> entries() {
return entries;
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.store.impl;
import com.google.common.base.MoreObjects;
import org.onosproject.store.Timestamp;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Describes a single put event in an EventuallyConsistentMap.
*/
final class PutEntry<K, V> {
private final K key;
private final V value;
private final Timestamp timestamp;
/**
* Creates a new put entry.
*
* @param key key of the entry
* @param value value of the entry
* @param timestamp timestamp of the put event
*/
public PutEntry(K key, V value, Timestamp timestamp) {
this.key = checkNotNull(key);
this.value = checkNotNull(value);
this.timestamp = checkNotNull(timestamp);
}
// Needed for serialization.
@SuppressWarnings("unused")
private PutEntry() {
this.key = null;
this.value = null;
this.timestamp = null;
}
/**
* Returns the key of the entry.
*
* @return the key
*/
public K key() {
return key;
}
/**
* Returns the value of the entry.
*
* @return the value
*/
public V value() {
return value;
}
/**
* Returns the timestamp of the event.
*
* @return the timestamp
*/
public Timestamp timestamp() {
return timestamp;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.add("key", key)
.add("value", value)
.add("timestamp", timestamp)
.toString();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.store.impl;
import com.google.common.base.MoreObjects;
import org.onosproject.store.Timestamp;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Describes a single remove event in an EventuallyConsistentMap.
*/
final class RemoveEntry<K> {
private final K key;
private final Timestamp timestamp;
/**
* Creates a new remove entry.
*
* @param key key of the entry
* @param timestamp timestamp of the remove event
*/
public RemoveEntry(K key, Timestamp timestamp) {
this.key = checkNotNull(key);
this.timestamp = checkNotNull(timestamp);
}
// Needed for serialization.
@SuppressWarnings("unused")
private RemoveEntry() {
this.key = null;
this.timestamp = null;
}
/**
* Returns the key of the entry.
*
* @return the key
*/
public K key() {
return key;
}
/**
* Returns the timestamp of the event.
*
* @return the timestamp
*/
public Timestamp timestamp() {
return timestamp;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.add("key", key)
.add("timestamp", timestamp)
.toString();
}
}
......@@ -100,11 +100,11 @@ public class EventuallyConsistentMapImplTest {
.register(TestTimestamp.class)
// Below is the classes that the map internally registers
.register(WallClockTimestamp.class)
.register(EventuallyConsistentMapImpl.PutEntry.class)
.register(EventuallyConsistentMapImpl.RemoveEntry.class)
.register(PutEntry.class)
.register(RemoveEntry.class)
.register(ArrayList.class)
.register(EventuallyConsistentMapImpl.InternalPutEvent.class)
.register(EventuallyConsistentMapImpl.InternalRemoveEvent.class)
.register(InternalPutEvent.class)
.register(InternalRemoveEvent.class)
.register(AntiEntropyAdvertisement.class)
.register(HashMap.class)
.build();
......@@ -586,9 +586,8 @@ public class EventuallyConsistentMapImplTest {
}
private ClusterMessage generatePutMessage(String key, String value, Timestamp timestamp) {
EventuallyConsistentMapImpl.InternalPutEvent<String, String> event =
new EventuallyConsistentMapImpl.InternalPutEvent<>(
key, value, timestamp);
InternalPutEvent<String, String> event =
new InternalPutEvent<>(key, value, timestamp);
return new ClusterMessage(
clusterService.getLocalNode().id(), PUT_MESSAGE_SUBJECT,
......@@ -596,21 +595,18 @@ public class EventuallyConsistentMapImplTest {
}
private ClusterMessage generatePutMessage(String key1, String value1, String key2, String value2) {
ArrayList<EventuallyConsistentMapImpl.PutEntry<String, String>> list = new ArrayList<>();
ArrayList<PutEntry<String, String>> list = new ArrayList<>();
Timestamp timestamp1 = clockService.peek(1);
Timestamp timestamp2 = clockService.peek(2);
EventuallyConsistentMapImpl.PutEntry<String, String> pe1
= new EventuallyConsistentMapImpl.PutEntry<>(key1, value1, timestamp1);
EventuallyConsistentMapImpl.PutEntry<String, String> pe2
= new EventuallyConsistentMapImpl.PutEntry<>(key2, value2, timestamp2);
PutEntry<String, String> pe1 = new PutEntry<>(key1, value1, timestamp1);
PutEntry<String, String> pe2 = new PutEntry<>(key2, value2, timestamp2);
list.add(pe1);
list.add(pe2);
EventuallyConsistentMapImpl.InternalPutEvent<String, String> event
= new EventuallyConsistentMapImpl.InternalPutEvent<>(list);
InternalPutEvent<String, String> event = new InternalPutEvent<>(list);
return new ClusterMessage(
clusterService.getLocalNode().id(), PUT_MESSAGE_SUBJECT,
......@@ -618,9 +614,7 @@ public class EventuallyConsistentMapImplTest {
}
private ClusterMessage generateRemoveMessage(String key, Timestamp timestamp) {
EventuallyConsistentMapImpl.InternalRemoveEvent<String> event =
new EventuallyConsistentMapImpl.InternalRemoveEvent<>(
key, timestamp);
InternalRemoveEvent<String> event = new InternalRemoveEvent<>(key, timestamp);
return new ClusterMessage(
clusterService.getLocalNode().id(), REMOVE_MESSAGE_SUBJECT,
......@@ -628,21 +622,18 @@ public class EventuallyConsistentMapImplTest {
}
private ClusterMessage generateRemoveMessage(String key1, String key2) {
ArrayList<EventuallyConsistentMapImpl.RemoveEntry<String>> list = new ArrayList<>();
ArrayList<RemoveEntry<String>> list = new ArrayList<>();
Timestamp timestamp1 = clockService.peek(1);
Timestamp timestamp2 = clockService.peek(2);
EventuallyConsistentMapImpl.RemoveEntry<String> re1
= new EventuallyConsistentMapImpl.RemoveEntry<>(key1, timestamp1);
EventuallyConsistentMapImpl.RemoveEntry<String> re2
= new EventuallyConsistentMapImpl.RemoveEntry<>(key2, timestamp2);
RemoveEntry<String> re1 = new RemoveEntry<>(key1, timestamp1);
RemoveEntry<String> re2 = new RemoveEntry<>(key2, timestamp2);
list.add(re1);
list.add(re2);
EventuallyConsistentMapImpl.InternalRemoveEvent<String> event
= new EventuallyConsistentMapImpl.InternalRemoveEvent<>(list);
InternalRemoveEvent<String> event = new InternalRemoveEvent<>(list);
return new ClusterMessage(
clusterService.getLocalNode().id(), REMOVE_MESSAGE_SUBJECT,
......