1 package com.quadcap.sql; 2 3 40 41 import java.io.ByteArrayInputStream ; 42 import java.io.ByteArrayOutputStream ; 43 import java.io.IOException ; 44 45 import java.util.Vector ; 46 47 import java.sql.SQLException ; 48 49 import com.quadcap.sql.io.ObjectInputStream; 50 import com.quadcap.sql.io.ObjectOutputStream; 51 52 import com.quadcap.sql.index.BCursor; 53 import com.quadcap.sql.index.Btree; 54 55 import com.quadcap.sql.file.BlockFile; 56 import com.quadcap.sql.file.PageManager; 57 58 import com.quadcap.sql.types.Op; 59 import com.quadcap.sql.types.Value; 60 61 import com.quadcap.util.Debug; 62 import com.quadcap.util.Util; 63 64 69 70 public class GroupByCursor extends FilterCursor { 71 Vector groupBy = null; 72 int currentRowNum = 0; 73 byte[] prevKey = new byte[32]; 74 int prevKeyLen = -1; 75 int[] gMap = null; 76 BCursor bc = null; 77 LazyRow prevRow = null; 78 LazyRow currRow = null; 79 boolean sameGroup = false; 80 boolean prev = false; 81 Key compare = null; 82 TempTable tempTable = null; 83 84 public GroupByCursor(Session session, Cursor cursor, Vector groupBy) 85 throws SQLException 86 { 87 super(session, cursor); 88 89 this.currRow = new LazyRow(cursor.getColumnCount()); 90 this.prevRow = new LazyRow(cursor.getColumnCount()); 91 this.groupBy = groupBy; 92 this.gMap = new int[groupBy.size()]; 93 this.compare = new Key(gMap.length); 94 for (int i = 0; i < gMap.length; i++) { 95 Column c = cursor.getColumn((String )groupBy.elementAt(i)); 96 gMap[i] = c.getColumn(); 97 } 98 try { 99 this.tempTable = new TempTable(session, new Key(gMap.length+1), 100 gMap); 101 tempTable.addRows(cursor); 102 this.bc = tempTable.getCursor(); 103 beforeFirst(); 104 } catch (IOException e) { 105 throw DbException.wrapThrowable(e); 106 } 107 } 108 109 public Row getRow() throws SQLException { 110 return prevRow; 112 } 113 114 public void updateRow(Row row) throws SQLException { 115 throw new SQLException ("GroupBy expressions aren't updateable", 116 "42000"); 117 } 118 119 public void deleteRow() throws SQLException { 120 throw new SQLException ("GroupBy expressions aren't updateable", 121 "42000"); 122 } 123 124 public void beforeFirst() throws SQLException { 125 try { 126 bc.beforeFirst(); 127 prev = nextRow(); 128 } catch (IOException e) { 129 throw DbException.wrapThrowable(e); 130 } catch (ClassNotFoundException e) { 131 throw DbException.wrapThrowable(e); 132 } 133 } 134 135 public boolean lastRowOfGroup() { 136 return !sameGroup; 138 } 139 140 public boolean next() throws SQLException { 141 try { 142 boolean ret = prev; 143 prev = nextRow(); 144 return ret; 145 } catch (IOException e) { 146 throw DbException.wrapThrowable(e); 147 } catch (ClassNotFoundException e) { 148 throw DbException.wrapThrowable(e); 149 } 150 } 151 152 public boolean nextRow() 153 throws SQLException , IOException , ClassNotFoundException 154 { 155 swapRows(); 156 sameGroup = false; 157 if (!bc.next()) { 158 return false; 159 } 160 final byte[] key = bc.getKeyBuf(); 161 final int len = bc.getKeyLen(); 162 163 if (prevKeyLen != -1) { 164 sameGroup = compare.compare(key, 0, len, 165 prevKey, 0, prevKeyLen) == 0; 166 } 167 prevKey = (byte[])Util.checkCapacity(prevKey, len); 168 prevKeyLen = len; 169 170 System.arraycopy(key, 0, prevKey, 0, len); 171 172 tempTable.getRow(bc.getValBuf(), currRow); 173 return true; 174 } 175 176 final void swapRows() { 177 LazyRow temp = prevRow; 178 prevRow = currRow; 179 currRow = temp; 180 181 } 182 183 public boolean isWritable(int col) throws SQLException { 184 return false; 185 } 186 187 190 public long size() { return -1; } 191 192 public void close() throws SQLException { 193 try { 194 super.close(); 195 } finally { 196 try { 197 if (bc != null) bc.release(); 198 } finally { 199 bc = null; 200 try { 201 if (tempTable != null) tempTable.release(); 202 } catch (IOException e2) { 203 throw DbException.wrapThrowable(e2); 204 } finally { 205 tempTable = null; 206 } 207 } 208 } 209 } 210 } 211 | Popular Tags |