Quadcap Embeddable Database

com/quadcap/sql/TempTableMerge.java

Go to the documentation of this file.
00001 package com.quadcap.sql; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.IOException; 00042 00043 import java.sql.SQLException; 00044 00045 import com.quadcap.sql.file.BlockFile; 00046 import com.quadcap.sql.file.ByteUtil; 00047 import com.quadcap.sql.file.PageManager; 00048 import com.quadcap.sql.file.SubPageManager; 00049 00050 import com.quadcap.sql.index.BCursor; 00051 import com.quadcap.sql.index.Btree; 00052 00053 import com.quadcap.util.Debug; 00054 import com.quadcap.util.Util; 00055 00056 /** 00057 * A special temp table used to implement <b>UNION</b> and <b>INTERSECT</b> 00058 * expressions. 00059 * 00060 * <p> 00061 * The temp table that we build has keys based on the union columns, 00062 * and the data as 13 bytes: 00063 * </p> 00064 * <table> 00065 * <tr><td>byte 0:</td> <td>if zero, the rowId refers to a real table row 00066 * if one, the rowId refers to a temp row</td></tr> 00067 * <tr><td>bytes 1-8:</td> <td>the rowId of the union row</td></tr> 00068 * <tr><td>bytes 9-12:</td> <td>the duplicate count (from table 1)</td></tr> 00069 * <tr><td>bytes 13-16:</td><td> the duplicate count (from table 2)</td></td> 00070 * </table> 00071 * 00072 * @author Stan Bailes 00073 */ 00074 public class TempTableMerge extends TempTable { 00075 00076 /** 00077 * Byte offset of 'isTemp' byte flag in a data row. 00078 */ 00079 final static int fIS_TEMP = 0; 00080 00081 /** 00082 * The byte offset of the (long) row ID in data data row 00083 */ 00084 final static int fROW_ID = 1; 00085 00086 /** 00087 * The byte offset of the two 'count' integers. in the data row 00088 */ 00089 final static int fCOUNT = 9; 00090 00091 /** 00092 * Total size of the data buffer in our temp index 00093 */ 00094 final static int BUFSIZE = 17; 00095 00096 /** 00097 * Constructor for session and key 00098 */ 00099 public TempTableMerge(Session session, Key compare) 00100 throws SQLException, IOException 00101 { 00102 super(session, compare, null); 00103 this.data = new byte[BUFSIZE]; 00104 } 00105 00106 /** 00107 * Add rows from one side of the {union/merge} operation, building a 00108 * temporary index on the key. 00109 */ 00110 public void addRows(Session session, Cursor cursor, int side, int[] map) 00111 throws SQLException, IOException 00112 { 00113 int cpos = fCOUNT + side * 4; 00114 MapRow mapRow = new MapRow(map); 00115 BCursor wc = index.getCursor(); 00116 //#ifdef DEBUG 00117 if (trace) { 00118 Debug.println("TempTable[" + mySerial + "].addRows() begin"); 00119 } 00120 //#endif 00121 try { 00122 while (cursor.next()) { 00123 final Row row = cursor.getRow(); 00124 mapRow.setRow(row); 00125 final byte[] key = Key.makeKey(null, mapRow, null, 0, false); 00126 final boolean found = wc.seek(key); 00127 if (found) { 00128 byte[] tdata = wc.getValBuf(); 00129 final int cnt = ByteUtil.getInt(tdata, cpos); 00130 ByteUtil.putInt(tdata, cpos, cnt+1); 00131 wc.replace(tdata, 0, BUFSIZE); 00132 } else { 00133 for (int i = 0; i < BUFSIZE; i++) { 00134 data[i] = 0; 00135 } 00136 data[cpos+3] = 1; //ByteUtil.putInt(data, cpos, 1); 00137 00138 long rowId = cursor.getRowId(); 00139 if (map == null && rowId != 0) { 00140 ByteUtil.putLong(data, fROW_ID, rowId); 00141 } else { 00142 rowId = session.getDatabase().putRow(session, 00143 tempFile, cursor, mapRow); 00144 //#ifdef DEBUG 00145 if (trace) { 00146 Debug.println("TempTable[" + mySerial + "].putRow: " + toString(rowId)); 00147 } 00148 //#endif 00149 ByteUtil.putLong(data, fROW_ID, rowId); 00150 data[fIS_TEMP] = 1; 00151 } 00152 wc.insert(key, data); 00153 } 00154 } 00155 } finally { 00156 wc.release(); 00157 } 00158 //#ifdef DEBUG 00159 if (trace) { 00160 Debug.println("TempTable[" + mySerial + "].addRows() complete"); 00161 } 00162 //#endif 00163 } 00164 00165 public byte[] getData(byte[] key) throws IOException { 00166 if (index.get(key, key.length, data) != data.length) { 00167 return null; 00168 } 00169 return data; 00170 } 00171 00172 final int getCount(int side) { 00173 return getCount(data, side); 00174 } 00175 00176 final static int getCount(byte[] data, int side) { 00177 return ByteUtil.getInt(data, fCOUNT + (side*4)); 00178 } 00179 00180 }