blob: 91f4bf6e7d1d8b1e5fedd75dd1a925a3799a19e9 [file] [log] [blame]
Madan Jampani619453b2015-07-22 23:47:09 -07001/*
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 */
Madan Jampanif4c88502016-01-21 12:35:36 -080016package org.onosproject.store.primitives.impl;
Ayaka Koshibe3a321562015-04-29 13:24:07 -070017
Madan Jampanicadd70b2016-02-08 13:45:43 -080018import java.util.concurrent.CompletableFuture;
19import java.util.function.Function;
20import java.util.function.Supplier;
21
22import org.onosproject.store.primitives.TransactionId;
23import org.onosproject.store.primitives.resources.impl.CommitResult;
24import org.onosproject.store.service.ConsistentMapBuilder;
Ayaka Koshibe3a321562015-04-29 13:24:07 -070025import org.onosproject.store.service.TransactionContext;
26import org.onosproject.store.service.TransactionContextBuilder;
27
28/**
29 * The default implementation of a transaction context builder. This builder
30 * generates a {@link DefaultTransactionContext}.
31 */
Madan Jampanicadd70b2016-02-08 13:45:43 -080032public class DefaultTransactionContextBuilder extends TransactionContextBuilder {
Ayaka Koshibe3a321562015-04-29 13:24:07 -070033
Madan Jampanicadd70b2016-02-08 13:45:43 -080034 private final Supplier<ConsistentMapBuilder> mapBuilderSupplier;
35 private final Function<Transaction, CompletableFuture<CommitResult>> transactionCommitter;
36 private final TransactionId transactionId;
Ayaka Koshibe3a321562015-04-29 13:24:07 -070037
Madan Jampanicadd70b2016-02-08 13:45:43 -080038 public DefaultTransactionContextBuilder(Supplier<ConsistentMapBuilder> mapBuilderSupplier,
39 Function<Transaction, CompletableFuture<CommitResult>> transactionCommiter,
40 TransactionId transactionId) {
41 this.mapBuilderSupplier = mapBuilderSupplier;
42 this.transactionCommitter = transactionCommiter;
Ayaka Koshibe3a321562015-04-29 13:24:07 -070043 this.transactionId = transactionId;
44 }
45
46 @Override
Ayaka Koshibe3a321562015-04-29 13:24:07 -070047 public TransactionContext build() {
Madan Jampanicadd70b2016-02-08 13:45:43 -080048 return new DefaultTransactionContext(transactionId, transactionCommitter, () -> {
49 ConsistentMapBuilder mapBuilder = mapBuilderSupplier.get();
50 if (partitionsDisabled()) {
Madan Jampani538be742016-02-10 14:55:38 -080051 mapBuilder = (ConsistentMapBuilder) mapBuilder.withPartitionsDisabled();
Madan Jampanicadd70b2016-02-08 13:45:43 -080052 }
53 return mapBuilder;
54 });
Ayaka Koshibe3a321562015-04-29 13:24:07 -070055 }
Ayaka Koshibe3a321562015-04-29 13:24:07 -070056}