File: SpaceTableTest.java

package info (click to toggle)
derby 10.14.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 79,056 kB
  • sloc: java: 691,961; sql: 42,686; xml: 20,512; sh: 3,373; sed: 96; makefile: 60
file content (418 lines) | stat: -rw-r--r-- 18,260 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*

 Derby - Class org.apache.derbyTesting.functionTests.tests.store.SpaceTableTest

 Licensed to the Apache Software Foundation (ASF) under one or more
 contributor license agreements.  See the NOTICE file distributed with
 this work for additional information regarding copyright ownership.
 The ASF licenses this file to You under the Apache License, Version 2.0
 (the "License"); you may not use this file except in compliance with
 the License.  You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

 */
package org.apache.derbyTesting.functionTests.tests.store;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;

import junit.framework.AssertionFailedError;
import junit.framework.Test;

import org.apache.derbyTesting.functionTests.util.Formatters;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.CleanDatabaseTestSetup;
import org.apache.derbyTesting.junit.JDBC;
import org.apache.derbyTesting.junit.TestConfiguration;

/**
 * Tests the printing of the WAIT state in the LOCK TABLE.
 */
public class SpaceTableTest extends BaseJDBCTestCase {

    public SpaceTableTest(String name) {
        super(name);
    }

    /**
     * Construct top level suite in this JUnit test
     * The suite is wrapped in a DatabasePropertyTestSetup to set
     * the lock wait timeout.
     *
     * @return A suite containing embedded fixtures
     */
    public static Test suite() {

        Test suite = TestConfiguration.embeddedSuite (SpaceTableTest.class);
        return new CleanDatabaseTestSetup(suite) {
            /**
             * Creates the schemas and table used in the test cases.
             *
             * @throws SQLException
             */
            protected void decorateSQL(Statement s) throws SQLException {
                Connection conn = getConnection();
                conn.setAutoCommit(false);
                String createWaitForPostCommit=
                    "CREATE PROCEDURE WAIT_FOR_POST_COMMIT() " +
                        "DYNAMIC RESULT SETS 0 " +
                        "LANGUAGE JAVA EXTERNAL NAME " +
                        "'org.apache.derbyTesting.functionTests.util.T_Access." +
                            "waitForPostCommitToFinish' " +
                        "PARAMETER STYLE JAVA";
                s.executeUpdate(createWaitForPostCommit);
                conn.commit();
                conn.setAutoCommit(true);
            }
        };
    }

    
    protected void setUp() throws Exception {
        super.setUp();
    }

    /**
     * Tear-down the fixture by removing the tables and schemas
     * @throws Exception
     */
    protected void tearDown() throws Exception {
        Statement stmt = createStatement();
        // cannot drop wait_for_post_commit or it will not exist in
        // all test methods. CleanDatabaseSetup should take care of it.
        // stmt.executeUpdate("drop procedure WAIT_FOR_POST_COMMIT");
        dropTable("IDELETEU");
        dropTable("PLATYPUS");
        dropTable("\"platypus2\"");
        dropFooTables(stmt);
        // force pagesize back to default
        stmt.execute("call SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(" +
                "'derby.storage.pageSize', NULL)");
        commit();
        super.tearDown();
    }

    public void testIDeleteu() throws SQLException, InterruptedException {
        Statement stmt = createStatement();
        stmt.execute("call SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(" +
                "'derby.storage.pageSize', '4096')");
        stmt.executeUpdate("create table ideleteu " +
                "(a varchar(2000), b varchar(2000))");
        String insertString = "insert into ideleteu values ('" +
                Formatters.padString("rrrrrrrrrr",2000) + "', '" +
                Formatters.padString("ssssssssssssssss",2000) + "')";
        for (int i=0; i<5; i++)
        stmt.executeUpdate(insertString);
        
        assertSpaceTableOK("IDELETEU", 
                new String[][]{{"IDELETEU","0","6","0","4096","0"}}, false);
        
        stmt.executeUpdate("delete from ideleteu");
        // explicit commit, superfluous, as autocommit set in decorateSQL
        commit();
        stmt.execute("call WAIT_FOR_POST_COMMIT()");
        // this is one of 2 places where in the sql version of this test we
        // still saw highly intermittent diffs even with the wait for post commit
        // see DERBY-5133. So call to local check.
        assertSpaceTableOK("IDELETEU", 
                new String[][]{{"IDELETEU","0","1","5","4096","20480"}}, true);
        stmt.executeUpdate("drop table ideleteu");
        commit();
        // the default is 4096 (with short columns), so no need to set to storage page size back.
    }
    
    public void testPlatypi() throws SQLException, InterruptedException
    {
        // check results when inserting data after creating indexes
        Statement stmt = createStatement();
        stmt.execute("call SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(" +
                "'derby.storage.pageSize', '4096')");
        JDBC.assertEmpty(doSpaceTableSelect("PLATYPUS"));
        stmt.executeUpdate("create table platypus " +
                "(a varchar(1000), b varchar(3500), " +
                "c varchar(400), d varchar(100))");
        stmt.executeUpdate("create index kookaburra on platypus (a)");
        stmt.executeUpdate("create index echidna on platypus (c)");
        stmt.executeUpdate("create index wallaby on platypus (a,c,d)");
        assertSpaceTableOK("PLATYPUS", new String[][]{
                {"ECHIDNA","1","1","0","4096","0"},
                {"KOOKABURRA","1","1","0","4096","0"},
                {"PLATYPUS","0","1","0","4096","0"},
                {"WALLABY","1","1","0","4096","0"},
                }, false);
        String insertString = "insert into platypus values ('" +
                Formatters.padString("wwwwwww",1000) + "', '" +
                Formatters.padString("xxx",3500) + "', '" +
                Formatters.padString("yy",400) + "', '" +
                Formatters.padString("zzz",100) + "')";
        for (int i=0; i<6; i++)
        stmt.executeUpdate(insertString);
        assertSpaceTableOK("PLATYPUS", new String[][]{
                {"ECHIDNA","1","1","0","4096","0"},
                {"KOOKABURRA","1","4","0","4096","0"},
                {"PLATYPUS","0","13","0","4096","0"},
                {"WALLABY","1","8","0","4096","0"},
                }, false);
        for (int i=0; i<4; i++)
        stmt.executeUpdate(insertString);
        assertSpaceTableOK("PLATYPUS", new String[][]{
                {"ECHIDNA","1","3","0","4096","0"},
                {"KOOKABURRA","1","8","0","4096","0"},
                {"PLATYPUS","0","21","0","4096","0"},
                {"WALLABY","1","16","0","4096","0"},
                }, false);
        stmt.executeUpdate("delete from platypus");
        // we've got autocommit on, so there was a commit...
        stmt.execute("call WAIT_FOR_POST_COMMIT()");
        assertSpaceTableOK("PLATYPUS", new String[][]{
                {"ECHIDNA","1","1","2","4096","8192"},
                {"KOOKABURRA","1","1","7","4096","28672"},
                {"PLATYPUS","0","1","20","4096","81920"},
                {"WALLABY","1","15","1","4096","4096"},
                }, true);
        
        // check the results when we create indexes after inserting data
        // also note this table has lower case name
        stmt.executeUpdate("create table \"platypus2\" " +
                "(a varchar(10), b varchar(1500), " +
                "c varchar(400), d varchar(100))");
        insertString = "insert into \"platypus2\" values ('" +
                Formatters.padString("wwwwwww",10) + "', '" +
                Formatters.padString("xxx",1500) + "', '" +
                Formatters.padString("yy",400) + "', '" +
                Formatters.padString("zzz",100) + "')";
        for (int i=0; i<6; i++)
        stmt.executeUpdate(insertString);
        stmt.executeUpdate("create index kookaburra2 on \"platypus2\" (a)");
        stmt.executeUpdate("create index echidna2 on \"platypus2\" (c)");
        stmt.executeUpdate("create index wallaby2 on \"platypus2\" (a,c,d)");
        assertSpaceTableOK("platypus2", new String[][]{
                {"ECHIDNA2","1","1","0","4096","0"},
                {"KOOKABURRA2","1","1","0","4096","0"},
                {"WALLABY2","1","1","0","4096","0"},
                {"platypus2","0","6","0","4096","0"},
                }, false);

        ResultSet rs = stmt.executeQuery(
                "select conglomeratename, isindex, numallocatedpages, " +
                "numfreepages, pagesize, estimspacesaving " +
                "from SYS.SYSSCHEMAS s, " +
                "SYS.SYSTABLES t, " +
                "new org.apache.derby.diag.SpaceTable(SCHEMANAME,TABLENAME) v " +
                "where s.SCHEMAID = t.SCHEMAID " +
                "and s.SCHEMANAME = 'APP' " +
                "order by conglomeratename");
        JDBC.assertFullResultSet(rs, new String [][] {
                {"ECHIDNA","1","1","2","4096","8192"},
                {"ECHIDNA2","1","1","0","4096","0"},
                {"KOOKABURRA","1","1","7","4096","28672"},
                {"KOOKABURRA2","1","1","0","4096","0"},
                {"PLATYPUS","0","1","20","4096","81920"},
                {"WALLABY","1","15","1","4096","4096"},
                {"WALLABY2","1","1","0","4096","0"},
                {"platypus2","0","6","0","4096","0"},
                });
        stmt.executeUpdate("drop table platypus");
        stmt.executeUpdate("drop table \"platypus2\"");
    }
    
    public void testReservedSpace() throws SQLException, InterruptedException
    {
        Statement stmt = createStatement();
        // first ensure we're using the default.
        stmt.execute("call SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(" +
                "'derby.storage.pageSize', '4096')");
        setAutoCommit(false);
        
        // no reserved space set - default
        stmt.executeUpdate("create table foo_int (a int)");
        stmt.executeUpdate("create table foo_char (a char(100))");
        stmt.executeUpdate("create table foo_varchar (a varchar(32000))");
        // let the foo_longxxx get created at 32K (the default for long types)
        stmt.execute("call SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(" +
                "'derby.storage.pageSize', NULL)");
        stmt.executeUpdate("create table foo_longvarchar (a long varchar)");
        stmt.executeUpdate("create table foo_longvarbinary " +
                "(a long varchar for bit data)");
        // Back to 4K
        stmt.execute("call SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(" +
                "'derby.storage.pageSize', '4096')");
        stmt.executeUpdate("create table foo_bit (a char(100) for bit data)");
        stmt.executeUpdate("create table foo_varbinary " +
                "(a varchar(1000) for bit data)");
        
        JDBC.assertFullResultSet(doSpaceTableSelect2(), new String [][] {
                {"FOO_BIT","4096"},
                {"FOO_CHAR","4096"},
                {"FOO_INT","4096"},
                {"FOO_LONGVARBINARY","32768"},
                {"FOO_LONGVARCHAR","32768"},
                {"FOO_VARBINARY","4096"},
                {"FOO_VARCHAR","4096"},
                });
        
        dropFooTables(stmt);
        
        // test with 65K reserved space
        stmt.execute("call SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(" +
                "'derby.storage.ReservedSpace', '65')");
        stmt.executeUpdate("create table foo_int (a int)");
        stmt.executeUpdate("create table foo_char (a char(100))");
        stmt.executeUpdate("create table foo_varchar (a varchar(32000))");
        // let the foo_longxxx get created at 32K (the default for long types)
        stmt.execute("call SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(" +
                "'derby.storage.pageSize', NULL)");
        stmt.executeUpdate("create table foo_longvarchar (a long varchar)");
        stmt.executeUpdate("create table foo_longvarbinary " +
                "(a long varchar for bit data)");
        // Back to 4K
        stmt.execute("call SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(" +
                "'derby.storage.pageSize', '4096')");
        stmt.executeUpdate("create table foo_bit (a char(100) for bit data)");
        stmt.executeUpdate("create table foo_varbinary " +
                "(a varchar(1000) for bit data)");
        
        // reset the reserved space to default (by setting property to NULL)
        stmt.execute("call SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(" +
                "'derby.storage.ReservedSpace', NULL)");
        
        JDBC.assertFullResultSet(doSpaceTableSelect2(), new String [][] {
            {"FOO_BIT","4096"},
            {"FOO_CHAR","4096"},
            {"FOO_INT","4096"},
            {"FOO_LONGVARBINARY","32768"},
            {"FOO_LONGVARCHAR","32768"},
            {"FOO_VARBINARY","4096"},
            {"FOO_VARCHAR","4096"},
            });
        
        dropFooTables(stmt);
        
        // 8K pagesize
        stmt.execute("call SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(" +
                "'derby.storage.pageSize', '8192')");
        stmt.executeUpdate("create table foo_int (a int)");
        stmt.executeUpdate("create table foo_char (a char(100))");
        stmt.executeUpdate("create table foo_varchar (a varchar(32000))");
        stmt.executeUpdate("create table foo_longvarchar (a long varchar)");
        stmt.executeUpdate("create table foo_longvarbinary " +
                "(a long varchar for bit data)");
        stmt.executeUpdate("create table foo_bit (a char(100) for bit data)");
        stmt.executeUpdate("create table foo_varbinary " +
                "(a varchar(1000) for bit data)");
        
        JDBC.assertFullResultSet(doSpaceTableSelect2(), new String [][] {
            {"FOO_BIT","8192"},
            {"FOO_CHAR","8192"},
            {"FOO_INT","8192"},
            {"FOO_LONGVARBINARY","8192"},
            {"FOO_LONGVARCHAR","8192"},
            {"FOO_VARBINARY","8192"},
            {"FOO_VARCHAR","8192"},
            });
        
        dropFooTables(stmt);
        
        // test with commit after setting pageSize at 4096
        stmt.execute("call SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(" +
                "'derby.storage.pageSize', '4096')");
        commit();
        stmt.executeUpdate("create table foo_int (a int)");
        stmt.executeUpdate("create table foo_char (a char(100))");
        stmt.executeUpdate("create table foo_varchar (a varchar(32000))");
        stmt.executeUpdate("create table foo_longvarchar (a long varchar)");
        stmt.executeUpdate("create table foo_longvarbinary " +
                "(a long varchar for bit data)");
        stmt.executeUpdate("create table foo_bit (a char(100) for bit data)");
        stmt.executeUpdate("create table foo_varbinary " +
                "(a varchar(1000) for bit data)");
        
        JDBC.assertFullResultSet(doSpaceTableSelect2(), new String [][] {
            {"FOO_BIT","4096"},
            {"FOO_CHAR","4096"},
            {"FOO_INT","4096"},
            {"FOO_LONGVARBINARY","4096"},
            {"FOO_LONGVARCHAR","4096"},
            {"FOO_VARBINARY","4096"},
            {"FOO_VARCHAR","4096"},
            });
        
        commit();
        dropFooTables(stmt);
        commit();
    }
    
    private ResultSet doSpaceTableSelect(String tableName) throws SQLException
    {
        ResultSet rs = null;
        String stmt_str = 
                "select " + 
                        "conglomeratename, " +
                        "isindex, "           + 
                        "numallocatedpages, " + 
                        "numfreepages, "      + 
                        "pagesize, "          + 
                        "estimspacesaving "   + 
                        "from new org.apache.derby.diag.SpaceTable('" +
                        tableName + "') t order by conglomeratename";

        PreparedStatement space_stmt = prepareStatement(stmt_str);
        rs = space_stmt.executeQuery();
        return rs;
    }
    
    private ResultSet doSpaceTableSelect2() throws SQLException
    {
        ResultSet rs = null;
        String stmt_str = 
                "select " + 
                        "v.conglomeratename, pagesize " +
                        "from SYS.SYSSCHEMAS s, SYS.SYSTABLES t, " +
                        "new org.apache.derby.diag.SpaceTable(" +
                        "schemaname, tablename) v " +
                        "where s.schemaid=t.schemaid and " +
                        "conglomeratename in (" +
                        "'FOO_INT', 'FOO_VARCHAR', 'FOO_CHAR', 'FOO_LONGVARCHAR'," +
                        "'FOO_VARBINARY', 'FOO_LONGVARBINARY', 'FOO_BIT') " +
                        "order by 1";

        PreparedStatement space_stmt = prepareStatement(stmt_str);
        rs = space_stmt.executeQuery();
        return rs;
    }
    
    private void assertSpaceTableOK(String tableName, String[][] expRS, 
            boolean trytwice)
            throws SQLException, InterruptedException {
        ResultSet rs = doSpaceTableSelect(tableName);
        try {
            JDBC.assertFullResultSet(rs, expRS );
        } catch (AssertionFailedError e) {
            if (trytwice) {
                assertSpaceTableOK(tableName, expRS, false);
            } else {
                throw e;
            }
        }
    }
    
    public void dropFooTables(Statement stmt) throws SQLException {
        dropTable("FOO_INT");
        dropTable("FOO_CHAR");
        dropTable("FOO_VARCHAR");
        dropTable("FOO_LONGVARCHAR");
        dropTable("FOO_LONGVARBINARY");
        dropTable("FOO_BIT");
        dropTable("FOO_VARBINARY");
    }
}