Madan Jampani | cadd70b | 2016-02-08 13:45:43 -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 | package org.onosproject.store.primitives.impl; |
| 17 | |
| 18 | import java.util.List; |
| 19 | |
Madan Jampani | 74da78b | 2016-02-09 21:18:36 -0800 | [diff] [blame] | 20 | import org.onosproject.store.primitives.MapUpdate; |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 21 | import org.onosproject.store.primitives.TransactionId; |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 22 | |
| 23 | import com.google.common.base.MoreObjects; |
| 24 | import com.google.common.collect.ImmutableList; |
| 25 | |
| 26 | /** |
| 27 | * An immutable transaction object. |
| 28 | */ |
| 29 | public class Transaction { |
| 30 | |
| 31 | enum State { |
| 32 | /** |
| 33 | * Indicates a new transaction that is about to be prepared. All transactions |
| 34 | * start their life in this state. |
| 35 | */ |
| 36 | PREPARING, |
| 37 | |
| 38 | /** |
| 39 | * Indicates a transaction that is successfully prepared i.e. all participants voted to commit |
| 40 | */ |
| 41 | PREPARED, |
| 42 | |
| 43 | /** |
| 44 | * Indicates a transaction that is about to be committed. |
| 45 | */ |
| 46 | COMMITTING, |
| 47 | |
| 48 | /** |
| 49 | * Indicates a transaction that has successfully committed. |
| 50 | */ |
| 51 | COMMITTED, |
| 52 | |
| 53 | /** |
| 54 | * Indicates a transaction that is about to be rolled back. |
| 55 | */ |
| 56 | ROLLINGBACK, |
| 57 | |
| 58 | /** |
| 59 | * Indicates a transaction that has been rolled back and all locks are released. |
| 60 | */ |
| 61 | ROLLEDBACK |
| 62 | } |
| 63 | |
| 64 | private final TransactionId transactionId; |
| 65 | private final List<MapUpdate<String, byte[]>> updates; |
| 66 | private final State state; |
| 67 | |
| 68 | public Transaction(TransactionId transactionId, List<MapUpdate<String, byte[]>> updates) { |
| 69 | this(transactionId, updates, State.PREPARING); |
| 70 | } |
| 71 | |
| 72 | private Transaction(TransactionId transactionId, |
| 73 | List<MapUpdate<String, byte[]>> updates, |
| 74 | State state) { |
| 75 | this.transactionId = transactionId; |
| 76 | this.updates = ImmutableList.copyOf(updates); |
| 77 | this.state = state; |
| 78 | } |
| 79 | |
| 80 | public TransactionId id() { |
| 81 | return transactionId; |
| 82 | } |
| 83 | |
| 84 | public List<MapUpdate<String, byte[]>> updates() { |
| 85 | return updates; |
| 86 | } |
| 87 | |
| 88 | public State state() { |
| 89 | return state; |
| 90 | } |
| 91 | |
| 92 | public Transaction transition(State newState) { |
| 93 | return new Transaction(transactionId, updates, newState); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public String toString() { |
| 98 | return MoreObjects.toStringHelper(getClass()) |
| 99 | .add("transactionId", transactionId) |
| 100 | .add("updates", updates) |
| 101 | .add("state", state) |
| 102 | .toString(); |
| 103 | } |
| 104 | } |