blob: 123615cf74ca70add8c2b5c52d3b7d5e1398b006 [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,
47 DatabaseProxy<String, byte[]> proxy,
48 Serializer serializer) {
49 asyncMap = new DefaultAsyncConsistentMap<>(name, proxy, serializer);
50 }
51
52 @Override
53 public int size() {
54 return complete(asyncMap.size());
55 }
56
57 @Override
58 public boolean isEmpty() {
59 return complete(asyncMap.isEmpty());
60 }
61
62 @Override
63 public boolean containsKey(K key) {
64 return complete(asyncMap.containsKey(key));
65 }
66
67 @Override
68 public boolean containsValue(V value) {
69 return complete(asyncMap.containsValue(value));
70 }
71
72 @Override
73 public Versioned<V> get(K key) {
74 return complete(asyncMap.get(key));
75 }
76
77 @Override
78 public Versioned<V> put(K key, V value) {
79 return complete(asyncMap.put(key, value));
80 }
81
82 @Override
83 public Versioned<V> remove(K key) {
84 return complete(asyncMap.remove(key));
85 }
86
87 @Override
88 public void clear() {
89 complete(asyncMap.clear());
90 }
91
92 @Override
93 public Set<K> keySet() {
94 return complete(asyncMap.keySet());
95 }
96
97 @Override
98 public Collection<Versioned<V>> values() {
99 return complete(asyncMap.values());
100 }
101
102 @Override
103 public Set<Entry<K, Versioned<V>>> entrySet() {
104 return complete(asyncMap.entrySet());
105 }
106
107 @Override
108 public Versioned<V> putIfAbsent(K key, V value) {
109 return complete(asyncMap.putIfAbsent(key, value));
110 }
111
112 @Override
113 public boolean remove(K key, V value) {
114 return complete(asyncMap.remove(key, value));
115 }
116
117 @Override
118 public boolean remove(K key, long version) {
119 return complete(asyncMap.remove(key, version));
120 }
121
122 @Override
123 public boolean replace(K key, V oldValue, V newValue) {
124 return complete(asyncMap.replace(key, oldValue, newValue));
125 }
126
127 @Override
128 public boolean replace(K key, long oldVersion, V newValue) {
129 return complete(asyncMap.replace(key, oldVersion, newValue));
130 }
131
132 private static <T> T complete(CompletableFuture<T> future) {
133 try {
134 return future.get(OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
135 } catch (InterruptedException e) {
136 Thread.currentThread().interrupt();
137 throw new ConsistentMapException.Interrupted();
138 } catch (TimeoutException e) {
139 throw new ConsistentMapException.Timeout();
140 } catch (ExecutionException e) {
141 throw new ConsistentMapException(e.getCause());
142 }
143 }
144}