KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > lang > streams


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.lang.streams
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.functionTests.tests.lang;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.DriverManager JavaDoc;
26 import java.sql.Statement JavaDoc;
27 import java.sql.PreparedStatement JavaDoc;
28 import java.sql.ResultSet JavaDoc;
29 import java.sql.SQLException JavaDoc;
30 import java.sql.SQLWarning JavaDoc;
31 import java.sql.Types JavaDoc;
32
33 import org.apache.derby.tools.ij;
34 import org.apache.derby.tools.JDBCDisplayUtil;
35
36 /**
37   This tests streams, and when we should not materialize it. Beetle entry 4896, 4955.
38
39   Some of the code comes from conn/largeStreams.java. But this program figures out whether
40   a stream is materialized or not in a different way. Part of the reason is that this test
41   should be run nightly to catch regressions and shouldn't require too much disk space. It
42   figures out whether a stream is materialized or not by comparing the stack levels of different
43   cases. The stack level is when reading the last byte of the stream. According to the current
44   code, the stack is about 10 levels deeper when reading the stream from store per page (not
45   materialized before hand), comparing to the case when materializing from sql language layer.
46   We don't expect this to change dramatically for some time. And this can always be adjusted
47   when needed.
48
49   For bug 5592 - match db's limits for long varchar which is 32700. In order to enforce that limit
50   we now materialize the stream to make sure we are not trying to overstuff data in long varchar.
51   Because of this, I had to make some changes into the stack level checking for long varchars.
52  */

53
54 public class streams {
55
56     private static int pkCount;
57     private static Connection JavaDoc conn;
58
59     public static void main(String JavaDoc[] args) {
60         System.out.println("Test streams starting");
61
62         try {
63             // use the ij utility to read the property file and
64
// make the initial connection.
65
ij.getPropertyArg(args);
66             conn = ij.startJBMS();
67
68             conn.setAutoCommit(true);
69
70             setup();
71
72             conn.setAutoCommit(false);
73
74             doWork();
75
76             conn.setAutoCommit(true);
77
78             teardown();
79
80             conn.close();
81
82         } catch (Throwable JavaDoc e) {
83             System.out.println("FAIL: exception thrown:");
84             JDBCDisplayUtil.ShowException(System.out,e);
85         }
86
87         System.out.println("Test streams finished");
88     }
89
90     static void setup() throws SQLException JavaDoc {
91         Statement JavaDoc stmt = conn.createStatement();
92         stmt.executeUpdate("call SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY('derby.storage.pageSize', '2048')");
93
94         verifyCount(
95             stmt.executeUpdate("create table t1 (id int, pid int, lvc long varchar, lvb long varchar for bit data)"),
96             0);
97         verifyCount(
98             stmt.executeUpdate("create table t2 (id int, pid int, lvc long varchar, lvb long varchar for bit data)"),
99             0);
100         verifyCount(
101             stmt.executeUpdate("create trigger tr21 after insert on t2 for each statement mode db2sql values 1"),
102             0);
103         verifyCount(
104             stmt.executeUpdate("create table t3 (id int not null primary key, pid int, lvc long varchar, lvb long varchar for bit data, CONSTRAINT FK1 Foreign Key(pid) REFERENCES T3 (id))"),
105             0);
106         verifyCount(
107             stmt.executeUpdate("create table t4 (id int, longcol long varchar)"),
108             0);
109         verifyCount(
110             stmt.executeUpdate("create table t5 (id int, longcol long varchar)"),
111             0);
112     }
113
114     static void teardown() throws SQLException JavaDoc {
115         Statement JavaDoc stmt = conn.createStatement();
116
117         verifyCount(
118             stmt.executeUpdate("drop table t1"),
119             0);
120
121         verifyCount(
122             stmt.executeUpdate("drop trigger tr21"),
123             0);
124
125         verifyCount(
126             stmt.executeUpdate("drop table t2"),
127             0);
128
129         verifyCount(
130             stmt.executeUpdate("drop table t3"),
131             0);
132
133         verifyCount(
134             stmt.executeUpdate("drop table t4"),
135             0);
136
137         verifyCount(
138             stmt.executeUpdate("drop table t5"),
139             0);
140
141         stmt.close();
142
143         System.out.println("teardown complete");
144     }
145
146     static void verifyCount(int count, int expect) throws SQLException JavaDoc {
147         if (count!=expect) {
148             System.out.println("FAIL: Expected "+expect+" got "+count+" rows");
149             throw new SQLException JavaDoc("Wrong number of rows returned");
150         }
151         else
152             System.out.println("PASS: expected and got "+count+
153                                (count == 1? " row":" rows"));
154     }
155
156     private static void doWork() throws Exception JavaDoc {
157
158         Statement JavaDoc s = conn.createStatement();
159
160         System.out.println("Start testing");
161
162         PreparedStatement JavaDoc ps = conn.prepareStatement("insert into t1 values(?, ?, ?,?)");
163         int level1 = insertLongString(ps, 8, true);
164         System.out.println("materialized insert: got reader stack level");
165
166         ps = conn.prepareStatement("insert into t2 values(?, ?, ?,?)");
167         int level2 = insertLongString(ps, 8, true);
168         System.out.println("materialized insert (for trigger): got reader stack level");
169
170         if (level1 != level2)
171             System.out.println("FAILED!! level difference not expected since streams are materialized.");
172         else
173             System.out.println("SUCCEED!! stack level as expected.");
174         
175         ps = conn.prepareStatement("insert into t3 values(?, ?, ?,?)");
176         int level3 = insertLongString(ps, 8, true);
177         System.out.println("self ref foreign key insert(should not materialize): got reader stack level");
178         if (level3 == level1)
179             System.out.println("SUCCEED!! levels expected.");
180         else
181             System.out.println("FAILED!! should not materialize in this case.");
182
183         conn.rollback();
184
185         s.executeUpdate("insert into t3 values (1,1,'a',null), (2,2,'b',null), (3,3,'c',null)");
186         ps = conn.prepareStatement("update t3 set id = ?, lvc = ? where pid = 2");
187         level1 = insertLongString(ps, 8, false);
188         System.out.println("materialized for multiple row update: got reader stack level");
189
190         ps = conn.prepareStatement("update t3 set id = ?, lvc = ? where pid = 2 and id = 2");
191         level2 = insertLongString(ps, 8, false);
192         System.out.println("single row update: got reader stack level");
193
194         if (level1 != level2)
195             System.out.println("FAILED!! level difference not expected because streams are materialized with fix for bug 5592.");
196         else
197             System.out.println("SUCCEED!! single row update materialized stream.");
198
199         s.executeUpdate("insert into t4 values (1, 'ccccc')");
200         ps = conn.prepareStatement("insert into t4 values(?, ?)");
201         insertLongString(ps, 6, false);
202         s.executeUpdate("insert into t4 values (3, 'aaaaabbbbbb')");
203         s.executeUpdate("insert into t4 values (4, 'bbbbbb')");
204         insertLongString(ps, 5, false);
205         ResultSet JavaDoc rs = s.executeQuery("select id, cast(longcol as varchar(8192)) lcol from t4 order by lcol");
206         if (rs.next()) // 3, aaaaabbbbbb
207
System.out.println("id = "+ rs.getInt(1) + " longcol = " + rs.getString(2));
208         if (rs.next()) // 4, bbbbbb
209
System.out.println("id = "+ rs.getInt(1) + " longcol = " + rs.getString(2));
210         for (int i = 0; i < 2; i++)
211         {
212             if (rs.next())
213             {
214                 String JavaDoc longcol = rs.getString(2);
215                 int collen = longcol.length();
216                 System.out.print("id = "+ rs.getInt(1) + " longcol length = " + collen);
217                 System.out.println(" longcol = " + longcol.substring(0, 5) + "..." +
218                                     longcol.substring(collen - 5, collen));
219             }
220         }
221         if (rs.next()) // 1, 'ccccc'
222
System.out.println("id = "+ rs.getInt(1) + " longcol = " + rs.getString(2));
223         if (rs.next())
224             System.out.println("FAILED, more rows left");
225         else
226             System.out.println("number of rows ok");
227
228         s.executeUpdate("insert into t5 values (1, 'bbbbbb')");
229         ps = conn.prepareStatement("insert into t5 values(?, ?)");
230         insertLongString(ps, 5, false);
231         insertLongString(ps, 7, false);
232         s.executeUpdate("insert into t5 values (3, 'aaaaabbbbbba')");
233         s.executeUpdate("insert into t5 values (4, 'bbbbbbbbb')");
234         rs = s.executeQuery("select t4.id, t4.longcol, t5.id, cast(t5.longcol as varchar(8192)) lcol from t4, t5 where cast(t4.longcol as varchar(8192)) = cast(t5.longcol as varchar(8192)) order by lcol");
235         while (rs.next())
236         {
237             System.out.println("t4 id = " + rs.getInt(1) + " t4 longcol length = " + rs.getString(2).length()
238                     + " t5 id = " + rs.getInt(3) + " t5 longcol length = " + rs.getString(4).length());
239         }
240
241         System.out.println("Start testing long var binary");
242         conn.rollback();
243
244         ps = conn.prepareStatement("insert into t1 values(?, ?, ?,?)");
245         level1 = insertLongBinary(ps, 8);
246         System.out.println("non materialized insert: got reader stack level");
247
248         ps = conn.prepareStatement("insert into t2 values(?, ?, ?,?)");
249         level2 = insertLongBinary(ps, 8);
250         System.out.println("materialized insert (for trigger): got reader stack level");
251
252         if (level1 > level2 + 5)
253             System.out.println("SUCCEED, level difference expected.");
254         else
255             System.out.println("FAILED, check stack level change.");
256         
257         ps = conn.prepareStatement("insert into t3 values(?, ?, ?,?)");
258         level3 = insertLongBinary(ps, 8);
259         System.out.println("self ref foreign key insert(should not materialize): got reader stack level");
260         if (level3 == level1)
261             System.out.println("SUCCEED!! levels expected.");
262         else
263             System.out.println("FAILED!! should not materialize stream in this case.");
264
265         conn.rollback();
266     }
267
268     private static int insertLongString(PreparedStatement JavaDoc ps, int kchars, boolean isInsert) throws SQLException JavaDoc
269     {
270         // don't end on a clean boundary
271
int chars = (kchars * 1024) + 273;
272
273         long start = System.currentTimeMillis();
274
275         DummyReader dr = new DummyReader(chars);
276         if (isInsert)
277         {
278             ps.setInt(1, pkCount);
279             ps.setInt(2, pkCount++);
280             ps.setCharacterStream(3, dr, chars);
281             ps.setNull(4, Types.VARBINARY);
282         }
283         else
284         {
285             ps.setInt(1, 2);
286             ps.setCharacterStream(2, dr, chars);
287         }
288
289         ps.executeUpdate();
290         long end = System.currentTimeMillis();
291
292         System.out.println("setCharacterStream " + chars + " chars");
293
294         return dr.readerStackLevel;
295
296     }
297
298     private static int insertLongBinary(PreparedStatement JavaDoc ps, int kbytes) throws SQLException JavaDoc {
299
300         // add a small number of bytes to ensure that we are not always ending on a clean Mb boundary
301
int bytes = (kbytes * 1024) + 273;
302
303         long start = System.currentTimeMillis();
304         ps.setInt(1, pkCount);
305         ps.setInt(2, pkCount++);
306         ps.setNull(3, Types.LONGVARCHAR);
307         DummyBinary db = new DummyBinary(bytes);
308         ps.setBinaryStream(4, db, bytes);
309
310         ps.executeUpdate();
311         long end = System.currentTimeMillis();
312
313         System.out.println("setBinaryStream " + bytes + " bytes");
314
315         return db.readerStackLevel;
316     }
317 }
318
319 class DummyReader extends java.io.Reader JavaDoc {
320
321     private int count;
322     public int readerStackLevel;
323
324     DummyReader(int length) {
325         this.count = length;
326     }
327
328     private void whereAmI() {
329         if (count == 0)
330         {
331             readerStackLevel = -1;
332             try {throw new Throwable JavaDoc();} catch (Throwable JavaDoc e) {
333                 try {
334                     readerStackLevel = e.getStackTrace().length;
335                 // System.out.println("================= stack array length is: " + readerStackLevel);
336
// e.printStackTrace();
337
} catch (NoSuchMethodError JavaDoc nme) {
338                     DummyOutputStream dos = new DummyOutputStream();
339                     DummyPrintStream dps = new DummyPrintStream(dos);
340                     e.printStackTrace(dps);
341                     dps.flush();
342                 // System.out.println("================= print to dop level num is: " + dps.lines);
343
readerStackLevel = dps.lines;
344                 // e.printStackTrace();
345
}
346             }
347         }
348     }
349
350     public int read() {
351         if (count == 0)
352             return -1;
353
354         count--;
355         whereAmI();
356
357         return 'b';
358     }
359
360     public int read(char[] buf, int offset, int length) {
361
362         if (count == 0)
363             return -1;
364
365         if (length > count)
366             length = count;
367
368         count -= length;
369         whereAmI();
370
371         java.util.Arrays.fill(buf, offset, offset + length, 'b');
372
373         return length;
374     }
375
376     public void close(){}
377 }
378
379 class DummyBinary extends java.io.InputStream JavaDoc {
380
381     public int readerStackLevel;
382     int count;
383     byte content = 42;
384     DummyBinary(int length) {
385         this.count = length;
386     }
387
388     private void whereAmI() {
389         if (count == 0)
390         {
391             readerStackLevel = -1;
392             try {throw new Throwable JavaDoc();} catch (Throwable JavaDoc e) {
393                 try {
394                     readerStackLevel = e.getStackTrace().length;
395                 // System.out.println("================= stack array length is: " + readerStackLevel);
396
// e.printStackTrace();
397
} catch (NoSuchMethodError JavaDoc nme) {
398                     DummyOutputStream dos = new DummyOutputStream();
399                     DummyPrintStream dps = new DummyPrintStream(dos);
400                     e.printStackTrace(dps);
401                     dps.flush();
402                 // System.out.println("================= print to dop level num is: " + dps.lines);
403
readerStackLevel = dps.lines;
404                 // e.printStackTrace();
405
}
406             }
407         }
408     }
409
410     public int read() {
411         if (count == 0)
412             return -1;
413
414         count--;
415         whereAmI();
416         return content++;
417     }
418
419     public int read(byte[] buf, int offset, int length) {
420
421         if (count == 0)
422             return -1;
423
424         if (length > count)
425             length = count;
426
427         count -= length;
428         whereAmI();
429
430         for (int i = 0; i < length; i++)
431             buf[offset + i] = content++;
432
433         return length;
434     }
435
436     public void close(){}
437 }
438
439
440 class DummyOutputStream extends java.io.OutputStream JavaDoc {
441     public void close() {}
442     public void flush() {}
443     public void write(byte[] b) {}
444     public void write(byte[] b, int off, int len) {}
445     public void write(int b) {}
446 }
447
448 class DummyPrintStream extends java.io.PrintStream JavaDoc {
449     public int lines;
450     public DummyPrintStream(DummyOutputStream dos) {super(dos);}
451     public void println() { lines++; }
452     public void println(String JavaDoc x) { lines++; }
453     public void println(Object JavaDoc x) { lines++; }
454     public void println(char[] x) { lines++; }
455     public void println(double x) { lines++; }
456     public void println(float x) { lines++; }
457     public void println(long x) { lines++; }
458     public void println(int x) { lines++; }
459     public void println(char x) { lines++; }
460     public void println(boolean x) { lines++; }
461 }
462
Popular Tags