001: /*
002: * $Id: SortedRowIterator.java,v 1.11 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: package org.axiondb.engine.rowiterators;
041:
042: import java.util.ArrayList;
043: import java.util.Arrays;
044: import java.util.Collections;
045: import java.util.Comparator;
046: import java.util.List;
047:
048: import org.apache.commons.collections.primitives.ArrayIntList;
049: import org.apache.commons.collections.primitives.IntList;
050: import org.axiondb.AxionException;
051: import org.axiondb.OrderNode;
052: import org.axiondb.Row;
053: import org.axiondb.RowComparator;
054: import org.axiondb.RowDecorator;
055: import org.axiondb.RowIterator;
056: import org.axiondb.RowSource;
057: import org.axiondb.util.ComparatorChain;
058:
059: /**
060: * @author Jonathan Giron
061: * @author Ahimanikya Satapathy
062: * @version $Revision: 1.11 $
063: */
064: public abstract class SortedRowIterator extends DelegatingRowIterator {
065:
066: protected SortedRowIterator() {
067: super (null);
068: }
069:
070: public String toString() {
071: return "SortedRowIterator (key=" + _keyString + ")";
072: }
073:
074: protected static final ComparatorChain buildComparatorChain(
075: List orderNodes, RowDecorator rowDecorator) {
076: ComparatorChain comparator = new ComparatorChain();
077: for (int i = 0, I = orderNodes.size(); i < I; i++) {
078: OrderNode node = (OrderNode) orderNodes.get(i);
079: if (node.isDescending()) {
080: comparator.setReverseSort(i);
081: }
082: comparator.addComparator(new RowComparator(node
083: .getSelectable(), rowDecorator));
084: }
085: return comparator;
086: }
087:
088: public static class MergeSort extends SortedRowIterator {
089:
090: public MergeSort(RowIterator unsortedRows, Comparator comparator)
091: throws AxionException {
092: List sortedList = getSortedRowList(unsortedRows, comparator);
093: this .setDelegate(new ListRowIterator(sortedList));
094: }
095:
096: public MergeSort(RowIterator unsortedRows, List orderNodes,
097: RowDecorator rowDecorator) throws AxionException {
098: this (unsortedRows, buildComparatorChain(orderNodes,
099: rowDecorator));
100: _keyString = orderNodes.toString();
101: }
102:
103: private List getSortedRowList(RowIterator unsortedRows,
104: Comparator comparator) throws AxionException {
105: List list = new ArrayList();
106:
107: while (unsortedRows.hasNext()) {
108: list.add(unsortedRows.next());
109: }
110:
111: Collections.sort(list, comparator);
112: return list;
113: }
114: }
115:
116: public static class MutableMergeSort extends SortedRowIterator {
117:
118: public MutableMergeSort(RowSource source,
119: RowIterator unsortedRows, Comparator comparator)
120: throws AxionException {
121: IntList ids = getSortedRowIds(unsortedRows, comparator);
122: this .setDelegate(new LazyRowRowIterator(source, ids
123: .listIterator(), ids.size()));
124: }
125:
126: public MutableMergeSort(RowSource source,
127: RowIterator unsortedRows, List orderNodes,
128: RowDecorator rowDecorator) throws AxionException {
129: this (source, unsortedRows, buildComparatorChain(orderNodes,
130: rowDecorator));
131: _keyString = orderNodes.toString();
132: }
133:
134: public void remove() throws AxionException {
135: getDelegate().remove();
136: }
137:
138: public void set(Row row) throws AxionException {
139: getDelegate().set(row);
140: }
141:
142: private IntList getSortedRowIds(RowIterator unsortedRows,
143: Comparator comparator) throws AxionException {
144: List list = new ArrayList();
145:
146: while (unsortedRows.hasNext()) {
147: list.add(unsortedRows.next());
148: }
149:
150: Object rowArry[] = list.toArray();
151: Arrays.sort(rowArry, comparator);
152:
153: IntList ids = new ArrayIntList(rowArry.length);
154: for (int j = 0; j < rowArry.length; j++) {
155: ids.add(((Row) rowArry[j]).getIdentifier());
156: }
157: return ids;
158: }
159: }
160:
161: protected String _keyString = "";
162: protected RowIterator _rowIter = null;
163: }
|