Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onlab.onos.store.hz; |
| 17 | |
Yuta HIGUCHI | c9438f1 | 2014-11-29 17:14:39 -0800 | [diff] [blame^] | 18 | import com.google.common.base.Function; |
| 19 | import com.google.common.collect.FluentIterable; |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 20 | import com.hazelcast.core.IQueue; |
| 21 | import com.hazelcast.core.ItemListener; |
| 22 | import com.hazelcast.monitor.LocalQueueStats; |
Yuta HIGUCHI | c9438f1 | 2014-11-29 17:14:39 -0800 | [diff] [blame^] | 23 | |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 24 | import org.onlab.onos.store.serializers.StoreSerializer; |
| 25 | |
| 26 | import java.util.Collection; |
| 27 | import java.util.Iterator; |
| 28 | import java.util.concurrent.TimeUnit; |
| 29 | |
| 30 | import static com.google.common.base.Preconditions.checkNotNull; |
| 31 | |
| 32 | // TODO: implementation is incomplete |
| 33 | |
| 34 | /** |
| 35 | * Wrapper around IQueue<byte[]> which serializes/deserializes |
| 36 | * key and value using StoreSerializer. |
| 37 | * |
| 38 | * @param <T> type |
| 39 | */ |
| 40 | public class SQueue<T> implements IQueue<T> { |
| 41 | |
| 42 | private final IQueue<byte[]> q; |
| 43 | private final StoreSerializer serializer; |
| 44 | |
| 45 | /** |
| 46 | * Creates a SQueue instance. |
| 47 | * |
| 48 | * @param baseQueue base IQueue to use |
| 49 | * @param serializer serializer to use for both key and value |
| 50 | */ |
| 51 | public SQueue(IQueue<byte[]> baseQueue, StoreSerializer serializer) { |
| 52 | this.q = checkNotNull(baseQueue); |
| 53 | this.serializer = checkNotNull(serializer); |
| 54 | } |
| 55 | |
| 56 | private byte[] serialize(Object key) { |
| 57 | return serializer.encode(key); |
| 58 | } |
| 59 | |
| 60 | private T deserialize(byte[] key) { |
| 61 | return serializer.decode(key); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public boolean add(T t) { |
| 66 | return q.add(serialize(t)); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public boolean offer(T t) { |
| 71 | return q.offer(serialize(t)); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public void put(T t) throws InterruptedException { |
| 76 | q.put(serialize(t)); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public boolean offer(T t, long l, TimeUnit timeUnit) throws InterruptedException { |
| 81 | return q.offer(serialize(t), l, timeUnit); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public T take() throws InterruptedException { |
| 86 | return deserialize(q.take()); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public T poll(long l, TimeUnit timeUnit) throws InterruptedException { |
| 91 | return deserialize(q.poll(l, timeUnit)); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public int remainingCapacity() { |
| 96 | return q.remainingCapacity(); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public boolean remove(Object o) { |
| 101 | return q.remove(serialize(o)); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public boolean contains(Object o) { |
| 106 | return q.contains(serialize(o)); |
| 107 | } |
| 108 | |
Yuta HIGUCHI | c9438f1 | 2014-11-29 17:14:39 -0800 | [diff] [blame^] | 109 | @Deprecated // not implemented yet |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 110 | @Override |
| 111 | public int drainTo(Collection<? super T> collection) { |
| 112 | throw new UnsupportedOperationException(); |
| 113 | } |
| 114 | |
Yuta HIGUCHI | c9438f1 | 2014-11-29 17:14:39 -0800 | [diff] [blame^] | 115 | @Deprecated // not implemented yet |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 116 | @Override |
| 117 | public int drainTo(Collection<? super T> collection, int i) { |
| 118 | throw new UnsupportedOperationException(); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public T remove() { |
| 123 | return deserialize(q.remove()); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public T poll() { |
| 128 | return deserialize(q.poll()); |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public T element() { |
| 133 | return deserialize(q.element()); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public T peek() { |
| 138 | return deserialize(q.peek()); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public int size() { |
| 143 | return q.size(); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public boolean isEmpty() { |
| 148 | return q.isEmpty(); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public Iterator<T> iterator() { |
Yuta HIGUCHI | c9438f1 | 2014-11-29 17:14:39 -0800 | [diff] [blame^] | 153 | return FluentIterable.from(q) |
| 154 | .transform(new DeserializeVal()) |
| 155 | .iterator(); |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Yuta HIGUCHI | c9438f1 | 2014-11-29 17:14:39 -0800 | [diff] [blame^] | 158 | @Deprecated // not implemented yet |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 159 | @Override |
| 160 | public Object[] toArray() { |
| 161 | throw new UnsupportedOperationException(); |
| 162 | } |
| 163 | |
Yuta HIGUCHI | c9438f1 | 2014-11-29 17:14:39 -0800 | [diff] [blame^] | 164 | @Deprecated // not implemented yet |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 165 | @Override |
| 166 | public <T1> T1[] toArray(T1[] t1s) { |
| 167 | throw new UnsupportedOperationException(); |
| 168 | } |
| 169 | |
Yuta HIGUCHI | c9438f1 | 2014-11-29 17:14:39 -0800 | [diff] [blame^] | 170 | @Deprecated // not implemented yet |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 171 | @Override |
| 172 | public boolean containsAll(Collection<?> collection) { |
| 173 | throw new UnsupportedOperationException(); |
| 174 | } |
| 175 | |
Yuta HIGUCHI | c9438f1 | 2014-11-29 17:14:39 -0800 | [diff] [blame^] | 176 | @Deprecated // not implemented yet |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 177 | @Override |
| 178 | public boolean addAll(Collection<? extends T> collection) { |
| 179 | throw new UnsupportedOperationException(); |
| 180 | } |
| 181 | |
Yuta HIGUCHI | c9438f1 | 2014-11-29 17:14:39 -0800 | [diff] [blame^] | 182 | @Deprecated // not implemented yet |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 183 | @Override |
| 184 | public boolean removeAll(Collection<?> collection) { |
| 185 | throw new UnsupportedOperationException(); |
| 186 | } |
| 187 | |
Yuta HIGUCHI | c9438f1 | 2014-11-29 17:14:39 -0800 | [diff] [blame^] | 188 | @Deprecated // not implemented yet |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 189 | @Override |
| 190 | public boolean retainAll(Collection<?> collection) { |
| 191 | throw new UnsupportedOperationException(); |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public void clear() { |
| 196 | q.clear(); |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public LocalQueueStats getLocalQueueStats() { |
| 201 | return q.getLocalQueueStats(); |
| 202 | } |
| 203 | |
Yuta HIGUCHI | c9438f1 | 2014-11-29 17:14:39 -0800 | [diff] [blame^] | 204 | @Deprecated // not implemented yet |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 205 | @Override |
| 206 | public String addItemListener(ItemListener<T> itemListener, boolean b) { |
| 207 | throw new UnsupportedOperationException(); |
| 208 | } |
| 209 | |
Yuta HIGUCHI | c9438f1 | 2014-11-29 17:14:39 -0800 | [diff] [blame^] | 210 | @Deprecated // not implemented yet |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 211 | @Override |
| 212 | public boolean removeItemListener(String s) { |
| 213 | throw new UnsupportedOperationException(); |
| 214 | } |
| 215 | |
| 216 | @Deprecated |
| 217 | @Override |
| 218 | public Object getId() { |
| 219 | return q.getId(); |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public String getPartitionKey() { |
| 224 | return q.getPartitionKey(); |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public String getName() { |
| 229 | return q.getName(); |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public String getServiceName() { |
| 234 | return q.getServiceName(); |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public void destroy() { |
| 239 | q.destroy(); |
| 240 | } |
Yuta HIGUCHI | c9438f1 | 2014-11-29 17:14:39 -0800 | [diff] [blame^] | 241 | |
| 242 | private final class DeserializeVal implements Function<byte[], T> { |
| 243 | @Override |
| 244 | public T apply(byte[] input) { |
| 245 | return deserialize(input); |
| 246 | } |
| 247 | } |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 248 | } |