blob: 9edfcd1f0ce9df27970e904ad27a4222571fcd1c [file] [log] [blame]
Aaron Kruglikova26f6542016-04-19 13:37:42 -07001/*
2 * Copyright 2016 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
17package org.onosproject.store.primitives.resources.impl;
18
19import com.google.common.base.MoreObjects;
20import com.google.common.collect.Multiset;
21import io.atomix.catalyst.buffer.BufferInput;
22import io.atomix.catalyst.buffer.BufferOutput;
23import io.atomix.catalyst.serializer.CatalystSerializable;
24import io.atomix.catalyst.serializer.SerializableTypeResolver;
25import io.atomix.catalyst.serializer.Serializer;
26import io.atomix.catalyst.serializer.SerializerRegistry;
27import io.atomix.catalyst.util.Assert;
28import io.atomix.copycat.Command;
29import io.atomix.copycat.Query;
30import org.onlab.util.Match;
31
32import java.util.Collection;
33import java.util.List;
34import java.util.Map;
35import java.util.Set;
36
37/**
38 * AsyncConsistentMultimap state machine commands.
39 */
40public final class AsyncConsistentMultimapCommands {
41
42 private AsyncConsistentMultimapCommands() {
43 }
44
45 /**
46 * Abstract multimap command.
47 */
48 @SuppressWarnings("serial")
49 public abstract static class MultimapCommand<V> implements Command<V>,
50 CatalystSerializable {
51 @Override
52 public ConsistencyLevel consistency() {
53 return ConsistencyLevel.SEQUENTIAL;
54 }
55
56 @Override
57 public String toString() {
58 return MoreObjects.toStringHelper(getClass())
59 .toString();
60 }
61
62 @Override
63 public void writeObject(BufferOutput<?> buffer,
64 Serializer serializer) {
65 }
66
67 @Override
68 public void readObject(BufferInput<?> buffer, Serializer serializer) {
69 }
70 }
71
72 /**
73 * Abstract multimap query.
74 */
75 @SuppressWarnings("serial")
76 public abstract static class MultimapQuery<V> implements Query<V>,
77 CatalystSerializable {
78 @Override
79 public ConsistencyLevel consistency() {
80 return ConsistencyLevel.SEQUENTIAL;
81 }
82
83 @Override
84 public String toString() {
85 return MoreObjects.toStringHelper(getClass())
86 .toString();
87 }
88
89 @Override
90 public void writeObject(BufferOutput<?> buffer,
91 Serializer serializer) {
92 }
93
94 @Override
95 public void readObject(BufferInput<?> buffer,
96 Serializer serializer) {
97 }
98 }
99
100 /**
101 * Abstract key-based multimap query.
102 */
103 @SuppressWarnings("serial")
104 public abstract static class KeyQuery<V> extends MultimapQuery<V> {
105 protected String key;
106
107 public KeyQuery() {
108 }
109
110 public KeyQuery(String key) {
111 this.key = Assert.notNull(key, "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 @Override
126 public void writeObject(BufferOutput<?> buffer, Serializer serializer) {
127 super.writeObject(buffer, serializer);
128 serializer.writeObject(key, buffer);
129 }
130
131 @Override
132 public void readObject(BufferInput<?> buffer, Serializer serializer) {
133 super.readObject(buffer, serializer);
134 key = serializer.readObject(buffer);
135 }
136 }
137
138 /**
139 * Abstract value-based query.
140 */
141 @SuppressWarnings("serial")
142 public abstract static class ValueQuery<V> extends MultimapQuery<V> {
143 protected byte[] value;
144
145 public ValueQuery() {
146 }
147
148 public ValueQuery(byte[] value) {
149 this.value = Assert.notNull(value, "value");
150 }
151
152 /**
153 * Returns the value.
154 *
155 * @return value.
156 */
157 public byte[] value() {
158 return value;
159 }
160
161 @Override
162 public String toString() {
163 return MoreObjects.toStringHelper(getClass())
164 .add("value", value)
165 .toString();
166 }
167
168 @Override
169 public void writeObject(BufferOutput<?> buffer, Serializer serializer) {
170 super.writeObject(buffer, serializer);
171 }
172
173 @Override
174 public void readObject(BufferInput<?> buffer, Serializer serializer) {
175 super.readObject(buffer, serializer);
176 }
177 }
178
179 /**
180 * Size query.
181 */
182 public static class Size extends MultimapQuery<Integer> {
183 }
184
185 /**
186 * Is empty query.
187 */
188 public static class IsEmpty extends MultimapQuery<Boolean> {
189 }
190
191 /**
192 * Contains key query.
193 */
194 @SuppressWarnings("serial")
195 public static class ContainsKey extends KeyQuery<Boolean> {
196 public ContainsKey() {
197 }
198
199 public ContainsKey(String key) {
200 super(key);
201 }
202
203 }
204
205 /**
206 * Contains value query.
207 */
208 @SuppressWarnings("serial")
209 public static class ContainsValue extends ValueQuery<Boolean> {
210 public ContainsValue() {
211 }
212
213 public ContainsValue(byte[] value) {
214 super(value);
215 }
216 }
217
218 /**
219 * Contains entry query.
220 */
221 @SuppressWarnings("serial")
222 public static class ContainsEntry extends MultimapQuery<Boolean> {
223 protected String key;
224 protected byte[] value;
225
226 public ContainsEntry() {
227 }
228
229 public ContainsEntry(String key, byte[] value) {
230 this.key = Assert.notNull(key, "key");
231 this.value = Assert.notNull(value, "value");
232 }
233
234 public String key() {
235 return key;
236 }
237
238 public byte[] value() {
239 return value;
240 }
241
242 @Override
243 public String toString() {
244 return MoreObjects.toStringHelper(getClass())
245 .add("key", key)
246 .add("value", value)
247 .toString();
248 }
249
250 @Override
251 public void writeObject(BufferOutput<?> buffer,
252 Serializer serializer) {
253 super.writeObject(buffer, serializer);
254 serializer.writeObject(key, buffer);
255 serializer.writeObject(value, buffer);
256 }
257
258 @Override
259 public void readObject(BufferInput<?> buffer, Serializer serializer) {
260 super.readObject(buffer, serializer);
261 key = serializer.readObject(buffer);
262 value = serializer.readObject(buffer);
263
264 }
265 }
266
267 /**
268 * Update and get command. Note that corresponding values must have the
269 * same index in the respective arrays.
270 */
271 @SuppressWarnings("serial")
272 public static class UpdateAndGet extends
273 MultimapCommand<MapEntryUpdateResult<String, Collection<byte[]>>> {
274 private String key;
275 private List<byte[]> values;
276 private List<Match<byte[]>> valueMatches;
277 private List<Match<Long>> versionMatches;
278
279 public UpdateAndGet() {
280 }
281
282 public UpdateAndGet(String key, List<byte[]> values,
283 List<Match<byte[]>> valueMatches,
284 List<Match<Long>> versionMatches) {
285 this.key = key;
286 this.values = values;
287 this.valueMatches = valueMatches;
288 this.versionMatches = versionMatches;
289 }
290
291 public String key() {
292 return this.key;
293 }
294
295 public List<byte[]> values() {
296 return values;
297 }
298
299 public List<Match<byte[]>> valueMatches() {
300 return valueMatches;
301 }
302
303 public List<Match<Long>> versionMatches() {
304 return versionMatches;
305 }
306
307 @Override
308 public CompactionMode compaction() {
309 return values == null ? CompactionMode.FULL :
310 CompactionMode.QUORUM;
311 }
312
313 @Override
314 public void writeObject(BufferOutput<?> buffer,
315 Serializer serializer) {
316 super.writeObject(buffer, serializer);
317 serializer.writeObject(key, buffer);
318 serializer.writeObject(values, buffer);
319 serializer.writeObject(valueMatches, buffer);
320 serializer.writeObject(versionMatches, buffer);
321 }
322
323 @Override
324 public void readObject(BufferInput<?> buffer, Serializer serializer) {
325 super.readObject(buffer, serializer);
326 key = serializer.readObject(buffer);
327 values = serializer.readObject(buffer);
328 valueMatches = serializer.readObject(buffer);
329 versionMatches = serializer.readObject(buffer);
330 }
331
332 @Override
333 public String toString() {
334 return super.toString();
335 }
336 }
337
338 /**
339 * Clear multimap command.
340 */
341 @SuppressWarnings("serial")
342 public static class Clear extends MultimapCommand<Void> {
343 }
344
345 /**
346 * Key set query.
347 */
348 @SuppressWarnings("serial")
349 public static class KeySet extends MultimapQuery<Set<String>> {
350 }
351
352 /**
353 * Key multiset query.
354 */
355 @SuppressWarnings("serial")
356 public static class Keys extends MultimapQuery<Multiset<String>> {
357 }
358
359 /**
360 * Value collection query.
361 */
362 @SuppressWarnings("serial")
363 public static class Values extends MultimapQuery<Collection<byte[]>> {
364 }
365
366 /**
367 * Entry set query.
368 */
369 @SuppressWarnings("serial")
370 public static class Entries extends
371 MultimapQuery<Collection<Map.Entry<String, byte[]>>> {
372 }
373
374 /**
375 * Get value query.
376 */
377 public static class Get extends KeyQuery<Collection<byte[]>> {
378 }
379
380 /**
381 * Multimap command type resolver.
382 */
383 @SuppressWarnings("serial")
384 public static class TypeResolver implements SerializableTypeResolver {
385 @Override
386 public void resolve(SerializerRegistry registry) {
387 registry.register(ContainsKey.class, -1000);
388 registry.register(ContainsValue.class, -1001);
389 registry.register(ContainsEntry.class, -1002);
390 registry.register(UpdateAndGet.class, -1003);
391 registry.register(Clear.class, -1004);
392 registry.register(KeySet.class, -1005);
393 registry.register(Keys.class, -1006);
394 registry.register(Values.class, -1007);
395 registry.register(Entries.class, -1008);
396 registry.register(Size.class, -1009);
397 registry.register(IsEmpty.class, -1010);
398 registry.register(Get.class, -1011);
399 }
400 }
401}