Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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.onosproject.store.newresource.impl; |
| 17 | |
| 18 | import com.google.common.annotations.Beta; |
Sho SHIMIZU | e7db614 | 2015-11-04 11:24:22 -0800 | [diff] [blame^] | 19 | import com.google.common.collect.ImmutableList; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.apache.felix.scr.annotations.Service; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 25 | import org.onosproject.net.newresource.ResourceConsumer; |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 26 | import org.onosproject.net.newresource.ResourceEvent; |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 27 | import org.onosproject.net.newresource.ResourcePath; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 28 | import org.onosproject.net.newresource.ResourceStore; |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 29 | import org.onosproject.net.newresource.ResourceStoreDelegate; |
| 30 | import org.onosproject.store.AbstractStore; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 31 | import org.onosproject.store.serializers.KryoNamespaces; |
| 32 | import org.onosproject.store.service.ConsistentMap; |
| 33 | import org.onosproject.store.service.Serializer; |
| 34 | import org.onosproject.store.service.StorageService; |
| 35 | import org.onosproject.store.service.TransactionContext; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 36 | import org.onosproject.store.service.TransactionalMap; |
| 37 | import org.onosproject.store.service.Versioned; |
| 38 | import org.slf4j.Logger; |
| 39 | import org.slf4j.LoggerFactory; |
| 40 | |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 41 | import java.util.ArrayList; |
| 42 | import java.util.Arrays; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 43 | import java.util.Collection; |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 44 | import java.util.Collections; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 45 | import java.util.Iterator; |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 46 | import java.util.LinkedHashSet; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 47 | import java.util.List; |
| 48 | import java.util.Map; |
| 49 | import java.util.Optional; |
| 50 | import java.util.stream.Collectors; |
| 51 | |
| 52 | import static com.google.common.base.Preconditions.checkArgument; |
| 53 | import static com.google.common.base.Preconditions.checkNotNull; |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 54 | import static org.onosproject.net.newresource.ResourceEvent.Type.*; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * Implementation of ResourceStore using TransactionalMap. |
| 58 | */ |
Sho SHIMIZU | 9a2b829 | 2015-10-28 13:00:16 -0700 | [diff] [blame] | 59 | @Component(immediate = true) |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 60 | @Service |
| 61 | @Beta |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 62 | public class ConsistentResourceStore extends AbstractStore<ResourceEvent, ResourceStoreDelegate> |
| 63 | implements ResourceStore { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 64 | private static final Logger log = LoggerFactory.getLogger(ConsistentResourceStore.class); |
| 65 | |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 66 | private static final String CONSUMER_MAP = "onos-resource-consumers"; |
| 67 | private static final String CHILD_MAP = "onos-resource-children"; |
| 68 | private static final Serializer SERIALIZER = Serializer.using( |
| 69 | Arrays.asList(KryoNamespaces.BASIC, KryoNamespaces.API)); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 70 | |
| 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 72 | protected StorageService service; |
| 73 | |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 74 | private ConsistentMap<ResourcePath, ResourceConsumer> consumerMap; |
| 75 | private ConsistentMap<ResourcePath, List<ResourcePath>> childMap; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 76 | |
| 77 | @Activate |
| 78 | public void activate() { |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 79 | consumerMap = service.<ResourcePath, ResourceConsumer>consistentMapBuilder() |
| 80 | .withName(CONSUMER_MAP) |
| 81 | .withSerializer(SERIALIZER) |
| 82 | .build(); |
| 83 | childMap = service.<ResourcePath, List<ResourcePath>>consistentMapBuilder() |
| 84 | .withName(CHILD_MAP) |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 85 | .withSerializer(SERIALIZER) |
| 86 | .build(); |
Sho SHIMIZU | e7db614 | 2015-11-04 11:24:22 -0800 | [diff] [blame^] | 87 | |
| 88 | childMap.put(ResourcePath.ROOT, ImmutableList.of()); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | @Override |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 92 | public Optional<ResourceConsumer> getConsumer(ResourcePath resource) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 93 | checkNotNull(resource); |
| 94 | |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 95 | Versioned<ResourceConsumer> consumer = consumerMap.get(resource); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 96 | if (consumer == null) { |
| 97 | return Optional.empty(); |
| 98 | } |
| 99 | |
| 100 | return Optional.of(consumer.value()); |
| 101 | } |
| 102 | |
| 103 | @Override |
Sho SHIMIZU | 83e17a0 | 2015-08-20 14:07:05 -0700 | [diff] [blame] | 104 | public boolean register(List<ResourcePath> resources) { |
| 105 | checkNotNull(resources); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 106 | |
| 107 | TransactionContext tx = service.transactionContextBuilder().build(); |
| 108 | tx.begin(); |
| 109 | |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 110 | TransactionalMap<ResourcePath, List<ResourcePath>> childTxMap = |
| 111 | tx.getTransactionalMap(CHILD_MAP, SERIALIZER); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 112 | |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 113 | Map<ResourcePath, List<ResourcePath>> resourceMap = resources.stream() |
| 114 | .filter(x -> x.parent().isPresent()) |
| 115 | .collect(Collectors.groupingBy(x -> x.parent().get())); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 116 | |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 117 | for (Map.Entry<ResourcePath, List<ResourcePath>> entry: resourceMap.entrySet()) { |
| 118 | if (!isRegistered(childTxMap, entry.getKey())) { |
| 119 | return abortTransaction(tx); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 122 | if (!appendValues(childTxMap, entry.getKey(), entry.getValue())) { |
| 123 | return abortTransaction(tx); |
| 124 | } |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 125 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 126 | |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 127 | boolean success = tx.commit(); |
| 128 | if (success) { |
| 129 | List<ResourceEvent> events = resources.stream() |
| 130 | .filter(x -> x.parent().isPresent()) |
| 131 | .map(x -> new ResourceEvent(RESOURCE_ADDED, x)) |
| 132 | .collect(Collectors.toList()); |
| 133 | notifyDelegate(events); |
| 134 | } |
| 135 | return success; |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Override |
Sho SHIMIZU | 83e17a0 | 2015-08-20 14:07:05 -0700 | [diff] [blame] | 139 | public boolean unregister(List<ResourcePath> resources) { |
| 140 | checkNotNull(resources); |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 141 | |
| 142 | TransactionContext tx = service.transactionContextBuilder().build(); |
| 143 | tx.begin(); |
| 144 | |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 145 | TransactionalMap<ResourcePath, List<ResourcePath>> childTxMap = |
| 146 | tx.getTransactionalMap(CHILD_MAP, SERIALIZER); |
| 147 | TransactionalMap<ResourcePath, ResourceConsumer> consumerTxMap = |
| 148 | tx.getTransactionalMap(CONSUMER_MAP, SERIALIZER); |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 149 | |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 150 | Map<ResourcePath, List<ResourcePath>> resourceMap = resources.stream() |
| 151 | .filter(x -> x.parent().isPresent()) |
| 152 | .collect(Collectors.groupingBy(x -> x.parent().get())); |
Sho SHIMIZU | 83e17a0 | 2015-08-20 14:07:05 -0700 | [diff] [blame] | 153 | |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 154 | // even if one of the resources is allocated to a consumer, |
| 155 | // all unregistrations are regarded as failure |
| 156 | for (Map.Entry<ResourcePath, List<ResourcePath>> entry: resourceMap.entrySet()) { |
| 157 | if (entry.getValue().stream().anyMatch(x -> consumerTxMap.get(x) != null)) { |
| 158 | return abortTransaction(tx); |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 161 | if (!removeValues(childTxMap, entry.getKey(), entry.getValue())) { |
| 162 | return abortTransaction(tx); |
| 163 | } |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 164 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 165 | |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 166 | boolean success = tx.commit(); |
| 167 | if (success) { |
| 168 | List<ResourceEvent> events = resources.stream() |
| 169 | .filter(x -> x.parent().isPresent()) |
| 170 | .map(x -> new ResourceEvent(RESOURCE_REMOVED, x)) |
| 171 | .collect(Collectors.toList()); |
| 172 | notifyDelegate(events); |
| 173 | } |
| 174 | return success; |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | @Override |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 178 | public boolean allocate(List<ResourcePath> resources, ResourceConsumer consumer) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 179 | checkNotNull(resources); |
| 180 | checkNotNull(consumer); |
| 181 | |
| 182 | TransactionContext tx = service.transactionContextBuilder().build(); |
| 183 | tx.begin(); |
| 184 | |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 185 | TransactionalMap<ResourcePath, List<ResourcePath>> childTxMap = |
| 186 | tx.getTransactionalMap(CHILD_MAP, SERIALIZER); |
| 187 | TransactionalMap<ResourcePath, ResourceConsumer> consumerTxMap = |
| 188 | tx.getTransactionalMap(CONSUMER_MAP, SERIALIZER); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 189 | |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 190 | for (ResourcePath resource: resources) { |
| 191 | if (!isRegistered(childTxMap, resource)) { |
| 192 | return abortTransaction(tx); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 193 | } |
Sho SHIMIZU | d29847f | 2015-08-13 09:10:59 -0700 | [diff] [blame] | 194 | |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 195 | ResourceConsumer oldValue = consumerTxMap.put(resource, consumer); |
| 196 | if (oldValue != null) { |
| 197 | return abortTransaction(tx); |
| 198 | } |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 199 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 200 | |
| 201 | return tx.commit(); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | @Override |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 205 | public boolean release(List<ResourcePath> resources, List<ResourceConsumer> consumers) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 206 | checkNotNull(resources); |
| 207 | checkNotNull(consumers); |
| 208 | checkArgument(resources.size() == consumers.size()); |
| 209 | |
| 210 | TransactionContext tx = service.transactionContextBuilder().build(); |
| 211 | tx.begin(); |
| 212 | |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 213 | TransactionalMap<ResourcePath, ResourceConsumer> consumerTxMap = |
| 214 | tx.getTransactionalMap(CONSUMER_MAP, SERIALIZER); |
| 215 | Iterator<ResourcePath> resourceIte = resources.iterator(); |
| 216 | Iterator<ResourceConsumer> consumerIte = consumers.iterator(); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 217 | |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 218 | while (resourceIte.hasNext() && consumerIte.hasNext()) { |
| 219 | ResourcePath resource = resourceIte.next(); |
| 220 | ResourceConsumer consumer = consumerIte.next(); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 221 | |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 222 | // if this single release fails (because the resource is allocated to another consumer, |
| 223 | // the whole release fails |
| 224 | if (!consumerTxMap.remove(resource, consumer)) { |
| 225 | return abortTransaction(tx); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 226 | } |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 227 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 228 | |
| 229 | return tx.commit(); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | @Override |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 233 | public Collection<ResourcePath> getResources(ResourceConsumer consumer) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 234 | checkNotNull(consumer); |
| 235 | |
| 236 | // NOTE: getting all entries may become performance bottleneck |
| 237 | // TODO: revisit for better backend data structure |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 238 | return consumerMap.entrySet().stream() |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 239 | .filter(x -> x.getValue().value().equals(consumer)) |
| 240 | .map(Map.Entry::getKey) |
| 241 | .collect(Collectors.toList()); |
| 242 | } |
| 243 | |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 244 | @Override |
Sho SHIMIZU | e7f4f3f | 2015-10-13 16:27:25 -0700 | [diff] [blame] | 245 | public Collection<ResourcePath> getChildResources(ResourcePath parent) { |
| 246 | checkNotNull(parent); |
| 247 | |
| 248 | Versioned<List<ResourcePath>> children = childMap.get(parent); |
| 249 | if (children == null) { |
| 250 | return Collections.emptyList(); |
| 251 | } |
| 252 | |
| 253 | return children.value(); |
| 254 | } |
| 255 | |
| 256 | @Override |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 257 | public <T> Collection<ResourcePath> getAllocatedResources(ResourcePath parent, Class<T> cls) { |
| 258 | checkNotNull(parent); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 259 | checkNotNull(cls); |
| 260 | |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 261 | Versioned<List<ResourcePath>> children = childMap.get(parent); |
| 262 | if (children == null) { |
| 263 | return Collections.emptyList(); |
| 264 | } |
| 265 | |
| 266 | return children.value().stream() |
| 267 | .filter(x -> x.lastComponent().getClass().equals(cls)) |
| 268 | .filter(consumerMap::containsKey) |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 269 | .collect(Collectors.toList()); |
| 270 | } |
Sho SHIMIZU | d29847f | 2015-08-13 09:10:59 -0700 | [diff] [blame] | 271 | |
| 272 | /** |
| 273 | * Abort the transaction. |
| 274 | * |
| 275 | * @param tx transaction context |
| 276 | * @return always false |
| 277 | */ |
| 278 | private boolean abortTransaction(TransactionContext tx) { |
| 279 | tx.abort(); |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | /** |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 284 | * Appends the values to the existing values associated with the specified key. |
Sho SHIMIZU | 4568c41 | 2015-08-21 16:39:07 -0700 | [diff] [blame] | 285 | * If the map already has all the given values, appending will not happen. |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 286 | * |
| 287 | * @param map map holding multiple values for a key |
| 288 | * @param key key specifying values |
| 289 | * @param values values to be appended |
| 290 | * @param <K> type of the key |
| 291 | * @param <V> type of the element of the list |
| 292 | * @return true if the operation succeeds, false otherwise. |
| 293 | */ |
Sho SHIMIZU | 2a1b233 | 2015-08-18 22:40:12 -0700 | [diff] [blame] | 294 | private <K, V> boolean appendValues(TransactionalMap<K, List<V>> map, K key, List<V> values) { |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 295 | List<V> oldValues = map.get(key); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 296 | if (oldValues == null) { |
Sho SHIMIZU | 4568c41 | 2015-08-21 16:39:07 -0700 | [diff] [blame] | 297 | return map.replace(key, oldValues, new ArrayList<>(values)); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Sho SHIMIZU | 4568c41 | 2015-08-21 16:39:07 -0700 | [diff] [blame] | 300 | LinkedHashSet<V> oldSet = new LinkedHashSet<>(oldValues); |
| 301 | if (oldSet.containsAll(values)) { |
| 302 | // don't write to map because all values are already stored |
| 303 | return true; |
| 304 | } |
| 305 | |
| 306 | oldSet.addAll(values); |
| 307 | return map.replace(key, oldValues, new ArrayList<>(oldSet)); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | /** |
Sho SHIMIZU | ba1f83b | 2015-10-14 08:11:20 -0700 | [diff] [blame] | 311 | * Removes the values from the existing values associated with the specified key. |
Sho SHIMIZU | 5618ee5 | 2015-08-21 17:19:44 -0700 | [diff] [blame] | 312 | * If the map doesn't contain the given values, removal will not happen. |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 313 | * |
| 314 | * @param map map holding multiple values for a key |
| 315 | * @param key key specifying values |
| 316 | * @param values values to be removed |
| 317 | * @param <K> type of the key |
| 318 | * @param <V> type of the element of the list |
| 319 | * @return true if the operation succeeds, false otherwise |
| 320 | */ |
| 321 | private <K, V> boolean removeValues(TransactionalMap<K, List<V>> map, K key, List<V> values) { |
| 322 | List<V> oldValues = map.get(key); |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 323 | if (oldValues == null) { |
Sho SHIMIZU | 5618ee5 | 2015-08-21 17:19:44 -0700 | [diff] [blame] | 324 | return map.replace(key, oldValues, new ArrayList<>()); |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Sho SHIMIZU | 5618ee5 | 2015-08-21 17:19:44 -0700 | [diff] [blame] | 327 | LinkedHashSet<V> oldSet = new LinkedHashSet<>(oldValues); |
| 328 | if (values.stream().allMatch(x -> !oldSet.contains(x))) { |
| 329 | // don't write map because none of the values are stored |
| 330 | return true; |
| 331 | } |
| 332 | |
| 333 | oldSet.removeAll(values); |
| 334 | return map.replace(key, oldValues, new ArrayList<>(oldSet)); |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | /** |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 338 | * Checks if the specified resource is registered as a child of a resource in the map. |
| 339 | * |
| 340 | * @param map map storing parent - child relationship of resources |
| 341 | * @param resource resource to be checked |
| 342 | * @return true if the resource is registered, false otherwise. |
| 343 | */ |
| 344 | private boolean isRegistered(TransactionalMap<ResourcePath, List<ResourcePath>> map, ResourcePath resource) { |
| 345 | // root is always regarded to be registered |
Sho SHIMIZU | 0112078 | 2015-08-21 15:48:43 -0700 | [diff] [blame] | 346 | if (resource.isRoot()) { |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 347 | return true; |
| 348 | } |
| 349 | |
| 350 | List<ResourcePath> value = map.get(resource.parent().get()); |
| 351 | return value != null && value.contains(resource); |
| 352 | } |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 353 | } |