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.AbstractMap; |
| 20 | |
| 21 | import com.google.common.base.MoreObjects; |
| 22 | import com.google.common.collect.Maps; |
| 23 | import io.atomix.protocols.raft.operation.OperationId; |
| 24 | import io.atomix.protocols.raft.operation.OperationType; |
| 25 | import org.onlab.util.KryoNamespace; |
| 26 | import org.onlab.util.Match; |
| 27 | import org.onosproject.store.serializers.KryoNamespaces; |
| 28 | import org.onosproject.store.service.Versioned; |
| 29 | |
| 30 | import static com.google.common.base.Preconditions.checkNotNull; |
| 31 | |
| 32 | /** |
| 33 | * {@link org.onosproject.store.service.AsyncConsistentTreeMap} Resource |
| 34 | * state machine operations. |
| 35 | */ |
| 36 | public enum AtomixConsistentTreeMapOperations implements OperationId { |
| 37 | CONTAINS_KEY("containsKey", OperationType.QUERY), |
| 38 | CONTAINS_VALUE("containsValue", OperationType.QUERY), |
| 39 | ENTRY_SET("entrySet", OperationType.QUERY), |
| 40 | GET("get", OperationType.QUERY), |
| 41 | GET_OR_DEFAULT("getOrDefault", OperationType.QUERY), |
| 42 | IS_EMPTY("isEmpty", OperationType.QUERY), |
| 43 | KEY_SET("keySet", OperationType.QUERY), |
| 44 | SIZE("size", OperationType.QUERY), |
| 45 | VALUES("values", OperationType.QUERY), |
| 46 | SUB_MAP("subMap", OperationType.QUERY), |
| 47 | FIRST_KEY("firstKey", OperationType.QUERY), |
| 48 | LAST_KEY("lastKey", OperationType.QUERY), |
| 49 | FIRST_ENTRY("firstEntry", OperationType.QUERY), |
| 50 | LAST_ENTRY("lastEntry", OperationType.QUERY), |
| 51 | POLL_FIRST_ENTRY("pollFirstEntry", OperationType.QUERY), |
| 52 | POLL_LAST_ENTRY("pollLastEntry", OperationType.QUERY), |
| 53 | LOWER_ENTRY("lowerEntry", OperationType.QUERY), |
| 54 | LOWER_KEY("lowerKey", OperationType.QUERY), |
| 55 | FLOOR_ENTRY("floorEntry", OperationType.QUERY), |
| 56 | FLOOR_KEY("floorKey", OperationType.QUERY), |
| 57 | CEILING_ENTRY("ceilingEntry", OperationType.QUERY), |
| 58 | CEILING_KEY("ceilingKey", OperationType.QUERY), |
| 59 | HIGHER_ENTRY("higherEntry", OperationType.QUERY), |
| 60 | HIGHER_KEY("higherKey", OperationType.QUERY), |
| 61 | UPDATE_AND_GET("updateAndGet", OperationType.COMMAND), |
| 62 | CLEAR("clear", OperationType.COMMAND), |
| 63 | ADD_LISTENER("addListener", OperationType.COMMAND), |
| 64 | REMOVE_LISTENER("removeListener", OperationType.COMMAND); |
| 65 | |
| 66 | private final String id; |
| 67 | private final OperationType type; |
| 68 | |
| 69 | AtomixConsistentTreeMapOperations(String id, OperationType type) { |
| 70 | this.id = id; |
| 71 | this.type = type; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public String id() { |
| 76 | return id; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public OperationType type() { |
| 81 | return type; |
| 82 | } |
| 83 | |
| 84 | public static final KryoNamespace NAMESPACE = KryoNamespace.newBuilder() |
| 85 | .register(KryoNamespaces.BASIC) |
| 86 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) |
| 87 | .register(ContainsKey.class) |
| 88 | .register(ContainsValue.class) |
| 89 | .register(Get.class) |
| 90 | .register(GetOrDefault.class) |
| 91 | .register(LowerKey.class) |
| 92 | .register(LowerEntry.class) |
| 93 | .register(HigherKey.class) |
| 94 | .register(HigherEntry.class) |
| 95 | .register(FloorKey.class) |
| 96 | .register(FloorEntry.class) |
| 97 | .register(CeilingKey.class) |
| 98 | .register(CeilingEntry.class) |
| 99 | .register(UpdateAndGet.class) |
| 100 | .register(Match.class) |
| 101 | .register(Versioned.class) |
| 102 | .register(MapEntryUpdateResult.class) |
| 103 | .register(MapEntryUpdateResult.Status.class) |
| 104 | .register(AbstractMap.SimpleImmutableEntry.class) |
| 105 | .register(Maps.immutableEntry("", "").getClass()) |
| 106 | .build("AtomixConsistentTreeMapOperations"); |
| 107 | |
| 108 | /** |
| 109 | * Abstract treeMap command. |
| 110 | */ |
| 111 | @SuppressWarnings("serial") |
| 112 | public abstract static class TreeOperation { |
| 113 | @Override |
| 114 | public String toString() { |
| 115 | return MoreObjects.toStringHelper(getClass()) |
| 116 | .toString(); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Abstract key-based query. |
| 122 | */ |
| 123 | @SuppressWarnings("serial") |
| 124 | public abstract static class KeyOperation extends TreeOperation { |
| 125 | protected String key; |
| 126 | |
| 127 | public KeyOperation(String key) { |
| 128 | this.key = checkNotNull(key); |
| 129 | } |
| 130 | |
| 131 | public KeyOperation() { |
| 132 | } |
| 133 | |
| 134 | public String key() { |
| 135 | return key; |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public String toString() { |
| 140 | return MoreObjects.toStringHelper(getClass()) |
| 141 | .add("key", key) |
| 142 | .toString(); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Abstract value-based query. |
| 148 | */ |
| 149 | @SuppressWarnings("serial") |
| 150 | public abstract static class ValueOperation extends TreeOperation { |
| 151 | protected byte[] value; |
| 152 | |
| 153 | public ValueOperation() {} |
| 154 | |
| 155 | public ValueOperation(byte[] value) { |
| 156 | this.value = checkNotNull(value); |
| 157 | } |
| 158 | |
| 159 | public byte[] value() { |
| 160 | return value; |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public String toString() { |
| 165 | return MoreObjects.toStringHelper(getClass()) |
| 166 | .add("value", value) |
| 167 | .toString(); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Contains key command. |
| 173 | */ |
| 174 | @SuppressWarnings("serial") |
| 175 | public static class ContainsKey extends KeyOperation { |
| 176 | |
| 177 | public ContainsKey(String key) { |
| 178 | super(key); |
| 179 | } |
| 180 | |
| 181 | public ContainsKey() { |
| 182 | } |
| 183 | } |
| 184 | /** |
| 185 | * Contains value command. |
| 186 | */ |
| 187 | @SuppressWarnings("serial") |
| 188 | public static class ContainsValue extends ValueOperation { |
| 189 | public ContainsValue() { |
| 190 | } |
| 191 | |
| 192 | public ContainsValue(byte[] value) { |
| 193 | super(value); |
| 194 | } |
| 195 | |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * AsyncConsistentTreeMap update command. |
| 200 | */ |
| 201 | @SuppressWarnings("serial") |
| 202 | public static class UpdateAndGet extends TreeOperation { |
| 203 | private String key; |
| 204 | private byte[] value; |
| 205 | private Match<byte[]> valueMatch; |
| 206 | private Match<Long> versionMatch; |
| 207 | public UpdateAndGet() { |
| 208 | } |
| 209 | |
| 210 | public UpdateAndGet(String key, |
| 211 | byte[] value, |
| 212 | Match<byte[]> valueMatch, |
| 213 | Match<Long> versionMatch) { |
| 214 | this.key = key; |
| 215 | this.value = value; |
| 216 | this.valueMatch = valueMatch; |
| 217 | this.versionMatch = versionMatch; |
| 218 | } |
| 219 | |
| 220 | public String key() { |
| 221 | return this.key; |
| 222 | } |
| 223 | |
| 224 | public byte[] value() { |
| 225 | return this.value; |
| 226 | } |
| 227 | |
| 228 | public Match<byte[]> valueMatch() { |
| 229 | return this.valueMatch; |
| 230 | } |
| 231 | |
| 232 | public Match<Long> versionMatch() { |
| 233 | return this.versionMatch; |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public String toString() { |
| 238 | return MoreObjects.toStringHelper(getClass()) |
| 239 | .add("key", key) |
| 240 | .add("value", value) |
| 241 | .add("valueMatch", valueMatch) |
| 242 | .add("versionMatch", versionMatch) |
| 243 | .toString(); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Get query. |
| 249 | */ |
| 250 | @SuppressWarnings("serial") |
| 251 | public static class Get extends KeyOperation { |
| 252 | public Get() { |
| 253 | } |
| 254 | |
| 255 | public Get(String key) { |
| 256 | super(key); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Get or default query. |
| 262 | */ |
| 263 | @SuppressWarnings("serial") |
| 264 | public static class GetOrDefault extends KeyOperation { |
| 265 | private byte[] defaultValue; |
| 266 | |
| 267 | public GetOrDefault() { |
| 268 | } |
| 269 | |
| 270 | public GetOrDefault(String key, byte[] defaultValue) { |
| 271 | super(key); |
| 272 | this.defaultValue = defaultValue; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Returns the default value. |
| 277 | * |
| 278 | * @return the default value |
| 279 | */ |
| 280 | public byte[] defaultValue() { |
| 281 | return defaultValue; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Query returns the entry associated with the largest key less than the |
| 287 | * passed in key. |
| 288 | */ |
| 289 | @SuppressWarnings("serial") |
| 290 | public static class LowerEntry extends KeyOperation { |
| 291 | public LowerEntry() { |
| 292 | } |
| 293 | |
| 294 | public LowerEntry(String key) { |
| 295 | super(key); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Query returns the largest key less than the specified key. |
| 301 | */ |
| 302 | @SuppressWarnings("serial") |
| 303 | public static class LowerKey extends KeyOperation { |
| 304 | public LowerKey() { |
| 305 | } |
| 306 | |
| 307 | public LowerKey(String key) { |
| 308 | super(key); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Query returns the entry associated with the largest key smaller than or |
| 314 | * equal to the specified key. |
| 315 | */ |
| 316 | @SuppressWarnings("serial") |
| 317 | public static class FloorEntry extends KeyOperation { |
| 318 | public FloorEntry() { |
| 319 | } |
| 320 | |
| 321 | public FloorEntry(String key) { |
| 322 | super(key); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Query returns the largest key smaller than or equal to the passed in |
| 328 | * key. |
| 329 | */ |
| 330 | @SuppressWarnings("serial") |
| 331 | public static class FloorKey extends KeyOperation { |
| 332 | public FloorKey() { |
| 333 | } |
| 334 | |
| 335 | public FloorKey(String key) { |
| 336 | super(key); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Returns the entry associated with the smallest key larger than or equal |
| 342 | * to the specified key. |
| 343 | */ |
| 344 | @SuppressWarnings("serial") |
| 345 | public static class CeilingEntry extends KeyOperation { |
| 346 | public CeilingEntry() { |
| 347 | } |
| 348 | |
| 349 | public CeilingEntry(String key) { |
| 350 | super(key); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Returns the smallest key larger than or equal to the specified key. |
| 356 | */ |
| 357 | @SuppressWarnings("serial") |
| 358 | public static class CeilingKey extends KeyOperation { |
| 359 | public CeilingKey() { |
| 360 | } |
| 361 | |
| 362 | public CeilingKey(String key) { |
| 363 | super(key); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Returns the entry associated with the smallest key larger than the |
| 369 | * specified key. |
| 370 | */ |
| 371 | @SuppressWarnings("serial") |
| 372 | public static class HigherEntry extends KeyOperation { |
| 373 | public HigherEntry() { |
| 374 | } |
| 375 | |
| 376 | public HigherEntry(String key) { |
| 377 | super(key); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * Returns the smallest key larger than the specified key. |
| 383 | */ |
| 384 | @SuppressWarnings("serial") |
| 385 | public static class HigherKey extends KeyOperation { |
| 386 | public HigherKey() { |
| 387 | } |
| 388 | |
| 389 | public HigherKey(String key) { |
| 390 | super(key); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | @SuppressWarnings("serial") |
| 395 | public static class SubMap<K, V> extends TreeOperation { |
| 396 | private K fromKey; |
| 397 | private K toKey; |
| 398 | private boolean inclusiveFrom; |
| 399 | private boolean inclusiveTo; |
| 400 | |
| 401 | public SubMap() { |
| 402 | } |
| 403 | |
| 404 | public SubMap(K fromKey, K toKey, boolean inclusiveFrom, |
| 405 | boolean inclusiveTo) { |
| 406 | this.fromKey = fromKey; |
| 407 | this.toKey = toKey; |
| 408 | this.inclusiveFrom = inclusiveFrom; |
| 409 | this.inclusiveTo = inclusiveTo; |
| 410 | } |
| 411 | |
| 412 | @Override |
| 413 | public String toString() { |
| 414 | return MoreObjects.toStringHelper(getClass()) |
| 415 | .add("getFromKey", fromKey) |
| 416 | .add("getToKey", toKey) |
| 417 | .add("inclusiveFrotBound", inclusiveFrom) |
| 418 | .add("inclusiveToBound", inclusiveTo) |
| 419 | .toString(); |
| 420 | } |
| 421 | |
| 422 | public K fromKey() { |
| 423 | return fromKey; |
| 424 | } |
| 425 | |
| 426 | public K toKey() { |
| 427 | return toKey; |
| 428 | } |
| 429 | |
| 430 | public boolean isInclusiveFrom() { |
| 431 | return inclusiveFrom; |
| 432 | } |
| 433 | |
| 434 | public boolean isInclusiveTo() { |
| 435 | return inclusiveTo; |
| 436 | } |
| 437 | } |
| 438 | } |