Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present 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.primitives.impl; |
| 17 | |
| 18 | import java.util.Map; |
| 19 | import java.util.stream.Stream; |
| 20 | |
| 21 | import com.google.common.collect.Maps; |
| 22 | import org.apache.commons.lang3.tuple.Pair; |
| 23 | import org.onosproject.store.primitives.MapUpdate; |
| 24 | import org.onosproject.store.service.ConsistentMap; |
| 25 | import org.onosproject.store.service.Versioned; |
| 26 | |
| 27 | /** |
| 28 | * Repeatable read based map participant. |
| 29 | */ |
| 30 | public class DefaultTransactionalMapParticipant<K, V> extends TransactionalMapParticipant<K, V> { |
| 31 | private final Map<K, Versioned<V>> readCache = Maps.newConcurrentMap(); |
| 32 | |
| 33 | public DefaultTransactionalMapParticipant( |
| 34 | ConsistentMap<K, V> backingMap, Transaction<MapUpdate<K, V>> transaction) { |
| 35 | super(backingMap, transaction); |
| 36 | } |
| 37 | |
| 38 | @Override |
| 39 | protected V read(K key) { |
| 40 | Versioned<V> value = readCache.computeIfAbsent(key, backingMap::get); |
| 41 | return value != null ? value.value() : null; |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | protected Stream<MapUpdate<K, V>> records() { |
| 46 | return Stream.concat(deleteStream(), writeStream()); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Returns a transaction record stream for deleted keys. |
| 51 | */ |
| 52 | private Stream<MapUpdate<K, V>> deleteStream() { |
| 53 | return deleteSet.stream() |
| 54 | .map(key -> Pair.of(key, readCache.get(key))) |
| 55 | .filter(e -> e.getValue() != null) |
| 56 | .map(e -> MapUpdate.<K, V>newBuilder() |
| 57 | .withType(MapUpdate.Type.REMOVE_IF_VERSION_MATCH) |
| 58 | .withKey(e.getKey()) |
| 59 | .withCurrentVersion(e.getValue().version()) |
| 60 | .build()); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Returns a transaction record stream for updated keys. |
| 65 | */ |
| 66 | private Stream<MapUpdate<K, V>> writeStream() { |
| 67 | return writeCache.entrySet().stream().map(entry -> { |
| 68 | Versioned<V> original = readCache.get(entry.getKey()); |
| 69 | if (original == null) { |
| 70 | return MapUpdate.<K, V>newBuilder() |
| 71 | .withType(MapUpdate.Type.PUT_IF_ABSENT) |
| 72 | .withKey(entry.getKey()) |
| 73 | .withValue(entry.getValue()) |
| 74 | .build(); |
| 75 | } else { |
| 76 | return MapUpdate.<K, V>newBuilder() |
| 77 | .withType(MapUpdate.Type.PUT_IF_VERSION_MATCH) |
| 78 | .withKey(entry.getKey()) |
| 79 | .withCurrentVersion(original.version()) |
| 80 | .withValue(entry.getValue()) |
| 81 | .build(); |
| 82 | } |
| 83 | }); |
| 84 | } |
| 85 | } |