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