| 1 | /** |
| 2 | * Copyright (C) 2006 NetMind Consulting Bt. |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 3 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | */ |
| 18 | |
| 19 | package hu.netmind.beankeeper.parser; |
| 20 | |
| 21 | import hu.netmind.beankeeper.serial.Serial; |
| 22 | import java.util.Date; |
| 23 | import java.util.Map; |
| 24 | import java.util.HashMap; |
| 25 | import org.apache.log4j.Logger; |
| 26 | |
| 27 | /** |
| 28 | * Keeps an exact moment in time for the query to run. |
| 29 | * @author Brautigam Robert |
| 30 | * @version Revision: $Revision$ |
| 31 | */ |
| 32 | public class TimeControl |
| 33 | { |
| 34 | private static Logger logger = Logger.getLogger(TimeControl.class); |
| 35 | |
| 36 | private Long serial; |
| 37 | private Long txSerial; |
| 38 | private boolean applyTransaction = false; |
| 39 | |
| 40 | public TimeControl(TimeControl timeControl) |
| 41 | { |
| 42 | this.serial=timeControl.serial; |
| 43 | this.txSerial=timeControl.txSerial; |
| 44 | this.applyTransaction=timeControl.applyTransaction; |
| 45 | } |
| 46 | |
| 47 | public TimeControl(Long serial, Long txSerial, boolean applyTransaction) |
| 48 | { |
| 49 | this.serial=serial; |
| 50 | this.txSerial=txSerial; |
| 51 | this.applyTransaction=applyTransaction; |
| 52 | } |
| 53 | |
| 54 | public boolean isApplyTransaction() |
| 55 | { |
| 56 | return applyTransaction; |
| 57 | } |
| 58 | public void setApplyTransaction(boolean applyTransaction) |
| 59 | { |
| 60 | this.applyTransaction=applyTransaction; |
| 61 | } |
| 62 | |
| 63 | public Long getSerial() |
| 64 | { |
| 65 | return serial; |
| 66 | } |
| 67 | public void setSerial(Long serial) |
| 68 | { |
| 69 | this.serial=serial; |
| 70 | } |
| 71 | |
| 72 | public Long getTxSerial() |
| 73 | { |
| 74 | return txSerial; |
| 75 | } |
| 76 | public void setTxSerial(Long txSerial) |
| 77 | { |
| 78 | this.txSerial=txSerial; |
| 79 | } |
| 80 | |
| 81 | public void setQueryDate(Date date) |
| 82 | { |
| 83 | serial = Serial.getSerial(date).getValue(); |
| 84 | if ( (txSerial!=null) && (serial.longValue() < txSerial.longValue() ) ) |
| 85 | { |
| 86 | // Serial is before the tx's first modification took place, |
| 87 | // so transaction conditions are not necessary |
| 88 | setApplyTransaction(false); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Apply this time control to the table given. |
| 94 | */ |
| 95 | public void apply(Expression expression, TableTerm term) |
| 96 | { |
| 97 | Expression timeExpr = new Expression(); |
| 98 | Long safeSerial = txSerial; |
| 99 | if ( (safeSerial == null) || (safeSerial.longValue()>serial.longValue()) ) |
| 100 | safeSerial = serial; |
| 101 | if ( logger.isDebugEnabled() ) |
| 102 | logger.debug("applying time control to: "+term+", safe serial: "+safeSerial+", txSerial: "+txSerial); |
| 103 | // Apply the constraints, first the start serial. |
| 104 | Expression partExpr = new Expression(); |
| 105 | if ( applyTransaction ) |
| 106 | { |
| 107 | // If entry is inside the transaction, then we allow entries |
| 108 | // inside the serial boundary. |
| 109 | partExpr.add(new ReferenceTerm(term,"persistence_txstartid")); |
| 110 | partExpr.add("="); |
| 111 | partExpr.add(new ConstantTerm(txSerial)); |
| 112 | partExpr.add("and"); |
| 113 | partExpr.add(new ReferenceTerm(term,"persistence_txstart")); |
| 114 | partExpr.add("<="); |
| 115 | partExpr.add(new ConstantTerm(serial)); |
| 116 | partExpr.add("or"); |
| 117 | } |
| 118 | partExpr.add(new ReferenceTerm(term,"persistence_start")); |
| 119 | partExpr.add("<="); |
| 120 | partExpr.add(new ConstantTerm(safeSerial)); |
| 121 | timeExpr.add(partExpr); |
| 122 | timeExpr.add("and"); |
| 123 | // Now add end constraints. This is a little different, because |
| 124 | // the end serials need to be enforced. |
| 125 | partExpr = new Expression(); |
| 126 | partExpr.add(new ReferenceTerm(term,"persistence_end")); |
| 127 | partExpr.add(">"); |
| 128 | partExpr.add(new ConstantTerm(safeSerial)); |
| 129 | if ( applyTransaction ) |
| 130 | { |
| 131 | // If entry is in current transaction, then the constraints |
| 132 | // are stricter. Disallow entries with endid lower then serial. |
| 133 | partExpr.add("and"); |
| 134 | Expression subExpr = new Expression(); |
| 135 | subExpr.add(new ReferenceTerm(term,"persistence_txendid")); |
| 136 | subExpr.add("<>"); |
| 137 | subExpr.add(new ConstantTerm(txSerial)); |
| 138 | subExpr.add("or"); |
| 139 | subExpr.add(new ReferenceTerm(term,"persistence_txend")); |
| 140 | subExpr.add(">"); |
| 141 | subExpr.add(new ConstantTerm(serial)); |
| 142 | partExpr.add(subExpr); |
| 143 | } |
| 144 | timeExpr.add(partExpr); |
| 145 | // Add all constraints |
| 146 | expression.addAll(timeExpr); |
| 147 | } |
| 148 | |
| 149 | public String toString() |
| 150 | { |
| 151 | return "["+serial+","+txSerial+","+applyTransaction+"]"; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | |