blob: 8340425814e375a4988884eefabd1be20aabd010 [file] [log] [blame]
Jordan Halterman00e92da2018-05-22 23:05:52 -07001/*
Thomas Vachuskab6d31672018-07-27 17:03:46 -07002 * Copyright 2018-present Open Networking Foundation
Jordan Halterman00e92da2018-05-22 23:05:52 -07003 *
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 Vachuskab6d31672018-07-27 17:03:46 -070016package org.onosproject.store.atomix.primitives.impl;
Jordan Halterman00e92da2018-05-22 23:05:52 -070017
18import io.atomix.core.Atomix;
19import io.atomix.primitive.Recovery;
20import io.atomix.protocols.raft.MultiRaftProtocol;
21import org.onosproject.store.service.AsyncConsistentMap;
22import org.onosproject.store.service.ConsistentMap;
23import org.onosproject.store.service.ConsistentMapBuilder;
24
25/**
26 * Default {@link AsyncConsistentMap} builder.
27 *
28 * @param <K> type for map key
29 * @param <V> type for map value
30 */
31public class AtomixConsistentMapBuilder<K, V> extends ConsistentMapBuilder<K, V> {
32 private static final int MAX_RETRIES = 5;
33 private final Atomix atomix;
34 private final String group;
35
36 public AtomixConsistentMapBuilder(Atomix atomix, String group) {
37 this.atomix = atomix;
38 this.group = group;
39 }
40
41 @Override
42 public ConsistentMap<K, V> build() {
43 return buildAsyncMap().asConsistentMap();
44 }
45
46 @Override
47 public AsyncConsistentMap<K, V> buildAsyncMap() {
48 return new AtomixConsistentMap<>(atomix.<K, V>atomicMapBuilder(name())
49 .withRegistrationRequired()
50 .withProtocol(MultiRaftProtocol.builder(group)
51 .withRecoveryStrategy(Recovery.RECOVER)
52 .withMaxRetries(MAX_RETRIES)
53 .build())
54 .withReadOnly(readOnly())
55 .withCacheEnabled(relaxedReadConsistency())
56 .withSerializer(new AtomixSerializerAdapter(serializer()))
57 .build()
58 .async());
59 }
60}