blob: 0f2737a11c3185f84cb7625e3682280f02520163 [file] [log] [blame]
Yuta HIGUCHI3cc4d9b2014-11-29 18:17:17 -08001/*
2 * Copyright 2014 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 */
16package org.onlab.onos.store.hz;
17
18import static com.google.common.base.Preconditions.checkNotNull;
19
20import java.util.ArrayList;
21import java.util.Collection;
22import java.util.HashSet;
23import java.util.Set;
24import java.util.concurrent.TimeUnit;
25
26import org.onlab.onos.store.serializers.StoreSerializer;
27
28import com.hazelcast.core.TransactionalMap;
29import com.hazelcast.query.Predicate;
30
31// TODO: implement Predicate, etc. if we need them.
32/**
33 * Wrapper around TransactionalMap<byte[], byte[]> which serializes/deserializes
34 * key and value using StoreSerializer.
35 *
36 * @param <K> key type
37 * @param <V> value type
38 */
39public class STxMap<K, V> implements TransactionalMap<K, V> {
40
41 private final TransactionalMap<byte[], byte[]> m;
42 private final StoreSerializer serializer;
43
44 /**
45 * Creates a STxMap instance.
46 *
47 * @param baseMap base IMap to use
48 * @param serializer serializer to use for both key and value
49 */
50 public STxMap(TransactionalMap<byte[], byte[]> baseMap, StoreSerializer serializer) {
51 this.m = checkNotNull(baseMap);
52 this.serializer = checkNotNull(serializer);
53 }
54
55 @Override
56 public int size() {
57 return m.size();
58 }
59
60 @Override
61 public boolean isEmpty() {
62 return m.isEmpty();
63 }
64
65 @Deprecated
66 @Override
67 public Object getId() {
68 return m.getId();
69 }
70
71 @Override
72 public String getPartitionKey() {
73 return m.getPartitionKey();
74 }
75
76 @Override
77 public String getName() {
78 return m.getName();
79 }
80
81 @Override
82 public String getServiceName() {
83 return m.getServiceName();
84 }
85
86 @Override
87 public void destroy() {
88 m.destroy();
89 }
90
91 @Override
92 public boolean containsKey(Object key) {
93 return m.containsKey(serializeKey(key));
94 }
95
96 @Override
97 public V get(Object key) {
98 return deserializeVal(m.get(serializeKey(key)));
99 }
100
101 @Override
102 public V getForUpdate(Object key) {
103 // TODO Auto-generated method stub
104 return deserializeVal(m.getForUpdate(serializeKey(key)));
105 }
106
107 @Override
108 public V put(K key, V value) {
109 return deserializeVal(m.put(serializeKey(key), serializeVal(value)));
110 }
111
112 @Override
113 public V remove(Object key) {
114 return deserializeVal(m.remove(serializeKey(key)));
115 }
116
117 @Override
118 public boolean remove(Object key, Object value) {
119 return m.remove(serializeKey(key), serializeVal(value));
120 }
121
122 @Override
123 public void delete(Object key) {
124 m.delete(serializeKey(key));
125 }
126
127 @Override
128 public V put(K key, V value, long ttl, TimeUnit timeunit) {
129 return deserializeVal(m.put(serializeKey(key), serializeVal(value), ttl, timeunit));
130 }
131
132 @Override
133 public V putIfAbsent(K key, V value) {
134 return deserializeVal(m.putIfAbsent(serializeKey(key), serializeVal(value)));
135 }
136
137 @Override
138 public boolean replace(K key, V oldValue, V newValue) {
139 return m.replace(serializeKey(key), serializeVal(oldValue), serializeVal(newValue));
140 }
141
142 @Override
143 public V replace(K key, V value) {
144 return deserializeVal(m.replace(serializeKey(key), serializeVal(value)));
145 }
146
147 @Override
148 public void set(K key, V value) {
149 m.set(serializeKey(key), serializeVal(value));
150 }
151
152
153 @Override
154 public Set<K> keySet() {
155 return deserializeKeySet(m.keySet());
156 }
157
158 @Override
159 public Collection<V> values() {
160 return deserializeVals(m.values());
161 }
162
163 @Deprecated // marking method not implemented
164 @SuppressWarnings("rawtypes")
165 @Override
166 public Set<K> keySet(Predicate predicate) {
167 throw new UnsupportedOperationException();
168 }
169
170 @Deprecated // marking method not implemented
171 @SuppressWarnings("rawtypes")
172 @Override
173 public Collection<V> values(Predicate predicate) {
174 throw new UnsupportedOperationException();
175 }
176
177 private byte[] serializeKey(Object key) {
178 return serializer.encode(key);
179 }
180
181 private K deserializeKey(byte[] key) {
182 return serializer.decode(key);
183 }
184
185 private byte[] serializeVal(Object val) {
186 return serializer.encode(val);
187 }
188
189 private V deserializeVal(byte[] val) {
190 if (val == null) {
191 return null;
192 }
193 return serializer.decode(val.clone());
194 }
195
196 private Set<K> deserializeKeySet(Set<byte[]> keys) {
197 Set<K> dsk = new HashSet<>(keys.size());
198 for (byte[] key : keys) {
199 dsk.add(deserializeKey(key));
200 }
201 return dsk;
202 }
203
204 private Collection<V> deserializeVals(Collection<byte[]> vals) {
205 Collection<V> dsl = new ArrayList<>(vals.size());
206 for (byte[] val : vals) {
207 dsl.add(deserializeVal(val));
208 }
209 return dsl;
210 }
211}