001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (license2)
004: * Initial Developer: H2 Group
005: */
006: package org.h2.test.synth;
007:
008: import java.sql.Connection;
009: import java.sql.DriverManager;
010: import java.sql.PreparedStatement;
011: import java.sql.ResultSet;
012: import java.sql.SQLException;
013: import java.sql.Statement;
014: import java.util.Random;
015:
016: import org.h2.test.TestBase;
017: import org.h2.tools.DeleteDbFiles;
018:
019: /**
020: * A b-tree index test.
021: */
022: public class TestBtreeIndex extends TestBase {
023:
024: public void test() throws Exception {
025: Random random = new Random();
026: while (true) {
027: int seed = random.nextInt();
028: testCase(seed);
029: }
030: }
031:
032: public void testCase(int seed) throws Exception {
033: String old = baseDir;
034: baseDir = TestBase.getTestDir("index");
035: testOne(seed);
036: baseDir = old;
037: }
038:
039: private void testOne(int seed) throws Exception {
040: Class.forName("org.h2.Driver");
041: printTime("testIndex " + seed);
042: Random random = new Random(seed);
043: int distinct, prefixLength;
044: if (random.nextBoolean()) {
045: distinct = random.nextInt(8000) + 1;
046: prefixLength = random.nextInt(8000) + 1;
047: } else if (random.nextBoolean()) {
048: distinct = random.nextInt(16000) + 1;
049: prefixLength = random.nextInt(100) + 1;
050: } else {
051: distinct = random.nextInt(10) + 1;
052: prefixLength = random.nextInt(10) + 1;
053: }
054: boolean delete = random.nextBoolean();
055: StringBuffer buff = new StringBuffer();
056: for (int j = 0; j < prefixLength; j++) {
057: buff.append("x");
058: }
059: String prefix = buff.toString();
060: DeleteDbFiles.execute(baseDir, null, true);
061: Connection conn = DriverManager.getConnection("jdbc:h2:"
062: + baseDir + "/index", "sa", "sa");
063: Statement stat = conn.createStatement();
064: stat.execute("CREATE TABLE a(text VARCHAR PRIMARY KEY)");
065: PreparedStatement prepInsert = conn
066: .prepareStatement("INSERT INTO a VALUES(?)");
067: PreparedStatement prepDelete = conn
068: .prepareStatement("DELETE FROM a WHERE text=?");
069: PreparedStatement prepDeleteAllButOne = conn
070: .prepareStatement("DELETE FROM a WHERE text <> ?");
071: int count = 0;
072: for (int i = 0; i < 1000; i++) {
073: int y = random.nextInt(distinct);
074: try {
075: prepInsert.setString(1, prefix + y);
076: prepInsert.executeUpdate();
077: count++;
078: } catch (SQLException e) {
079: if (e.getSQLState().equals("23001")) {
080: // ignore
081: } else {
082: TestBase.logError("error", e);
083: break;
084: }
085: }
086: if (delete && random.nextInt(10) == 1) {
087: if (random.nextInt(4) == 1) {
088: try {
089: prepDeleteAllButOne.setString(1, prefix + y);
090: int deleted = prepDeleteAllButOne
091: .executeUpdate();
092: if (deleted < count - 1) {
093: System.out.println("ERROR deleted:"
094: + deleted);
095: System.out.println("new TestBtreeIndex().");
096: }
097: count -= deleted;
098: } catch (SQLException e) {
099: TestBase.logError("error", e);
100: break;
101: }
102: } else {
103: try {
104: prepDelete.setString(1, prefix + y);
105: int deleted = prepDelete.executeUpdate();
106: if (deleted > 1) {
107: System.out.println("ERROR deleted:"
108: + deleted);
109: System.out.println("new TestIndex().");
110: }
111: count -= deleted;
112: } catch (SQLException e) {
113: TestBase.logError("error", e);
114: break;
115: }
116: }
117: }
118: }
119: ResultSet rs = conn.createStatement().executeQuery(
120: "SELECT text FROM a ORDER BY text");
121: int testCount = 0;
122: while (rs.next()) {
123: testCount++;
124: }
125: if (testCount != count) {
126: System.out.println("ERROR count:" + count + " testCount:"
127: + testCount);
128: System.out.println("new TestIndex().");
129: }
130: rs = conn
131: .createStatement()
132: .executeQuery(
133: "SELECT text, count(*) FROM a GROUP BY text HAVING COUNT(*)>1");
134: if (rs.next()) {
135: System.out.println("ERROR");
136: System.out.println("new TestIndex().");
137: }
138: conn.close();
139: }
140:
141: }
|