提交 96d8c078 authored 作者: Thomas Mueller's avatar Thomas Mueller

--no commit message

--no commit message
上级 9a25c1c1
差异被折叠。
package org.h2.test.cases;
/*
* DatDb.java
*
* Created on 21 maggio 2007, 12.30
*
*/
//package com.impl.util;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.*;
import java.util.HashMap;
import java.util.Random;
/**
*
* @author fbi
*/
public class DatDb {
private static DatDb datdb;
static HashMap cP = new HashMap();
static Connection sharedCon ;
//private String url = "jdbc:h2:file:" + Constants.DAT_DB;
// private String url = "jdbc:h2:file:/temp/datdb;TRACE_LEVEL_FILE=3";
private String url = "jdbc:h2:file:/temp/datdb;DATABASE_EVENT_LISTENER='org.h2.samples.ShowProgress'";
/** Creates a new instance of DatDb */
protected DatDb() {
try {
long t1 = System.currentTimeMillis();
Class.forName("org.h2.Driver");
System.out.println (" getConnection --> ");
//This is a 'special' connection used only to execute select statements (no transactions)
sharedCon = DriverManager.getConnection(url, "sa", "");
System.out.println (" getConnection <-- " + (System.currentTimeMillis()- t1));
sharedCon.setReadOnly(true);
} catch (Exception e) {
//Util.logThrowable(e);
System.out.println(" error: " + e.getMessage());
}
}
public static synchronized DatDb getInstance() {
if (datdb == null) {
datdb = new DatDb();
}
return datdb;
}
public void init (String tableName) {
//String sc = ma.substring(ma.lastIndexOf("/")+1);
try {
Connection con = DriverManager.getConnection(url, "sa", "");
cP.put(tableName, con);
createTable(con, tableName);
} catch (Exception e) {
//Util.logThrowable(e);
}
}
public void save(String tableName, String key, String xml) {
Connection con = null;
//String tableName = ma.substring(ma.lastIndexOf("/")+1);
try {
con = (Connection) cP.get(tableName);
if (con == null) throw new RuntimeException ("Unable to find the connection");
con.setAutoCommit(false);
PreparedStatement stm = null;
stm = con.prepareStatement("delete from "+tableName+" where fname = ?");
stm.setString(1, key);
stm.executeUpdate();
stm.close();
stm = con.prepareStatement("insert into "+tableName+" (fname, gendate, dat) values(?,?,?)");
stm.setString(1, key);
stm.setLong(2, System.currentTimeMillis());
//--- original code ---->>>> stm.setObject(3, com.fw.util.Util.compressString(xml));
stm.setObject(3, xml);
stm.executeUpdate();
con.commit();
} catch (Exception e) {
try {
con.rollback();
} catch (SQLException ex) {
ex.printStackTrace();
}
// Util.logThrowable(e);
}
}
public long getGenDate (String tableName, String key) {
Connection con = null;
long gendate = 0;
PreparedStatement stm = null;
try {
stm = sharedCon.prepareStatement("select gendate from "+tableName+" where fname = ?");
stm.setString(1, key);
ResultSet rs = stm.executeQuery();
while (rs.next()) {
gendate = rs.getLong(1);
}
} catch (Exception e) {
//Util.logThrowable(e);
} finally {
try {
stm.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
//System.out.println ("getGenDate table "+tableName+" key "+key+" returns "+gendate);
return gendate;
}
public boolean containsKey(String tableName, String key) {
Connection con = null;
boolean b = false;
PreparedStatement stm = null;
try {
stm = sharedCon.prepareStatement("select fname from "+tableName+" where fname = ?");
stm.setString(1, key);
ResultSet rs = stm.executeQuery();
while (rs.next()) {
b = true;
}
} catch (Exception e) {
//Util.logThrowable(e);
} finally {
try {
stm.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
//System.out.println ("containsKey table "+tableName+" key "+key+" returns "+b);
return b;
}
public String getDat(String tableName, String key) {
String ret = null;
byte buf[] = null;
PreparedStatement stm = null;
try {
stm = sharedCon.prepareStatement("select dat from "+tableName+" where fname = ?");
stm.setString(1, key);
ResultSet rs = stm.executeQuery();
while (rs.next()) {
buf = rs.getBytes(1);
}
if (buf == null) {
//System.out.println (" NULL BUF for table "+tableName+" KEY "+ key);
return ret;
}
// ------->>>> Original code ------<<<< ret = Util.uncompressString(buf);
ret = new String(buf);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
stm.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
return ret;
}
private void createTable(Connection con, String tableName) {
ResultSet rs = null;
try {
DatabaseMetaData dmd = con.getMetaData();
rs = dmd.getTables(null, null, null, null);
String temp = null;
while (rs.next()) {
temp = rs.getString(3);
if (temp.equalsIgnoreCase(tableName)) {
return;
}
}
} catch (Exception e) {
//Util.logThrowable(e);
}
try {
java.sql.Statement stm = con.createStatement();
stm.execute("CREATE TABLE "+tableName+" (fname varchar(80) primary key, gendate bigint, dat varchar) ");
System.out.println (" Table "+tableName+" created");
} catch (Exception e) {
// Util.logThrowable(e);
}
}
public void close(String tableName) {
Connection c = (Connection) cP.remove(tableName);
try {
c.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
//-------------------------------------- TEST START ------------------------
public static void main(String ar[]) {
System.out.println (" Starting ");
DatDb db = new DatDb();
db.go();
}
private void go () {
/*
* Please change the values as needed.
* The test I've made uses MAX_REC = 400000
* and MAX_TAB = 50,
* 20.000.000 records!
*/
int MAX_REC = 400000;
int MAX_TAB = 10;
String tableName = null;
long start = System.currentTimeMillis();
System.out.println(" Start test ");
long trec = 0;
for (int j = 0; j < MAX_TAB; j++) {
tableName = "n"+j;
try {
Connection con = DriverManager.getConnection(url, "sa","");
cP.put(tableName, con);
createTable(con, tableName);
} catch (Exception e ){ e.printStackTrace(); }
long t1 = System.currentTimeMillis();
trec = t1;
for (int k = 0; k < MAX_REC; k++ ){
this.save(tableName,"this_is_a_possible_key_value"+k, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm");
if (k > 0 && (k % 10000) == 0) {
System.out.println(" added more 10000 records to "+tableName+" spent "+ (System.currentTimeMillis()-trec)+ " K "+k);
trec = System.currentTimeMillis();
}
}
close(tableName);
System.out.println(" TABLE "+tableName+" completed with "+MAX_REC +" millis "+ (System.currentTimeMillis() - t1));
}
// Now, try to get back some raws
int tkey = 0;
int rkey = 0;
Random rnd = new Random();
for (int l = 0; l < 100; l++) {
start = System.currentTimeMillis();
tkey = rnd.nextInt(MAX_TAB);
rkey = rnd.nextInt(MAX_REC);
this.getDat("n"+tkey, "questa_essere_la_chivae_numero_"+rkey);
// System.out.println ("estract dat key "+rkey+" from table n"+ tkey+" in millis "+ (System.currentTimeMillis() - start));
}
System.out.println (" exercise end" );
try {
sharedCon.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
package org.h2.test.cases;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class TestMemoryDb {
public static void main(String[] args) throws Exception {
Class.forName("org.h2.Driver");
String url = "jdbc:h2:mem:EventDB";
String user = "sa";
String password = "sa";
Connection conn1 = DriverManager.getConnection(url, user, password);
Statement stat1 = conn1.createStatement();
stat1.execute("create table test(id int)");
Connection conn2 = DriverManager.getConnection(url, user, password);
Statement stat2 = conn2.createStatement();
stat2.executeQuery("select * from test");
conn1.close();
conn2.close();
}
}
package org.h2.test.poweroff;
import java.io.File;
import java.io.FileDescriptor;
import java.io.RandomAccessFile;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
public class TestWrite {
public static void main(String[] args) throws Exception {
testFile("rw", false);
testFile("rwd", false);
testFile("rws", false);
testFile("rw", true);
testFile("rwd", true);
testFile("rws", true);
testDatabase("org.h2.Driver", "jdbc:h2:test", "sa", "");
testDatabase("org.hsqldb.jdbcDriver", "jdbc:hsqldb:test4", "sa", "");
testDatabase("org.apache.derby.jdbc.EmbeddedDriver", "jdbc:derby:test;create=true", "sa", "");
testDatabase("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/test", "sa", "sa");
testDatabase("org.postgresql.Driver", "jdbc:postgresql:test", "sa", "sa");
}
static void testFile(String mode, boolean flush) throws Exception {
System.out.println("Testing RandomAccessFile(.., \"" + mode + "\")...");
if(flush) {
System.out.println(" with FileDescriptor.sync()");
}
RandomAccessFile file = new RandomAccessFile("test.txt", mode);
file.setLength(0);
FileDescriptor fd = file.getFD();
long start = System.currentTimeMillis();
byte[] data = new byte[] {0};
file.write(data);
int i=0;
if(flush) {
for(; ; i++) {
file.seek(0);
file.write(data);
fd.sync();
if((i & 15) == 0) {
long time = System.currentTimeMillis() - start;
if(time > 5000) {
break;
}
}
}
} else {
for(; ; i++) {
file.seek(0);
file.write(data);
if((i & 1023) == 0) {
long time = System.currentTimeMillis() - start;
if(time > 5000) {
break;
}
}
}
}
long time = System.currentTimeMillis() - start;
System.out.println("Time: " + time);
System.out.println("Operations: " + i);
System.out.println("Operations/second: " + (i * 1000 / time));
System.out.println();
file.close();
new File("test.txt").delete();
}
static void testDatabase(String driver, String url, String user, String password) throws Exception {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println("Testing Database, URL=" + url);
Statement stat = conn.createStatement();
try {
stat.execute("DROP TABLE TEST");
} catch(SQLException e) {
// ignore
}
stat.execute("CREATE TABLE TEST(ID INT)");
PreparedStatement prep = conn.prepareStatement("INSERT INTO TEST VALUES(?)");
long start = System.currentTimeMillis();
int i=0;
for(; ; i++) {
prep.setInt(1, i);
// autocommit is on by default, so this commits as well
prep.execute();
if((i & 15) == 0) {
long time = System.currentTimeMillis() - start;
if(time > 5000) {
break;
}
}
}
long time = System.currentTimeMillis() - start;
System.out.println("Time: " + time);
System.out.println("Operations: " + i);
System.out.println("Operations/second: " + (i * 1000 / time));
System.out.println();
stat.execute("DROP TABLE TEST");
conn.close();
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论