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.io.IOException; 23 import java.util.Map; 24 import java.util.concurrent.locks.ReentrantLock; 25 26 import org.apache.directory.mavibot.btree.exception.BadTransactionStateException; 27 28 /** 29 * A data structure used to manage a write transaction 30 * 31 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 32 */ 33 /* no qualifier */ class WriteTransaction 34 { 35 /** The recordManager on which this transaction is applied */ 36 private RecordManager recordManager; 37 38 /** A lock used to protect the write operation against concurrent access */ 39 protected ReentrantLock writeLock; 40 41 /* no qualifier */WriteTransaction( RecordManager recordManager ) 42 { 43 System.out.println( "Creating the transaction oject" ); 44 this.recordManager = recordManager; 45 writeLock = new ReentrantLock(); 46 } 47 48 49 /* no qualifier */ void start() 50 { 51 if ( writeLock.isLocked() ) 52 { 53 throw new BadTransactionStateException( "Cannot start a write transaction when it's already started" ); 54 } 55 56 writeLock.lock(); 57 } 58 59 60 /* no qualifier */ void commit() 61 { 62 if ( !writeLock.isLocked() ) 63 { 64 throw new BadTransactionStateException( "Cannot commit a write transaction when it's not started" ); 65 } 66 67 Map<?, ?> pendingPages = recordManager.getPendingPages(); 68 69 for ( Object object : pendingPages.keySet() ) 70 { 71 BTree btree = (BTree)pendingPages.get( object ); 72 73 try 74 { 75 recordManager.writePage( btree, (Page)object, ((Page)object).getRevision() ); 76 } 77 catch ( IOException e ) 78 { 79 // TODO Auto-generated catch block 80 e.printStackTrace(); 81 } 82 } 83 84 /* 85 recordManager.updateRecordManagerHeader(); 86 87 // Update the BTree header now 88 recordManager.updateBtreeHeader( btree, ( ( AbstractPage<K, V> ) rootPage ).getOffset() ); 89 90 // Moved the free pages into the list of free pages 91 recordManager.addFreePages( this, result.getCopiedPages() ); 92 93 // Store the created rootPage into the revision BTree, this will be stored in RecordManager only if revisions are set to keep 94 recordManager.storeRootPage( this, rootPage ); 95 */ 96 97 pendingPages.clear(); 98 99 writeLock.unlock(); 100 } 101 102 103 /* no qualifier */ void rollback() 104 { 105 if ( !writeLock.isLocked() ) 106 { 107 throw new BadTransactionStateException( "Cannot commit a write transaction when it's not started" ); 108 } 109 110 writeLock.unlock(); 111 } 112 113 114 /** 115 * Tells if the transaction has started 116 * @return true if the transaction has started 117 */ 118 /* no qualifier */ boolean isStarted() 119 { 120 return writeLock.isLocked(); 121 } 122 }