| 1 package com.quadcap.sql; 2 3 40 41 import java.io.IOException ; 42 43 import java.sql.SQLException ; 44 45 import com.quadcap.sql.file.BlockFile; 46 import com.quadcap.sql.file.ByteUtil; 47 import com.quadcap.sql.file.PageManager; 48 import com.quadcap.sql.file.SubPageManager; 49 50 import com.quadcap.sql.index.Btree; 51 import com.quadcap.sql.index.BCursor; 52 53 import com.quadcap.sql.types.Value; 54 55 import com.quadcap.util.Debug; 56 import com.quadcap.util.Util; 57 58 68 public class TempTable { 69 Session session = null; 70 int[] map = null; 71 BlockFile file; 72 BlockFile tempFile = null; 73 Key compare = null; 74 Btree index = null; 75 long keyCount = 0; 76 byte[] data = new byte[9]; 77 boolean inMemory = false; 78 79 static int serial = 0; 81 int mySerial = serial++; 82 static final boolean trace = false; 83 85 public TempTable(Session session, Key compare, int[] map) 86 throws SQLException , IOException 87 { 88 this.inMemory = session.getDatabase().inMemory(); 89 this.session = session; 90 this.compare = compare; 91 this.map = map; 92 this.index = session.makeTempTree(compare); 93 this.file = session.getDatabase().getFile(); 94 this.tempFile = session.getDatabase().getTempFile(false); 95 } 96 97 public int addRows(Cursor cursor) 98 throws SQLException , IOException 99 { 100 int count = 0; 101 if (trace) { 103 Debug.println("TempTable[" + mySerial + "].addRows() begin"); 104 Debug.println("cursor = " + cursor); 105 } 106 cursor.beforeFirst(); 108 Database db = session.getDatabase(); 109 while (cursor.next()) { 110 Row row = cursor.getRow(); 111 byte[] key = makeKey(row); 112 long rowId = cursor.getRowId(); 113 if (rowId == 0) { 114 rowId = db.putRow(session, tempFile, null, row); 115 count++; 116 if (trace) { 118 Debug.println("TempTable[" + mySerial + "].putRow: " + 119 toString(rowId) + " " + row); 120 } 121 data[0] = 1; 123 } else { 124 data[0] = 0; 125 if (trace) { 127 Debug.println("TempTable[" + mySerial + "] refersTo row " + 128 toString(rowId) + " " + row); 129 } 130 } 132 ByteUtil.putLong(data, 1, rowId); 133 index.set(key, data); 134 } 135 if (trace) { 137 Debug.println("TempTable[" + mySerial + "].addRows() complete"); 138 } 139 return count; 141 } 142 143 public byte[] makeKey(Row row) throws SQLException { 144 return Key.makeKey(null, row, map, keyCount++, true); 145 } 146 147 public BCursor getCursor() throws IOException { 148 return index.getCursor(false); 149 } 150 151 public void getRow(byte[] rowIdBuf, LazyRow row) 152 throws SQLException , IOException 153 { 154 long rowId = ByteUtil.getLong(rowIdBuf, 1); 155 boolean isTemp = rowIdBuf[0] != 0; 156 if (trace) { 158 Debug.println("TempTable[" + mySerial + "].getRow(" + 159 toString(rowId) + ", " + isTemp + ")"); 160 } 161 session.getDatabase().getRow(rowId, row, isTemp); 163 } 164 165 public void release() throws IOException { 166 if (trace) { 168 Debug.println("TempTable[" + mySerial + "].release()"); 169 } 170 try { 172 try { 173 BCursor c = index.getCursor(false); 174 try { 175 while (c.next()) { 176 byte[] dataBuf = c.getValBuf(); 177 if (dataBuf[0] == 1) { 178 long ref = ByteUtil.getLong(dataBuf, 1); 179 if (inMemory) { 180 tempFile.removeObject(ref); 181 } else { 182 } 187 } 188 } 189 } finally { 190 c.release(); 191 } 192 } finally { 193 index.free(); 194 } 195 } finally { 196 session.getDatabase().releaseTempFile(); 197 } 198 } 199 200 public String toString(long rowId) { 202 return String.valueOf(rowId); 204 } 205 207 } 208 | Popular Tags |