| 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.BCursor; 51 import com.quadcap.sql.index.Btree; 52 53 import com.quadcap.util.Debug; 54 import com.quadcap.util.Util; 55 56 74 public class TempTableMerge extends TempTable { 75 76 79 final static int fIS_TEMP = 0; 80 81 84 final static int fROW_ID = 1; 85 86 89 final static int fCOUNT = 9; 90 91 94 final static int BUFSIZE = 17; 95 96 99 public TempTableMerge(Session session, Key compare) 100 throws SQLException , IOException 101 { 102 super(session, compare, null); 103 this.data = new byte[BUFSIZE]; 104 } 105 106 110 public void addRows(Session session, Cursor cursor, int side, int[] map) 111 throws SQLException , IOException 112 { 113 int cpos = fCOUNT + side * 4; 114 MapRow mapRow = new MapRow(map); 115 BCursor wc = index.getCursor(); 116 if (trace) { 118 Debug.println("TempTable[" + mySerial + "].addRows() begin"); 119 } 120 try { 122 while (cursor.next()) { 123 final Row row = cursor.getRow(); 124 mapRow.setRow(row); 125 final byte[] key = Key.makeKey(null, mapRow, null, 0, false); 126 final boolean found = wc.seek(key); 127 if (found) { 128 byte[] tdata = wc.getValBuf(); 129 final int cnt = ByteUtil.getInt(tdata, cpos); 130 ByteUtil.putInt(tdata, cpos, cnt+1); 131 wc.replace(tdata, 0, BUFSIZE); 132 } else { 133 for (int i = 0; i < BUFSIZE; i++) { 134 data[i] = 0; 135 } 136 data[cpos+3] = 1; 138 long rowId = cursor.getRowId(); 139 if (map == null && rowId != 0) { 140 ByteUtil.putLong(data, fROW_ID, rowId); 141 } else { 142 rowId = session.getDatabase().putRow(session, 143 tempFile, cursor, mapRow); 144 if (trace) { 146 Debug.println("TempTable[" + mySerial + "].putRow: " + toString(rowId)); 147 } 148 ByteUtil.putLong(data, fROW_ID, rowId); 150 data[fIS_TEMP] = 1; 151 } 152 wc.insert(key, data); 153 } 154 } 155 } finally { 156 wc.release(); 157 } 158 if (trace) { 160 Debug.println("TempTable[" + mySerial + "].addRows() complete"); 161 } 162 } 164 165 public byte[] getData(byte[] key) throws IOException { 166 if (index.get(key, key.length, data) != data.length) { 167 return null; 168 } 169 return data; 170 } 171 172 final int getCount(int side) { 173 return getCount(data, side); 174 } 175 176 final static int getCount(byte[] data, int side) { 177 return ByteUtil.getInt(data, fCOUNT + (side*4)); 178 } 179 180 } 181 | Popular Tags |