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.server.core.partition.impl.btree.jdbm;
21  
22  import java.io.IOException;
23  
24  import org.apache.directory.server.core.api.partition.PartitionWriteTxn;
25  
26  import jdbm.RecordManager;
27  import jdbm.recman.BaseRecordManager;
28  import jdbm.recman.CacheRecordManager;
29  
30  /**
31   * The JDBM partition write transaction
32   *  
33   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34   */
35  public class JdbmPartitionWriteTxn extends PartitionWriteTxn
36  {
37      /** The associated record manager */
38      private RecordManager recordManager;
39      
40      /** A flag used to flush data immediately or not */
41      private boolean syncOnWrite = false;
42      
43      /**
44       * Create an instance of JdbmPartitionWriteTxn
45       * 
46       * @param recordManager The RecordManager instance
47       * @param syncOnWrite If we want to data to be flushed on each write
48       */
49      public JdbmPartitionWriteTxn( RecordManager recordManager, boolean syncOnWrite )
50      {
51          this.recordManager = recordManager;
52          this.syncOnWrite = syncOnWrite;
53      }
54      
55      
56      /**
57       * {@inheritDoc}
58       */
59      @Override
60      public void commit() throws IOException
61      {
62          recordManager.commit();
63          
64          // And flush the journal
65          BaseRecordManager baseRecordManager = null;
66  
67          if ( recordManager instanceof CacheRecordManager )
68          {
69              baseRecordManager = ( ( BaseRecordManager ) ( ( CacheRecordManager ) recordManager ).getRecordManager() );
70          }
71          else
72          {
73              baseRecordManager = ( ( BaseRecordManager ) recordManager );
74          }
75  
76  
77          if ( syncOnWrite )
78          {
79              baseRecordManager.getTransactionManager().synchronizeLog();
80          }
81      }
82  
83  
84      /**
85       * {@inheritDoc}
86       */
87      @Override
88      public void abort() throws IOException
89      {
90          recordManager.rollback();
91      }
92  
93  
94      /**
95       * {@inheritDoc}
96       */
97      @Override
98      public boolean isClosed()
99      {
100         return false;
101     }
102 
103     
104     /**
105      * {@inheritDoc}
106      */
107     @Override
108     public void close() throws IOException
109     {
110         commit();
111     }
112 }