001: /*
002: * $Id: DistinctRowIterator.java,v 1.13 2005/12/20 18:32:41 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2005 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.engine.rowiterators;
042:
043: import java.util.ArrayList;
044: import java.util.HashMap;
045: import java.util.List;
046: import java.util.Map;
047:
048: import org.axiondb.AxionException;
049: import org.axiondb.Row;
050: import org.axiondb.RowDecorator;
051: import org.axiondb.RowIterator;
052: import org.axiondb.Selectable;
053: import org.axiondb.util.ValuePool;
054:
055: /**
056: * A {@link DelegatingRowIterator}implementing DISTINCT.
057: *
058: * @version $Revision: 1.13 $ $Date: 2005/12/20 18:32:41 $
059: * @author Rodney Waldhoff
060: * @author Ahimanikya Satapathy
061: */
062: public class DistinctRowIterator extends AbstractAcceptingRowIterator {
063:
064: public DistinctRowIterator(RowIterator iter, Map selectableMap,
065: Selectable[] selectables) {
066: super (iter);
067: _decorator = new RowDecorator(selectableMap);
068: _selectables = selectables;
069: _timesEncountered = new HashMap();
070: }
071:
072: /** Not supported in the base implementation. */
073: public void set(Row row) throws AxionException {
074: throw new UnsupportedOperationException();
075: }
076:
077: /** Not supported in the base implementation. */
078: public void remove() throws AxionException {
079: throw new UnsupportedOperationException();
080: }
081:
082: public void reset() throws AxionException {
083: super .reset();
084: _timesEncountered.clear();
085: }
086:
087: public String toString() {
088: StringBuffer buf = new StringBuffer(20);
089: buf.append("Distinct (");
090: for (int i = 0; i < _selectables.length; i++) {
091: buf.append(_selectables[i] + (i != 0 ? "," : ""));
092: }
093: buf.append(")");
094: return buf.toString();
095: }
096:
097: protected boolean acceptable(int rowindex, Row row)
098: throws AxionException {
099: List values = populateValueList(row);
100: Integer count = (Integer) (_timesEncountered.get(values));
101: if (null == count) {
102: count = ValuePool.getInt(0);
103: }
104:
105: // next
106: if (rowindex > previousIndex()) {
107: _timesEncountered.put(values, ValuePool.getInt(count
108: .intValue() + 1));
109: return (0 == count.intValue());
110: }
111:
112: // previous
113: if (1 == count.intValue()) {
114: _timesEncountered.remove(values);
115: } else {
116: _timesEncountered.put(values, ValuePool.getInt(count
117: .intValue() - 1));
118: }
119: return (1 == count.intValue());
120: }
121:
122: private List populateValueList(Row row) throws AxionException {
123: _decorator.setRow(row);
124: List values = new ArrayList(_selectables.length);
125: for (int i = 0; i < _selectables.length; i++) {
126: values.add(_selectables[i].evaluate(_decorator));
127: }
128: return values;
129: }
130:
131: private RowDecorator _decorator = null;
132: private Selectable[] _selectables = null;
133: private Map _timesEncountered = null;
134: }
|