Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [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 | |
| 17 | package org.onosproject.store.consistent.impl; |
| 18 | |
Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [diff] [blame] | 19 | import java.util.List; |
| 20 | import java.util.Map; |
Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [diff] [blame] | 21 | import java.util.Set; |
| 22 | |
| 23 | import org.onlab.util.HexString; |
| 24 | import org.onosproject.store.service.ConsistentMap; |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 25 | import org.onosproject.store.service.DatabaseUpdate; |
Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [diff] [blame] | 26 | import org.onosproject.store.service.Serializer; |
| 27 | import org.onosproject.store.service.TransactionContext; |
| 28 | import org.onosproject.store.service.TransactionalMap; |
Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [diff] [blame] | 29 | import org.onosproject.store.service.Versioned; |
| 30 | |
| 31 | import static com.google.common.base.Preconditions.*; |
| 32 | |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 33 | import com.google.common.base.Objects; |
| 34 | import com.google.common.cache.CacheBuilder; |
| 35 | import com.google.common.cache.CacheLoader; |
| 36 | import com.google.common.cache.LoadingCache; |
Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [diff] [blame] | 37 | import com.google.common.collect.Lists; |
| 38 | import com.google.common.collect.Maps; |
| 39 | import com.google.common.collect.Sets; |
| 40 | |
| 41 | /** |
| 42 | * Default Transactional Map implementation that provides a repeatable reads |
| 43 | * transaction isolation level. |
| 44 | * |
| 45 | * @param <K> key type |
| 46 | * @param <V> value type. |
| 47 | */ |
| 48 | public class DefaultTransactionalMap<K, V> implements TransactionalMap<K, V> { |
| 49 | |
| 50 | private final TransactionContext txContext; |
| 51 | private static final String TX_CLOSED_ERROR = "Transaction is closed"; |
| 52 | private final ConsistentMap<K, V> backingMap; |
| 53 | private final String name; |
| 54 | private final Serializer serializer; |
| 55 | private final Map<K, Versioned<V>> readCache = Maps.newConcurrentMap(); |
| 56 | private final Map<K, V> writeCache = Maps.newConcurrentMap(); |
| 57 | private final Set<K> deleteSet = Sets.newConcurrentHashSet(); |
| 58 | |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 59 | private static final String ERROR_NULL_VALUE = "Null values are not allowed"; |
| 60 | private static final String ERROR_NULL_KEY = "Null key is not allowed"; |
| 61 | |
| 62 | private final LoadingCache<K, String> keyCache = CacheBuilder.newBuilder() |
| 63 | .softValues() |
| 64 | .build(new CacheLoader<K, String>() { |
| 65 | |
| 66 | @Override |
| 67 | public String load(K key) { |
| 68 | return HexString.toHexString(serializer.encode(key)); |
| 69 | } |
| 70 | }); |
| 71 | |
| 72 | protected K dK(String key) { |
| 73 | return serializer.decode(HexString.fromHexString(key)); |
| 74 | } |
| 75 | |
Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [diff] [blame] | 76 | public DefaultTransactionalMap( |
| 77 | String name, |
| 78 | ConsistentMap<K, V> backingMap, |
| 79 | TransactionContext txContext, |
| 80 | Serializer serializer) { |
| 81 | this.name = name; |
| 82 | this.backingMap = backingMap; |
| 83 | this.txContext = txContext; |
| 84 | this.serializer = serializer; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public V get(K key) { |
| 89 | checkState(txContext.isOpen(), TX_CLOSED_ERROR); |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 90 | checkNotNull(key, ERROR_NULL_KEY); |
Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [diff] [blame] | 91 | if (deleteSet.contains(key)) { |
| 92 | return null; |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 93 | } |
| 94 | V latest = writeCache.get(key); |
| 95 | if (latest != null) { |
| 96 | return latest; |
Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [diff] [blame] | 97 | } else { |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 98 | Versioned<V> v = readCache.computeIfAbsent(key, k -> backingMap.get(k)); |
Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [diff] [blame] | 99 | return v != null ? v.value() : null; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public V put(K key, V value) { |
| 105 | checkState(txContext.isOpen(), TX_CLOSED_ERROR); |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 106 | checkNotNull(value, ERROR_NULL_VALUE); |
| 107 | |
| 108 | V latest = get(key); |
| 109 | writeCache.put(key, value); |
Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [diff] [blame] | 110 | deleteSet.remove(key); |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 111 | return latest; |
Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public V remove(K key) { |
| 116 | checkState(txContext.isOpen(), TX_CLOSED_ERROR); |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 117 | V latest = get(key); |
| 118 | if (latest != null) { |
| 119 | writeCache.remove(key); |
| 120 | deleteSet.add(key); |
| 121 | } |
| 122 | return latest; |
Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public boolean remove(K key, V value) { |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 127 | checkState(txContext.isOpen(), TX_CLOSED_ERROR); |
| 128 | checkNotNull(value, ERROR_NULL_VALUE); |
| 129 | V latest = get(key); |
| 130 | if (Objects.equal(value, latest)) { |
Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [diff] [blame] | 131 | remove(key); |
| 132 | return true; |
| 133 | } |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public boolean replace(K key, V oldValue, V newValue) { |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 139 | checkState(txContext.isOpen(), TX_CLOSED_ERROR); |
| 140 | checkNotNull(oldValue, ERROR_NULL_VALUE); |
| 141 | checkNotNull(newValue, ERROR_NULL_VALUE); |
| 142 | V latest = get(key); |
| 143 | if (Objects.equal(oldValue, latest)) { |
Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [diff] [blame] | 144 | put(key, newValue); |
| 145 | return true; |
| 146 | } |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | @Override |
Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [diff] [blame] | 151 | public V putIfAbsent(K key, V value) { |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 152 | checkState(txContext.isOpen(), TX_CLOSED_ERROR); |
| 153 | checkNotNull(value, ERROR_NULL_VALUE); |
| 154 | V latest = get(key); |
| 155 | if (latest == null) { |
Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [diff] [blame] | 156 | put(key, value); |
Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [diff] [blame] | 157 | } |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 158 | return latest; |
Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 161 | protected List<DatabaseUpdate> prepareDatabaseUpdates() { |
| 162 | List<DatabaseUpdate> updates = Lists.newLinkedList(); |
Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [diff] [blame] | 163 | deleteSet.forEach(key -> { |
| 164 | Versioned<V> original = readCache.get(key); |
| 165 | if (original != null) { |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 166 | updates.add(DatabaseUpdate.newBuilder() |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 167 | .withMapName(name) |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 168 | .withType(DatabaseUpdate.Type.REMOVE_IF_VERSION_MATCH) |
| 169 | .withKey(keyCache.getUnchecked(key)) |
Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [diff] [blame] | 170 | .withCurrentVersion(original.version()) |
| 171 | .build()); |
| 172 | } |
| 173 | }); |
| 174 | writeCache.forEach((key, value) -> { |
| 175 | Versioned<V> original = readCache.get(key); |
| 176 | if (original == null) { |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 177 | updates.add(DatabaseUpdate.newBuilder() |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 178 | .withMapName(name) |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 179 | .withType(DatabaseUpdate.Type.PUT_IF_ABSENT) |
| 180 | .withKey(keyCache.getUnchecked(key)) |
| 181 | .withValue(serializer.encode(value)) |
Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [diff] [blame] | 182 | .build()); |
| 183 | } else { |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 184 | updates.add(DatabaseUpdate.newBuilder() |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 185 | .withMapName(name) |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 186 | .withType(DatabaseUpdate.Type.PUT_IF_VERSION_MATCH) |
| 187 | .withKey(keyCache.getUnchecked(key)) |
Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [diff] [blame] | 188 | .withCurrentVersion(original.version()) |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 189 | .withValue(serializer.encode(value)) |
Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [diff] [blame] | 190 | .build()); |
| 191 | } |
| 192 | }); |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 193 | return updates; |
Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Discards all changes made to this transactional map. |
| 198 | */ |
| 199 | protected void rollback() { |
| 200 | readCache.clear(); |
| 201 | writeCache.clear(); |
| 202 | deleteSet.clear(); |
| 203 | } |
| 204 | } |