00001
package com.quadcap.sql;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
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
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074 public class TempTableMerge extends TempTable {
00075
00076
00077
00078
00079 final static int fIS_TEMP = 0;
00080
00081
00082
00083
00084 final static int fROW_ID = 1;
00085
00086
00087
00088
00089 final static int fCOUNT = 9;
00090
00091
00092
00093
00094 final static int BUFSIZE = 17;
00095
00096
00097
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
00108
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
00117
if (trace) {
00118
Debug.println(
"TempTable[" + mySerial +
"].addRows() begin");
00119 }
00120
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;
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
00145
if (trace) {
00146
Debug.println(
"TempTable[" + mySerial +
"].putRow: " + toString(rowId));
00147 }
00148
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
00159
if (trace) {
00160
Debug.println(
"TempTable[" + mySerial +
"].addRows() complete");
00161 }
00162
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 }