Madan Jampani | 2546111 | 2015-02-17 14:17:29 -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 | |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 17 | package org.onosproject.store.consistent.impl; |
| 18 | |
| 19 | import java.util.Arrays; |
| 20 | import java.util.Collection; |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 21 | import java.util.HashSet; |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 22 | import java.util.Map; |
| 23 | import java.util.Map.Entry; |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame^] | 24 | import java.util.concurrent.atomic.AtomicLong; |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 25 | import java.util.stream.Collectors; |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 26 | import java.util.Set; |
| 27 | |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 28 | import org.apache.commons.lang3.tuple.Pair; |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 29 | import org.onosproject.store.service.DatabaseUpdate; |
| 30 | import org.onosproject.store.service.Transaction; |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 31 | import org.onosproject.store.service.Versioned; |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 32 | import org.onosproject.store.service.DatabaseUpdate.Type; |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 33 | |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 34 | import com.google.common.base.Objects; |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 35 | import com.google.common.collect.ImmutableList; |
| 36 | import com.google.common.collect.ImmutableSet; |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 37 | import com.google.common.collect.Maps; |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 38 | |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 39 | import net.kuujo.copycat.state.Initializer; |
| 40 | import net.kuujo.copycat.state.StateContext; |
| 41 | |
| 42 | /** |
| 43 | * Default database state. |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 44 | */ |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 45 | public class DefaultDatabaseState implements DatabaseState<String, byte[]> { |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 46 | private Long nextVersion; |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame^] | 47 | private Map<String, AtomicLong> counters; |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 48 | private Map<String, Map<String, Versioned<byte[]>>> tables; |
| 49 | |
| 50 | /** |
| 51 | * This locks map has a structure similar to the "tables" map above and |
| 52 | * holds all the provisional updates made during a transaction's prepare phase. |
| 53 | * The entry value is represented as the tuple: (transactionId, newValue) |
| 54 | * If newValue == null that signifies this update is attempting to |
| 55 | * delete the existing value. |
| 56 | * This map also serves as a lock on the entries that are being updated. |
| 57 | * The presence of a entry in this map indicates that element is |
| 58 | * participating in a transaction and is currently locked for updates. |
| 59 | */ |
| 60 | private Map<String, Map<String, Pair<Long, byte[]>>> locks; |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 61 | |
| 62 | @Initializer |
| 63 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 64 | public void init(StateContext<DatabaseState<String, byte[]>> context) { |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame^] | 65 | counters = context.get("counters"); |
| 66 | if (counters == null) { |
| 67 | counters = Maps.newConcurrentMap(); |
| 68 | context.put("counters", counters); |
| 69 | } |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 70 | tables = context.get("tables"); |
| 71 | if (tables == null) { |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 72 | tables = Maps.newConcurrentMap(); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 73 | context.put("tables", tables); |
| 74 | } |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 75 | locks = context.get("locks"); |
| 76 | if (locks == null) { |
| 77 | locks = Maps.newConcurrentMap(); |
| 78 | context.put("locks", locks); |
| 79 | } |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 80 | nextVersion = context.get("nextVersion"); |
| 81 | if (nextVersion == null) { |
| 82 | nextVersion = new Long(0); |
| 83 | context.put("nextVersion", nextVersion); |
| 84 | } |
| 85 | } |
| 86 | |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 87 | @Override |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 88 | public Set<String> tableNames() { |
| 89 | return new HashSet<>(tables.keySet()); |
| 90 | } |
| 91 | |
| 92 | @Override |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame^] | 93 | public Map<String, Long> counters() { |
| 94 | Map<String, Long> counterMap = Maps.newHashMap(); |
| 95 | counters.forEach((k, v) -> counterMap.put(k, v.get())); |
| 96 | return counterMap; |
| 97 | } |
| 98 | |
| 99 | @Override |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 100 | public int size(String tableName) { |
| 101 | return getTableMap(tableName).size(); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public boolean isEmpty(String tableName) { |
| 106 | return getTableMap(tableName).isEmpty(); |
| 107 | } |
| 108 | |
| 109 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 110 | public boolean containsKey(String tableName, String key) { |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 111 | return getTableMap(tableName).containsKey(key); |
| 112 | } |
| 113 | |
| 114 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 115 | public boolean containsValue(String tableName, byte[] value) { |
| 116 | return getTableMap(tableName).values().stream().anyMatch(v -> Arrays.equals(v.value(), value)); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 120 | public Versioned<byte[]> get(String tableName, String key) { |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 121 | return getTableMap(tableName).get(key); |
| 122 | } |
| 123 | |
| 124 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 125 | public Result<Versioned<byte[]>> put(String tableName, String key, byte[] value) { |
| 126 | return isLockedForUpdates(tableName, key) |
| 127 | ? Result.locked() |
| 128 | : Result.ok(getTableMap(tableName).put(key, new Versioned<>(value, ++nextVersion))); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 132 | public Result<Versioned<byte[]>> remove(String tableName, String key) { |
| 133 | return isLockedForUpdates(tableName, key) |
| 134 | ? Result.locked() |
| 135 | : Result.ok(getTableMap(tableName).remove(key)); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 139 | public Result<Void> clear(String tableName) { |
| 140 | if (areTransactionsInProgress(tableName)) { |
| 141 | return Result.locked(); |
| 142 | } |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 143 | getTableMap(tableName).clear(); |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 144 | return Result.ok(null); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 148 | public Set<String> keySet(String tableName) { |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 149 | return ImmutableSet.copyOf(getTableMap(tableName).keySet()); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 153 | public Collection<Versioned<byte[]>> values(String tableName) { |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 154 | return ImmutableList.copyOf(getTableMap(tableName).values()); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 158 | public Set<Entry<String, Versioned<byte[]>>> entrySet(String tableName) { |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 159 | return ImmutableSet.copyOf(getTableMap(tableName) |
| 160 | .entrySet() |
| 161 | .stream() |
| 162 | .map(entry -> Pair.of(entry.getKey(), entry.getValue())) |
| 163 | .collect(Collectors.toSet())); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 167 | public Result<Versioned<byte[]>> putIfAbsent(String tableName, String key, byte[] value) { |
| 168 | if (isLockedForUpdates(tableName, key)) { |
| 169 | return Result.locked(); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 170 | } |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 171 | Versioned<byte[]> existingValue = get(tableName, key); |
| 172 | Versioned<byte[]> currentValue = existingValue != null ? existingValue : put(tableName, key, value).value(); |
| 173 | return Result.ok(currentValue); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 177 | public Result<Boolean> remove(String tableName, String key, byte[] value) { |
| 178 | if (isLockedForUpdates(tableName, key)) { |
| 179 | return Result.locked(); |
| 180 | } |
| 181 | Versioned<byte[]> existing = get(tableName, key); |
| 182 | if (existing != null && Arrays.equals(existing.value(), value)) { |
| 183 | getTableMap(tableName).remove(key); |
| 184 | return Result.ok(true); |
| 185 | } |
| 186 | return Result.ok(false); |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public Result<Boolean> remove(String tableName, String key, long version) { |
| 191 | if (isLockedForUpdates(tableName, key)) { |
| 192 | return Result.locked(); |
| 193 | } |
| 194 | Versioned<byte[]> existing = get(tableName, key); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 195 | if (existing != null && existing.version() == version) { |
| 196 | remove(tableName, key); |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 197 | return Result.ok(true); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 198 | } |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 199 | return Result.ok(false); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 203 | public Result<Boolean> replace(String tableName, String key, byte[] oldValue, byte[] newValue) { |
| 204 | if (isLockedForUpdates(tableName, key)) { |
| 205 | return Result.locked(); |
| 206 | } |
| 207 | Versioned<byte[]> existing = get(tableName, key); |
| 208 | if (existing != null && Arrays.equals(existing.value(), oldValue)) { |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 209 | put(tableName, key, newValue); |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 210 | return Result.ok(true); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 211 | } |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 212 | return Result.ok(false); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 216 | public Result<Boolean> replace(String tableName, String key, long oldVersion, byte[] newValue) { |
| 217 | if (isLockedForUpdates(tableName, key)) { |
| 218 | return Result.locked(); |
| 219 | } |
| 220 | Versioned<byte[]> existing = get(tableName, key); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 221 | if (existing != null && existing.version() == oldVersion) { |
| 222 | put(tableName, key, newValue); |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 223 | return Result.ok(true); |
| 224 | } |
| 225 | return Result.ok(false); |
| 226 | } |
| 227 | |
| 228 | @Override |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame^] | 229 | public Long nextValue(String counterName) { |
| 230 | return getCounter(counterName).incrementAndGet(); |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public Long currentValue(String counterName) { |
| 235 | return getCounter(counterName).get(); |
| 236 | } |
| 237 | |
| 238 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 239 | public boolean prepareAndCommit(Transaction transaction) { |
| 240 | if (prepare(transaction)) { |
| 241 | return commit(transaction); |
| 242 | } |
| 243 | return false; |
| 244 | } |
| 245 | |
| 246 | @Override |
| 247 | public boolean prepare(Transaction transaction) { |
| 248 | if (transaction.updates().stream().anyMatch(update -> |
| 249 | isLockedByAnotherTransaction(update.tableName(), |
| 250 | update.key(), |
| 251 | transaction.id()))) { |
| 252 | return false; |
| 253 | } |
| 254 | |
| 255 | if (transaction.updates().stream().allMatch(this::isUpdatePossible)) { |
| 256 | transaction.updates().forEach(update -> doProvisionalUpdate(update, transaction.id())); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 257 | return true; |
| 258 | } |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 263 | public boolean commit(Transaction transaction) { |
| 264 | transaction.updates().forEach(update -> commitProvisionalUpdate(update, transaction.id())); |
| 265 | return true; |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 266 | } |
| 267 | |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 268 | @Override |
| 269 | public boolean rollback(Transaction transaction) { |
| 270 | transaction.updates().forEach(update -> undoProvisionalUpdate(update, transaction.id())); |
| 271 | return true; |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 272 | } |
| 273 | |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 274 | private Map<String, Versioned<byte[]>> getTableMap(String tableName) { |
| 275 | return tables.computeIfAbsent(tableName, name -> Maps.newConcurrentMap()); |
| 276 | } |
| 277 | |
| 278 | private Map<String, Pair<Long, byte[]>> getLockMap(String tableName) { |
| 279 | return locks.computeIfAbsent(tableName, name -> Maps.newConcurrentMap()); |
| 280 | } |
| 281 | |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame^] | 282 | private AtomicLong getCounter(String counterName) { |
| 283 | return counters.computeIfAbsent(counterName, name -> new AtomicLong(0)); |
| 284 | } |
| 285 | |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 286 | private boolean isUpdatePossible(DatabaseUpdate update) { |
| 287 | Versioned<byte[]> existingEntry = get(update.tableName(), update.key()); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 288 | switch (update.type()) { |
| 289 | case PUT: |
| 290 | case REMOVE: |
| 291 | return true; |
| 292 | case PUT_IF_ABSENT: |
| 293 | return existingEntry == null; |
| 294 | case PUT_IF_VERSION_MATCH: |
| 295 | return existingEntry != null && existingEntry.version() == update.currentVersion(); |
| 296 | case PUT_IF_VALUE_MATCH: |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 297 | return existingEntry != null && Arrays.equals(existingEntry.value(), update.currentValue()); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 298 | case REMOVE_IF_VERSION_MATCH: |
| 299 | return existingEntry == null || existingEntry.version() == update.currentVersion(); |
| 300 | case REMOVE_IF_VALUE_MATCH: |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 301 | return existingEntry == null || Arrays.equals(existingEntry.value(), update.currentValue()); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 302 | default: |
| 303 | throw new IllegalStateException("Unsupported type: " + update.type()); |
| 304 | } |
| 305 | } |
| 306 | |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 307 | private void doProvisionalUpdate(DatabaseUpdate update, long transactionId) { |
| 308 | Map<String, Pair<Long, byte[]>> lockMap = getLockMap(update.tableName()); |
| 309 | switch (update.type()) { |
| 310 | case PUT: |
| 311 | case PUT_IF_ABSENT: |
| 312 | case PUT_IF_VERSION_MATCH: |
| 313 | case PUT_IF_VALUE_MATCH: |
| 314 | lockMap.put(update.key(), Pair.of(transactionId, update.value())); |
| 315 | break; |
| 316 | case REMOVE: |
| 317 | case REMOVE_IF_VERSION_MATCH: |
| 318 | case REMOVE_IF_VALUE_MATCH: |
| 319 | lockMap.put(update.key(), null); |
| 320 | break; |
| 321 | default: |
| 322 | throw new IllegalStateException("Unsupported type: " + update.type()); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 323 | } |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | private void commitProvisionalUpdate(DatabaseUpdate update, long transactionId) { |
| 327 | String tableName = update.tableName(); |
| 328 | String key = update.key(); |
| 329 | Type type = update.type(); |
| 330 | Pair<Long, byte[]> provisionalUpdate = getLockMap(tableName).get(key); |
| 331 | if (Objects.equal(transactionId, provisionalUpdate.getLeft())) { |
| 332 | getLockMap(tableName).remove(key); |
| 333 | } else { |
| 334 | return; |
| 335 | } |
| 336 | |
| 337 | switch (type) { |
| 338 | case PUT: |
| 339 | case PUT_IF_ABSENT: |
| 340 | case PUT_IF_VERSION_MATCH: |
| 341 | case PUT_IF_VALUE_MATCH: |
| 342 | put(tableName, key, provisionalUpdate.getRight()); |
| 343 | break; |
| 344 | case REMOVE: |
| 345 | case REMOVE_IF_VERSION_MATCH: |
| 346 | case REMOVE_IF_VALUE_MATCH: |
| 347 | remove(tableName, key); |
| 348 | break; |
| 349 | default: |
| 350 | break; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | private void undoProvisionalUpdate(DatabaseUpdate update, long transactionId) { |
| 355 | String tableName = update.tableName(); |
| 356 | String key = update.key(); |
| 357 | Pair<Long, byte[]> provisionalUpdate = getLockMap(tableName).get(key); |
| 358 | if (provisionalUpdate == null) { |
| 359 | return; |
| 360 | } |
| 361 | if (Objects.equal(transactionId, provisionalUpdate.getLeft())) { |
| 362 | getLockMap(tableName).remove(key); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | private boolean isLockedByAnotherTransaction(String tableName, String key, long transactionId) { |
| 367 | Pair<Long, byte[]> update = getLockMap(tableName).get(key); |
| 368 | return update != null && !Objects.equal(transactionId, update.getLeft()); |
| 369 | } |
| 370 | |
| 371 | private boolean isLockedForUpdates(String tableName, String key) { |
| 372 | return getLockMap(tableName).containsKey(key); |
| 373 | } |
| 374 | |
| 375 | private boolean areTransactionsInProgress(String tableName) { |
| 376 | return !getLockMap(tableName).isEmpty(); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 377 | } |
| 378 | } |