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

Code style.

上级 51de3761
...@@ -21,13 +21,14 @@ import java.util.Random; ...@@ -21,13 +21,14 @@ import java.util.Random;
*/ */
public class BenchB implements Bench, Runnable { 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 // master data
private Database database; 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; private int transactionPerClient;
// client data // client data
...@@ -83,7 +84,7 @@ public class BenchB implements Bench, Runnable { ...@@ -83,7 +84,7 @@ public class BenchB implements Bench, Runnable {
int commitEvery = 1000; int commitEvery = 1000;
prep = db.prepare( prep = db.prepare(
"INSERT INTO BRANCHES(BID, BBALANCE) VALUES(?, 0)"); "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); prep.setInt(1, i);
db.update(prep, "insertBranches"); db.update(prep, "insertBranches");
if (i % commitEvery == 0) { if (i % commitEvery == 0) {
...@@ -93,21 +94,21 @@ public class BenchB implements Bench, Runnable { ...@@ -93,21 +94,21 @@ public class BenchB implements Bench, Runnable {
db.commit(); db.commit();
prep = db.prepare( prep = db.prepare(
"INSERT INTO TELLERS(TID, BID, TBALANCE) VALUES(?, ?, 0)"); "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(1, i);
prep.setInt(2, i / tellers); prep.setInt(2, i / TELLERS);
db.update(prep, "insertTellers"); db.update(prep, "insertTellers");
if (i % commitEvery == 0) { if (i % commitEvery == 0) {
db.commit(); db.commit();
} }
} }
db.commit(); db.commit();
int len = accounts * scale; int len = ACCOUNTS * SCALE;
prep = db.prepare( prep = db.prepare(
"INSERT INTO ACCOUNTS(AID, BID, ABALANCE) VALUES(?, ?, 0)"); "INSERT INTO ACCOUNTS(AID, BID, ABALANCE) VALUES(?, ?, 0)");
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
prep.setInt(1, i); prep.setInt(1, i);
prep.setInt(2, i / accounts); prep.setInt(2, i / ACCOUNTS);
db.update(prep, "insertAccounts"); db.update(prep, "insertAccounts");
if (i % commitEvery == 0) { if (i % commitEvery == 0) {
db.commit(); db.commit();
...@@ -123,15 +124,15 @@ public class BenchB implements Bench, Runnable { ...@@ -123,15 +124,15 @@ public class BenchB implements Bench, Runnable {
} }
public void run() { public void run() {
int accountsPerBranch = accounts / branches; int accountsPerBranch = ACCOUNTS / BRANCHES;
for (int i = 0; i < master.transactionPerClient; i++) { for (int i = 0; i < master.transactionPerClient; i++) {
int branch = random.nextInt(master.branches); int branch = random.nextInt(BRANCHES);
int teller = random.nextInt(master.tellers); int teller = random.nextInt(TELLERS);
int account; int account;
if (random.nextInt(100) < 85) { if (random.nextInt(100) < 85) {
account = random.nextInt(accountsPerBranch) + branch * accountsPerBranch; account = random.nextInt(accountsPerBranch) + branch * accountsPerBranch;
} else { } else {
account = random.nextInt(accounts); account = random.nextInt(ACCOUNTS);
} }
int delta = random.nextInt(1000); int delta = random.nextInt(1000);
doOne(branch, teller, account, delta); doOne(branch, teller, account, delta);
...@@ -194,8 +195,8 @@ public class BenchB implements Bench, Runnable { ...@@ -194,8 +195,8 @@ public class BenchB implements Bench, Runnable {
} }
private void processTransactions() throws Exception { private void processTransactions() throws Exception {
Thread[] threads = new Thread[clients]; Thread[] threads = new Thread[CLIENTS];
for (int i = 0; i < clients; i++) { for (int i = 0; i < CLIENTS; i++) {
threads[i] = new Thread(new BenchB(this, i)); threads[i] = new Thread(new BenchB(this, i));
} }
for (Thread t : threads) { for (Thread t : threads) {
......
...@@ -21,8 +21,11 @@ import java.sql.Types; ...@@ -21,8 +21,11 @@ import java.sql.Types;
*/ */
public class BenchC implements Bench { 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" }; "NEW_ORDER", "ITEM", "STOCK", "ORDER_LINE", "RESULTS" };
private static final String[] CREATE_SQL = { private static final String[] CREATE_SQL = {
"CREATE TABLE WAREHOUSE(\n" + "CREATE TABLE WAREHOUSE(\n" +
" W_ID INT NOT NULL PRIMARY KEY,\n" + " W_ID INT NOT NULL PRIMARY KEY,\n" +
...@@ -176,9 +179,6 @@ public class BenchC implements Bench { ...@@ -176,9 +179,6 @@ public class BenchC implements Bench {
private BenchCRandom random; private BenchCRandom random;
private String action; private String action;
private static final int commitEvery = 1000;
public void init(Database db, int size) throws SQLException { public void init(Database db, int size) throws SQLException {
this.database = db; this.database = db;
...@@ -248,7 +248,7 @@ public class BenchC implements Bench { ...@@ -248,7 +248,7 @@ public class BenchC implements Bench {
prep.setString(5, data); prep.setString(5, data);
database.update(prep, "insertItem"); database.update(prep, "insertItem");
trace(id, items); trace(id, items);
if (id % commitEvery == 0) { if (id % COMMIT_EVERY == 0) {
database.commit(); database.commit();
} }
} }
...@@ -282,7 +282,7 @@ public class BenchC implements Bench { ...@@ -282,7 +282,7 @@ public class BenchC implements Bench {
database.update(prep, "insertWarehouse"); database.update(prep, "insertWarehouse");
loadStock(id); loadStock(id);
loadDistrict(id); loadDistrict(id);
if (id % commitEvery == 0) { if (id % COMMIT_EVERY == 0) {
database.commit(); database.commit();
} }
} }
...@@ -296,7 +296,7 @@ public class BenchC implements Bench { ...@@ -296,7 +296,7 @@ public class BenchC implements Bench {
for (int districtId = 1; districtId <= districtsPerWarehouse; districtId++) { for (int districtId = 1; districtId <= districtsPerWarehouse; districtId++) {
loadCustomerSub(districtId, id); loadCustomerSub(districtId, id);
trace(i++, max); trace(i++, max);
if (i % commitEvery == 0) { if (i % COMMIT_EVERY == 0) {
database.commit(); database.commit();
} }
} }
...@@ -452,7 +452,7 @@ public class BenchC implements Bench { ...@@ -452,7 +452,7 @@ public class BenchC implements Bench {
prepLine.setBigDecimal(8, amount); prepLine.setBigDecimal(8, amount);
prepLine.setString(9, distInfo); prepLine.setString(9, distInfo);
database.update(prepLine, "insertOrderLine"); database.update(prepLine, "insertOrderLine");
if (i++ % commitEvery == 0) { if (i++ % COMMIT_EVERY == 0) {
database.commit(); database.commit();
} }
} }
...@@ -502,7 +502,7 @@ public class BenchC implements Bench { ...@@ -502,7 +502,7 @@ public class BenchC implements Bench {
prep.setInt(16, 0); prep.setInt(16, 0);
prep.setInt(17, 0); prep.setInt(17, 0);
database.update(prep, "insertStock"); database.update(prep, "insertStock");
if (id % commitEvery == 0) { if (id % COMMIT_EVERY == 0) {
database.commit(); database.commit();
} }
trace(id, items); trace(id, items);
......
...@@ -30,6 +30,8 @@ import org.h2.util.StringUtils; ...@@ -30,6 +30,8 @@ import org.h2.util.StringUtils;
*/ */
class Database { class Database {
private static final boolean TRACE = true;
private TestPerformance test; private TestPerformance test;
private int id; private int id;
private String name, url, user, password; private String name, url, user, password;
...@@ -38,7 +40,6 @@ class Database { ...@@ -38,7 +40,6 @@ class Database {
private long startTime; private long startTime;
private Connection conn; private Connection conn;
private Statement stat; private Statement stat;
private static final boolean trace = true;
private long lastTrace; private long lastTrace;
private final Random random = new Random(1); private final Random random = new Random(1);
private final ArrayList<Object[]> results = new ArrayList<Object[]>(); private final ArrayList<Object[]> results = new ArrayList<Object[]>();
...@@ -373,7 +374,7 @@ class Database { ...@@ -373,7 +374,7 @@ class Database {
* @param max the maximum value * @param max the maximum value
*/ */
void trace(String action, int i, int max) { void trace(String action, int i, int max) {
if (trace) { if (TRACE) {
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
if (i == 0 || lastTrace == 0) { if (i == 0 || lastTrace == 0) {
lastTrace = time; lastTrace = time;
......
...@@ -12,7 +12,6 @@ import java.io.FileReader; ...@@ -12,7 +12,6 @@ import java.io.FileReader;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.LineNumberReader; import java.io.LineNumberReader;
import java.io.Writer;
/** /**
* The class used at runtime to measure the code usage and performance. * The class used at runtime to measure the code usage and performance.
......
...@@ -24,7 +24,7 @@ import org.h2.util.JdbcUtils; ...@@ -24,7 +24,7 @@ import org.h2.util.JdbcUtils;
*/ */
public class TestPowerOff extends TestBase { public class TestPowerOff extends TestBase {
private static final String dbName = "powerOff"; private static final String DB_NAME = "powerOff";
private String dir, url; private String dir, url;
private int maxPowerOffCount; private int maxPowerOffCount;
...@@ -44,10 +44,10 @@ public class TestPowerOff extends TestBase { ...@@ -44,10 +44,10 @@ public class TestPowerOff extends TestBase {
} }
if (config.big || config.googleAppEngine) { if (config.big || config.googleAppEngine) {
dir = getBaseDir(); dir = getBaseDir();
url = dbName; url = DB_NAME;
} else { } else {
dir = "memFS:"; dir = "memFS:";
url = "memFS:/" + dbName; url = "memFS:/" + DB_NAME;
} }
url += ";FILE_LOCK=NO;TRACE_LEVEL_FILE=0"; url += ";FILE_LOCK=NO;TRACE_LEVEL_FILE=0";
testLobCrash(); testLobCrash();
...@@ -56,14 +56,14 @@ public class TestPowerOff extends TestBase { ...@@ -56,14 +56,14 @@ public class TestPowerOff extends TestBase {
testShutdown(); testShutdown();
testMemoryTables(); testMemoryTables();
testPersistentTables(); testPersistentTables();
deleteDb(dir, dbName); deleteDb(dir, DB_NAME);
} }
private void testLobCrash() throws SQLException { private void testLobCrash() throws SQLException {
if (config.networked) { if (config.networked) {
return; return;
} }
deleteDb(dir, dbName); deleteDb(dir, DB_NAME);
Connection conn = getConnection(url); Connection conn = getConnection(url);
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
stat.execute("create table test(id identity, data clob)"); stat.execute("create table test(id identity, data clob)");
...@@ -97,7 +97,7 @@ public class TestPowerOff extends TestBase { ...@@ -97,7 +97,7 @@ public class TestPowerOff extends TestBase {
if (config.networked) { if (config.networked) {
return; return;
} }
deleteDb(dir, dbName); deleteDb(dir, DB_NAME);
Connection conn = getConnection(url); Connection conn = getConnection(url);
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
...@@ -141,7 +141,7 @@ public class TestPowerOff extends TestBase { ...@@ -141,7 +141,7 @@ public class TestPowerOff extends TestBase {
if (config.networked) { if (config.networked) {
return; return;
} }
deleteDb(dir, dbName); deleteDb(dir, DB_NAME);
Random random = new Random(1); Random random = new Random(1);
SysProperties.runFinalize = false; SysProperties.runFinalize = false;
int repeat = getSize(1, 20); int repeat = getSize(1, 20);
...@@ -179,7 +179,7 @@ public class TestPowerOff extends TestBase { ...@@ -179,7 +179,7 @@ public class TestPowerOff extends TestBase {
} }
private void testShutdown() throws SQLException { private void testShutdown() throws SQLException {
deleteDb(dir, dbName); deleteDb(dir, DB_NAME);
Connection conn = getConnection(url); Connection conn = getConnection(url);
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))"); stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))");
...@@ -199,7 +199,7 @@ public class TestPowerOff extends TestBase { ...@@ -199,7 +199,7 @@ public class TestPowerOff extends TestBase {
if (config.networked) { if (config.networked) {
return; return;
} }
deleteDb(dir, dbName); deleteDb(dir, DB_NAME);
Connection conn = getConnection(url); Connection conn = getConnection(url);
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
...@@ -239,7 +239,7 @@ public class TestPowerOff extends TestBase { ...@@ -239,7 +239,7 @@ public class TestPowerOff extends TestBase {
// individual writes, many thousand operations) // individual writes, many thousand operations)
return; return;
} }
deleteDb(dir, dbName); deleteDb(dir, DB_NAME);
// ((JdbcConnection)conn).setPowerOffCount(Integer.MAX_VALUE); // ((JdbcConnection)conn).setPowerOffCount(Integer.MAX_VALUE);
testRun(true); testRun(true);
...@@ -253,7 +253,7 @@ public class TestPowerOff extends TestBase { ...@@ -253,7 +253,7 @@ public class TestPowerOff extends TestBase {
private void runTest(int min, int max, boolean withConsistencyCheck) throws SQLException { private void runTest(int min, int max, boolean withConsistencyCheck) throws SQLException {
for (int i = min; i < max; i++) { for (int i = min; i < max; i++) {
deleteDb(dir, dbName); deleteDb(dir, DB_NAME);
Database.setInitialPowerOffCount(i); Database.setInitialPowerOffCount(i);
int expect = testRun(false); int expect = testRun(false);
if (withConsistencyCheck) { if (withConsistencyCheck) {
......
...@@ -37,8 +37,9 @@ public class TestBatchUpdates extends TestBase { ...@@ -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_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 String COFFEE_SELECT_CONTINUED = "SELECT COUNT(*) FROM TEST WHERE C_NAME='Continue-1'";
private static final int coffeeSize = 10; private static final int COFFEE_SIZE = 10;
private static final int coffeeType = 11; private static final int COFFEE_TYPE = 11;
private Connection conn; private Connection conn;
private Statement stat; private Statement stat;
private PreparedStatement prep; private PreparedStatement prep;
...@@ -173,8 +174,8 @@ public class TestBatchUpdates extends TestBase { ...@@ -173,8 +174,8 @@ public class TestBatchUpdates extends TestBase {
int newType = 0; int newType = 0;
prep = conn.prepareStatement("INSERT INTO TEST VALUES(?,?,?,?)"); prep = conn.prepareStatement("INSERT INTO TEST VALUES(?,?,?,?)");
int newKey = 1; int newKey = 1;
for (int i = 1; i <= coffeeType && newKey <= coffeeSize; i++) { for (int i = 1; i <= COFFEE_TYPE && newKey <= COFFEE_SIZE; i++) {
for (int j = 1; j <= i && newKey <= coffeeSize; j++) { for (int j = 1; j <= i && newKey <= COFFEE_SIZE; j++) {
newName = "COFFEE-" + newKey; newName = "COFFEE-" + newKey;
newPrice = newKey + (float) .00; newPrice = newKey + (float) .00;
newType = i; newType = i;
......
...@@ -42,14 +42,14 @@ public class TestJavaObjectSerializer extends TestBase { ...@@ -42,14 +42,14 @@ public class TestJavaObjectSerializer extends TestBase {
Utils.serializer = new JavaObjectSerializer() { Utils.serializer = new JavaObjectSerializer() {
@Override @Override
public byte[] serialize(Object obj) throws Exception { 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 @Override
public Object deserialize(byte[] bytes) throws Exception { public Object deserialize(byte[] bytes) throws Exception {
assertEquals(new byte[]{1,2,3}, bytes); assertEquals(new byte[] { 1, 2, 3 }, bytes);
return 100500; return 100500;
} }
...@@ -72,7 +72,7 @@ public class TestJavaObjectSerializer extends TestBase { ...@@ -72,7 +72,7 @@ public class TestJavaObjectSerializer extends TestBase {
assertTrue(rs.next()); assertTrue(rs.next());
assertEquals(100500, ((Integer) rs.getObject(1)).intValue()); 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"); deleteDb("javaSerializer");
} finally { } finally {
......
...@@ -7,6 +7,9 @@ ...@@ -7,6 +7,9 @@
package org.h2.test.store; package org.h2.test.store;
import java.util.ArrayList; 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.DataType;
import org.h2.dev.store.btree.MVMap; import org.h2.dev.store.btree.MVMap;
import org.h2.dev.store.btree.MVStore; import org.h2.dev.store.btree.MVStore;
...@@ -21,7 +24,7 @@ import org.h2.util.New; ...@@ -21,7 +24,7 @@ import org.h2.util.New;
*/ */
public class MVRTreeMap<K, V> extends MVMap<K, V> { public class MVRTreeMap<K, V> extends MVMap<K, V> {
private final SpatialType keyType; final SpatialType keyType;
private boolean quadraticSplit; private boolean quadraticSplit;
MVRTreeMap(MVStore store, int id, String name, DataType keyType, MVRTreeMap(MVStore store, int id, String name, DataType keyType,
...@@ -36,6 +39,39 @@ public class MVRTreeMap<K, V> extends MVMap<K, V> { ...@@ -36,6 +39,39 @@ public class MVRTreeMap<K, V> extends MVMap<K, V> {
return (V) get(root, key); 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) { private boolean contains(Page p, int index, Object key) {
return keyType.contains(p.getKey(index), key); return keyType.contains(p.getKey(index), key);
} }
...@@ -411,4 +447,88 @@ public class MVRTreeMap<K, V> extends MVMap<K, V> { ...@@ -411,4 +447,88 @@ public class MVRTreeMap<K, V> extends MVMap<K, V> {
return p.getChildPageCount() - 1; 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; ...@@ -7,10 +7,10 @@ package org.h2.test.store;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.InputStream; import java.io.InputStream;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Random; import java.util.Random;
...@@ -44,7 +44,9 @@ public class TestConcurrent extends TestMVStore { ...@@ -44,7 +44,9 @@ public class TestConcurrent extends TestMVStore {
} }
private void testConcurrentOnlineBackup() throws Exception { 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"; String fileNameRestore = getBaseDir() + "/onlineRestore.h3";
final MVStore s = openStore(fileName); final MVStore s = openStore(fileName);
final MVMap<Integer, byte[]> map = s.openMap("test"); final MVMap<Integer, byte[]> map = s.openMap("test");
...@@ -59,7 +61,7 @@ public class TestConcurrent extends TestMVStore { ...@@ -59,7 +61,7 @@ public class TestConcurrent extends TestMVStore {
s.store(); s.store();
map.clear(); map.clear();
s.store(); s.store();
long len = new File(s.getFileName()).length(); long len = s.getFile().size();
if (len > 1024 * 100) { if (len > 1024 * 100) {
// slow down writing // slow down writing
Thread.sleep(10); Thread.sleep(10);
...@@ -70,8 +72,8 @@ public class TestConcurrent extends TestMVStore { ...@@ -70,8 +72,8 @@ public class TestConcurrent extends TestMVStore {
t.execute(); t.execute();
// the wrong way to back up // the wrong way to back up
try { try {
for (int i = 0; i < 10; i++) { for (int i = 0; i < 100; i++) {
byte[] buff = readFileSlowly(fileName); byte[] buff = readFileSlowly(s.getFile());
FileOutputStream out = new FileOutputStream(fileNameRestore); FileOutputStream out = new FileOutputStream(fileNameRestore);
out.write(buff); out.write(buff);
MVStore s2 = openStore(fileNameRestore); MVStore s2 = openStore(fileNameRestore);
...@@ -92,7 +94,7 @@ public class TestConcurrent extends TestMVStore { ...@@ -92,7 +94,7 @@ public class TestConcurrent extends TestMVStore {
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
// System.out.println("test " + i); // System.out.println("test " + i);
s.setReuseSpace(false); s.setReuseSpace(false);
byte[] buff = readFileSlowly(fileName); byte[] buff = readFileSlowly(s.getFile());
s.setReuseSpace(true); s.setReuseSpace(true);
FileOutputStream out = new FileOutputStream(fileNameRestore); FileOutputStream out = new FileOutputStream(fileNameRestore);
out.write(buff); out.write(buff);
...@@ -109,9 +111,9 @@ public class TestConcurrent extends TestMVStore { ...@@ -109,9 +111,9 @@ public class TestConcurrent extends TestMVStore {
s.close(); s.close();
} }
private static byte[] readFileSlowly(String fileName) throws Exception { private static byte[] readFileSlowly(FileChannel file) throws Exception {
InputStream in = new BufferedInputStream(new FileInputStream( file.position(0);
fileName)); InputStream in = new BufferedInputStream(Channels.newInputStream(file));
ByteArrayOutputStream buff = new ByteArrayOutputStream(); ByteArrayOutputStream buff = new ByteArrayOutputStream();
for (int j = 0;; j++) { for (int j = 0;; j++) {
int x = in.read(); int x = in.read();
...@@ -123,7 +125,8 @@ public class TestConcurrent extends TestMVStore { ...@@ -123,7 +125,8 @@ public class TestConcurrent extends TestMVStore {
Thread.sleep(1); Thread.sleep(1);
} }
} }
in.close(); // in.close() could close the stream
// in.close();
return buff.toByteArray(); return buff.toByteArray();
} }
......
...@@ -15,6 +15,7 @@ import java.io.IOException; ...@@ -15,6 +15,7 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.Random; import java.util.Random;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.imageio.ImageWriter; import javax.imageio.ImageWriter;
...@@ -40,7 +41,7 @@ public class TestMVRTree extends TestMVStore { ...@@ -40,7 +41,7 @@ public class TestMVRTree extends TestMVStore {
public void test() { public void test() {
testMany(); testMany();
testTree(); testSimple();
testRandom(); testRandom();
testCustomMapType(); testCustomMapType();
} }
...@@ -108,25 +109,26 @@ public class TestMVRTree extends TestMVStore { ...@@ -108,25 +109,26 @@ public class TestMVRTree extends TestMVStore {
// System.out.println("remove: " + (System.currentTimeMillis() - t)); // System.out.println("remove: " + (System.currentTimeMillis() - t));
} }
private void testTree() { private void testSimple() {
String fileName = getBaseDir() + "/testTree.h3"; String fileName = getBaseDir() + "/testTree.h3";
FileUtils.delete(fileName); FileUtils.delete(fileName);
MVStore s; MVStore s;
s = openStore(fileName); s = openStore(fileName);
MVRTreeMap<SpatialKey, String> r = s.openMap("data", "r", "s2", ""); MVRTreeMap<SpatialKey, String> r = s.openMap("data", "r", "s2", "");
add(r, "Bern", 46.57, 7.27, 124381); add(r, "Bern", key(0, 46.57, 7.27, 124381));
add(r, "Basel", 47.34, 7.36, 170903); add(r, "Basel", key(1, 47.34, 7.36, 170903));
add(r, "Zurich", 47.22, 8.33, 376008); add(r, "Zurich", key(2, 47.22, 8.33, 376008));
add(r, "Lucerne", 47.03, 8.18, 77491); add(r, "Lucerne", key(3, 47.03, 8.18, 77491));
add(r, "Geneva", 46.12, 6.09, 191803); add(r, "Geneva", key(4, 46.12, 6.09, 191803));
add(r, "Lausanne", 46.31, 6.38, 127821); add(r, "Lausanne", key(5, 46.31, 6.38, 127821));
add(r, "Winterthur", 47.30, 8.45, 102966); add(r, "Winterthur", key(6, 47.30, 8.45, 102966));
add(r, "St. Gallen", 47.25, 9.22, 73500); add(r, "St. Gallen", key(7, 47.25, 9.22, 73500));
add(r, "Biel/Bienne", 47.08, 7.15, 51203); add(r, "Biel/Bienne", key(8, 47.08, 7.15, 51203));
add(r, "Lugano", 46.00, 8.57, 54667); add(r, "Lugano", key(9, 46.00, 8.57, 54667));
add(r, "Thun", 46.46, 7.38, 42623); add(r, "Thun", key(10, 46.46, 7.38, 42623));
add(r, "Bellinzona", 46.12, 9.01, 17373); add(r, "Bellinzona", key(11, 46.12, 9.01, 17373));
add(r, "Chur", 46.51, 9.32, 33756); add(r, "Chur", key(12, 46.51, 9.32, 33756));
// render(r, getBaseDir() + "/test.png");
ArrayList<String> list = New.arrayList(); ArrayList<String> list = New.arrayList();
for (SpatialKey x : r.keySet()) { for (SpatialKey x : r.keySet()) {
list.add(r.get(x)); list.add(r.get(x));
...@@ -135,17 +137,46 @@ public class TestMVRTree extends TestMVStore { ...@@ -135,17 +137,46 @@ public class TestMVRTree extends TestMVStore {
assertEquals("[Basel, Bellinzona, Bern, Biel/Bienne, Chur, Geneva, " + assertEquals("[Basel, Bellinzona, Bern, Biel/Bienne, Chur, Geneva, " +
"Lausanne, Lucerne, Lugano, St. Gallen, Thun, Winterthur, Zurich]", "Lausanne, Lucerne, Lugano, St. Gallen, Thun, Winterthur, Zurich]",
list.toString()); 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(); s.close();
} }
private static void add(MVRTreeMap<SpatialKey, String> r, String name, double y, double x, int population) { private static void add(MVRTreeMap<SpatialKey, String> r, String name, SpatialKey k) {
int id = r.size(); 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 a = (float) ((int) x + (x - (int) x) * 5 / 3);
float b = 50 - (float) ((int) y + (y - (int) y) * 5 / 3); float b = 50 - (float) ((int) y + (y - (int) y) * 5 / 3);
float s = (float) Math.sqrt(population / 10000000.); float s = (float) Math.sqrt(population / 10000000.);
SpatialKey k = new SpatialKey(id, a - s, a + s, b - s, b + s); 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) { private static void render(MVRTreeMap<SpatialKey, String> r, String fileName) {
......
...@@ -25,8 +25,9 @@ public class TestKill extends TestBase { ...@@ -25,8 +25,9 @@ public class TestKill extends TestBase {
private static final String DIR = TestBase.getTestDir("kill"); private static final String DIR = TestBase.getTestDir("kill");
private static final int ACCOUNTS = 10;
private Connection conn; private Connection conn;
private static final int accounts = 10;
private final Random random = new Random(1); private final Random random = new Random(1);
/** /**
...@@ -51,7 +52,7 @@ public class TestKill extends TestBase { ...@@ -51,7 +52,7 @@ public class TestKill extends TestBase {
"java", selfDestruct, "java", selfDestruct,
"-cp", getClassPath(), "-cp", getClassPath(),
"org.h2.test.synth.TestKillProcess", url, user, "org.h2.test.synth.TestKillProcess", url, user,
password, getBaseDir(), "" + accounts }; password, getBaseDir(), "" + ACCOUNTS };
for (int i = 0;; i++) { for (int i = 0;; i++) {
printTime("TestKill " + i); printTime("TestKill " + i);
...@@ -102,13 +103,13 @@ public class TestKill extends TestBase { ...@@ -102,13 +103,13 @@ public class TestKill extends TestBase {
conn.createStatement().execute("DROP TABLE TEST_B"); conn.createStatement().execute("DROP TABLE TEST_B");
createTables(); createTables();
PreparedStatement prep = conn.prepareStatement("INSERT INTO ACCOUNT VALUES(?, 0)"); 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.setInt(1, i);
prep.execute(); prep.execute();
} }
PreparedStatement p1 = conn.prepareStatement("INSERT INTO TEST_A VALUES(?, '')"); PreparedStatement p1 = conn.prepareStatement("INSERT INTO TEST_A VALUES(?, '')");
PreparedStatement p2 = conn.prepareStatement("INSERT INTO TEST_B 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); p1.setInt(1, i);
p2.setInt(1, i); p2.setInt(1, i);
p1.execute(); p1.execute();
...@@ -132,7 +133,7 @@ public class TestKill extends TestBase { ...@@ -132,7 +133,7 @@ public class TestKill extends TestBase {
} }
PreparedStatement p1 = conn.prepareStatement("SELECT * FROM TEST_A WHERE ID=?"); PreparedStatement p1 = conn.prepareStatement("SELECT * FROM TEST_A WHERE ID=?");
PreparedStatement p2 = conn.prepareStatement("SELECT * FROM TEST_B 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); p1.setInt(1, i);
p2.setInt(1, i); p2.setInt(1, i);
ResultSet r1 = p1.executeQuery(); ResultSet r1 = p1.executeQuery();
......
...@@ -24,11 +24,12 @@ import org.h2.util.New; ...@@ -24,11 +24,12 @@ import org.h2.util.New;
*/ */
public class TestPowerOffFs2 extends TestBase { public class TestPowerOffFs2 extends TestBase {
private static final String USER = "sa";
private static final String PASSWORD = "sa";
private FilePathDebug fs; private FilePathDebug fs;
private String url; 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<Connection> connections = New.arrayList();
private final ArrayList<String> tables = New.arrayList(); private final ArrayList<String> tables = New.arrayList();
...@@ -165,7 +166,7 @@ public class TestPowerOffFs2 extends TestBase { ...@@ -165,7 +166,7 @@ public class TestPowerOffFs2 extends TestBase {
} }
private Connection openConnection() throws SQLException { private Connection openConnection() throws SQLException {
Connection conn = DriverManager.getConnection(url, user, password); Connection conn = DriverManager.getConnection(url, USER, PASSWORD);
connections.add(conn); connections.add(conn);
return conn; return conn;
} }
......
...@@ -21,7 +21,7 @@ public class TestMultiNews extends TestMultiThread { ...@@ -21,7 +21,7 @@ public class TestMultiNews extends TestMultiThread {
private static final String PREFIX_URL = private static final String PREFIX_URL =
"http://feeds.wizbangblog.com/WizbangFullFeed?m="; "http://feeds.wizbangblog.com/WizbangFullFeed?m=";
private static final int len = 10000; private static final int LEN = 10000;
private Connection conn; private Connection conn;
TestMultiNews(TestMulti base) throws SQLException { TestMultiNews(TestMulti base) throws SQLException {
...@@ -49,7 +49,7 @@ public class TestMultiNews extends TestMultiThread { ...@@ -49,7 +49,7 @@ public class TestMultiNews extends TestMultiThread {
} else { } else {
prep = conn.prepareStatement("SELECT * FROM NEWS WHERE VALUE = ?"); 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(); ResultSet rs = prep.executeQuery();
if (!rs.next()) { if (!rs.next()) {
throw new SQLException("expected one row, got none"); throw new SQLException("expected one row, got none");
...@@ -60,7 +60,7 @@ public class TestMultiNews extends TestMultiThread { ...@@ -60,7 +60,7 @@ public class TestMultiNews extends TestMultiThread {
} else { } else {
PreparedStatement prep = conn.prepareStatement("UPDATE NEWS SET STATE = ? WHERE FID = ?"); PreparedStatement prep = conn.prepareStatement("UPDATE NEWS SET STATE = ? WHERE FID = ?");
prep.setInt(1, random.nextInt(100)); prep.setInt(1, random.nextInt(100));
prep.setInt(2, random.nextInt(len)); prep.setInt(2, random.nextInt(LEN));
int count = prep.executeUpdate(); int count = prep.executeUpdate();
if (count != 1) { if (count != 1) {
throw new SQLException("expected one row, got " + count); throw new SQLException("expected one row, got " + count);
...@@ -93,7 +93,7 @@ public class TestMultiNews extends TestMultiThread { ...@@ -93,7 +93,7 @@ public class TestMultiNews extends TestMultiThread {
PreparedStatement prep = c.prepareStatement("INSERT INTO NEWS (FID, COMMENTS, LINK, STATE, VALUE) VALUES " PreparedStatement prep = c.prepareStatement("INSERT INTO NEWS (FID, COMMENTS, LINK, STATE, VALUE) VALUES "
+ "(?, ?, ?, ?, ?) "); + "(?, ?, ?, ?, ?) ");
PreparedStatement prep2 = c.prepareStatement("INSERT INTO TEST (NAME) 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; int x = random.nextInt(10) * 128;
StringBuilder buff = new StringBuilder(); StringBuilder buff = new StringBuilder();
while (buff.length() < x) { while (buff.length() < x) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论