001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020package org.apache.directory.server.core.partition.impl.btree.jdbm;
021
022import java.io.IOException;
023
024import org.apache.directory.server.core.api.partition.PartitionWriteTxn;
025
026import jdbm.RecordManager;
027import jdbm.recman.BaseRecordManager;
028import jdbm.recman.CacheRecordManager;
029
030/**
031 * The JDBM partition write transaction
032 *  
033 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
034 */
035public class JdbmPartitionWriteTxn extends PartitionWriteTxn
036{
037    /** The associated record manager */
038    private RecordManager recordManager;
039    
040    /** A flag used to flush data immediately or not */
041    private boolean syncOnWrite = false;
042    
043    /**
044     * Create an instance of JdbmPartitionWriteTxn
045     * 
046     * @param recordManager The RecordManager instance
047     * @param syncOnWrite If we want to data to be flushed on each write
048     */
049    public JdbmPartitionWriteTxn( RecordManager recordManager, boolean syncOnWrite )
050    {
051        this.recordManager = recordManager;
052        this.syncOnWrite = syncOnWrite;
053    }
054    
055    
056    /**
057     * {@inheritDoc}
058     */
059    @Override
060    public void commit() throws IOException
061    {
062        recordManager.commit();
063        
064        // And flush the journal
065        BaseRecordManager baseRecordManager = null;
066
067        if ( recordManager instanceof CacheRecordManager )
068        {
069            baseRecordManager = ( ( BaseRecordManager ) ( ( CacheRecordManager ) recordManager ).getRecordManager() );
070        }
071        else
072        {
073            baseRecordManager = ( ( BaseRecordManager ) recordManager );
074        }
075
076
077        if ( syncOnWrite )
078        {
079            baseRecordManager.getTransactionManager().synchronizeLog();
080        }
081    }
082
083
084    /**
085     * {@inheritDoc}
086     */
087    @Override
088    public void abort() throws IOException
089    {
090        recordManager.rollback();
091    }
092
093
094    /**
095     * {@inheritDoc}
096     */
097    @Override
098    public boolean isClosed()
099    {
100        return false;
101    }
102
103    
104    /**
105     * {@inheritDoc}
106     */
107    @Override
108    public void close() throws IOException
109    {
110        commit();
111    }
112}