2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onosproject.store.consistent.impl;
19 import java.util.List;
21 import java.util.function.Supplier;
23 import static com.google.common.base.Preconditions.*;
25 import org.onosproject.store.service.ConsistentMapBuilder;
26 import org.onosproject.store.service.DatabaseUpdate;
27 import org.onosproject.store.service.Serializer;
28 import org.onosproject.store.service.Transaction;
29 import org.onosproject.store.service.TransactionContext;
30 import org.onosproject.store.service.TransactionalMap;
32 import com.google.common.collect.Lists;
33 import com.google.common.collect.Maps;
34 import com.google.common.util.concurrent.Futures;
37 * Default TransactionContext implementation.
39 public class DefaultTransactionContext implements TransactionContext {
40 private static final String TX_NOT_OPEN_ERROR = "Transaction Context is not open";
42 @SuppressWarnings("rawtypes")
43 private final Map<String, DefaultTransactionalMap> txMaps = Maps.newConcurrentMap();
44 private boolean isOpen = false;
45 private final Database database;
46 private final long transactionId;
47 private final Supplier<ConsistentMapBuilder> mapBuilderSupplier;
49 public DefaultTransactionContext(long transactionId,
51 Supplier<ConsistentMapBuilder> mapBuilderSupplier) {
52 this.transactionId = transactionId;
53 this.database = checkNotNull(database);
54 this.mapBuilderSupplier = checkNotNull(mapBuilderSupplier);
58 public long transactionId() {
64 checkState(!isOpen, "Transaction Context is already open");
69 public boolean isOpen() {
74 @SuppressWarnings("unchecked")
75 public <K, V> TransactionalMap<K, V> getTransactionalMap(String mapName,
76 Serializer serializer) {
77 checkState(isOpen, TX_NOT_OPEN_ERROR);
78 checkNotNull(mapName);
79 checkNotNull(serializer);
80 return txMaps.computeIfAbsent(mapName, name -> new DefaultTransactionalMap<>(
82 mapBuilderSupplier.get().withName(name).withSerializer(serializer).build(),
87 @SuppressWarnings("unchecked")
89 public void commit() {
90 // TODO: rework commit implementation to be more intuitive
91 checkState(isOpen, TX_NOT_OPEN_ERROR);
92 CommitResponse response = null;
94 List<DatabaseUpdate> updates = Lists.newLinkedList();
95 txMaps.values().forEach(m -> updates.addAll(m.prepareDatabaseUpdates()));
96 Transaction transaction = new DefaultTransaction(transactionId, updates);
97 response = Futures.getUnchecked(database.prepareAndCommit(transaction));
99 if (response != null && !response.success()) {
107 public void abort() {
110 txMaps.values().forEach(m -> m.rollback());