Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [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 | package org.onosproject.store.primitives.resources.impl; |
| 17 | |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 18 | import java.util.concurrent.CompletableFuture; |
| 19 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 20 | import io.atomix.protocols.raft.proxy.RaftProxy; |
| 21 | import org.onlab.util.KryoNamespace; |
| 22 | import org.onosproject.store.serializers.KryoNamespaces; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 23 | import org.onosproject.store.service.AsyncAtomicCounter; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 24 | import org.onosproject.store.service.Serializer; |
| 25 | |
| 26 | import static org.onosproject.store.primitives.resources.impl.AtomixCounterOperations.ADD_AND_GET; |
| 27 | import static org.onosproject.store.primitives.resources.impl.AtomixCounterOperations.AddAndGet; |
| 28 | import static org.onosproject.store.primitives.resources.impl.AtomixCounterOperations.COMPARE_AND_SET; |
| 29 | import static org.onosproject.store.primitives.resources.impl.AtomixCounterOperations.CompareAndSet; |
| 30 | import static org.onosproject.store.primitives.resources.impl.AtomixCounterOperations.GET; |
| 31 | import static org.onosproject.store.primitives.resources.impl.AtomixCounterOperations.GET_AND_ADD; |
| 32 | import static org.onosproject.store.primitives.resources.impl.AtomixCounterOperations.GET_AND_INCREMENT; |
| 33 | import static org.onosproject.store.primitives.resources.impl.AtomixCounterOperations.GetAndAdd; |
| 34 | import static org.onosproject.store.primitives.resources.impl.AtomixCounterOperations.INCREMENT_AND_GET; |
| 35 | import static org.onosproject.store.primitives.resources.impl.AtomixCounterOperations.SET; |
| 36 | import static org.onosproject.store.primitives.resources.impl.AtomixCounterOperations.Set; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 37 | |
| 38 | /** |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 39 | * Atomix counter implementation. |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 40 | */ |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 41 | public class AtomixCounter extends AbstractRaftPrimitive implements AsyncAtomicCounter { |
| 42 | private static final Serializer SERIALIZER = Serializer.using(KryoNamespace.newBuilder() |
| 43 | .register(KryoNamespaces.BASIC) |
| 44 | .register(AtomixCounterOperations.NAMESPACE) |
| 45 | .build()); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 46 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 47 | public AtomixCounter(RaftProxy proxy) { |
| 48 | super(proxy); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 51 | private long nullOrZero(Long value) { |
| 52 | return value != null ? value : 0; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public CompletableFuture<Long> get() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 57 | return proxy.<Long>invoke(GET, SERIALIZER::decode).thenApply(this::nullOrZero); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public CompletableFuture<Void> set(long value) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 62 | return proxy.invoke(SET, SERIALIZER::encode, new Set(value)); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | @Override |
Madan Jampani | e14a09c | 2016-02-11 10:43:21 -0800 | [diff] [blame] | 66 | public CompletableFuture<Boolean> compareAndSet(long expectedValue, long updateValue) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 67 | return proxy.invoke(COMPARE_AND_SET, SERIALIZER::encode, |
| 68 | new CompareAndSet(expectedValue, updateValue), SERIALIZER::decode); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public CompletableFuture<Long> addAndGet(long delta) { |
| 73 | return proxy.invoke(ADD_AND_GET, SERIALIZER::encode, new AddAndGet(delta), SERIALIZER::decode); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public CompletableFuture<Long> getAndAdd(long delta) { |
| 78 | return proxy.invoke(GET_AND_ADD, SERIALIZER::encode, new GetAndAdd(delta), SERIALIZER::decode); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public CompletableFuture<Long> incrementAndGet() { |
| 83 | return proxy.invoke(INCREMENT_AND_GET, SERIALIZER::decode); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public CompletableFuture<Long> getAndIncrement() { |
| 88 | return proxy.invoke(GET_AND_INCREMENT, SERIALIZER::decode); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 89 | } |
| 90 | } |