Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0a4e674 | 2016-09-15 23:03:10 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.store.primitives.resources.impl; |
| 18 | |
| 19 | import com.google.common.collect.Lists; |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Multiset; |
| 21 | import io.atomix.copycat.client.CopycatClient; |
| 22 | import io.atomix.resource.AbstractResource; |
Aaron Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame] | 23 | import io.atomix.resource.ResourceTypeInfo; |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 24 | import org.onosproject.store.service.AsyncConsistentMultimap; |
Jonathan Hart | 46bf89b | 2017-02-27 15:56:42 -0800 | [diff] [blame^] | 25 | import org.onosproject.store.service.MultimapEvent; |
| 26 | import org.onosproject.store.service.MultimapEventListener; |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 27 | import org.onosproject.store.service.Versioned; |
| 28 | |
| 29 | import java.util.Collection; |
| 30 | import java.util.ConcurrentModificationException; |
Jonathan Hart | 46bf89b | 2017-02-27 15:56:42 -0800 | [diff] [blame^] | 31 | import java.util.List; |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 32 | import java.util.Map; |
| 33 | import java.util.Properties; |
| 34 | import java.util.Set; |
| 35 | import java.util.concurrent.CompletableFuture; |
Jonathan Hart | 46bf89b | 2017-02-27 15:56:42 -0800 | [diff] [blame^] | 36 | import java.util.concurrent.ConcurrentHashMap; |
| 37 | import java.util.concurrent.Executor; |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 38 | |
Aaron Kruglikov | a1801aa | 2016-07-12 15:17:30 -0700 | [diff] [blame] | 39 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMultimapCommands.Clear; |
| 40 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMultimapCommands.ContainsEntry; |
| 41 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMultimapCommands.ContainsKey; |
| 42 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMultimapCommands.ContainsValue; |
| 43 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMultimapCommands.Entries; |
| 44 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMultimapCommands.Get; |
| 45 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMultimapCommands.IsEmpty; |
| 46 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMultimapCommands.KeySet; |
| 47 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMultimapCommands.Keys; |
Jonathan Hart | 46bf89b | 2017-02-27 15:56:42 -0800 | [diff] [blame^] | 48 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMultimapCommands.Listen; |
Aaron Kruglikov | a1801aa | 2016-07-12 15:17:30 -0700 | [diff] [blame] | 49 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMultimapCommands.MultiRemove; |
| 50 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMultimapCommands.Put; |
| 51 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMultimapCommands.RemoveAll; |
| 52 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMultimapCommands.Replace; |
| 53 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMultimapCommands.Size; |
Jonathan Hart | 46bf89b | 2017-02-27 15:56:42 -0800 | [diff] [blame^] | 54 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMultimapCommands.Unlisten; |
Aaron Kruglikov | a1801aa | 2016-07-12 15:17:30 -0700 | [diff] [blame] | 55 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMultimapCommands.Values; |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 56 | |
Jonathan Hart | 46bf89b | 2017-02-27 15:56:42 -0800 | [diff] [blame^] | 57 | |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 58 | /** |
| 59 | * Set based implementation of the {@link AsyncConsistentMultimap}. |
| 60 | * <p> |
| 61 | * Note: this implementation does not allow null entries or duplicate entries. |
| 62 | */ |
Aaron Kruglikov | a1801aa | 2016-07-12 15:17:30 -0700 | [diff] [blame] | 63 | @ResourceTypeInfo(id = -153, factory = AtomixConsistentSetMultimapFactory.class) |
| 64 | public class AtomixConsistentSetMultimap |
| 65 | extends AbstractResource<AtomixConsistentSetMultimap> |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 66 | implements AsyncConsistentMultimap<String, byte[]> { |
| 67 | |
Jonathan Hart | 46bf89b | 2017-02-27 15:56:42 -0800 | [diff] [blame^] | 68 | private final Map<MultimapEventListener<String, byte[]>, Executor> mapEventListeners = new ConcurrentHashMap<>(); |
| 69 | |
| 70 | public static final String CHANGE_SUBJECT = "multimapChangeEvents"; |
| 71 | |
Aaron Kruglikov | a1801aa | 2016-07-12 15:17:30 -0700 | [diff] [blame] | 72 | public AtomixConsistentSetMultimap(CopycatClient client, |
| 73 | Properties properties) { |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 74 | super(client, properties); |
| 75 | } |
| 76 | |
| 77 | @Override |
Aaron Kruglikov | a1801aa | 2016-07-12 15:17:30 -0700 | [diff] [blame] | 78 | public CompletableFuture<AtomixConsistentSetMultimap> open() { |
Jonathan Hart | 46bf89b | 2017-02-27 15:56:42 -0800 | [diff] [blame^] | 79 | return super.open().thenApply(result -> { |
| 80 | client.onStateChange(state -> { |
| 81 | if (state == CopycatClient.State.CONNECTED && isListening()) { |
| 82 | client.submit(new Listen()); |
| 83 | } |
| 84 | }); |
| 85 | client.onEvent(CHANGE_SUBJECT, this::handleEvent); |
| 86 | return result; |
| 87 | }); |
| 88 | } |
| 89 | |
| 90 | private void handleEvent(List<MultimapEvent<String, byte[]>> events) { |
| 91 | events.forEach(event -> |
| 92 | mapEventListeners.forEach((listener, executor) -> executor.execute(() -> listener.event(event)))); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public CompletableFuture<Integer> size() { |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame] | 97 | return client.submit(new Size()); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public CompletableFuture<Boolean> isEmpty() { |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame] | 102 | return client.submit(new IsEmpty()); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public CompletableFuture<Boolean> containsKey(String key) { |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame] | 107 | return client.submit(new ContainsKey(key)); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public CompletableFuture<Boolean> containsValue(byte[] value) { |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame] | 112 | return client.submit(new ContainsValue(value)); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public CompletableFuture<Boolean> containsEntry(String key, byte[] value) { |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame] | 117 | return client.submit(new ContainsEntry(key, value)); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public CompletableFuture<Boolean> put(String key, byte[] value) { |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame] | 122 | return client.submit(new Put(key, Lists.newArrayList(value), null)); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public CompletableFuture<Boolean> remove(String key, byte[] value) { |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame] | 127 | return client.submit(new MultiRemove(key, |
| 128 | Lists.newArrayList(value), |
| 129 | null)); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame] | 133 | public CompletableFuture<Boolean> removeAll(String key, Collection<? extends byte[]> values) { |
| 134 | return client.submit(new MultiRemove(key, (Collection<byte[]>) values, null)); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Override |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame] | 138 | public CompletableFuture<Versioned<Collection<? extends byte[]>>> removeAll(String key) { |
| 139 | return client.submit(new RemoveAll(key, null)); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Override |
Aaron Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame] | 143 | public CompletableFuture<Boolean> putAll( |
| 144 | String key, Collection<? extends byte[]> values) { |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame] | 145 | return client.submit(new Put(key, values, null)); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | @Override |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame] | 149 | public CompletableFuture<Versioned<Collection<? extends byte[]>>> replaceValues( |
Aaron Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame] | 150 | String key, Collection<byte[]> values) { |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame] | 151 | return client.submit(new Replace(key, values, null)); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public CompletableFuture<Void> clear() { |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame] | 156 | return client.submit(new Clear()); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | @Override |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame] | 160 | public CompletableFuture<Versioned<Collection<? extends byte[]>>> get(String key) { |
| 161 | return client.submit(new Get(key)); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public CompletableFuture<Set<String>> keySet() { |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame] | 166 | return client.submit(new KeySet()); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public CompletableFuture<Multiset<String>> keys() { |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame] | 171 | return client.submit(new Keys()); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Jonathan Hart | ad0c302 | 2017-02-22 14:06:01 -0800 | [diff] [blame] | 174 | @Override |
Aaron Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame] | 175 | public CompletableFuture<Multiset<byte[]>> values() { |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame] | 176 | return client.submit(new Values()); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public CompletableFuture<Collection<Map.Entry<String, byte[]>>> entries() { |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame] | 181 | return client.submit(new Entries()); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | @Override |
Jonathan Hart | 46bf89b | 2017-02-27 15:56:42 -0800 | [diff] [blame^] | 185 | public CompletableFuture<Void> addListener(MultimapEventListener<String, byte[]> listener, Executor executor) { |
| 186 | if (mapEventListeners.isEmpty()) { |
| 187 | return client.submit(new Listen()).thenRun(() -> mapEventListeners.put(listener, executor)); |
| 188 | } else { |
| 189 | mapEventListeners.put(listener, executor); |
| 190 | return CompletableFuture.completedFuture(null); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public CompletableFuture<Void> removeListener(MultimapEventListener<String, byte[]> listener) { |
| 196 | if (mapEventListeners.remove(listener) != null && mapEventListeners.isEmpty()) { |
| 197 | return client.submit(new Unlisten()).thenApply(v -> null); |
| 198 | } |
| 199 | return CompletableFuture.completedFuture(null); |
| 200 | } |
| 201 | |
| 202 | @Override |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 203 | public CompletableFuture<Map<String, Collection<byte[]>>> asMap() { |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 204 | throw new UnsupportedOperationException("Expensive operation."); |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public String name() { |
| 209 | return null; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Helper to check if there was a lock based issue. |
| 214 | * @param status the status of an update result |
| 215 | */ |
| 216 | private void throwIfLocked(MapEntryUpdateResult.Status status) { |
| 217 | if (status == MapEntryUpdateResult.Status.WRITE_LOCK) { |
Aaron Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame] | 218 | throw new ConcurrentModificationException("Cannot update map: " + |
| 219 | "Another transaction " + |
| 220 | "in progress"); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 221 | } |
| 222 | } |
Jonathan Hart | 46bf89b | 2017-02-27 15:56:42 -0800 | [diff] [blame^] | 223 | |
| 224 | private boolean isListening() { |
| 225 | return !mapEventListeners.isEmpty(); |
| 226 | } |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 227 | } |