Optimize TransactionProxy for write-only transactions
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DOMTransactionFactory.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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 java.util.HashMap;
11 import java.util.Map;
12 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
13 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
14 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransaction;
15 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
16 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionFactory;
17 import org.slf4j.Logger;
18
19 /**
20  * A factory for creating DOM transactions, either normal or chained.
21  *
22  * @author Thomas Pantelis
23  */
24 public class DOMTransactionFactory {
25
26     private final Map<String, DOMStoreTransactionChain> transactionChains = new HashMap<>();
27     private final InMemoryDOMDataStore store;
28     private final ShardStats shardMBean;
29     private final Logger log;
30     private final String name;
31
32     public DOMTransactionFactory(InMemoryDOMDataStore store, ShardStats shardMBean, Logger log, String name) {
33         this.store = store;
34         this.shardMBean = shardMBean;
35         this.log = log;
36         this.name = name;
37     }
38
39     @SuppressWarnings("unchecked")
40     public <T extends DOMStoreTransaction> T newTransaction(TransactionProxy.TransactionType type,
41             String transactionID, String transactionChainID) {
42
43         DOMStoreTransactionFactory factory = store;
44
45         if(!transactionChainID.isEmpty()) {
46             factory = transactionChains.get(transactionChainID);
47             if(factory == null) {
48                 if(log.isDebugEnabled()) {
49                     log.debug("{}: Creating transaction with ID {} from chain {}", name, transactionID,
50                             transactionChainID);
51                 }
52
53                 DOMStoreTransactionChain transactionChain = store.createTransactionChain();
54                 transactionChains.put(transactionChainID, transactionChain);
55                 factory = transactionChain;
56             }
57         } else {
58             log.debug("{}: Creating transaction with ID {}", name, transactionID);
59         }
60
61         T transaction = null;
62         switch(type) {
63             case READ_ONLY:
64                 transaction = (T) factory.newReadOnlyTransaction();
65                 shardMBean.incrementReadOnlyTransactionCount();
66                 break;
67             case READ_WRITE:
68                 transaction = (T) factory.newReadWriteTransaction();
69                 shardMBean.incrementReadWriteTransactionCount();
70                 break;
71             case WRITE_ONLY:
72                 transaction = (T) factory.newWriteOnlyTransaction();
73                 shardMBean.incrementWriteOnlyTransactionCount();
74                 break;
75         }
76
77         return transaction;
78     }
79
80     public void closeTransactionChain(String transactionChainID) {
81         DOMStoreTransactionChain chain =
82                 transactionChains.remove(transactionChainID);
83
84         if(chain != null) {
85             chain.close();
86         }
87     }
88
89     public void closeAllTransactionChains() {
90         for(Map.Entry<String, DOMStoreTransactionChain> entry : transactionChains.entrySet()){
91             entry.getValue().close();
92         }
93
94         transactionChains.clear();
95     }
96 }