Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.store.impl; |
| 17 | |
| 18 | import java.util.Collection; |
| 19 | import java.util.Map; |
| 20 | import java.util.Set; |
| 21 | |
| 22 | /** |
| 23 | * A distributed, eventually consistent map. |
| 24 | * |
| 25 | * This map does not offer read after writes consistency. Operations are |
| 26 | * serialized via the timestamps issued by the clock service. If two updates |
| 27 | * are in conflict, the update with the more recent timestamp will endure. |
| 28 | * |
| 29 | * The interface is mostly similar to {@link java.util.Map} with some minor |
| 30 | * semantic changes and the addition of a listener framework (because the map |
| 31 | * can be mutated by clients on other instances, not only through the local Java |
| 32 | * API). |
| 33 | * |
| 34 | * Clients are expected to register an |
| 35 | * {@link org.onosproject.store.impl.EventuallyConsistentMapListener} if they |
| 36 | * are interested in receiving notifications of update to the map. |
| 37 | */ |
| 38 | public interface EventuallyConsistentMap<K, V> { |
| 39 | |
| 40 | /** |
| 41 | * Returns the number of key-value mappings in this map. |
| 42 | * |
| 43 | * @return number of key-value mappings |
| 44 | */ |
| 45 | public int size(); |
| 46 | |
| 47 | /** |
| 48 | * Returns true if this map is empty. |
| 49 | * |
| 50 | * @return true if this map is empty, otherwise false |
| 51 | */ |
| 52 | public boolean isEmpty(); |
| 53 | |
| 54 | /** |
| 55 | * Returns true if the map contains a mapping for the specified key. |
| 56 | * |
| 57 | * @param key the key to check if this map contains |
| 58 | * @return true if this map has a mapping for the key, otherwise false |
| 59 | */ |
| 60 | public boolean containsKey(K key); |
| 61 | |
| 62 | /** |
| 63 | * Returns true if the map contains a mapping from any key to the specified |
| 64 | * value. |
| 65 | * |
| 66 | * @param value the value to check if this map has a mapping for |
| 67 | * @return true if this map has a mapping to this value, otherwise false |
| 68 | */ |
| 69 | public boolean containsValue(V value); |
| 70 | |
| 71 | /** |
| 72 | * Returns the value mapped to the specified key. |
| 73 | * |
| 74 | * @param key the key to look up in this map |
| 75 | * @return the value mapped to the key, or null if no mapping is found |
| 76 | */ |
| 77 | public V get(K key); |
| 78 | |
| 79 | /** |
| 80 | * Associates the specified value to the specified key in this map. |
| 81 | * <p> |
| 82 | * Note: this differs from the specification of {@link java.util.Map} |
| 83 | * because it does not return the previous value associated with the key. |
| 84 | * Clients are expected to register an |
| 85 | * {@link org.onosproject.store.impl.EventuallyConsistentMapListener} if |
| 86 | * they are interested in receiving notification of updates to the map. |
| 87 | * </p> |
| 88 | * |
| 89 | * @param key the key to add a mapping for in this map |
| 90 | * @param value the value to associate with the key in this map |
| 91 | */ |
| 92 | public void put(K key, V value); |
| 93 | |
| 94 | /** |
| 95 | * Removes the mapping associated with the specified key from the map. |
| 96 | * <p> |
| 97 | * Note: this differs from the specification of {@link java.util.Map} |
| 98 | * because it does not return the previous value associated with the key. |
| 99 | * Clients are expected to register an |
| 100 | * {@link org.onosproject.store.impl.EventuallyConsistentMapListener} if |
| 101 | * they are interested in receiving notification of updates to the map. |
| 102 | * </p> |
| 103 | * |
| 104 | * @param key the key to remove the mapping for |
| 105 | */ |
| 106 | public void remove(K key); |
| 107 | |
| 108 | /** |
| 109 | * Adds mappings for all key-value pairs in the specified map to this map. |
| 110 | * <p> |
| 111 | * This will be more efficient in communication than calling individual put |
| 112 | * operations. |
| 113 | * </p> |
| 114 | * |
| 115 | * @param m a map of values to add to this map |
| 116 | */ |
| 117 | public void putAll(Map<? extends K, ? extends V> m); |
| 118 | |
| 119 | /** |
| 120 | * Removes all mappings from this map. |
| 121 | */ |
| 122 | public void clear(); |
| 123 | |
| 124 | /** |
| 125 | * Returns a set of the keys in this map. Changes to the set are not |
| 126 | * reflected back to the map. |
| 127 | * |
| 128 | * @return set of keys in the map |
| 129 | */ |
| 130 | public Set<K> keySet(); |
| 131 | |
| 132 | /** |
| 133 | * Returns a collections of values in this map. Changes to the collection |
| 134 | * are not reflected back to the map. |
| 135 | * |
| 136 | * @return collection of values in the map |
| 137 | */ |
| 138 | public Collection<V> values(); |
| 139 | |
| 140 | /** |
| 141 | * Returns a set of mappings contained in this map. Changes to the set are |
| 142 | * not reflected back to the map. |
| 143 | * |
| 144 | * @return set of key-value mappings in this map |
| 145 | */ |
| 146 | public Set<Map.Entry<K, V>> entrySet(); |
| 147 | |
| 148 | /** |
| 149 | * Adds the specified listener to the map which will be notified whenever |
| 150 | * the mappings in the map are changed. |
| 151 | * |
| 152 | * @param listener listener to register for events |
| 153 | */ |
| 154 | public void addListener(EventuallyConsistentMapListener listener); |
| 155 | |
| 156 | /** |
| 157 | * Removes the specified listener from the map such that it will no longer |
| 158 | * receive change notifications. |
| 159 | * |
| 160 | * @param listener listener to deregister for events |
| 161 | */ |
| 162 | public void removeListener(EventuallyConsistentMapListener listener); |
| 163 | |
| 164 | /** |
| 165 | * Shuts down the map and breaks communication between different instances. |
| 166 | * This allows the map objects to be cleaned up and garbage collected. |
| 167 | * Calls to any methods on the map subsequent to calling destroy() will |
| 168 | * throw a {@link java.lang.RuntimeException}. |
| 169 | */ |
| 170 | public void destroy(); |
| 171 | } |