View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.directory.mavibot.btree;
21  
22  import java.util.concurrent.locks.ReentrantLock;
23  
24  import org.apache.directory.mavibot.btree.exception.BadTransactionStateException;
25  
26  /**
27   * A data structure used to manage a write transaction
28   *
29   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
30   */
31  /* no qualifier */ class WriteTransaction
32  {
33      /** A lock used to protect the write operation against concurrent access */
34      protected ReentrantLock writeLock;
35  
36      /* no qualifier */WriteTransaction()
37      {
38          //System.out.println( "Creating the transaction oject" );
39          writeLock = new ReentrantLock();
40      }
41  
42  
43      /* no qualifier */ void start()
44      {
45          /*
46          if ( writeLock.isLocked() )
47          {
48              throw new BadTransactionStateException( "Cannot start a write transaction when it's already started" );
49          }
50          */
51  
52          //System.out.println( "Start a TXN [" + Thread.currentThread().getName() + "]" );
53  
54          writeLock.lock();
55          //System.out.println( "WriteTransaction " + Thread.currentThread().getName() );
56      }
57  
58  
59      /* no qualifier */ void commit()
60      {
61          if ( !writeLock.isLocked() )
62          {
63              throw new BadTransactionStateException( "Cannot commit a write transaction when it's not started" );
64          }
65  
66          //System.out.println( "Commit a TXN[" + Thread.currentThread().getName() + "]" );
67  
68          writeLock.unlock();
69      }
70  
71  
72      /* no qualifier */ void rollback()
73      {
74          if ( !writeLock.isLocked() )
75          {
76              throw new BadTransactionStateException( "Cannot commit a write transaction when it's not started" );
77          }
78  
79          //System.out.println( "Rollback a TXN" );
80          writeLock.unlock();
81      }
82  
83  
84      /**
85       * Tells if the transaction has started
86       * @return true if the transaction has started
87       */
88      /* no qualifier */ boolean isStarted()
89      {
90          return writeLock.isLocked();
91      }
92  }