Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | */ |
Thomas Vachuska | b6d3167 | 2018-07-27 17:03:46 -0700 | [diff] [blame] | 16 | package org.onosproject.store.atomix.primitives.impl; |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 17 | |
| 18 | import io.atomix.core.Atomix; |
| 19 | import io.atomix.primitive.Recovery; |
| 20 | import io.atomix.protocols.raft.MultiRaftProtocol; |
| 21 | import org.onosproject.store.service.AsyncConsistentMultimap; |
| 22 | import org.onosproject.store.service.ConsistentMultimap; |
| 23 | import org.onosproject.store.service.ConsistentMultimapBuilder; |
| 24 | |
| 25 | /** |
| 26 | * Default {@link AsyncConsistentMultimap} builder. |
| 27 | * |
| 28 | * @param <K> type for multimap key |
| 29 | * @param <V> type for multimap value |
| 30 | */ |
| 31 | public class AtomixConsistentMultimapBuilder<K, V> extends ConsistentMultimapBuilder<K, V> { |
| 32 | private static final int MAX_RETRIES = 5; |
| 33 | private final Atomix atomix; |
| 34 | private final String group; |
| 35 | |
| 36 | public AtomixConsistentMultimapBuilder(Atomix atomix, String group) { |
| 37 | this.atomix = atomix; |
| 38 | this.group = group; |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | public AsyncConsistentMultimap<K, V> buildMultimap() { |
| 43 | return new AtomixConsistentMultimap<>(atomix.<K, V>atomicMultimapBuilder(name()) |
| 44 | .withProtocol(MultiRaftProtocol.builder(group) |
| 45 | .withRecoveryStrategy(Recovery.RECOVER) |
| 46 | .withMaxRetries(MAX_RETRIES) |
| 47 | .build()) |
| 48 | .withReadOnly(readOnly()) |
| 49 | .withCacheEnabled(relaxedReadConsistency()) |
| 50 | .withSerializer(new AtomixSerializerAdapter(serializer())) |
| 51 | .build() |
| 52 | .async()); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public ConsistentMultimap<K, V> build() { |
| 57 | return buildMultimap().asMultimap(); |
| 58 | } |
| 59 | } |