001: /*
002: * $Id: UniqueConstraint.java,v 1.16 2007/11/13 19:04:02 rwald Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2003 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.constraints;
042:
043: import java.util.ArrayList;
044: import java.util.Collections;
045: import java.util.HashSet;
046: import java.util.Iterator;
047: import java.util.List;
048: import java.util.Set;
049:
050: import org.axiondb.AxionException;
051: import org.axiondb.ColumnIdentifier;
052: import org.axiondb.Index;
053: import org.axiondb.Row;
054: import org.axiondb.RowDecorator;
055: import org.axiondb.RowIterator;
056: import org.axiondb.Selectable;
057: import org.axiondb.Table;
058: import org.axiondb.engine.rowiterators.ChangingIndexedRowIterator;
059: import org.axiondb.engine.rowiterators.IndexNestedLoopJoinedRowIterator;
060: import org.axiondb.engine.rowiterators.MutableIndexedRowIterator;
061: import org.axiondb.engine.rows.JoinedRow;
062: import org.axiondb.event.RowEvent;
063: import org.axiondb.event.RowInsertedEvent;
064: import org.axiondb.functions.EqualFunction;
065:
066: /**
067: * A UNIQUE constraint, which is violated when my collection of {@link Selectable}s is
068: * not unique within my table.
069: *
070: * @version $Revision: 1.16 $ $Date: 2007/11/13 19:04:02 $
071: * @author Rodney Waldhoff
072: * @author James Strachan
073: * @author Ahimanikya Satapathy
074: */
075: public class UniqueConstraint extends BaseSelectableBasedConstraint {
076:
077: public UniqueConstraint(String name) {
078: this (name, "UNIQUE");
079: }
080:
081: public UniqueConstraint(String name, String type) {
082: super (name, type);
083: }
084:
085: public boolean evaluate(RowEvent event) throws AxionException {
086: return evaluate(event, event.getTable().makeRowDecorator(),
087: false);
088: }
089:
090: public boolean evaluate(RowEvent event, RowDecorator dec)
091: throws AxionException {
092: return evaluate(event, dec, false);
093: }
094:
095: public boolean evaluate(RowEvent event, RowDecorator dec,
096: boolean wasDeferred) throws AxionException {
097: if (null == event.getNewRow()) {
098: return true;
099: }
100:
101: Table table = event.getTable();
102: dec.setRow(event.getNewRow());
103:
104: List<Object> values = new ArrayList<Object>(
105: getSelectableCount());
106: for (int i = 0, c = getSelectableCount(); i < c; i++) {
107: values.add(getSelectable(i).evaluate(dec));
108: }
109:
110: int acceptableRowId = -1;
111: if (null != event.getOldRow()) {
112: acceptableRowId = event.getOldRow().getIdentifier();
113: } else if (isDeferred() || wasDeferred) {
114: acceptableRowId = event.getNewRow().getIdentifier();
115: }
116:
117: RowIterator matching = table.getMatchingRows(
118: getSelectableList(), values, true);
119: while (matching.hasNext()) {
120: Row row = matching.next();
121: if (isDeferred() && matching.hasNext()) {
122: return false;
123: } else if (-1 == acceptableRowId) {
124: // in this case, any matching row causes failure
125: return false;
126: } else if (row.getIdentifier() != acceptableRowId) {
127: // otherwise, check that the matching row isn't the one we're updating
128: return false;
129: }
130: }
131: return true;
132: }
133:
134: public boolean evaluate(RowIterator oldRows, RowIterator newRows,
135: Table table) throws AxionException {
136: if (null == newRows || newRows.isEmpty()) {
137: return true;
138: }
139:
140: Selectable sel = getSelectable(0);
141: if (sel instanceof ColumnIdentifier) {
142: boolean update = (oldRows != null);
143: String key = sel.getName();
144: int colPos = table.getColumnIndex(key);
145: Index index = table.getIndexForColumn(table.getColumn(key));
146:
147: MutableIndexedRowIterator currentRows = new ChangingIndexedRowIterator(
148: index, table, new EqualFunction());
149: RowIterator matching = new IndexNestedLoopJoinedRowIterator(
150: newRows, colPos, currentRows, colPos, false, false);
151:
152: int lastId = -1;
153: matching.reset();
154: while (matching.hasNext()) {
155: JoinedRow joinRow = (JoinedRow) matching.next();
156: if (!update && !checkOtherColumns(joinRow, table)) {
157: return false;
158: } else {
159:
160: Row oldRow = joinRow.getRow(0);
161: int acceptableRowId = oldRow.getIdentifier();
162:
163: if (-1 == acceptableRowId) {
164: // in this case, any matching row causes failure
165: return false;
166: } else if (acceptableRowId == lastId) {
167: return false;
168: } else {
169: lastId = oldRow.getIdentifier();
170: }
171: }
172: }
173: } else {
174: newRows.reset();
175: RowDecorator dec = table.makeRowDecorator();
176: for (RowIterator iter = newRows; iter.hasNext();) {
177: Row row = iter.next();
178: RowEvent event = new RowInsertedEvent(table, null, row);
179: if (!evaluate(event, dec, true)) {
180: return false;
181: }
182: }
183: }
184: return true;
185: }
186:
187: private boolean checkOtherColumns(JoinedRow joinRow, Table table)
188: throws AxionException {
189: for (int i = 1, I = getSelectableCount(); i < I; i++) {
190: String key = getSelectable(i).getName();
191: int colPos = table.getColumnIndex(key);
192: Row oldRow = joinRow.getRow(0);
193: Row newRow = joinRow.getRow(1);
194: if (oldRow.get(colPos).equals(newRow.get(colPos))) {
195: return false;
196: }
197: }
198: return true;
199: }
200:
201: public void addFK(String name) {
202: if (fkSet == null) {
203: fkSet = new HashSet(4);
204: }
205: fkSet.add(name);
206: }
207:
208: public Iterator getFKs() {
209: if (fkSet == null) {
210: return Collections.EMPTY_LIST.iterator();
211: }
212: return fkSet.iterator();
213: }
214:
215: private Set fkSet;
216: private static final long serialVersionUID = -54506312013696264L;
217: }
|