Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-present 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 | |
| 17 | package org.onosproject.store.primitives.resources.impl; |
| 18 | |
| 19 | import java.util.ArrayList; |
| 20 | import java.util.Collection; |
| 21 | |
| 22 | import com.google.common.base.MoreObjects; |
| 23 | import com.google.common.collect.Maps; |
| 24 | import io.atomix.protocols.raft.operation.OperationId; |
| 25 | import io.atomix.protocols.raft.operation.OperationType; |
| 26 | import org.onlab.util.KryoNamespace; |
| 27 | import org.onlab.util.Match; |
| 28 | import org.onosproject.store.serializers.KryoNamespaces; |
| 29 | import org.onosproject.store.service.Versioned; |
| 30 | |
| 31 | import static com.google.common.base.Preconditions.checkNotNull; |
| 32 | |
| 33 | /** |
| 34 | * AsyncConsistentMultimap state machine commands. |
| 35 | */ |
| 36 | public enum AtomixConsistentSetMultimapOperations implements OperationId { |
| 37 | GET("get", OperationType.QUERY), |
| 38 | SIZE("size", OperationType.QUERY), |
| 39 | IS_EMPTY("isEmpty", OperationType.QUERY), |
| 40 | CONTAINS_KEY("containsKey", OperationType.QUERY), |
| 41 | CONTAINS_VALUE("containsValue", OperationType.QUERY), |
| 42 | CONTAINS_ENTRY("containsEntry", OperationType.QUERY), |
| 43 | KEY_SET("keySet", OperationType.QUERY), |
| 44 | KEYS("keys", OperationType.QUERY), |
| 45 | VALUES("values", OperationType.QUERY), |
| 46 | ENTRIES("entries", OperationType.QUERY), |
| 47 | PUT("put", OperationType.COMMAND), |
| 48 | REMOVE("remove", OperationType.COMMAND), |
| 49 | REMOVE_ALL("removeAll", OperationType.COMMAND), |
| 50 | REPLACE("replace", OperationType.COMMAND), |
| 51 | CLEAR("clear", OperationType.COMMAND), |
| 52 | ADD_LISTENER("addListener", OperationType.COMMAND), |
| 53 | REMOVE_LISTENER("removeListener", OperationType.COMMAND); |
| 54 | |
| 55 | private final String id; |
| 56 | private final OperationType type; |
| 57 | |
| 58 | AtomixConsistentSetMultimapOperations(String id, OperationType type) { |
| 59 | this.id = id; |
| 60 | this.type = type; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public String id() { |
| 65 | return id; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public OperationType type() { |
| 70 | return type; |
| 71 | } |
| 72 | |
| 73 | public static final KryoNamespace NAMESPACE = KryoNamespace.newBuilder() |
| 74 | .register(KryoNamespaces.BASIC) |
| 75 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) |
| 76 | .register(ContainsEntry.class) |
| 77 | .register(ContainsKey.class) |
| 78 | .register(ContainsValue.class) |
| 79 | .register(Get.class) |
| 80 | .register(MultiRemove.class) |
| 81 | .register(Put.class) |
| 82 | .register(RemoveAll.class) |
| 83 | .register(Replace.class) |
| 84 | .register(Match.class) |
| 85 | .register(Versioned.class) |
| 86 | .register(ArrayList.class) |
| 87 | .register(Maps.immutableEntry("", "").getClass()) |
| 88 | .build("AtomixConsistentSetMultimapOperations"); |
| 89 | |
| 90 | /** |
| 91 | * Abstract multimap command. |
| 92 | */ |
| 93 | @SuppressWarnings("serial") |
| 94 | public abstract static class MultimapOperation { |
| 95 | @Override |
| 96 | public String toString() { |
| 97 | return MoreObjects.toStringHelper(getClass()) |
| 98 | .toString(); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Abstract key-based multimap query. |
| 104 | */ |
| 105 | @SuppressWarnings("serial") |
| 106 | public abstract static class KeyOperation extends MultimapOperation { |
| 107 | protected String key; |
| 108 | |
| 109 | public KeyOperation() { |
| 110 | } |
| 111 | |
| 112 | public KeyOperation(String key) { |
| 113 | this.key = checkNotNull(key); |
| 114 | } |
| 115 | |
| 116 | public String key() { |
| 117 | return key; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public String toString() { |
| 122 | return MoreObjects.toStringHelper(getClass()) |
| 123 | .add("key", key) |
| 124 | .toString(); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Abstract value-based query. |
| 130 | */ |
| 131 | @SuppressWarnings("serial") |
| 132 | public abstract static class ValueOperation extends MultimapOperation { |
| 133 | protected byte[] value; |
| 134 | |
| 135 | public ValueOperation() { |
| 136 | } |
| 137 | |
| 138 | public ValueOperation(byte[] value) { |
| 139 | this.value = checkNotNull(value); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Returns the value. |
| 144 | * |
| 145 | * @return value. |
| 146 | */ |
| 147 | public byte[] value() { |
| 148 | return value; |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public String toString() { |
| 153 | return MoreObjects.toStringHelper(getClass()) |
| 154 | .add("value", value) |
| 155 | .toString(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Contains key query. |
| 161 | */ |
| 162 | @SuppressWarnings("serial") |
| 163 | public static class ContainsKey extends KeyOperation { |
| 164 | public ContainsKey() { |
| 165 | } |
| 166 | |
| 167 | public ContainsKey(String key) { |
| 168 | super(key); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Contains value query. |
| 174 | */ |
| 175 | @SuppressWarnings("serial") |
| 176 | public static class ContainsValue extends ValueOperation { |
| 177 | public ContainsValue() { |
| 178 | } |
| 179 | |
| 180 | public ContainsValue(byte[] value) { |
| 181 | super(value); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Contains entry query. |
| 187 | */ |
| 188 | @SuppressWarnings("serial") |
| 189 | public static class ContainsEntry extends MultimapOperation { |
| 190 | protected String key; |
| 191 | protected byte[] value; |
| 192 | |
| 193 | public ContainsEntry() { |
| 194 | } |
| 195 | |
| 196 | public ContainsEntry(String key, byte[] value) { |
| 197 | this.key = checkNotNull(key); |
| 198 | this.value = checkNotNull(value); |
| 199 | } |
| 200 | |
| 201 | public String key() { |
| 202 | return key; |
| 203 | } |
| 204 | |
| 205 | public byte[] value() { |
| 206 | return value; |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public String toString() { |
| 211 | return MoreObjects.toStringHelper(getClass()) |
| 212 | .add("key", key) |
| 213 | .add("value", value) |
| 214 | .toString(); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Remove command, backs remove and removeAll's that return booleans. |
| 220 | */ |
| 221 | @SuppressWarnings("serial") |
| 222 | public static class RemoveAll extends MultimapOperation { |
| 223 | private String key; |
| 224 | private Match<Long> versionMatch; |
| 225 | |
| 226 | public RemoveAll() { |
| 227 | } |
| 228 | |
| 229 | public RemoveAll(String key, Match<Long> versionMatch) { |
| 230 | this.key = checkNotNull(key); |
| 231 | this.versionMatch = versionMatch; |
| 232 | } |
| 233 | |
| 234 | public String key() { |
| 235 | return this.key; |
| 236 | } |
| 237 | |
| 238 | public Match<Long> versionMatch() { |
| 239 | return versionMatch; |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public String toString() { |
| 244 | return MoreObjects.toStringHelper(getClass()) |
| 245 | .add("key", key) |
| 246 | .add("versionMatch", versionMatch) |
| 247 | .toString(); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Remove command, backs remove and removeAll's that return booleans. |
| 253 | */ |
| 254 | @SuppressWarnings("serial") |
| 255 | public static class MultiRemove extends MultimapOperation { |
| 256 | private String key; |
| 257 | private Collection<byte[]> values; |
| 258 | private Match<Long> versionMatch; |
| 259 | |
| 260 | public MultiRemove() { |
| 261 | } |
| 262 | |
| 263 | public MultiRemove(String key, Collection<byte[]> valueMatches, |
| 264 | Match<Long> versionMatch) { |
| 265 | this.key = checkNotNull(key); |
| 266 | this.values = valueMatches; |
| 267 | this.versionMatch = versionMatch; |
| 268 | } |
| 269 | |
| 270 | public String key() { |
| 271 | return this.key; |
| 272 | } |
| 273 | |
| 274 | public Collection<byte[]> values() { |
| 275 | return values; |
| 276 | } |
| 277 | |
| 278 | public Match<Long> versionMatch() { |
| 279 | return versionMatch; |
| 280 | } |
| 281 | |
| 282 | @Override |
| 283 | public String toString() { |
| 284 | return MoreObjects.toStringHelper(getClass()) |
| 285 | .add("key", key) |
| 286 | .add("values", values) |
| 287 | .add("versionMatch", versionMatch) |
| 288 | .toString(); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Command to back the put and putAll methods. |
| 294 | */ |
| 295 | @SuppressWarnings("serial") |
| 296 | public static class Put extends MultimapOperation { |
| 297 | private String key; |
| 298 | private Collection<? extends byte[]> values; |
| 299 | private Match<Long> versionMatch; |
| 300 | |
| 301 | public Put() { |
| 302 | } |
| 303 | |
| 304 | public Put(String key, Collection<? extends byte[]> values, Match<Long> versionMatch) { |
| 305 | this.key = checkNotNull(key); |
| 306 | this.values = values; |
| 307 | this.versionMatch = versionMatch; |
| 308 | } |
| 309 | |
| 310 | public String key() { |
| 311 | return key; |
| 312 | } |
| 313 | |
| 314 | public Collection<? extends byte[]> values() { |
| 315 | return values; |
| 316 | } |
| 317 | |
| 318 | public Match<Long> versionMatch() { |
| 319 | return versionMatch; |
| 320 | } |
| 321 | |
| 322 | @Override |
| 323 | public String toString() { |
| 324 | return MoreObjects.toStringHelper(getClass()) |
| 325 | .add("key", key) |
| 326 | .add("values", values) |
| 327 | .add("versionMatch", versionMatch) |
| 328 | .toString(); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Replace command, returns the collection that was replaced. |
| 334 | */ |
| 335 | @SuppressWarnings("serial") |
| 336 | public static class Replace extends MultimapOperation { |
| 337 | private String key; |
| 338 | private Collection<byte[]> values; |
| 339 | private Match<Long> versionMatch; |
| 340 | |
| 341 | public Replace() { |
| 342 | } |
| 343 | |
| 344 | public Replace(String key, Collection<byte[]> values, |
| 345 | Match<Long> versionMatch) { |
| 346 | this.key = checkNotNull(key); |
| 347 | this.values = values; |
| 348 | this.versionMatch = versionMatch; |
| 349 | } |
| 350 | |
| 351 | public String key() { |
| 352 | return this.key; |
| 353 | } |
| 354 | |
| 355 | public Match<Long> versionMatch() { |
| 356 | return versionMatch; |
| 357 | } |
| 358 | |
| 359 | public Collection<byte[]> values() { |
| 360 | return values; |
| 361 | } |
| 362 | |
| 363 | @Override |
| 364 | public String toString() { |
| 365 | return MoreObjects.toStringHelper(getClass()) |
| 366 | .add("key", key) |
| 367 | .add("values", values) |
| 368 | .add("versionMatch", versionMatch) |
| 369 | .toString(); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * Get value query. |
| 375 | */ |
| 376 | public static class Get extends KeyOperation { |
| 377 | public Get() { |
| 378 | } |
| 379 | |
| 380 | public Get(String key) { |
| 381 | super(key); |
| 382 | } |
| 383 | } |
| 384 | } |