Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
| 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; |
| 25 | import org.onosproject.store.service.Versioned; |
| 26 | |
| 27 | import java.util.Collection; |
| 28 | import java.util.ConcurrentModificationException; |
| 29 | import java.util.Map; |
| 30 | import java.util.Properties; |
| 31 | import java.util.Set; |
| 32 | import java.util.concurrent.CompletableFuture; |
| 33 | |
Aaron Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame^] | 34 | import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.Clear; |
| 35 | import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.ContainsEntry; |
| 36 | import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.ContainsKey; |
| 37 | import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.ContainsValue; |
| 38 | import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.Entries; |
| 39 | import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.Get; |
| 40 | import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.IsEmpty; |
| 41 | import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.KeySet; |
| 42 | import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.Keys; |
| 43 | import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.MultiRemove; |
| 44 | import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.Put; |
| 45 | import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.RemoveAll; |
| 46 | import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.Replace; |
| 47 | import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.Size; |
| 48 | import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.Values; |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Set based implementation of the {@link AsyncConsistentMultimap}. |
| 52 | * <p> |
| 53 | * Note: this implementation does not allow null entries or duplicate entries. |
| 54 | */ |
Aaron Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame^] | 55 | @ResourceTypeInfo(id = -153, factory = AsyncConsistentSetMultimapFactory.class) |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 56 | public class AsyncConsistentSetMultimap |
| 57 | extends AbstractResource<AsyncConsistentSetMultimap> |
| 58 | implements AsyncConsistentMultimap<String, byte[]> { |
| 59 | |
| 60 | public AsyncConsistentSetMultimap(CopycatClient client, |
| 61 | Properties properties) { |
| 62 | super(client, properties); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public CompletableFuture<AsyncConsistentSetMultimap> open() { |
| 67 | return super.open(); |
| 68 | //TODO |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public CompletableFuture<Integer> size() { |
| 73 | return submit(new Size()); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public CompletableFuture<Boolean> isEmpty() { |
| 78 | return submit(new IsEmpty()); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public CompletableFuture<Boolean> containsKey(String key) { |
| 83 | return submit(new ContainsKey(key)); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public CompletableFuture<Boolean> containsValue(byte[] value) { |
| 88 | return submit(new ContainsValue(value)); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public CompletableFuture<Boolean> containsEntry(String key, byte[] value) { |
| 93 | return submit(new ContainsEntry(key, value)); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public CompletableFuture<Boolean> put(String key, byte[] value) { |
Aaron Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame^] | 98 | return submit(new Put(key, Lists.newArrayList(value), null)); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public CompletableFuture<Boolean> remove(String key, byte[] value) { |
Aaron Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame^] | 103 | return submit(new MultiRemove(key, |
| 104 | Lists.newArrayList(value), |
| 105 | null)); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | @Override |
Aaron Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame^] | 109 | public CompletableFuture<Boolean> removeAll( |
| 110 | String key, Collection<? extends byte[]> values) { |
| 111 | return submit(new MultiRemove(key, (Collection<byte[]>) values, null)); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Override |
Aaron Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame^] | 115 | public CompletableFuture< |
| 116 | Versioned<Collection<? extends byte[]>>> removeAll(String key) { |
| 117 | return submit(new RemoveAll(key, null)); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @Override |
Aaron Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame^] | 121 | public CompletableFuture<Boolean> putAll( |
| 122 | String key, Collection<? extends byte[]> values) { |
| 123 | return submit(new Put(key, values, null)); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Override |
Aaron Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame^] | 127 | public CompletableFuture< |
| 128 | Versioned<Collection<? extends byte[]>>> replaceValues( |
| 129 | String key, Collection<byte[]> values) { |
| 130 | return submit(new Replace(key, values, null)); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public CompletableFuture<Void> clear() { |
Aaron Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame^] | 135 | return submit(new Clear()); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Override |
Aaron Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame^] | 139 | public CompletableFuture< |
| 140 | Versioned<Collection<? extends byte[]>>> get(String key) { |
| 141 | return submit(new Get(key)); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public CompletableFuture<Set<String>> keySet() { |
| 146 | return submit(new KeySet()); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public CompletableFuture<Multiset<String>> keys() { |
| 151 | return submit(new Keys()); |
| 152 | } |
| 153 | |
| 154 | @Override |
Aaron Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame^] | 155 | public CompletableFuture<Multiset<byte[]>> values() { |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 156 | return submit(new Values()); |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public CompletableFuture<Collection<Map.Entry<String, byte[]>>> entries() { |
| 161 | return submit(new Entries()); |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public CompletableFuture<Map<String, Collection<byte[]>>> asMap() { |
| 166 | //TODO |
| 167 | throw new UnsupportedOperationException("Expensive operation."); |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public String name() { |
| 172 | return null; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Helper to check if there was a lock based issue. |
| 177 | * @param status the status of an update result |
| 178 | */ |
| 179 | private void throwIfLocked(MapEntryUpdateResult.Status status) { |
| 180 | if (status == MapEntryUpdateResult.Status.WRITE_LOCK) { |
Aaron Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame^] | 181 | throw new ConcurrentModificationException("Cannot update map: " + |
| 182 | "Another transaction " + |
| 183 | "in progress"); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | } |