KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > TempTableMerge


1 package com.quadcap.sql;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.IOException JavaDoc;
42
43 import java.sql.SQLException JavaDoc;
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 /**
57  * A special temp table used to implement <b>UNION</b> and <b>INTERSECT</b>
58  * expressions.
59  *
60  * <p>
61  * The temp table that we build has keys based on the union columns,
62  * and the data as 13 bytes:
63  * </p>
64  * <table>
65  * <tr><td>byte 0:</td> <td>if zero, the rowId refers to a real table row
66  * if one, the rowId refers to a temp row</td></tr>
67  * <tr><td>bytes 1-8:</td> <td>the rowId of the union row</td></tr>
68  * <tr><td>bytes 9-12:</td> <td>the duplicate count (from table 1)</td></tr>
69  * <tr><td>bytes 13-16:</td><td> the duplicate count (from table 2)</td></td>
70  * </table>
71  *
72  * @author Stan Bailes
73  */

74 public class TempTableMerge extends TempTable {
75
76     /**
77      * Byte offset of 'isTemp' byte flag in a data row.
78      */

79     final static int fIS_TEMP = 0;
80
81     /**
82      * The byte offset of the (long) row ID in data data row
83      */

84     final static int fROW_ID = 1;
85
86     /**
87      * The byte offset of the two 'count' integers. in the data row
88      */

89     final static int fCOUNT = 9;
90
91     /**
92      * Total size of the data buffer in our temp index
93      */

94     final static int BUFSIZE = 17;
95
96     /**
97      * Constructor for session and key
98      */

99     public TempTableMerge(Session session, Key compare)
100     throws SQLException JavaDoc, IOException JavaDoc
101     {
102     super(session, compare, null);
103         this.data = new byte[BUFSIZE];
104     }
105     
106     /**
107      * Add rows from one side of the {union/merge} operation, building a
108      * temporary index on the key.
109      */

110     public void addRows(Session session, Cursor cursor, int side, int[] map)
111         throws SQLException JavaDoc, IOException JavaDoc
112     {
113     int cpos = fCOUNT + side * 4;
114         MapRow mapRow = new MapRow(map);
115         BCursor wc = index.getCursor();
116         //#ifdef DEBUG
117
if (trace) {
118             Debug.println("TempTable[" + mySerial + "].addRows() begin");
119         }
120         //#endif
121
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; //ByteUtil.putInt(data, cpos, 1);
137

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                         //#ifdef DEBUG
145
if (trace) {
146                             Debug.println("TempTable[" + mySerial + "].putRow: " + toString(rowId));
147                         }
148                         //#endif
149
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         //#ifdef DEBUG
159
if (trace) {
160             Debug.println("TempTable[" + mySerial + "].addRows() complete");
161         }
162         //#endif
163
}
164
165     public byte[] getData(byte[] key) throws IOException JavaDoc {
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