blob: 7b0ad106152eb70adbdd898ddd46d84e7b06382d [file] [log] [blame]
Madan Jampani7c521002015-03-23 12:23:01 -07001/*
2 * Copyright 2015 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.consistent.impl;
18
19import java.util.Collection;
20import java.util.Map.Entry;
21import java.util.concurrent.CompletableFuture;
22import java.util.concurrent.ExecutionException;
23import java.util.concurrent.TimeUnit;
24import java.util.concurrent.TimeoutException;
25import java.util.Set;
26
27import org.onosproject.store.service.AsyncConsistentMap;
28import org.onosproject.store.service.ConsistentMap;
29import org.onosproject.store.service.ConsistentMapException;
30import org.onosproject.store.service.Serializer;
31import org.onosproject.store.service.Versioned;
32
33/**
34 * ConsistentMap implementation that is backed by a Raft consensus
35 * based database.
36 *
37 * @param <K> type of key.
38 * @param <V> type of value.
39 */
40public class DefaultConsistentMap<K, V> implements ConsistentMap<K, V> {
41
42 private static final int OPERATION_TIMEOUT_MILLIS = 5000;
43
44 private final AsyncConsistentMap<K, V> asyncMap;
45
46 public DefaultConsistentMap(String name,
Madan Jampanif1b8e172015-03-23 11:42:02 -070047 Database database,
Madan Jampani02b7fb82015-05-01 13:01:20 -070048 Serializer serializer,
49 boolean readOnly) {
50 asyncMap = new DefaultAsyncConsistentMap<>(name, database, serializer, readOnly);
Madan Jampani7c521002015-03-23 12:23:01 -070051 }
52
53 @Override
54 public int size() {
55 return complete(asyncMap.size());
56 }
57
58 @Override
59 public boolean isEmpty() {
60 return complete(asyncMap.isEmpty());
61 }
62
63 @Override
64 public boolean containsKey(K key) {
65 return complete(asyncMap.containsKey(key));
66 }
67
68 @Override
69 public boolean containsValue(V value) {
70 return complete(asyncMap.containsValue(value));
71 }
72
73 @Override
74 public Versioned<V> get(K key) {
75 return complete(asyncMap.get(key));
76 }
77
78 @Override
79 public Versioned<V> put(K key, V value) {
80 return complete(asyncMap.put(key, value));
81 }
82
83 @Override
84 public Versioned<V> remove(K key) {
85 return complete(asyncMap.remove(key));
86 }
87
88 @Override
89 public void clear() {
90 complete(asyncMap.clear());
91 }
92
93 @Override
94 public Set<K> keySet() {
95 return complete(asyncMap.keySet());
96 }
97
98 @Override
99 public Collection<Versioned<V>> values() {
100 return complete(asyncMap.values());
101 }
102
103 @Override
104 public Set<Entry<K, Versioned<V>>> entrySet() {
105 return complete(asyncMap.entrySet());
106 }
107
108 @Override
109 public Versioned<V> putIfAbsent(K key, V value) {
110 return complete(asyncMap.putIfAbsent(key, value));
111 }
112
113 @Override
114 public boolean remove(K key, V value) {
115 return complete(asyncMap.remove(key, value));
116 }
117
118 @Override
119 public boolean remove(K key, long version) {
120 return complete(asyncMap.remove(key, version));
121 }
122
123 @Override
124 public boolean replace(K key, V oldValue, V newValue) {
125 return complete(asyncMap.replace(key, oldValue, newValue));
126 }
127
128 @Override
129 public boolean replace(K key, long oldVersion, V newValue) {
130 return complete(asyncMap.replace(key, oldVersion, newValue));
131 }
132
133 private static <T> T complete(CompletableFuture<T> future) {
134 try {
135 return future.get(OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
136 } catch (InterruptedException e) {
137 Thread.currentThread().interrupt();
138 throw new ConsistentMapException.Interrupted();
139 } catch (TimeoutException e) {
140 throw new ConsistentMapException.Timeout();
141 } catch (ExecutionException e) {
142 throw new ConsistentMapException(e.getCause());
143 }
144 }
145}