buff.append(buff.length());
}
}
String prefix = buff.toString().substring(0, prefixLength);
DeleteDbFiles.execute(getBaseDir() + "/index", null, true);
Connection conn = getConnection("index");
try {
Statement stat = conn.createStatement();
stat.execute("CREATE TABLE a(text VARCHAR PRIMARY KEY)");
PreparedStatement prepInsert = conn.prepareStatement("INSERT INTO a VALUES(?)");
PreparedStatement prepDelete = conn.prepareStatement("DELETE FROM a WHERE text=?");
PreparedStatement prepDeleteAllButOne = conn.prepareStatement("DELETE FROM a WHERE text <> ?");
int count = 0;
for (int i = 0; i < 1000; i++) {
int y = random.nextInt(distinct);
try {
prepInsert.setString(1, prefix + y);
prepInsert.executeUpdate();
count++;
} catch (SQLException e) {
if (e.getSQLState().equals("23505")) {
// ignore
} else {
TestBase.logError("error", e);
break;
}
}
if (delete && random.nextInt(10) == 1) {
if (random.nextInt(4) == 1) {
try {
prepDeleteAllButOne.setString(1, prefix + y);
int deleted = prepDeleteAllButOne.executeUpdate();
if (deleted < count - 1) {
printError(seed, "deleted:" + deleted + " i:" + i);
}
count -= deleted;
} catch (SQLException e) {
TestBase.logError("error", e);
break;
}
} else {
try {
prepDelete.setString(1, prefix + y);
int deleted = prepDelete.executeUpdate();
if (deleted > 1) {
printError(seed, "deleted:" + deleted + " i:" + i);
}
count -= deleted;
} catch (SQLException e) {
TestBase.logError("error", e);
break;
}
}
}
}
int testCount;
testCount = 0;
ResultSet rs = stat.executeQuery("SELECT text FROM a ORDER BY text");
ResultSet rs2 = conn.createStatement().executeQuery("SELECT text FROM a ORDER BY 'x' || text");
//System.out.println("-----------");
//while(rs.next()) {
// System.out.println(rs.getString(1));
//}
//System.out.println("-----------");
//while(rs2.next()) {
// System.out.println(rs2.getString(1));
//}
//if (true) throw new AssertionError("stop");
//
testCount = 0;
while (rs.next() && rs2.next()) {
if (!rs.getString(1).equals(rs2.getString(1))) {
assertEquals("" + testCount, rs.getString(1), rs.getString(2));
}
testCount++;
}
assertFalse(rs.next());
assertFalse(rs2.next());
if (testCount != count) {
printError(seed, "count:" + count + " testCount:" + testCount);
}
rs = stat.executeQuery("SELECT text, count(*) FROM a GROUP BY text HAVING COUNT(*)>1");
if (rs.next()) {
printError(seed, "testCount:" + testCount + " " + rs.getString(1));
}
} finally {
conn.close();
}
deleteDb("index");
}