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