Jordan Halterman | c955df7 | 2017-02-04 20:43:28 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-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 | package org.onosproject.store.primitives.resources.impl; |
| 17 | |
| 18 | import io.atomix.catalyst.buffer.BufferInput; |
| 19 | import io.atomix.catalyst.buffer.BufferOutput; |
| 20 | import io.atomix.catalyst.serializer.CatalystSerializable; |
| 21 | import io.atomix.catalyst.serializer.SerializableTypeResolver; |
| 22 | import io.atomix.catalyst.serializer.Serializer; |
| 23 | import io.atomix.catalyst.serializer.SerializerRegistry; |
| 24 | import io.atomix.copycat.Command; |
| 25 | import io.atomix.copycat.Query; |
| 26 | |
| 27 | /** |
| 28 | * Atomic counter map commands. |
| 29 | */ |
| 30 | public final class AtomixAtomicCounterMapCommands { |
| 31 | private AtomixAtomicCounterMapCommands() { |
| 32 | } |
| 33 | |
| 34 | public abstract static class AtomicCounterMapCommand<V> implements Command<V>, CatalystSerializable { |
| 35 | @Override |
| 36 | public CompactionMode compaction() { |
| 37 | return CompactionMode.SNAPSHOT; |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | public void writeObject(BufferOutput<?> buffer, Serializer serializer) { |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public void readObject(BufferInput<?> buffer, Serializer serializer) { |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | public abstract static class AtomicCounterMapQuery<V> implements Query<V>, CatalystSerializable { |
| 50 | @Override |
| 51 | public void writeObject(BufferOutput<?> buffer, Serializer serializer) { |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public void readObject(BufferInput<?> buffer, Serializer serializer) { |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | public abstract static class KeyCommand<V> extends AtomicCounterMapCommand<V> { |
| 60 | private String key; |
| 61 | |
| 62 | public KeyCommand() { |
| 63 | } |
| 64 | |
| 65 | public KeyCommand(String key) { |
| 66 | this.key = key; |
| 67 | } |
| 68 | |
| 69 | public String key() { |
| 70 | return key; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public void writeObject(BufferOutput<?> buffer, Serializer serializer) { |
| 75 | buffer.writeString(key); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public void readObject(BufferInput<?> buffer, Serializer serializer) { |
| 80 | key = buffer.readString(); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | public abstract static class KeyQuery<V> extends AtomicCounterMapQuery<V> { |
| 85 | private String key; |
| 86 | |
| 87 | public KeyQuery() { |
| 88 | } |
| 89 | |
| 90 | public KeyQuery(String key) { |
| 91 | this.key = key; |
| 92 | } |
| 93 | |
| 94 | public String key() { |
| 95 | return key; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public void writeObject(BufferOutput<?> buffer, Serializer serializer) { |
| 100 | buffer.writeString(key); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void readObject(BufferInput<?> buffer, Serializer serializer) { |
| 105 | key = buffer.readString(); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | public static class KeyValueCommand<V> extends KeyCommand<V> { |
| 110 | private long value; |
| 111 | |
| 112 | public KeyValueCommand() { |
| 113 | } |
| 114 | |
| 115 | public KeyValueCommand(String key, long value) { |
| 116 | super(key); |
| 117 | this.value = value; |
| 118 | } |
| 119 | |
| 120 | public long value() { |
| 121 | return value; |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public void writeObject(BufferOutput<?> buffer, Serializer serializer) { |
| 126 | super.writeObject(buffer, serializer); |
| 127 | buffer.writeLong(value); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void readObject(BufferInput<?> buffer, Serializer serializer) { |
| 132 | super.readObject(buffer, serializer); |
| 133 | value = buffer.readLong(); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | public static class Get extends KeyQuery<Long> { |
| 138 | public Get() { |
| 139 | } |
| 140 | |
| 141 | public Get(String key) { |
| 142 | super(key); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | public static class Put extends KeyValueCommand<Long> { |
| 147 | public Put() { |
| 148 | } |
| 149 | |
| 150 | public Put(String key, long value) { |
| 151 | super(key, value); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | public static class PutIfAbsent extends KeyValueCommand<Long> { |
| 156 | public PutIfAbsent() { |
| 157 | } |
| 158 | |
| 159 | public PutIfAbsent(String key, long value) { |
| 160 | super(key, value); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | public static class Replace extends KeyCommand<Boolean> { |
| 165 | private long replace; |
| 166 | private long value; |
| 167 | |
| 168 | public Replace() { |
| 169 | } |
| 170 | |
| 171 | public Replace(String key, long replace, long value) { |
| 172 | super(key); |
| 173 | this.replace = replace; |
| 174 | this.value = value; |
| 175 | } |
| 176 | |
| 177 | public long replace() { |
| 178 | return replace; |
| 179 | } |
| 180 | |
| 181 | public long value() { |
| 182 | return value; |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public void writeObject(BufferOutput<?> buffer, Serializer serializer) { |
| 187 | super.writeObject(buffer, serializer); |
| 188 | buffer.writeLong(replace); |
| 189 | buffer.writeLong(value); |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public void readObject(BufferInput<?> buffer, Serializer serializer) { |
| 194 | super.readObject(buffer, serializer); |
| 195 | replace = buffer.readLong(); |
| 196 | value = buffer.readLong(); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | public static class Remove extends KeyCommand<Long> { |
| 201 | public Remove() { |
| 202 | } |
| 203 | |
| 204 | public Remove(String key) { |
| 205 | super(key); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | public static class RemoveValue extends KeyValueCommand<Boolean> { |
| 210 | public RemoveValue() { |
| 211 | } |
| 212 | |
| 213 | public RemoveValue(String key, long value) { |
| 214 | super(key, value); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | public static class IncrementAndGet extends KeyCommand<Long> { |
| 219 | public IncrementAndGet() { |
| 220 | } |
| 221 | |
| 222 | public IncrementAndGet(String key) { |
| 223 | super(key); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | public static class DecrementAndGet extends KeyCommand<Long> { |
| 228 | public DecrementAndGet(String key) { |
| 229 | super(key); |
| 230 | } |
| 231 | |
| 232 | public DecrementAndGet() { |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | public static class GetAndIncrement extends KeyCommand<Long> { |
| 237 | public GetAndIncrement() { |
| 238 | } |
| 239 | |
| 240 | public GetAndIncrement(String key) { |
| 241 | super(key); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | public static class GetAndDecrement extends KeyCommand<Long> { |
| 246 | public GetAndDecrement() { |
| 247 | } |
| 248 | |
| 249 | public GetAndDecrement(String key) { |
| 250 | super(key); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | public abstract static class DeltaCommand extends KeyCommand<Long> { |
| 255 | private long delta; |
| 256 | |
| 257 | public DeltaCommand() { |
| 258 | } |
| 259 | |
| 260 | public DeltaCommand(String key, long delta) { |
| 261 | super(key); |
| 262 | this.delta = delta; |
| 263 | } |
| 264 | |
| 265 | public long delta() { |
| 266 | return delta; |
| 267 | } |
| 268 | |
| 269 | @Override |
| 270 | public void writeObject(BufferOutput<?> buffer, Serializer serializer) { |
| 271 | super.writeObject(buffer, serializer); |
| 272 | buffer.writeLong(delta); |
| 273 | } |
| 274 | |
| 275 | @Override |
| 276 | public void readObject(BufferInput<?> buffer, Serializer serializer) { |
| 277 | super.readObject(buffer, serializer); |
| 278 | delta = buffer.readLong(); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | public static class AddAndGet extends DeltaCommand { |
| 283 | public AddAndGet() { |
| 284 | } |
| 285 | |
| 286 | public AddAndGet(String key, long delta) { |
| 287 | super(key, delta); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | public static class GetAndAdd extends DeltaCommand { |
| 292 | public GetAndAdd() { |
| 293 | } |
| 294 | |
| 295 | public GetAndAdd(String key, long delta) { |
| 296 | super(key, delta); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | public static class Size extends AtomicCounterMapQuery<Integer> { |
| 301 | } |
| 302 | |
| 303 | public static class IsEmpty extends AtomicCounterMapQuery<Boolean> { |
| 304 | } |
| 305 | |
| 306 | public static class Clear extends AtomicCounterMapCommand<Void> { |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Counter map command type resolver. |
| 311 | */ |
| 312 | public static class TypeResolver implements SerializableTypeResolver { |
| 313 | @Override |
| 314 | public void resolve(SerializerRegistry registry) { |
| 315 | registry.register(Get.class, -790); |
| 316 | registry.register(Put.class, -791); |
| 317 | registry.register(PutIfAbsent.class, -792); |
| 318 | registry.register(Replace.class, -793); |
| 319 | registry.register(Remove.class, -794); |
| 320 | registry.register(RemoveValue.class, -795); |
| 321 | registry.register(IncrementAndGet.class, -796); |
| 322 | registry.register(DecrementAndGet.class, -797); |
| 323 | registry.register(GetAndIncrement.class, -798); |
| 324 | registry.register(GetAndDecrement.class, -799); |
| 325 | registry.register(AddAndGet.class, -800); |
| 326 | registry.register(GetAndAdd.class, -801); |
| 327 | registry.register(Size.class, -801); |
| 328 | registry.register(IsEmpty.class, -801); |
| 329 | registry.register(Clear.class, -801); |
| 330 | } |
| 331 | } |
| 332 | } |