1 21 22 package org.apache.derbyTesting.functionTests.tests.storetests; 23 24 25 import org.apache.derby.iapi.services.sanity.SanityManager; 26 27 import org.apache.derbyTesting.functionTests.tests.store.BaseTest; 28 29 import java.sql.CallableStatement ; 30 import java.sql.Connection ; 31 import java.sql.PreparedStatement ; 32 import java.sql.ResultSet ; 33 import java.sql.SQLException ; 34 import java.sql.Statement ; 35 36 import java.util.Arrays ; 37 38 import org.apache.derby.tools.ij; 39 40 41 61 62 public class st_reclaim_longcol extends BaseTest 63 { 64 static boolean verbose = false; 65 66 public st_reclaim_longcol() 67 { 68 } 69 70 71 74 private static void setup() 75 throws Exception 76 { 77 } 78 79 87 private static final int SHORT_BLOB_SIZE = 10; 88 public void test1(Connection conn, int blob_size, int num_rows) 89 throws SQLException 90 { 91 byte[] long_byteVal = new byte[blob_size]; 92 byte[] short_byteVal = new byte[10]; 93 94 beginTest( 95 conn, 96 "test1:insert/delete of " + num_rows + 97 " rows with blob(" + blob_size + ")"); 98 99 Arrays.fill(long_byteVal, (byte)'L'); 100 Arrays.fill(short_byteVal, (byte)'S'); 101 102 createTable( 103 conn, 104 "longcol", 105 "create table longcol (id int primary key not null, val blob(" + 106 blob_size + "))"); 107 108 conn.commit(); 109 110 PreparedStatement ins_stmt = 111 conn.prepareStatement("insert into longcol values (?, ?)"); 112 PreparedStatement del_stmt = 113 conn.prepareStatement("delete from longcol where id = ?"); 114 115 122 for (int iter = 1; iter < num_rows; iter++) 128 { 129 ins_stmt.setInt( 1, iter); 131 ins_stmt.setBytes(2, long_byteVal); 132 ins_stmt.executeUpdate(); 133 134 ins_stmt.setInt( 1, -(iter)); 136 ins_stmt.setBytes(2, short_byteVal); 137 ins_stmt.executeUpdate(); 138 139 del_stmt.setInt(1, iter); 141 del_stmt.executeUpdate(); 142 143 conn.commit(); 146 147 try 150 { 151 Thread.sleep(20); 152 } 153 catch (Exception ex) 154 { 155 } 157 } 158 159 int[] sp_info = getSpaceInfo(conn, "APP", "LONGCOL", true); 160 161 int total_pages = 162 sp_info[SPACE_INFO_NUM_ALLOC] + sp_info[SPACE_INFO_NUM_FREE]; 163 164 int total_expected_page_max = 12 + num_rows; 165 166 if (total_pages > total_expected_page_max) 167 { 168 179 180 181 System.out.println( 182 "Test failed, expected less than " + 183 total_expected_page_max + " pages - count is:\n" + 184 "free pages : " + sp_info[SPACE_INFO_NUM_FREE] + 185 "\nallocated pages: " + sp_info[SPACE_INFO_NUM_ALLOC]); 186 } 187 188 if (verbose) 189 { 190 System.out.println( 191 "Space information after " + num_rows + 192 "insert/delete pairs of rows in longcol table containing " + 193 blob_size + "blobs:"); 194 195 System.out.println("isindex = " + sp_info[SPACE_INFO_IS_INDEX]); 196 System.out.println("num_alloc = " + sp_info[SPACE_INFO_NUM_ALLOC]); 197 System.out.println("num_free = " + sp_info[SPACE_INFO_NUM_FREE]); 198 System.out.println("page_size = " + sp_info[SPACE_INFO_PAGE_SIZE]); 199 System.out.println( 200 "estimspacesaving = " + sp_info[SPACE_INFO_ESTIMSPACESAVING]); 201 } 202 203 endTest( 204 conn, 205 "test1:insert/delete of " + num_rows + 206 " rows with blob(" + blob_size + ")"); 207 } 208 209 218 public void test2( 219 Connection conn, 220 int blob_size, 221 int work_size, 222 int total_work) 223 throws SQLException 224 { 225 byte[] long_byteVal = new byte[blob_size]; 226 byte[] short_byteVal = new byte[10]; 227 228 beginTest( 229 conn, 230 "test2:queue of " + work_size + 231 " rows with blob(" + blob_size + "), total_work = " + 232 total_work); 233 234 Arrays.fill(long_byteVal, (byte)'L'); 235 Arrays.fill(short_byteVal, (byte)'S'); 236 237 createTable( 238 conn, 239 "longcol", 240 "create table longcol (id int primary key not null, val blob(" + 241 blob_size + "))"); 242 243 conn.commit(); 244 245 PreparedStatement ins_stmt = 246 conn.prepareStatement("insert into longcol values (?, ?)"); 247 PreparedStatement del_stmt = 248 conn.prepareStatement("delete from longcol where id = ?"); 249 250 for (int iter = 0; iter < work_size; iter++) 252 { 253 ins_stmt.setInt( 1, iter); 255 ins_stmt.setBytes(2, long_byteVal); 256 ins_stmt.executeUpdate(); 257 258 } 261 conn.commit(); 262 263 264 for (int iter = work_size; iter < total_work; iter++) 267 { 268 ins_stmt.setInt( 1, iter); 270 ins_stmt.setBytes(2, long_byteVal); 271 ins_stmt.executeUpdate(); 272 273 274 del_stmt.setInt(1, iter - work_size - 1); 276 del_stmt.executeUpdate(); 277 278 conn.commit(); 281 282 try 283 { 284 Thread.sleep(20); 285 } 286 catch (Exception ex) 287 { 288 } 290 } 291 292 293 int[] sp_info = getSpaceInfo(conn, "APP", "LONGCOL", true); 294 295 int total_pages = 296 sp_info[SPACE_INFO_NUM_ALLOC] + sp_info[SPACE_INFO_NUM_FREE]; 297 298 int total_expected_page_max = 30 * work_size; 310 311 if (total_pages > total_expected_page_max) 312 { 313 System.out.println( 314 "Test failed, expected less than " + 315 total_expected_page_max + " pages - count is:\n" + 316 "free pages : " + sp_info[SPACE_INFO_NUM_FREE] + 317 "\nallocated pages: " + sp_info[SPACE_INFO_NUM_ALLOC]); 318 } 319 320 if (verbose) 321 { 322 System.out.println("Space information:"); 323 324 System.out.println("isindex = " + sp_info[SPACE_INFO_IS_INDEX]); 325 System.out.println("num_alloc = " + sp_info[SPACE_INFO_NUM_ALLOC]); 326 System.out.println("num_free = " + sp_info[SPACE_INFO_NUM_FREE]); 327 System.out.println("page_size = " + sp_info[SPACE_INFO_PAGE_SIZE]); 328 System.out.println( 329 "estimspacesaving = " + sp_info[SPACE_INFO_ESTIMSPACESAVING]); 330 } 331 332 for (int iter = work_size + total_work; iter < (total_work * 2); iter++) 335 { 336 ins_stmt.setInt( 1, iter); 338 ins_stmt.setBytes(2, long_byteVal); 339 ins_stmt.executeUpdate(); 340 341 342 del_stmt.setInt(1, iter - work_size - 1); 344 del_stmt.executeUpdate(); 345 346 conn.commit(); 349 350 try 351 { 352 Thread.sleep(100); 353 } 354 catch (Exception ex) 355 { 356 } 358 } 359 360 361 int[] second_sp_info = getSpaceInfo(conn, "APP", "LONGCOL", true); 362 363 int second_total_pages = 364 sp_info[SPACE_INFO_NUM_ALLOC] + sp_info[SPACE_INFO_NUM_FREE]; 365 366 if (total_pages != second_total_pages) 367 { 368 System.out.println( 369 "Test failed, expected constant memory after second run." + 370 "initial total = " + total_pages + 371 "second total = " + second_total_pages); 372 } 373 374 if (verbose) 375 { 376 System.out.println("Space information:"); 377 378 System.out.println("isindex = " + sp_info[SPACE_INFO_IS_INDEX]); 379 System.out.println("num_alloc = " + sp_info[SPACE_INFO_NUM_ALLOC]); 380 System.out.println("num_free = " + sp_info[SPACE_INFO_NUM_FREE]); 381 System.out.println("page_size = " + sp_info[SPACE_INFO_PAGE_SIZE]); 382 System.out.println( 383 "estimspacesaving = " + sp_info[SPACE_INFO_ESTIMSPACESAVING]); 384 } 385 386 endTest( 387 conn, 388 "test2:queue of " + work_size + 389 " rows with blob(" + blob_size + "), total_work = " + 390 total_work); 391 } 392 393 394 public void testList(Connection conn) 395 throws SQLException 396 { 397 test1(conn, 250000, 20); 398 test2(conn, 250000, 5, 500); 399 } 400 401 public static void main(String [] argv) 402 throws Throwable 403 { 404 st_reclaim_longcol test = new st_reclaim_longcol(); 405 406 ij.getPropertyArg(argv); 407 Connection conn = ij.startJBMS(); 408 409 try 410 { 411 test.testList(conn); 412 } 413 catch (SQLException sqle) 414 { 415 org.apache.derby.tools.JDBCDisplayUtil.ShowSQLException( 416 System.out, sqle); 417 sqle.printStackTrace(System.out); 418 } 419 } 420 } 421 | Popular Tags |