1 5 package org.h2.test.synth; 6 7 import java.sql.Connection ; 8 import java.sql.DriverManager ; 9 import java.sql.PreparedStatement ; 10 import java.sql.ResultSet ; 11 import java.sql.SQLException ; 12 import java.sql.Statement ; 13 import java.util.Random ; 14 15 import org.h2.test.TestBase; 16 import org.h2.tools.DeleteDbFiles; 17 18 public class TestBtreeIndex extends TestBase { 19 20 public void test() throws Exception { 21 Random random = new Random (); 22 while(true) { 23 int seed = random.nextInt(); 24 testCase(seed); 25 } 26 } 27 28 public void testCase(int seed) throws Exception { 29 BASE_DIR = "dataIndex"; 30 testOne(seed); 31 BASE_DIR = "data"; 32 } 33 34 private void testOne(int seed) throws Exception { 35 Class.forName("org.h2.Driver"); 36 printTime("testIndex " + seed); 37 Random random = new Random (seed); 38 int distinct, prefixLength; 39 if(random.nextBoolean()) { 40 distinct=random.nextInt(8000)+1; 41 prefixLength=random.nextInt(8000)+1; 42 } else if(random.nextBoolean()) { 43 distinct=random.nextInt(16000)+1; 44 prefixLength=random.nextInt(100)+1; 45 } else { 46 distinct=random.nextInt(10)+1; 47 prefixLength=random.nextInt(10)+1; 48 } 49 boolean delete = random.nextBoolean(); 50 StringBuffer buff = new StringBuffer (); 51 for(int j=0; j<prefixLength; j++) { 52 buff.append("x"); 53 } 54 String prefix = buff.toString(); 55 DeleteDbFiles.execute(BASE_DIR, null, true); 56 Connection conn = DriverManager.getConnection("jdbc:h2:" +BASE_DIR + "/index", "sa", "sa"); 57 Statement stat = conn.createStatement(); 58 stat.execute("CREATE TABLE a(text VARCHAR PRIMARY KEY)"); 59 PreparedStatement prepInsert = conn.prepareStatement("INSERT INTO a VALUES(?)"); 60 PreparedStatement prepDelete = conn.prepareStatement("DELETE FROM a WHERE text=?"); 61 PreparedStatement prepDeleteAllButOne = conn.prepareStatement("DELETE FROM a WHERE text <> ?"); 62 int count=0; 63 for(int i=0; i<1000; i++) { 64 int y = random.nextInt(distinct); 65 try { 66 prepInsert.setString(1, prefix + y); 67 prepInsert.executeUpdate(); 68 count ++; 69 } catch(SQLException e) { 70 if(e.getSQLState().equals("23001")) { 71 } else { 73 TestBase.logError("error", e); 74 break; 75 } 76 } 77 if(delete && random.nextInt(10) == 1) { 78 if(random.nextInt(4) == 1) { 79 try { 80 prepDeleteAllButOne.setString(1, prefix + y); 81 int deleted = prepDeleteAllButOne.executeUpdate(); 82 if(deleted < count-1) { 83 System.out.println("ERROR deleted:"+deleted); 84 System.out.println("new TestBtreeIndex()."); 85 } 86 count -= deleted; 87 } catch(SQLException e) { 88 TestBase.logError("error", e); 89 break; 90 } 91 } else { 92 try { 93 prepDelete.setString(1, prefix + y); 94 int deleted = prepDelete.executeUpdate(); 95 if(deleted > 1) { 96 System.out.println("ERROR deleted:"+deleted); 97 System.out.println("new TestIndex()."); 98 } 99 count -= deleted; 100 } catch(SQLException e) { 101 TestBase.logError("error", e); 102 break; 103 } 104 } 105 } 106 } 107 ResultSet rs = conn.createStatement().executeQuery("SELECT text FROM a ORDER BY text"); 108 int testCount = 0; 109 while(rs.next()) { 110 testCount++; 111 } 112 if(testCount != count) { 113 System.out.println("ERROR count:"+count+" testCount:"+testCount); 114 System.out.println("new TestIndex()."); 115 } 116 rs = conn.createStatement().executeQuery("SELECT text, count(*) FROM a GROUP BY text HAVING COUNT(*)>1"); 117 if(rs.next()) { 118 System.out.println("ERROR"); 119 System.out.println("new TestIndex()."); 120 } 121 conn.close(); 122 } 123 124 } 125 | Popular Tags |