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