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