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