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 23 import java.util.Date; 24 25 26 /** 27 * The Transaction is used to protect the BTree against concurrent modifcation, 28 * and insure that a read is always done against one single revision. It's also 29 * used to gather many modifications under one single revision, if needed. 30 * <p/> 31 * A Transaction should be closed when the user is done with it, otherwise the 32 * pages associated with the given revision, and all the referenced pages, will 33 * remain on the storage. 34 * <p/> 35 * A Transaction can be hold for quite a long time, for instance while doing 36 * a browse against a big BTree. At some point, transactions which are pending 37 * for too long will be closed by the transaction manager. 38 * 39 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 40 * 41 * @param <K> The type for the Key 42 * @param <V> The type for the stored value 43 */ 44 public class Transaction<K, V> 45 { 46 /** The associated revision */ 47 private long revision; 48 49 /** The date of creation */ 50 private long creationDate; 51 52 /** The revision on which we are having a transaction */ 53 private volatile Page<K, V> root; 54 55 /** A flag used to tell if a transaction is closed ot not */ 56 private volatile boolean closed; 57 58 59 /** 60 * Creates a new transaction instance 61 * 62 * @param root The associated root 63 * @param revision The revision this transaction is using 64 * @param creationDate The creation date for this transaction 65 */ 66 public Transaction( Page<K, V> root, long revision, long creationDate ) 67 { 68 this.revision = revision; 69 this.creationDate = creationDate; 70 this.root = root; 71 closed = false; 72 } 73 74 75 /** 76 * @return the associated revision 77 */ 78 public long getRevision() 79 { 80 return revision; 81 } 82 83 84 /** 85 * @return the associated root 86 */ 87 public Page<K, V> getRoot() 88 { 89 return root; 90 } 91 92 93 /** 94 * @return the creationDate 95 */ 96 public long getCreationDate() 97 { 98 return creationDate; 99 } 100 101 102 /** 103 * Close the transaction, releasing the revision it was using. 104 */ 105 public void close() 106 { 107 root = null; 108 closed = true; 109 } 110 111 112 /** 113 * @return true if this transaction has been closed 114 */ 115 public boolean isClosed() 116 { 117 return closed; 118 } 119 120 121 /** 122 * @see Object#toString() 123 */ 124 public String toString() 125 { 126 return "Transaction[" + revision + ":" + new Date( creationDate ) + ", closed :" + closed + "]"; 127 } 128 }