Bump versions to 6.0.1-SNAPSHOT
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / FrontendHistoryMetadataBuilder.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.datastore;
9
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.primitives.UnsignedLong;
14 import java.util.HashMap;
15 import java.util.Map;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
18 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
19 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
20 import org.opendaylight.controller.cluster.datastore.persisted.FrontendHistoryMetadata;
21 import org.opendaylight.controller.cluster.datastore.utils.ImmutableUnsignedLongSet;
22 import org.opendaylight.controller.cluster.datastore.utils.MutableUnsignedLongSet;
23 import org.opendaylight.controller.cluster.datastore.utils.UnsignedLongBitmap;
24 import org.opendaylight.yangtools.concepts.Identifiable;
25
26 final class FrontendHistoryMetadataBuilder implements Identifiable<LocalHistoryIdentifier> {
27     private final @NonNull Map<UnsignedLong, Boolean> closedTransactions;
28     private final @NonNull MutableUnsignedLongSet purgedTransactions;
29     private final @NonNull LocalHistoryIdentifier identifier;
30
31     private boolean closed;
32
33     FrontendHistoryMetadataBuilder(final LocalHistoryIdentifier identifier) {
34         this.identifier = requireNonNull(identifier);
35         purgedTransactions = MutableUnsignedLongSet.of();
36         closedTransactions = new HashMap<>(2);
37     }
38
39     FrontendHistoryMetadataBuilder(final ClientIdentifier clientId, final FrontendHistoryMetadata meta) {
40         identifier = new LocalHistoryIdentifier(clientId, meta.getHistoryId(), meta.getCookie());
41         closedTransactions = meta.getClosedTransactions().mutableCopy();
42         purgedTransactions = meta.getPurgedTransactions().mutableCopy();
43         closed = meta.isClosed();
44     }
45
46     @Override
47     public LocalHistoryIdentifier getIdentifier() {
48         return identifier;
49     }
50
51     public FrontendHistoryMetadata build() {
52         return new FrontendHistoryMetadata(identifier.getHistoryId(), identifier.getCookie(), closed,
53             UnsignedLongBitmap.copyOf(closedTransactions), purgedTransactions.immutableCopy());
54     }
55
56     void onHistoryClosed() {
57         checkState(identifier.getHistoryId() != 0);
58         closed = true;
59     }
60
61     void onTransactionAborted(final TransactionIdentifier txId) {
62         closedTransactions.put(UnsignedLong.fromLongBits(txId.getTransactionId()), Boolean.FALSE);
63     }
64
65     void onTransactionCommitted(final TransactionIdentifier txId) {
66         closedTransactions.put(UnsignedLong.fromLongBits(txId.getTransactionId()), Boolean.TRUE);
67     }
68
69     void onTransactionPurged(final TransactionIdentifier txId) {
70         final long txidBits = txId.getTransactionId();
71         closedTransactions.remove(UnsignedLong.fromLongBits(txidBits));
72         purgedTransactions.add(txidBits);
73     }
74
75     void onTransactionsSkipped(final ImmutableUnsignedLongSet txIds) {
76         purgedTransactions.addAll(txIds);
77     }
78
79     /**
80      * Transform frontend metadata for a particular client history into its {@link LocalFrontendHistory} counterpart.
81      *
82      * @param shard parent shard
83      * @return Leader history state
84      */
85     @NonNull AbstractFrontendHistory toLeaderState(final @NonNull Shard shard) {
86         if (identifier.getHistoryId() == 0) {
87             return StandaloneFrontendHistory.recreate(shard.persistenceId(), identifier.getClientId(),
88                 shard.getDataStore(), closedTransactions, purgedTransactions);
89         }
90
91         return LocalFrontendHistory.recreate(shard.persistenceId(), shard.getDataStore(),
92             shard.getDataStore().recreateTransactionChain(identifier, closed), closedTransactions, purgedTransactions);
93     }
94 }