blob: f1bba2543b2f3a03aa0ef843e0d4c3a14598b7b2 [file] [log] [blame]
Madan Jampani25461112015-02-17 14:17:29 -08001/*
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 Jampani94c23532015-02-05 17:40:01 -080017package org.onosproject.store.consistent.impl;
18
19import java.util.Arrays;
20import java.util.Collection;
Madan Jampania89f8f92015-04-01 14:39:54 -070021import java.util.HashSet;
Madan Jampani63c659f2015-06-11 00:52:58 -070022import java.util.LinkedList;
Madan Jampani94c23532015-02-05 17:40:01 -080023import java.util.Map;
24import java.util.Map.Entry;
Madan Jampani63c659f2015-06-11 00:52:58 -070025import java.util.Queue;
Madan Jampanib5d72d52015-04-03 16:53:50 -070026import java.util.concurrent.atomic.AtomicLong;
Madan Jampani393e0f02015-02-12 07:35:39 +053027import java.util.stream.Collectors;
Madan Jampani94c23532015-02-05 17:40:01 -080028import java.util.Set;
29
Madan Jampani393e0f02015-02-12 07:35:39 +053030import org.apache.commons.lang3.tuple.Pair;
Madan Jampani63c659f2015-06-11 00:52:58 -070031import org.onosproject.cluster.NodeId;
Madan Jampanibff6d8f2015-03-31 16:53:47 -070032import org.onosproject.store.service.DatabaseUpdate;
33import org.onosproject.store.service.Transaction;
Madan Jampani393e0f02015-02-12 07:35:39 +053034import org.onosproject.store.service.Versioned;
Madan Jampanibff6d8f2015-03-31 16:53:47 -070035import org.onosproject.store.service.DatabaseUpdate.Type;
Madan Jampani346d4f52015-05-04 11:09:39 -070036
Madan Jampanibff6d8f2015-03-31 16:53:47 -070037import com.google.common.base.Objects;
Madan Jampani393e0f02015-02-12 07:35:39 +053038import com.google.common.collect.ImmutableList;
39import com.google.common.collect.ImmutableSet;
Madan Jampanibff6d8f2015-03-31 16:53:47 -070040import com.google.common.collect.Maps;
Madan Jampani393e0f02015-02-12 07:35:39 +053041
Madan Jampani94c23532015-02-05 17:40:01 -080042import net.kuujo.copycat.state.Initializer;
43import net.kuujo.copycat.state.StateContext;
44
45/**
46 * Default database state.
Madan Jampani94c23532015-02-05 17:40:01 -080047 */
Madan Jampanibff6d8f2015-03-31 16:53:47 -070048public class DefaultDatabaseState implements DatabaseState<String, byte[]> {
Madan Jampani94c23532015-02-05 17:40:01 -080049 private Long nextVersion;
Madan Jampanib5d72d52015-04-03 16:53:50 -070050 private Map<String, AtomicLong> counters;
Madan Jampanibff6d8f2015-03-31 16:53:47 -070051 private Map<String, Map<String, Versioned<byte[]>>> tables;
Madan Jampani63c659f2015-06-11 00:52:58 -070052 private Map<String, Queue<byte[]>> queues;
53 private Map<String, Set<NodeId>> queueUpdateNotificationTargets;
Madan Jampanibff6d8f2015-03-31 16:53:47 -070054
55 /**
56 * This locks map has a structure similar to the "tables" map above and
57 * holds all the provisional updates made during a transaction's prepare phase.
58 * The entry value is represented as the tuple: (transactionId, newValue)
59 * If newValue == null that signifies this update is attempting to
60 * delete the existing value.
61 * This map also serves as a lock on the entries that are being updated.
62 * The presence of a entry in this map indicates that element is
63 * participating in a transaction and is currently locked for updates.
64 */
Ayaka Koshibe2c6b7ef2015-04-28 17:18:05 -070065 private Map<String, Map<String, Update>> locks;
Madan Jampani94c23532015-02-05 17:40:01 -080066
67 @Initializer
68 @Override
Madan Jampanibff6d8f2015-03-31 16:53:47 -070069 public void init(StateContext<DatabaseState<String, byte[]>> context) {
Madan Jampanib5d72d52015-04-03 16:53:50 -070070 counters = context.get("counters");
71 if (counters == null) {
72 counters = Maps.newConcurrentMap();
73 context.put("counters", counters);
74 }
Madan Jampani94c23532015-02-05 17:40:01 -080075 tables = context.get("tables");
76 if (tables == null) {
Madan Jampanibff6d8f2015-03-31 16:53:47 -070077 tables = Maps.newConcurrentMap();
Madan Jampani94c23532015-02-05 17:40:01 -080078 context.put("tables", tables);
79 }
Madan Jampanibff6d8f2015-03-31 16:53:47 -070080 locks = context.get("locks");
81 if (locks == null) {
82 locks = Maps.newConcurrentMap();
83 context.put("locks", locks);
84 }
Madan Jampani63c659f2015-06-11 00:52:58 -070085 queues = context.get("queues");
86 if (queues == null) {
87 queues = Maps.newConcurrentMap();
88 context.put("queues", queues);
89 }
90 queueUpdateNotificationTargets = context.get("queueUpdateNotificationTargets");
91 if (queueUpdateNotificationTargets == null) {
92 queueUpdateNotificationTargets = Maps.newConcurrentMap();
93 context.put("queueUpdateNotificationTargets", queueUpdateNotificationTargets);
94 }
Madan Jampani94c23532015-02-05 17:40:01 -080095 nextVersion = context.get("nextVersion");
96 if (nextVersion == null) {
97 nextVersion = new Long(0);
98 context.put("nextVersion", nextVersion);
99 }
100 }
101
Madan Jampani94c23532015-02-05 17:40:01 -0800102 @Override
Madan Jampania89f8f92015-04-01 14:39:54 -0700103 public Set<String> tableNames() {
104 return new HashSet<>(tables.keySet());
105 }
106
107 @Override
Madan Jampanib5d72d52015-04-03 16:53:50 -0700108 public Map<String, Long> counters() {
109 Map<String, Long> counterMap = Maps.newHashMap();
110 counters.forEach((k, v) -> counterMap.put(k, v.get()));
111 return counterMap;
112 }
113
114 @Override
Madan Jampani94c23532015-02-05 17:40:01 -0800115 public int size(String tableName) {
116 return getTableMap(tableName).size();
117 }
118
119 @Override
120 public boolean isEmpty(String tableName) {
121 return getTableMap(tableName).isEmpty();
122 }
123
124 @Override
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700125 public boolean containsKey(String tableName, String key) {
Madan Jampani94c23532015-02-05 17:40:01 -0800126 return getTableMap(tableName).containsKey(key);
127 }
128
129 @Override
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700130 public boolean containsValue(String tableName, byte[] value) {
131 return getTableMap(tableName).values().stream().anyMatch(v -> Arrays.equals(v.value(), value));
Madan Jampani94c23532015-02-05 17:40:01 -0800132 }
133
134 @Override
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700135 public Versioned<byte[]> get(String tableName, String key) {
Madan Jampani94c23532015-02-05 17:40:01 -0800136 return getTableMap(tableName).get(key);
137 }
138
139 @Override
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700140 public Result<Versioned<byte[]>> put(String tableName, String key, byte[] value) {
141 return isLockedForUpdates(tableName, key)
142 ? Result.locked()
143 : Result.ok(getTableMap(tableName).put(key, new Versioned<>(value, ++nextVersion)));
Madan Jampani94c23532015-02-05 17:40:01 -0800144 }
145
146 @Override
Madan Jampani346d4f52015-05-04 11:09:39 -0700147 public Result<UpdateResult<Versioned<byte[]>>> putAndGet(String tableName,
148 String key,
149 byte[] value) {
150 if (isLockedForUpdates(tableName, key)) {
151 return Result.locked();
152 } else {
153 Versioned<byte[]> newValue = new Versioned<>(value, ++nextVersion);
154 Versioned<byte[]> oldValue = getTableMap(tableName).put(key, newValue);
155 return Result.ok(new UpdateResult<>(true, oldValue, newValue));
156 }
157 }
158
159 @Override
160 public Result<UpdateResult<Versioned<byte[]>>> putIfAbsentAndGet(String tableName,
161 String key,
162 byte[] value) {
163 if (isLockedForUpdates(tableName, key)) {
164 return Result.locked();
165 }
166 Versioned<byte[]> currentValue = getTableMap(tableName).get(key);
167 if (currentValue != null) {
168 return Result.ok(new UpdateResult<>(false, currentValue, currentValue));
169 } else {
170 Versioned<byte[]> newValue = new Versioned<>(value, ++nextVersion);
171 getTableMap(tableName).put(key, newValue);
172 return Result.ok(new UpdateResult<>(true, null, newValue));
173 }
174 }
175
176 @Override
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700177 public Result<Versioned<byte[]>> remove(String tableName, String key) {
178 return isLockedForUpdates(tableName, key)
179 ? Result.locked()
180 : Result.ok(getTableMap(tableName).remove(key));
Madan Jampani94c23532015-02-05 17:40:01 -0800181 }
182
183 @Override
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700184 public Result<Void> clear(String tableName) {
185 if (areTransactionsInProgress(tableName)) {
186 return Result.locked();
187 }
Madan Jampani94c23532015-02-05 17:40:01 -0800188 getTableMap(tableName).clear();
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700189 return Result.ok(null);
Madan Jampani94c23532015-02-05 17:40:01 -0800190 }
191
192 @Override
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700193 public Set<String> keySet(String tableName) {
Madan Jampani393e0f02015-02-12 07:35:39 +0530194 return ImmutableSet.copyOf(getTableMap(tableName).keySet());
Madan Jampani94c23532015-02-05 17:40:01 -0800195 }
196
197 @Override
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700198 public Collection<Versioned<byte[]>> values(String tableName) {
Madan Jampani393e0f02015-02-12 07:35:39 +0530199 return ImmutableList.copyOf(getTableMap(tableName).values());
Madan Jampani94c23532015-02-05 17:40:01 -0800200 }
201
202 @Override
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700203 public Set<Entry<String, Versioned<byte[]>>> entrySet(String tableName) {
Madan Jampani393e0f02015-02-12 07:35:39 +0530204 return ImmutableSet.copyOf(getTableMap(tableName)
205 .entrySet()
206 .stream()
207 .map(entry -> Pair.of(entry.getKey(), entry.getValue()))
208 .collect(Collectors.toSet()));
Madan Jampani94c23532015-02-05 17:40:01 -0800209 }
210
211 @Override
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700212 public Result<Versioned<byte[]>> putIfAbsent(String tableName, String key, byte[] value) {
213 if (isLockedForUpdates(tableName, key)) {
214 return Result.locked();
Madan Jampani94c23532015-02-05 17:40:01 -0800215 }
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700216 Versioned<byte[]> existingValue = get(tableName, key);
217 Versioned<byte[]> currentValue = existingValue != null ? existingValue : put(tableName, key, value).value();
218 return Result.ok(currentValue);
Madan Jampani94c23532015-02-05 17:40:01 -0800219 }
220
221 @Override
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700222 public Result<Boolean> remove(String tableName, String key, byte[] value) {
223 if (isLockedForUpdates(tableName, key)) {
224 return Result.locked();
225 }
226 Versioned<byte[]> existing = get(tableName, key);
227 if (existing != null && Arrays.equals(existing.value(), value)) {
228 getTableMap(tableName).remove(key);
229 return Result.ok(true);
230 }
231 return Result.ok(false);
232 }
233
234 @Override
235 public Result<Boolean> remove(String tableName, String key, long version) {
236 if (isLockedForUpdates(tableName, key)) {
237 return Result.locked();
238 }
239 Versioned<byte[]> existing = get(tableName, key);
Madan Jampani94c23532015-02-05 17:40:01 -0800240 if (existing != null && existing.version() == version) {
241 remove(tableName, key);
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700242 return Result.ok(true);
Madan Jampani94c23532015-02-05 17:40:01 -0800243 }
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700244 return Result.ok(false);
Madan Jampani94c23532015-02-05 17:40:01 -0800245 }
246
247 @Override
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700248 public Result<Boolean> replace(String tableName, String key, byte[] oldValue, byte[] newValue) {
249 if (isLockedForUpdates(tableName, key)) {
250 return Result.locked();
251 }
252 Versioned<byte[]> existing = get(tableName, key);
253 if (existing != null && Arrays.equals(existing.value(), oldValue)) {
Madan Jampani94c23532015-02-05 17:40:01 -0800254 put(tableName, key, newValue);
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700255 return Result.ok(true);
Madan Jampani94c23532015-02-05 17:40:01 -0800256 }
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700257 return Result.ok(false);
Madan Jampani94c23532015-02-05 17:40:01 -0800258 }
259
260 @Override
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700261 public Result<Boolean> replace(String tableName, String key, long oldVersion, byte[] newValue) {
262 if (isLockedForUpdates(tableName, key)) {
263 return Result.locked();
264 }
265 Versioned<byte[]> existing = get(tableName, key);
Madan Jampani94c23532015-02-05 17:40:01 -0800266 if (existing != null && existing.version() == oldVersion) {
267 put(tableName, key, newValue);
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700268 return Result.ok(true);
269 }
270 return Result.ok(false);
271 }
272
273 @Override
Madan Jampani346d4f52015-05-04 11:09:39 -0700274 public Result<UpdateResult<Versioned<byte[]>>> replaceAndGet(
275 String tableName, String key, long oldVersion, byte[] newValue) {
276 if (isLockedForUpdates(tableName, key)) {
277 return Result.locked();
278 }
279 boolean updated = false;
280 Versioned<byte[]> previous = get(tableName, key);
281 Versioned<byte[]> current = previous;
282 if (previous != null && previous.version() == oldVersion) {
283 current = new Versioned<>(newValue, ++nextVersion);
284 getTableMap(tableName).put(key, current);
285 updated = true;
286 }
287 return Result.ok(new UpdateResult<>(updated, previous, current));
288 }
289
290 @Override
Madan Jampani55ac1342015-05-04 19:05:04 -0700291 public Long counterAddAndGet(String counterName, long delta) {
292 return getCounter(counterName).addAndGet(delta);
Madan Jampani04aeb452015-05-02 16:12:24 -0700293 }
294
295 @Override
296 public Long counterGetAndAdd(String counterName, long delta) {
297 return getCounter(counterName).getAndAdd(delta);
298 }
299
300 @Override
301 public Long counterGet(String counterName) {
Madan Jampanib5d72d52015-04-03 16:53:50 -0700302 return getCounter(counterName).get();
303 }
304
305 @Override
Madan Jampani63c659f2015-06-11 00:52:58 -0700306 public Long queueSize(String queueName) {
307 return Long.valueOf(getQueue(queueName).size());
308 }
309
310 @Override
311 public byte[] queuePeek(String queueName) {
312 Queue<byte[]> queue = getQueue(queueName);
313 return queue.peek();
314 }
315
316 @Override
317 public byte[] queuePop(String queueName, NodeId requestor) {
318 Queue<byte[]> queue = getQueue(queueName);
319 if (queue.size() == 0 && requestor != null) {
320 getQueueUpdateNotificationTargets(queueName).add(requestor);
321 return null;
322 } else {
323 return queue.remove();
324 }
325 }
326
327 @Override
328 public Set<NodeId> queuePush(String queueName, byte[] entry) {
329 getQueue(queueName).add(entry);
330 Set<NodeId> notifyList = ImmutableSet.copyOf(getQueueUpdateNotificationTargets(queueName));
331 getQueueUpdateNotificationTargets(queueName).clear();
332 return notifyList;
333 }
334
335 @Override
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700336 public boolean prepareAndCommit(Transaction transaction) {
337 if (prepare(transaction)) {
338 return commit(transaction);
339 }
340 return false;
341 }
342
343 @Override
344 public boolean prepare(Transaction transaction) {
345 if (transaction.updates().stream().anyMatch(update ->
346 isLockedByAnotherTransaction(update.tableName(),
347 update.key(),
348 transaction.id()))) {
349 return false;
350 }
351
352 if (transaction.updates().stream().allMatch(this::isUpdatePossible)) {
353 transaction.updates().forEach(update -> doProvisionalUpdate(update, transaction.id()));
Madan Jampani94c23532015-02-05 17:40:01 -0800354 return true;
355 }
356 return false;
357 }
358
359 @Override
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700360 public boolean commit(Transaction transaction) {
361 transaction.updates().forEach(update -> commitProvisionalUpdate(update, transaction.id()));
362 return true;
Madan Jampani94c23532015-02-05 17:40:01 -0800363 }
364
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700365 @Override
366 public boolean rollback(Transaction transaction) {
367 transaction.updates().forEach(update -> undoProvisionalUpdate(update, transaction.id()));
368 return true;
Madan Jampani94c23532015-02-05 17:40:01 -0800369 }
370
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700371 private Map<String, Versioned<byte[]>> getTableMap(String tableName) {
372 return tables.computeIfAbsent(tableName, name -> Maps.newConcurrentMap());
373 }
374
Ayaka Koshibe2c6b7ef2015-04-28 17:18:05 -0700375 private Map<String, Update> getLockMap(String tableName) {
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700376 return locks.computeIfAbsent(tableName, name -> Maps.newConcurrentMap());
377 }
378
Madan Jampanib5d72d52015-04-03 16:53:50 -0700379 private AtomicLong getCounter(String counterName) {
380 return counters.computeIfAbsent(counterName, name -> new AtomicLong(0));
381 }
382
Madan Jampani63c659f2015-06-11 00:52:58 -0700383 private Queue<byte[]> getQueue(String queueName) {
384 return queues.computeIfAbsent(queueName, name -> new LinkedList<>());
385 }
386
387 private Set<NodeId> getQueueUpdateNotificationTargets(String queueName) {
388 return queueUpdateNotificationTargets.computeIfAbsent(queueName, name -> new HashSet<>());
389 }
390
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700391 private boolean isUpdatePossible(DatabaseUpdate update) {
392 Versioned<byte[]> existingEntry = get(update.tableName(), update.key());
Madan Jampani94c23532015-02-05 17:40:01 -0800393 switch (update.type()) {
394 case PUT:
395 case REMOVE:
396 return true;
397 case PUT_IF_ABSENT:
398 return existingEntry == null;
399 case PUT_IF_VERSION_MATCH:
400 return existingEntry != null && existingEntry.version() == update.currentVersion();
401 case PUT_IF_VALUE_MATCH:
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700402 return existingEntry != null && Arrays.equals(existingEntry.value(), update.currentValue());
Madan Jampani94c23532015-02-05 17:40:01 -0800403 case REMOVE_IF_VERSION_MATCH:
404 return existingEntry == null || existingEntry.version() == update.currentVersion();
405 case REMOVE_IF_VALUE_MATCH:
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700406 return existingEntry == null || Arrays.equals(existingEntry.value(), update.currentValue());
Madan Jampani94c23532015-02-05 17:40:01 -0800407 default:
408 throw new IllegalStateException("Unsupported type: " + update.type());
409 }
410 }
411
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700412 private void doProvisionalUpdate(DatabaseUpdate update, long transactionId) {
Ayaka Koshibe2c6b7ef2015-04-28 17:18:05 -0700413 Map<String, Update> lockMap = getLockMap(update.tableName());
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700414 switch (update.type()) {
415 case PUT:
416 case PUT_IF_ABSENT:
417 case PUT_IF_VERSION_MATCH:
418 case PUT_IF_VALUE_MATCH:
Ayaka Koshibe2c6b7ef2015-04-28 17:18:05 -0700419 lockMap.put(update.key(), new Update(transactionId, update.value()));
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700420 break;
421 case REMOVE:
422 case REMOVE_IF_VERSION_MATCH:
423 case REMOVE_IF_VALUE_MATCH:
Ayaka Koshibe2c6b7ef2015-04-28 17:18:05 -0700424 lockMap.put(update.key(), new Update(transactionId, null));
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700425 break;
426 default:
427 throw new IllegalStateException("Unsupported type: " + update.type());
Madan Jampani94c23532015-02-05 17:40:01 -0800428 }
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700429 }
430
431 private void commitProvisionalUpdate(DatabaseUpdate update, long transactionId) {
432 String tableName = update.tableName();
433 String key = update.key();
434 Type type = update.type();
Ayaka Koshibe2c6b7ef2015-04-28 17:18:05 -0700435 Update provisionalUpdate = getLockMap(tableName).get(key);
436 if (Objects.equal(transactionId, provisionalUpdate.transactionId())) {
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700437 getLockMap(tableName).remove(key);
438 } else {
439 return;
440 }
441
442 switch (type) {
443 case PUT:
444 case PUT_IF_ABSENT:
445 case PUT_IF_VERSION_MATCH:
446 case PUT_IF_VALUE_MATCH:
Ayaka Koshibe2c6b7ef2015-04-28 17:18:05 -0700447 put(tableName, key, provisionalUpdate.value());
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700448 break;
449 case REMOVE:
450 case REMOVE_IF_VERSION_MATCH:
451 case REMOVE_IF_VALUE_MATCH:
452 remove(tableName, key);
453 break;
454 default:
455 break;
456 }
457 }
458
459 private void undoProvisionalUpdate(DatabaseUpdate update, long transactionId) {
460 String tableName = update.tableName();
461 String key = update.key();
Ayaka Koshibe2c6b7ef2015-04-28 17:18:05 -0700462 Update provisionalUpdate = getLockMap(tableName).get(key);
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700463 if (provisionalUpdate == null) {
464 return;
465 }
Ayaka Koshibe2c6b7ef2015-04-28 17:18:05 -0700466 if (Objects.equal(transactionId, provisionalUpdate.transactionId())) {
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700467 getLockMap(tableName).remove(key);
468 }
469 }
470
471 private boolean isLockedByAnotherTransaction(String tableName, String key, long transactionId) {
Ayaka Koshibe2c6b7ef2015-04-28 17:18:05 -0700472 Update update = getLockMap(tableName).get(key);
473 return update != null && !Objects.equal(transactionId, update.transactionId());
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700474 }
475
476 private boolean isLockedForUpdates(String tableName, String key) {
477 return getLockMap(tableName).containsKey(key);
478 }
479
480 private boolean areTransactionsInProgress(String tableName) {
481 return !getLockMap(tableName).isEmpty();
Madan Jampani94c23532015-02-05 17:40:01 -0800482 }
Ayaka Koshibe2c6b7ef2015-04-28 17:18:05 -0700483
484 private class Update {
485 private final long transactionId;
486 private final byte[] value;
487
488 public Update(long txId, byte[] value) {
489 this.transactionId = txId;
490 this.value = value;
491 }
492
493 public long transactionId() {
494 return this.transactionId;
495 }
496
497 public byte[] value() {
498 return this.value;
499 }
500 }
Madan Jampani94c23532015-02-05 17:40:01 -0800501}