KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 /**
59  * Temporary tables are Useful for several operations, including
60  * <b>GROUP BY</b>,
61  * <b>DISTINCT</b>,
62  * and <b>ORDER BY</b>.
63  *
64  * Todo: in-memory version of this based on configurable 'max temp' size.
65  *
66  * @author Stan Bailes
67  */

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     //#ifdef DEBUG
80
static int serial = 0;
81     int mySerial = serial++;
82     static final boolean trace = false;
83     //#endif
84

85     public TempTable(Session session, Key compare, int[] map)
86     throws SQLException JavaDoc, IOException JavaDoc
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 JavaDoc, IOException JavaDoc
99     {
100         int count = 0;
101         //#ifdef DEBUG
102
if (trace) {
103             Debug.println("TempTable[" + mySerial + "].addRows() begin");
104             Debug.println("cursor = " + cursor);
105         }
106         //#endif
107
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                 //#ifdef DEBUG
117
if (trace) {
118                     Debug.println("TempTable[" + mySerial + "].putRow: " +
119                                   toString(rowId) + " " + row);
120                 }
121                 //#endif
122
data[0] = 1;
123         } else {
124         data[0] = 0;
125                 //#ifdef DEBUG
126
if (trace) {
127                     Debug.println("TempTable[" + mySerial + "] refersTo row " +
128                                   toString(rowId) + " " + row);
129                 }
130                 //#endif
131
}
132             ByteUtil.putLong(data, 1, rowId);
133         index.set(key, data);
134     }
135         //#ifdef DEBUG
136
if (trace) {
137             Debug.println("TempTable[" + mySerial + "].addRows() complete");
138         }
139         //#endif
140
return count;
141     }
142
143     public byte[] makeKey(Row row) throws SQLException JavaDoc {
144     return Key.makeKey(null, row, map, keyCount++, true);
145     }
146
147     public BCursor getCursor() throws IOException JavaDoc {
148     return index.getCursor(false);
149     }
150
151     public void getRow(byte[] rowIdBuf, LazyRow row)
152     throws SQLException JavaDoc, IOException JavaDoc
153     {
154     long rowId = ByteUtil.getLong(rowIdBuf, 1);
155         boolean isTemp = rowIdBuf[0] != 0;
156         //#ifdef DEBUG
157
if (trace) {
158             Debug.println("TempTable[" + mySerial + "].getRow(" +
159                           toString(rowId) + ", " + isTemp + ")");
160         }
161         //#endif
162
session.getDatabase().getRow(rowId, row, isTemp);
163     }
164
165     public void release() throws IOException JavaDoc {
166         //#ifdef DEBUG
167
if (trace) {
168             Debug.println("TempTable[" + mySerial + "].release()");
169         }
170         //#endif
171
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                                 // We don't bother freeing the stream, to
183
// save time. Ultimately, we can just toss
184
// the entire temp file....
185
//tempFile.freeStream(ref);
186
}
187                         }
188                     }
189                 } finally {
190                     c.release();
191                 }
192             } finally {
193                 index.free();
194             }
195         } finally {
196             session.getDatabase().releaseTempFile();
197         }
198     }
199
200     //#ifdef DEBUG
201
public String JavaDoc toString(long rowId) {
202     //return SubPageManager.toString(rowId);
203
return String.valueOf(rowId);
204     }
205     //#endif
206

207 }
208
Popular Tags