提交 eb18c317 authored 作者: Thomas Mueller's avatar Thomas Mueller

Code style.

上级 51de3761
......@@ -21,13 +21,14 @@ import java.util.Random;
*/
public class BenchB implements Bench, Runnable {
private static final int SCALE = 1;
private static final int BRANCHES = 1;
private static final int TELLERS = 10;
private static final int ACCOUNTS = 100000;
private static final int CLIENTS = 10;
// master data
private Database database;
private static final int scale = 1;
private static final int branches = 1;
private static final int tellers = 10;
private static final int accounts = 100000;
private static final int clients = 10;
private int transactionPerClient;
// client data
......@@ -83,7 +84,7 @@ public class BenchB implements Bench, Runnable {
int commitEvery = 1000;
prep = db.prepare(
"INSERT INTO BRANCHES(BID, BBALANCE) VALUES(?, 0)");
for (int i = 0; i < branches * scale; i++) {
for (int i = 0; i < BRANCHES * SCALE; i++) {
prep.setInt(1, i);
db.update(prep, "insertBranches");
if (i % commitEvery == 0) {
......@@ -93,21 +94,21 @@ public class BenchB implements Bench, Runnable {
db.commit();
prep = db.prepare(
"INSERT INTO TELLERS(TID, BID, TBALANCE) VALUES(?, ?, 0)");
for (int i = 0; i < tellers * scale; i++) {
for (int i = 0; i < TELLERS * SCALE; i++) {
prep.setInt(1, i);
prep.setInt(2, i / tellers);
prep.setInt(2, i / TELLERS);
db.update(prep, "insertTellers");
if (i % commitEvery == 0) {
db.commit();
}
}
db.commit();
int len = accounts * scale;
int len = ACCOUNTS * SCALE;
prep = db.prepare(
"INSERT INTO ACCOUNTS(AID, BID, ABALANCE) VALUES(?, ?, 0)");
for (int i = 0; i < len; i++) {
prep.setInt(1, i);
prep.setInt(2, i / accounts);
prep.setInt(2, i / ACCOUNTS);
db.update(prep, "insertAccounts");
if (i % commitEvery == 0) {
db.commit();
......@@ -123,15 +124,15 @@ public class BenchB implements Bench, Runnable {
}
public void run() {
int accountsPerBranch = accounts / branches;
int accountsPerBranch = ACCOUNTS / BRANCHES;
for (int i = 0; i < master.transactionPerClient; i++) {
int branch = random.nextInt(master.branches);
int teller = random.nextInt(master.tellers);
int branch = random.nextInt(BRANCHES);
int teller = random.nextInt(TELLERS);
int account;
if (random.nextInt(100) < 85) {
account = random.nextInt(accountsPerBranch) + branch * accountsPerBranch;
} else {
account = random.nextInt(accounts);
account = random.nextInt(ACCOUNTS);
}
int delta = random.nextInt(1000);
doOne(branch, teller, account, delta);
......@@ -194,8 +195,8 @@ public class BenchB implements Bench, Runnable {
}
private void processTransactions() throws Exception {
Thread[] threads = new Thread[clients];
for (int i = 0; i < clients; i++) {
Thread[] threads = new Thread[CLIENTS];
for (int i = 0; i < CLIENTS; i++) {
threads[i] = new Thread(new BenchB(this, i));
}
for (Thread t : threads) {
......
......@@ -21,8 +21,11 @@ import java.sql.Types;
*/
public class BenchC implements Bench {
private static final String[] TABLES = { "WAREHOUSE", "DISTRICT", "CUSTOMER", "HISTORY", "ORDERS",
private static final int COMMIT_EVERY = 1000;
private static final String[] TABLES = { "WAREHOUSE", "DISTRICT", "CUSTOMER", "HISTORY", "ORDERS",
"NEW_ORDER", "ITEM", "STOCK", "ORDER_LINE", "RESULTS" };
private static final String[] CREATE_SQL = {
"CREATE TABLE WAREHOUSE(\n" +
" W_ID INT NOT NULL PRIMARY KEY,\n" +
......@@ -176,9 +179,6 @@ public class BenchC implements Bench {
private BenchCRandom random;
private String action;
private static final int commitEvery = 1000;
public void init(Database db, int size) throws SQLException {
this.database = db;
......@@ -248,7 +248,7 @@ public class BenchC implements Bench {
prep.setString(5, data);
database.update(prep, "insertItem");
trace(id, items);
if (id % commitEvery == 0) {
if (id % COMMIT_EVERY == 0) {
database.commit();
}
}
......@@ -282,7 +282,7 @@ public class BenchC implements Bench {
database.update(prep, "insertWarehouse");
loadStock(id);
loadDistrict(id);
if (id % commitEvery == 0) {
if (id % COMMIT_EVERY == 0) {
database.commit();
}
}
......@@ -296,7 +296,7 @@ public class BenchC implements Bench {
for (int districtId = 1; districtId <= districtsPerWarehouse; districtId++) {
loadCustomerSub(districtId, id);
trace(i++, max);
if (i % commitEvery == 0) {
if (i % COMMIT_EVERY == 0) {
database.commit();
}
}
......@@ -452,7 +452,7 @@ public class BenchC implements Bench {
prepLine.setBigDecimal(8, amount);
prepLine.setString(9, distInfo);
database.update(prepLine, "insertOrderLine");
if (i++ % commitEvery == 0) {
if (i++ % COMMIT_EVERY == 0) {
database.commit();
}
}
......@@ -502,7 +502,7 @@ public class BenchC implements Bench {
prep.setInt(16, 0);
prep.setInt(17, 0);
database.update(prep, "insertStock");
if (id % commitEvery == 0) {
if (id % COMMIT_EVERY == 0) {
database.commit();
}
trace(id, items);
......
......@@ -30,6 +30,8 @@ import org.h2.util.StringUtils;
*/
class Database {
private static final boolean TRACE = true;
private TestPerformance test;
private int id;
private String name, url, user, password;
......@@ -38,7 +40,6 @@ class Database {
private long startTime;
private Connection conn;
private Statement stat;
private static final boolean trace = true;
private long lastTrace;
private final Random random = new Random(1);
private final ArrayList<Object[]> results = new ArrayList<Object[]>();
......@@ -373,7 +374,7 @@ class Database {
* @param max the maximum value
*/
void trace(String action, int i, int max) {
if (trace) {
if (TRACE) {
long time = System.currentTimeMillis();
if (i == 0 || lastTrace == 0) {
lastTrace = time;
......
......@@ -12,7 +12,6 @@ import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.Writer;
/**
* The class used at runtime to measure the code usage and performance.
......
......@@ -24,7 +24,7 @@ import org.h2.util.JdbcUtils;
*/
public class TestPowerOff extends TestBase {
private static final String dbName = "powerOff";
private static final String DB_NAME = "powerOff";
private String dir, url;
private int maxPowerOffCount;
......@@ -44,10 +44,10 @@ public class TestPowerOff extends TestBase {
}
if (config.big || config.googleAppEngine) {
dir = getBaseDir();
url = dbName;
url = DB_NAME;
} else {
dir = "memFS:";
url = "memFS:/" + dbName;
url = "memFS:/" + DB_NAME;
}
url += ";FILE_LOCK=NO;TRACE_LEVEL_FILE=0";
testLobCrash();
......@@ -56,14 +56,14 @@ public class TestPowerOff extends TestBase {
testShutdown();
testMemoryTables();
testPersistentTables();
deleteDb(dir, dbName);
deleteDb(dir, DB_NAME);
}
private void testLobCrash() throws SQLException {
if (config.networked) {
return;
}
deleteDb(dir, dbName);
deleteDb(dir, DB_NAME);
Connection conn = getConnection(url);
Statement stat = conn.createStatement();
stat.execute("create table test(id identity, data clob)");
......@@ -97,7 +97,7 @@ public class TestPowerOff extends TestBase {
if (config.networked) {
return;
}
deleteDb(dir, dbName);
deleteDb(dir, DB_NAME);
Connection conn = getConnection(url);
Statement stat = conn.createStatement();
for (int i = 0; i < 10; i++) {
......@@ -141,7 +141,7 @@ public class TestPowerOff extends TestBase {
if (config.networked) {
return;
}
deleteDb(dir, dbName);
deleteDb(dir, DB_NAME);
Random random = new Random(1);
SysProperties.runFinalize = false;
int repeat = getSize(1, 20);
......@@ -179,7 +179,7 @@ public class TestPowerOff extends TestBase {
}
private void testShutdown() throws SQLException {
deleteDb(dir, dbName);
deleteDb(dir, DB_NAME);
Connection conn = getConnection(url);
Statement stat = conn.createStatement();
stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))");
......@@ -199,7 +199,7 @@ public class TestPowerOff extends TestBase {
if (config.networked) {
return;
}
deleteDb(dir, dbName);
deleteDb(dir, DB_NAME);
Connection conn = getConnection(url);
Statement stat = conn.createStatement();
......@@ -239,7 +239,7 @@ public class TestPowerOff extends TestBase {
// individual writes, many thousand operations)
return;
}
deleteDb(dir, dbName);
deleteDb(dir, DB_NAME);
// ((JdbcConnection)conn).setPowerOffCount(Integer.MAX_VALUE);
testRun(true);
......@@ -253,7 +253,7 @@ public class TestPowerOff extends TestBase {
private void runTest(int min, int max, boolean withConsistencyCheck) throws SQLException {
for (int i = min; i < max; i++) {
deleteDb(dir, dbName);
deleteDb(dir, DB_NAME);
Database.setInitialPowerOffCount(i);
int expect = testRun(false);
if (withConsistencyCheck) {
......
......@@ -37,8 +37,9 @@ public class TestBatchUpdates extends TestBase {
private static final String COFFEE_UPDATE_SET = "UPDATE TEST SET KEY_ID=?, C_NAME=? WHERE C_NAME=?";
private static final String COFFEE_SELECT_CONTINUED = "SELECT COUNT(*) FROM TEST WHERE C_NAME='Continue-1'";
private static final int coffeeSize = 10;
private static final int coffeeType = 11;
private static final int COFFEE_SIZE = 10;
private static final int COFFEE_TYPE = 11;
private Connection conn;
private Statement stat;
private PreparedStatement prep;
......@@ -173,8 +174,8 @@ public class TestBatchUpdates extends TestBase {
int newType = 0;
prep = conn.prepareStatement("INSERT INTO TEST VALUES(?,?,?,?)");
int newKey = 1;
for (int i = 1; i <= coffeeType && newKey <= coffeeSize; i++) {
for (int j = 1; j <= i && newKey <= coffeeSize; j++) {
for (int i = 1; i <= COFFEE_TYPE && newKey <= COFFEE_SIZE; i++) {
for (int j = 1; j <= i && newKey <= COFFEE_SIZE; j++) {
newName = "COFFEE-" + newKey;
newPrice = newKey + (float) .00;
newType = i;
......
......@@ -42,14 +42,14 @@ public class TestJavaObjectSerializer extends TestBase {
Utils.serializer = new JavaObjectSerializer() {
@Override
public byte[] serialize(Object obj) throws Exception {
assertEquals(100500, ((Integer)obj).intValue());
assertEquals(100500, ((Integer) obj).intValue());
return new byte[]{1,2,3};
return new byte[] { 1, 2, 3 };
}
@Override
public Object deserialize(byte[] bytes) throws Exception {
assertEquals(new byte[]{1,2,3}, bytes);
assertEquals(new byte[] { 1, 2, 3 }, bytes);
return 100500;
}
......@@ -72,7 +72,7 @@ public class TestJavaObjectSerializer extends TestBase {
assertTrue(rs.next());
assertEquals(100500, ((Integer) rs.getObject(1)).intValue());
assertEquals(new byte[]{1,2,3}, rs.getBytes(1));
assertEquals(new byte[] { 1, 2, 3 }, rs.getBytes(1));
deleteDb("javaSerializer");
} finally {
......
......@@ -23,10 +23,10 @@ import org.h2.value.DataType;
*/
public class TestMetaData extends TestBase {
private static final String CATALOG = "METADATA";
private Connection conn;
private DatabaseMetaData meta;
private Statement stat;
private static final String catalog = "METADATA";
/**
* Run just this test.
......@@ -65,7 +65,7 @@ public class TestMetaData extends TestBase {
rs = meta.getCatalogs();
rs.next();
assertEquals(catalog, rs.getString(1));
assertEquals(CATALOG, rs.getString(1));
assertFalse(rs.next());
rs = meta.getSchemas();
......@@ -326,7 +326,7 @@ public class TestMetaData extends TestBase {
}, new int[] {
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.INTEGER, Types.INTEGER, Types.INTEGER,
Types.VARCHAR, Types.SMALLINT, Types.VARCHAR }, null, null);
assertResultSetOrdered(rs, new String[][] { { catalog, Constants.SCHEMA_MAIN, "EXIT", "1", "0", "0", "",
assertResultSetOrdered(rs, new String[][] { { CATALOG, Constants.SCHEMA_MAIN, "EXIT", "1", "0", "0", "",
"" + DatabaseMetaData.procedureNoResult } });
rs = meta.getProcedureColumns(null, null, null, null);
assertResultSetMeta(rs, 20,
......@@ -338,9 +338,9 @@ public class TestMetaData extends TestBase {
Types.VARCHAR, Types.INTEGER, Types.INTEGER, Types.SMALLINT, Types.SMALLINT, Types.SMALLINT, Types.VARCHAR,
Types.VARCHAR, Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.VARCHAR, Types.VARCHAR}, null, null);
assertResultSetOrdered(rs, new String[][] {
{ catalog, Constants.SCHEMA_MAIN, "EXIT", "P1", "" + DatabaseMetaData.procedureColumnIn,
{ CATALOG, Constants.SCHEMA_MAIN, "EXIT", "P1", "" + DatabaseMetaData.procedureColumnIn,
"" + Types.INTEGER, "INTEGER", "10", "10", "0", "10", "" + DatabaseMetaData.procedureNoNulls },
{ catalog, Constants.SCHEMA_MAIN, "PROP", "P1", "" + DatabaseMetaData.procedureColumnIn,
{ CATALOG, Constants.SCHEMA_MAIN, "PROP", "P1", "" + DatabaseMetaData.procedureColumnIn,
"" + Types.VARCHAR, "VARCHAR", "" + Integer.MAX_VALUE, "" + Integer.MAX_VALUE, "0", "10",
"" + DatabaseMetaData.procedureNullable }, });
stat.execute("DROP ALIAS EXIT");
......@@ -383,10 +383,10 @@ public class TestMetaData extends TestBase {
Types.SMALLINT, Types.SMALLINT, Types.SMALLINT, Types.VARCHAR, Types.VARCHAR, Types.SMALLINT }, null,
null);
assertResultSetOrdered(rs, new String[][] {
{ catalog, Constants.SCHEMA_MAIN, "PARENT", "A", catalog, Constants.SCHEMA_MAIN, "CHILD", "PA", "1",
{ CATALOG, Constants.SCHEMA_MAIN, "PARENT", "A", CATALOG, Constants.SCHEMA_MAIN, "CHILD", "PA", "1",
"" + DatabaseMetaData.importedKeyRestrict, "" + DatabaseMetaData.importedKeyRestrict, "AB",
"PRIMARY_KEY_8", "" + DatabaseMetaData.importedKeyNotDeferrable },
{ catalog, Constants.SCHEMA_MAIN, "PARENT", "B", catalog, Constants.SCHEMA_MAIN, "CHILD", "PB", "2",
{ CATALOG, Constants.SCHEMA_MAIN, "PARENT", "B", CATALOG, Constants.SCHEMA_MAIN, "CHILD", "PB", "2",
"" + DatabaseMetaData.importedKeyRestrict, "" + DatabaseMetaData.importedKeyRestrict, "AB",
"PRIMARY_KEY_8", "" + DatabaseMetaData.importedKeyNotDeferrable } });
}
......@@ -680,7 +680,7 @@ public class TestMetaData extends TestBase {
stat.executeUpdate("CREATE TABLE TEST(" + "ID INT PRIMARY KEY," + "TEXT_V VARCHAR(120),"
+ "DEC_V DECIMAL(12,3)," + "DATE_V DATETIME," + "BLOB_V BLOB," + "CLOB_V CLOB" + ")");
rs = meta.getTables(null, Constants.SCHEMA_MAIN, null, new String[] { "TABLE" });
assertResultSetOrdered(rs, new String[][] { { catalog, Constants.SCHEMA_MAIN, "TEST", "TABLE", "" } });
assertResultSetOrdered(rs, new String[][] { { CATALOG, Constants.SCHEMA_MAIN, "TEST", "TABLE", "" } });
trace("getColumns");
rs = meta.getColumns(null, null, "TEST", null);
assertResultSetMeta(rs, 24, new String[] { "TABLE_CAT", "TABLE_SCHEM", "TABLE_NAME", "COLUMN_NAME", "DATA_TYPE",
......@@ -693,23 +693,23 @@ public class TestMetaData extends TestBase {
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.SMALLINT, Types.VARCHAR, Types.VARCHAR }, null, null);
assertResultSetOrdered(rs,
new String[][] {
{ catalog, Constants.SCHEMA_MAIN, "TEST", "ID", "" + Types.INTEGER, "INTEGER", "10", "10", "0",
{ CATALOG, Constants.SCHEMA_MAIN, "TEST", "ID", "" + Types.INTEGER, "INTEGER", "10", "10", "0",
"10", "" + DatabaseMetaData.columnNoNulls, "", null, "" + Types.INTEGER, "0", "10", "1",
"NO" },
{ catalog, Constants.SCHEMA_MAIN, "TEST", "TEXT_V", "" + Types.VARCHAR, "VARCHAR", "120",
{ CATALOG, Constants.SCHEMA_MAIN, "TEST", "TEXT_V", "" + Types.VARCHAR, "VARCHAR", "120",
"120", "0", "10", "" + DatabaseMetaData.columnNullable, "", null, "" + Types.VARCHAR,
"0", "120", "2", "YES" },
{ catalog, Constants.SCHEMA_MAIN, "TEST", "DEC_V", "" + Types.DECIMAL, "DECIMAL", "12", "12",
{ CATALOG, Constants.SCHEMA_MAIN, "TEST", "DEC_V", "" + Types.DECIMAL, "DECIMAL", "12", "12",
"3", "10", "" + DatabaseMetaData.columnNullable, "", null, "" + Types.DECIMAL, "0", "12",
"3", "YES" },
{ catalog, Constants.SCHEMA_MAIN, "TEST", "DATE_V", "" + Types.TIMESTAMP, "TIMESTAMP", "23",
{ CATALOG, Constants.SCHEMA_MAIN, "TEST", "DATE_V", "" + Types.TIMESTAMP, "TIMESTAMP", "23",
"23", "10", "10", "" + DatabaseMetaData.columnNullable, "", null, "" + Types.TIMESTAMP,
"0", "23", "4", "YES" },
{ catalog, Constants.SCHEMA_MAIN, "TEST", "BLOB_V", "" + Types.BLOB, "BLOB",
{ CATALOG, Constants.SCHEMA_MAIN, "TEST", "BLOB_V", "" + Types.BLOB, "BLOB",
"" + Integer.MAX_VALUE, "" + Integer.MAX_VALUE, "0", "10",
"" + DatabaseMetaData.columnNullable, "", null, "" + Types.BLOB, "0",
"" + Integer.MAX_VALUE, "5", "YES" },
{ catalog, Constants.SCHEMA_MAIN, "TEST", "CLOB_V", "" + Types.CLOB, "CLOB",
{ CATALOG, Constants.SCHEMA_MAIN, "TEST", "CLOB_V", "" + Types.CLOB, "CLOB",
"" + Integer.MAX_VALUE, "" + Integer.MAX_VALUE, "0", "10",
"" + DatabaseMetaData.columnNullable, "", null, "" + Types.CLOB, "0",
"" + Integer.MAX_VALUE, "6", "YES" } });
......@@ -727,13 +727,13 @@ public class TestMetaData extends TestBase {
Types.BOOLEAN, Types.VARCHAR, Types.VARCHAR, Types.SMALLINT, Types.SMALLINT, Types.VARCHAR,
Types.VARCHAR, Types.INTEGER, Types.INTEGER, Types.VARCHAR, Types.INTEGER}, null, null);
assertResultSetOrdered(rs, new String[][] {
{ catalog, Constants.SCHEMA_MAIN, "TEST", "FALSE", catalog, "IDX_DATE",
{ CATALOG, Constants.SCHEMA_MAIN, "TEST", "FALSE", CATALOG, "IDX_DATE",
"" + DatabaseMetaData.tableIndexOther, "1", "DATE_V", "A", "0", "0", "" },
{ catalog, Constants.SCHEMA_MAIN, "TEST", "FALSE", catalog, "PRIMARY_KEY_2",
{ CATALOG, Constants.SCHEMA_MAIN, "TEST", "FALSE", CATALOG, "PRIMARY_KEY_2",
"" + DatabaseMetaData.tableIndexOther, "1", "ID", "A", "0", "0", "" },
{ catalog, Constants.SCHEMA_MAIN, "TEST", "TRUE", catalog, "IDX_TEXT_DEC",
{ CATALOG, Constants.SCHEMA_MAIN, "TEST", "TRUE", CATALOG, "IDX_TEXT_DEC",
"" + DatabaseMetaData.tableIndexOther, "1", "TEXT_V", "A", "0", "0", "" },
{ catalog, Constants.SCHEMA_MAIN, "TEST", "TRUE", catalog, "IDX_TEXT_DEC",
{ CATALOG, Constants.SCHEMA_MAIN, "TEST", "TRUE", CATALOG, "IDX_TEXT_DEC",
"" + DatabaseMetaData.tableIndexOther, "2", "DEC_V", "A", "0", "0", "" }, });
stat.executeUpdate("DROP INDEX IDX_TEXT_DEC");
stat.executeUpdate("DROP INDEX IDX_DATE");
......@@ -743,7 +743,7 @@ public class TestMetaData extends TestBase {
"CARDINALITY", "PAGES", "FILTER_CONDITION", "SORT_TYPE" }, new int[] { Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
Types.BOOLEAN, Types.VARCHAR, Types.VARCHAR, Types.SMALLINT, Types.SMALLINT, Types.VARCHAR,
Types.VARCHAR, Types.INTEGER, Types.INTEGER, Types.VARCHAR, Types.INTEGER }, null, null);
assertResultSetOrdered(rs, new String[][] { { catalog, Constants.SCHEMA_MAIN, "TEST", "FALSE", catalog,
assertResultSetOrdered(rs, new String[][] { { CATALOG, Constants.SCHEMA_MAIN, "TEST", "FALSE", CATALOG,
"PRIMARY_KEY_2", "" + DatabaseMetaData.tableIndexOther, "1", "ID", "A", "0", "0", "" } });
trace("getPrimaryKeys");
rs = meta.getPrimaryKeys(null, null, "TEST");
......@@ -751,63 +751,63 @@ public class TestMetaData extends TestBase {
"PK_NAME" }, new int[] { Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.SMALLINT,
Types.VARCHAR }, null, null);
assertResultSetOrdered(rs,
new String[][] { { catalog, Constants.SCHEMA_MAIN, "TEST", "ID", "1", "CONSTRAINT_2" }, });
new String[][] { { CATALOG, Constants.SCHEMA_MAIN, "TEST", "ID", "1", "CONSTRAINT_2" }, });
trace("getTables - using a wildcard");
stat.executeUpdate("CREATE TABLE T_2(B INT,A VARCHAR(6),C INT,PRIMARY KEY(C,A,B))");
stat.executeUpdate("CREATE TABLE TX2(B INT,A VARCHAR(6),C INT,PRIMARY KEY(C,A,B))");
rs = meta.getTables(null, null, "T_2", null);
assertResultSetOrdered(rs, new String[][] { { catalog, Constants.SCHEMA_MAIN, "TX2", "TABLE", "" },
{ catalog, Constants.SCHEMA_MAIN, "T_2", "TABLE", "" } });
assertResultSetOrdered(rs, new String[][] { { CATALOG, Constants.SCHEMA_MAIN, "TX2", "TABLE", "" },
{ CATALOG, Constants.SCHEMA_MAIN, "T_2", "TABLE", "" } });
trace("getTables - using a quoted _ character");
rs = meta.getTables(null, null, "T\\_2", null);
assertResultSetOrdered(rs, new String[][] { { catalog, Constants.SCHEMA_MAIN, "T_2", "TABLE", "" } });
assertResultSetOrdered(rs, new String[][] { { CATALOG, Constants.SCHEMA_MAIN, "T_2", "TABLE", "" } });
trace("getTables - using the % wildcard");
rs = meta.getTables(null, Constants.SCHEMA_MAIN, "%", new String[] { "TABLE" });
assertResultSetOrdered(rs, new String[][] { { catalog, Constants.SCHEMA_MAIN, "TEST", "TABLE", "" },
{ catalog, Constants.SCHEMA_MAIN, "TX2", "TABLE", "" },
{ catalog, Constants.SCHEMA_MAIN, "T_2", "TABLE", "" } });
assertResultSetOrdered(rs, new String[][] { { CATALOG, Constants.SCHEMA_MAIN, "TEST", "TABLE", "" },
{ CATALOG, Constants.SCHEMA_MAIN, "TX2", "TABLE", "" },
{ CATALOG, Constants.SCHEMA_MAIN, "T_2", "TABLE", "" } });
stat.execute("DROP TABLE TEST");
trace("getColumns - using wildcards");
rs = meta.getColumns(null, null, "___", "B%");
assertResultSetOrdered(rs, new String[][] {
{ catalog, Constants.SCHEMA_MAIN, "TX2", "B", "" + Types.INTEGER, "INTEGER", "10"},
{ catalog, Constants.SCHEMA_MAIN, "T_2", "B", "" + Types.INTEGER, "INTEGER", "10"}, });
{ CATALOG, Constants.SCHEMA_MAIN, "TX2", "B", "" + Types.INTEGER, "INTEGER", "10"},
{ CATALOG, Constants.SCHEMA_MAIN, "T_2", "B", "" + Types.INTEGER, "INTEGER", "10"}, });
trace("getColumns - using wildcards");
rs = meta.getColumns(null, null, "_\\__", "%");
assertResultSetOrdered(rs, new String[][] {
{ catalog, Constants.SCHEMA_MAIN, "T_2", "B", "" + Types.INTEGER, "INTEGER", "10"},
{ catalog, Constants.SCHEMA_MAIN, "T_2", "A", "" + Types.VARCHAR, "VARCHAR", "6"},
{ catalog, Constants.SCHEMA_MAIN, "T_2", "C", "" + Types.INTEGER, "INTEGER", "10"}, });
{ CATALOG, Constants.SCHEMA_MAIN, "T_2", "B", "" + Types.INTEGER, "INTEGER", "10"},
{ CATALOG, Constants.SCHEMA_MAIN, "T_2", "A", "" + Types.VARCHAR, "VARCHAR", "6"},
{ CATALOG, Constants.SCHEMA_MAIN, "T_2", "C", "" + Types.INTEGER, "INTEGER", "10"}, });
trace("getIndexInfo");
stat.executeUpdate("CREATE UNIQUE INDEX A_INDEX ON TX2(B,C,A)");
stat.executeUpdate("CREATE INDEX B_INDEX ON TX2(A,B,C)");
rs = meta.getIndexInfo(null, null, "TX2", false, false);
assertResultSetOrdered(rs, new String[][] {
{ catalog, Constants.SCHEMA_MAIN, "TX2", "FALSE", catalog, "A_INDEX",
{ CATALOG, Constants.SCHEMA_MAIN, "TX2", "FALSE", CATALOG, "A_INDEX",
"" + DatabaseMetaData.tableIndexOther, "1", "B", "A" },
{ catalog, Constants.SCHEMA_MAIN, "TX2", "FALSE", catalog, "A_INDEX",
{ CATALOG, Constants.SCHEMA_MAIN, "TX2", "FALSE", CATALOG, "A_INDEX",
"" + DatabaseMetaData.tableIndexOther, "2", "C", "A"},
{ catalog, Constants.SCHEMA_MAIN, "TX2", "FALSE", catalog, "A_INDEX",
{ CATALOG, Constants.SCHEMA_MAIN, "TX2", "FALSE", CATALOG, "A_INDEX",
"" + DatabaseMetaData.tableIndexOther, "3", "A", "A"},
{ catalog, Constants.SCHEMA_MAIN, "TX2", "FALSE", catalog, "PRIMARY_KEY_14",
{ CATALOG, Constants.SCHEMA_MAIN, "TX2", "FALSE", CATALOG, "PRIMARY_KEY_14",
"" + DatabaseMetaData.tableIndexOther, "1", "C", "A"},
{ catalog, Constants.SCHEMA_MAIN, "TX2", "FALSE", catalog, "PRIMARY_KEY_14",
{ CATALOG, Constants.SCHEMA_MAIN, "TX2", "FALSE", CATALOG, "PRIMARY_KEY_14",
"" + DatabaseMetaData.tableIndexOther, "2", "A", "A" },
{ catalog, Constants.SCHEMA_MAIN, "TX2", "FALSE", catalog, "PRIMARY_KEY_14",
{ CATALOG, Constants.SCHEMA_MAIN, "TX2", "FALSE", CATALOG, "PRIMARY_KEY_14",
"" + DatabaseMetaData.tableIndexOther, "3", "B", "A"},
{ catalog, Constants.SCHEMA_MAIN, "TX2", "TRUE", catalog, "B_INDEX",
{ CATALOG, Constants.SCHEMA_MAIN, "TX2", "TRUE", CATALOG, "B_INDEX",
"" + DatabaseMetaData.tableIndexOther, "1", "A", "A" },
{ catalog, Constants.SCHEMA_MAIN, "TX2", "TRUE", catalog, "B_INDEX",
{ CATALOG, Constants.SCHEMA_MAIN, "TX2", "TRUE", CATALOG, "B_INDEX",
"" + DatabaseMetaData.tableIndexOther, "2", "B", "A" },
{ catalog, Constants.SCHEMA_MAIN, "TX2", "TRUE", catalog, "B_INDEX",
{ CATALOG, Constants.SCHEMA_MAIN, "TX2", "TRUE", CATALOG, "B_INDEX",
"" + DatabaseMetaData.tableIndexOther, "3", "C", "A" }, });
trace("getPrimaryKeys");
rs = meta.getPrimaryKeys(null, null, "T_2");
assertResultSetOrdered(rs, new String[][] {
{ catalog, Constants.SCHEMA_MAIN, "T_2", "A", "2", "CONSTRAINT_1" },
{ catalog, Constants.SCHEMA_MAIN, "T_2", "B", "3", "CONSTRAINT_1" },
{ catalog, Constants.SCHEMA_MAIN, "T_2", "C", "1", "CONSTRAINT_1" }, });
{ CATALOG, Constants.SCHEMA_MAIN, "T_2", "A", "2", "CONSTRAINT_1" },
{ CATALOG, Constants.SCHEMA_MAIN, "T_2", "B", "3", "CONSTRAINT_1" },
{ CATALOG, Constants.SCHEMA_MAIN, "T_2", "C", "1", "CONSTRAINT_1" }, });
stat.executeUpdate("DROP TABLE TX2");
stat.executeUpdate("DROP TABLE T_2");
stat.executeUpdate("CREATE TABLE PARENT(ID INT PRIMARY KEY)");
......@@ -874,7 +874,7 @@ public class TestMetaData extends TestBase {
rs = meta.getCatalogs();
assertResultSetMeta(rs, 1, new String[] { "TABLE_CAT" }, new int[] { Types.VARCHAR }, null, null);
assertResultSetOrdered(rs, new String[][] { { catalog } });
assertResultSetOrdered(rs, new String[][] { { CATALOG } });
rs = meta.getTableTypes();
assertResultSetMeta(rs, 1, new String[] { "TABLE_TYPE" }, new int[] { Types.VARCHAR }, null, null);
......
......@@ -7,6 +7,9 @@
package org.h2.test.store;
import java.util.ArrayList;
import java.util.Iterator;
import org.h2.dev.store.btree.Cursor;
import org.h2.dev.store.btree.CursorPos;
import org.h2.dev.store.btree.DataType;
import org.h2.dev.store.btree.MVMap;
import org.h2.dev.store.btree.MVStore;
......@@ -21,7 +24,7 @@ import org.h2.util.New;
*/
public class MVRTreeMap<K, V> extends MVMap<K, V> {
private final SpatialType keyType;
final SpatialType keyType;
private boolean quadraticSplit;
MVRTreeMap(MVStore store, int id, String name, DataType keyType,
......@@ -36,6 +39,39 @@ public class MVRTreeMap<K, V> extends MVMap<K, V> {
return (V) get(root, key);
}
/**
* Iterate over all keys that have an intersection with the given rectangle.
*
* @param x the rectangle
* @return the iterator
*/
public Iterator<K> findIntersectingKeys(K x) {
checkOpen();
return new RTreeCursor<K>(this, root, x) {
protected boolean check(K key, K test) {
System.out.println("key " + key + " contains " + test + " = " + keyType.contains(key, test));
return keyType.contains(key, test);
}
};
}
/**
* Iterate over all keys that are fully contained within the given rectangle.
*
* @param x the rectangle
* @return the iterator
*/
public Iterator<K> findContainedKeys(K x) {
checkOpen();
return new RTreeCursor<K>(this, root, x) {
protected boolean check(K key, K test) {
System.out.println("key " + key + " isInside " + test + " = " + keyType.contains(test, key));
return keyType.isInside(test, key);
}
};
}
private boolean contains(Page p, int index, Object key) {
return keyType.contains(p.getKey(index), key);
}
......@@ -411,4 +447,88 @@ public class MVRTreeMap<K, V> extends MVMap<K, V> {
return p.getChildPageCount() - 1;
}
/**
* A cursor to iterate over a subset of the keys.
*
* @param <K> the key class
*/
static class RTreeCursor<K> extends Cursor<K> {
protected RTreeCursor(MVRTreeMap<K, ?> map, Page root, K from) {
super(map, root, from);
}
public void skip(long n) {
if (!hasNext()) {
return;
}
while (n-- > 0) {
fetchNext();
}
}
/**
* Check a given key.
*
* @param key the stored key
* @param test the user-supplied test key
* @return true if there is a match
*/
protected boolean check(K key, K test) {
return true;
}
@SuppressWarnings("unchecked")
protected void min(Page p, K x) {
while (true) {
if (p.isLeaf()) {
pos = new CursorPos(p, 0, pos);
return;
// for (int i = 0; i < p.getKeyCount(); i++) {
// if (check((K) p.getKey(i), x)) {
// pos = new CursorPos(p, i, pos);
// return;
// }
// }
// break;
}
boolean found = false;
for (int i = 0; i < p.getKeyCount(); i++) {
if (check((K) p.getKey(i), x)) {
pos = new CursorPos(p, i + 1, pos);
p = p.getChildPage(i);
found = true;
break;
}
}
if (!found) {
break;
}
}
fetchNext();
}
@SuppressWarnings("unchecked")
protected void fetchNext() {
while (pos != null) {
while (pos.index < pos.page.getKeyCount()) {
K k = (K) pos.page.getKey(pos.index++);
if (check(k, from)) {
current = k;
return;
}
}
pos = pos.parent;
if (pos == null) {
break;
}
MVRTreeMap<K, ?> m = (MVRTreeMap<K, ?>) map;
if (pos.index < m.getChildPageCount(pos.page)) {
min(pos.page.getChildPage(pos.index++), from);
}
}
current = null;
}
}
}
......@@ -7,10 +7,10 @@ package org.h2.test.store;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
......@@ -44,7 +44,9 @@ public class TestConcurrent extends TestMVStore {
}
private void testConcurrentOnlineBackup() throws Exception {
String fileName = getBaseDir() + "/onlineBackup.h3";
// because absolute and relative reads are mixed, this currently
// only works when using FileChannel directly
String fileName = "nio:" + getBaseDir() + "/onlineBackup.h3";
String fileNameRestore = getBaseDir() + "/onlineRestore.h3";
final MVStore s = openStore(fileName);
final MVMap<Integer, byte[]> map = s.openMap("test");
......@@ -59,7 +61,7 @@ public class TestConcurrent extends TestMVStore {
s.store();
map.clear();
s.store();
long len = new File(s.getFileName()).length();
long len = s.getFile().size();
if (len > 1024 * 100) {
// slow down writing
Thread.sleep(10);
......@@ -70,8 +72,8 @@ public class TestConcurrent extends TestMVStore {
t.execute();
// the wrong way to back up
try {
for (int i = 0; i < 10; i++) {
byte[] buff = readFileSlowly(fileName);
for (int i = 0; i < 100; i++) {
byte[] buff = readFileSlowly(s.getFile());
FileOutputStream out = new FileOutputStream(fileNameRestore);
out.write(buff);
MVStore s2 = openStore(fileNameRestore);
......@@ -92,7 +94,7 @@ public class TestConcurrent extends TestMVStore {
for (int i = 0; i < 10; i++) {
// System.out.println("test " + i);
s.setReuseSpace(false);
byte[] buff = readFileSlowly(fileName);
byte[] buff = readFileSlowly(s.getFile());
s.setReuseSpace(true);
FileOutputStream out = new FileOutputStream(fileNameRestore);
out.write(buff);
......@@ -109,9 +111,9 @@ public class TestConcurrent extends TestMVStore {
s.close();
}
private static byte[] readFileSlowly(String fileName) throws Exception {
InputStream in = new BufferedInputStream(new FileInputStream(
fileName));
private static byte[] readFileSlowly(FileChannel file) throws Exception {
file.position(0);
InputStream in = new BufferedInputStream(Channels.newInputStream(file));
ByteArrayOutputStream buff = new ByteArrayOutputStream();
for (int j = 0;; j++) {
int x = in.read();
......@@ -123,7 +125,8 @@ public class TestConcurrent extends TestMVStore {
Thread.sleep(1);
}
}
in.close();
// in.close() could close the stream
// in.close();
return buff.toByteArray();
}
......
......@@ -15,6 +15,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
......@@ -40,7 +41,7 @@ public class TestMVRTree extends TestMVStore {
public void test() {
testMany();
testTree();
testSimple();
testRandom();
testCustomMapType();
}
......@@ -108,25 +109,26 @@ public class TestMVRTree extends TestMVStore {
// System.out.println("remove: " + (System.currentTimeMillis() - t));
}
private void testTree() {
private void testSimple() {
String fileName = getBaseDir() + "/testTree.h3";
FileUtils.delete(fileName);
MVStore s;
s = openStore(fileName);
MVRTreeMap<SpatialKey, String> r = s.openMap("data", "r", "s2", "");
add(r, "Bern", 46.57, 7.27, 124381);
add(r, "Basel", 47.34, 7.36, 170903);
add(r, "Zurich", 47.22, 8.33, 376008);
add(r, "Lucerne", 47.03, 8.18, 77491);
add(r, "Geneva", 46.12, 6.09, 191803);
add(r, "Lausanne", 46.31, 6.38, 127821);
add(r, "Winterthur", 47.30, 8.45, 102966);
add(r, "St. Gallen", 47.25, 9.22, 73500);
add(r, "Biel/Bienne", 47.08, 7.15, 51203);
add(r, "Lugano", 46.00, 8.57, 54667);
add(r, "Thun", 46.46, 7.38, 42623);
add(r, "Bellinzona", 46.12, 9.01, 17373);
add(r, "Chur", 46.51, 9.32, 33756);
add(r, "Bern", key(0, 46.57, 7.27, 124381));
add(r, "Basel", key(1, 47.34, 7.36, 170903));
add(r, "Zurich", key(2, 47.22, 8.33, 376008));
add(r, "Lucerne", key(3, 47.03, 8.18, 77491));
add(r, "Geneva", key(4, 46.12, 6.09, 191803));
add(r, "Lausanne", key(5, 46.31, 6.38, 127821));
add(r, "Winterthur", key(6, 47.30, 8.45, 102966));
add(r, "St. Gallen", key(7, 47.25, 9.22, 73500));
add(r, "Biel/Bienne", key(8, 47.08, 7.15, 51203));
add(r, "Lugano", key(9, 46.00, 8.57, 54667));
add(r, "Thun", key(10, 46.46, 7.38, 42623));
add(r, "Bellinzona", key(11, 46.12, 9.01, 17373));
add(r, "Chur", key(12, 46.51, 9.32, 33756));
// render(r, getBaseDir() + "/test.png");
ArrayList<String> list = New.arrayList();
for (SpatialKey x : r.keySet()) {
list.add(r.get(x));
......@@ -135,17 +137,46 @@ public class TestMVRTree extends TestMVStore {
assertEquals("[Basel, Bellinzona, Bern, Biel/Bienne, Chur, Geneva, " +
"Lausanne, Lucerne, Lugano, St. Gallen, Thun, Winterthur, Zurich]",
list.toString());
// render(r, getBaseDir() + "/test.png");
SpatialKey k;
// intersection
list.clear();
k = key(0, 47.34, 7.36, 0);
for (Iterator<SpatialKey> it = r.findIntersectingKeys(k); it.hasNext();) {
list.add(r.get(it.next()));
}
Collections.sort(list);
assertEquals("[Basel]",
list.toString());
// contains
list.clear();
k = key(0, 47.34, 7.36, 0);
for (Iterator<SpatialKey> it = r.findContainedKeys(k); it.hasNext();) {
list.add(r.get(it.next()));
}
assertEquals(0, list.size());
k = key(0, 47.34, 7.36, 170903);
Collections.sort(list);
assertEquals("[Bern]",
list.toString());
s.close();
}
private static void add(MVRTreeMap<SpatialKey, String> r, String name, double y, double x, int population) {
int id = r.size();
private static void add(MVRTreeMap<SpatialKey, String> r, String name, SpatialKey k) {
r.put(k, name);
}
private static SpatialKey key(int id, double y, double x, int population) {
float a = (float) ((int) x + (x - (int) x) * 5 / 3);
float b = 50 - (float) ((int) y + (y - (int) y) * 5 / 3);
float s = (float) Math.sqrt(population / 10000000.);
SpatialKey k = new SpatialKey(id, a - s, a + s, b - s, b + s);
r.put(k, name);
return k;
}
private static void render(MVRTreeMap<SpatialKey, String> r, String fileName) {
......
......@@ -25,8 +25,9 @@ public class TestKill extends TestBase {
private static final String DIR = TestBase.getTestDir("kill");
private static final int ACCOUNTS = 10;
private Connection conn;
private static final int accounts = 10;
private final Random random = new Random(1);
/**
......@@ -51,7 +52,7 @@ public class TestKill extends TestBase {
"java", selfDestruct,
"-cp", getClassPath(),
"org.h2.test.synth.TestKillProcess", url, user,
password, getBaseDir(), "" + accounts };
password, getBaseDir(), "" + ACCOUNTS };
for (int i = 0;; i++) {
printTime("TestKill " + i);
......@@ -102,13 +103,13 @@ public class TestKill extends TestBase {
conn.createStatement().execute("DROP TABLE TEST_B");
createTables();
PreparedStatement prep = conn.prepareStatement("INSERT INTO ACCOUNT VALUES(?, 0)");
for (int i = 0; i < accounts; i++) {
for (int i = 0; i < ACCOUNTS; i++) {
prep.setInt(1, i);
prep.execute();
}
PreparedStatement p1 = conn.prepareStatement("INSERT INTO TEST_A VALUES(?, '')");
PreparedStatement p2 = conn.prepareStatement("INSERT INTO TEST_B VALUES(?, '')");
for (int i = 0; i < accounts; i++) {
for (int i = 0; i < ACCOUNTS; i++) {
p1.setInt(1, i);
p2.setInt(1, i);
p1.execute();
......@@ -132,7 +133,7 @@ public class TestKill extends TestBase {
}
PreparedStatement p1 = conn.prepareStatement("SELECT * FROM TEST_A WHERE ID=?");
PreparedStatement p2 = conn.prepareStatement("SELECT * FROM TEST_B WHERE ID=?");
for (int i = 0; i < accounts; i++) {
for (int i = 0; i < ACCOUNTS; i++) {
p1.setInt(1, i);
p2.setInt(1, i);
ResultSet r1 = p1.executeQuery();
......
......@@ -24,11 +24,12 @@ import org.h2.util.New;
*/
public class TestPowerOffFs2 extends TestBase {
private static final String USER = "sa";
private static final String PASSWORD = "sa";
private FilePathDebug fs;
private String url;
private static final String user = "sa";
private static final String password = "sa";
private final ArrayList<Connection> connections = New.arrayList();
private final ArrayList<String> tables = New.arrayList();
......@@ -165,7 +166,7 @@ public class TestPowerOffFs2 extends TestBase {
}
private Connection openConnection() throws SQLException {
Connection conn = DriverManager.getConnection(url, user, password);
Connection conn = DriverManager.getConnection(url, USER, PASSWORD);
connections.add(conn);
return conn;
}
......
......@@ -21,7 +21,7 @@ public class TestMultiNews extends TestMultiThread {
private static final String PREFIX_URL =
"http://feeds.wizbangblog.com/WizbangFullFeed?m=";
private static final int len = 10000;
private static final int LEN = 10000;
private Connection conn;
TestMultiNews(TestMulti base) throws SQLException {
......@@ -49,7 +49,7 @@ public class TestMultiNews extends TestMultiThread {
} else {
prep = conn.prepareStatement("SELECT * FROM NEWS WHERE VALUE = ?");
}
prep.setString(1, PREFIX_URL + random.nextInt(len));
prep.setString(1, PREFIX_URL + random.nextInt(LEN));
ResultSet rs = prep.executeQuery();
if (!rs.next()) {
throw new SQLException("expected one row, got none");
......@@ -60,7 +60,7 @@ public class TestMultiNews extends TestMultiThread {
} else {
PreparedStatement prep = conn.prepareStatement("UPDATE NEWS SET STATE = ? WHERE FID = ?");
prep.setInt(1, random.nextInt(100));
prep.setInt(2, random.nextInt(len));
prep.setInt(2, random.nextInt(LEN));
int count = prep.executeUpdate();
if (count != 1) {
throw new SQLException("expected one row, got " + count);
......@@ -93,7 +93,7 @@ public class TestMultiNews extends TestMultiThread {
PreparedStatement prep = c.prepareStatement("INSERT INTO NEWS (FID, COMMENTS, LINK, STATE, VALUE) VALUES "
+ "(?, ?, ?, ?, ?) ");
PreparedStatement prep2 = c.prepareStatement("INSERT INTO TEST (NAME) VALUES (?)");
for (int i = 0; i < len; i++) {
for (int i = 0; i < LEN; i++) {
int x = random.nextInt(10) * 128;
StringBuilder buff = new StringBuilder();
while (buff.length() < x) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论