-
Notifications
You must be signed in to change notification settings - Fork 15
/
OrderBy.java
86 lines (75 loc) · 2.32 KB
/
OrderBy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package simpledb;
import java.util.*;
/**
* OrderBy is an operator that implements a relational ORDER BY.
*/
public class OrderBy extends Operator {
DbIterator child;
TupleDesc td;
ArrayList<Tuple> childTups = new ArrayList<Tuple>();
int orderByField;
Iterator<Tuple> it;
boolean asc;
/**
* Creates a new OrderBy node over the tuples from the iterator.
*
* @param orderbyField the field to which the sort is applied.
* @param asc true if the sort order is ascending.
* @param child the tuples to sort.
*/
public OrderBy(int orderbyField, boolean asc, DbIterator child) {
this.child = child;
td= child.getTupleDesc();
this.orderByField = orderbyField;
this.asc = asc;
}
public TupleDesc getTupleDesc() {
return td;
}
public void open()
throws DbException, NoSuchElementException, TransactionAbortedException {
child.open();
//load all the tuples in a collection, and sort it
while (child.hasNext())
childTups.add((Tuple)child.next());
Collections.sort(childTups, new TupleComparator(orderByField, asc));
it = childTups.iterator();
}
public void close() {
it = null;
}
public void rewind() throws DbException, TransactionAbortedException {
it = childTups.iterator();
}
/**
* Operator.fetchNext implementation.
* Returns tuples from the child operator in order
*
* @return The next tuple in the ordering, or null if there are no more tuples
*/
protected Tuple fetchNext()
throws NoSuchElementException, TransactionAbortedException, DbException {
if (it != null && it.hasNext()) {
return it.next();
} else
return null;
}
}
class TupleComparator implements Comparator<Tuple> {
int field;
boolean asc;
public TupleComparator(int field, boolean asc) {
this.field = field;
this.asc = asc;
}
public int compare(Tuple o1, Tuple o2) {
Field t1 = (o1).getField(field);
Field t2 = (o2).getField(field);
if (t1.compare(Predicate.Op.EQUALS, t2))
return 0;
if (t1.compare(Predicate.Op.GREATER_THAN, t2))
return asc?1:-1;
else
return asc?-1:1;
}
}