提交 4f409b1c authored 作者: Thomas Mueller's avatar Thomas Mueller

--no commit message

--no commit message
上级 ab1e8d62
......@@ -322,6 +322,10 @@ public class Session implements SessionInterface {
}
public void unlockReadLocks() {
if (database.isMultiVersion()) {
// MVCC: keep shared locks (insert / update / delete)
return;
}
for (int i = 0; i < locks.size(); i++) {
Table t = (Table) locks.get(i);
if (!t.isLockedExclusively()) {
......
......@@ -5,7 +5,6 @@
package org.h2.engine;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.sql.SQLException;
......@@ -21,7 +20,6 @@ import org.h2.message.TraceSystem;
import org.h2.store.DataHandler;
import org.h2.store.FileStore;
import org.h2.util.FileUtils;
import org.h2.util.MathUtils;
import org.h2.util.NetUtils;
import org.h2.util.ObjectArray;
import org.h2.util.RandomUtils;
......@@ -61,19 +59,7 @@ public class SessionRemote implements SessionInterface, DataHandler {
private Object lobSyncObject = new Object();
private Transfer initTransfer(ConnectionInfo ci, String db, String server) throws IOException, SQLException {
int port = Constants.DEFAULT_SERVER_PORT;
// IPv6: RFC 2732 format is '[a:b:c:d:e:f:g:h]' or
// '[a:b:c:d:e:f:g:h]:port'
// RFC 2396 format is 'a.b.c.d' or 'a.b.c.d:port' or 'hostname' or
// 'hostname:port'
int startIndex = server.startsWith("[") ? server.indexOf(']') : 0;
int idx = server.indexOf(':', startIndex);
if (idx >= 0) {
port = MathUtils.decodeInt(server.substring(idx + 1));
server = server.substring(0, idx);
}
InetAddress address = InetAddress.getByName(server);
Socket socket = NetUtils.createSocket(address, port, ci.isSSL());
Socket socket = NetUtils.createSocket(server, Constants.DEFAULT_SERVER_PORT, ci.isSSL());
Transfer trans = new Transfer(this);
trans.setSocket(socket);
trans.init();
......
......@@ -1128,9 +1128,14 @@ public class Function extends Expression implements FunctionCall {
}
private static int locate(String search, String s, int start) {
int i = (start < 0) ? 0 : start - 1;
if (start < 0) {
int i = s.length() + start;
return s.lastIndexOf(search, i) + 1;
} else {
int i = (start == 0) ? 0 : start - 1;
return s.indexOf(search, i) + 1;
}
}
private static String right(String s, int count) {
if (count < 0) {
......
......@@ -121,6 +121,9 @@ public class Operation extends Expression {
switch (opType) {
case NEGATE:
dataType = left.getType();
if (dataType == Value.UNKNOWN) {
dataType = Value.DECIMAL;
}
break;
case CONCAT:
right = right.optimize(session);
......@@ -136,7 +139,7 @@ public class Operation extends Expression {
right = right.optimize(session);
int l = left.getType();
int r = right.getType();
if (l == Value.NULL && r == Value.NULL) {
if ((l == Value.NULL && r == Value.NULL) || (l == Value.UNKNOWN && r == Value.UNKNOWN)) {
// example: (? + ?) - the most safe data type is probably
// decimal
dataType = Value.DECIMAL;
......
/*
* Copyright 2004-2007 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.fulltext;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.DateTools;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexModifier;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Searcher;
import org.h2.api.Trigger;
import org.h2.tools.SimpleResultSet;
import org.h2.util.ByteUtils;
import org.h2.util.FileUtils;
import org.h2.util.StringUtils;
public class FullTextLucene implements Trigger {
private static HashMap indexers = new HashMap();
private static final String FIELD_DATA = "data";
private static final String FIELD_QUERY = "query";
private static final String FIELD_COLUMN_PREFIX = "_";
private static final String TRIGGER_PREFIX = "FTL_";
private static final String SCHEMA = "FTL";
private IndexModifier indexer;
private String schemaName;
private String tableName;
private int[] keys;
private int[] indexColumns;
private String[] columnNames;
private int[] dataTypes;
/**
* Create a new full text index for a table and column list. Each table may only have one index at any time.
*
* @param conn the connection
* @param name the name of the index (must be unique)
* @param schema the schema name of the table
* @param table the table name
* @param columnList the column list (null for all columns)
*/
public static void createIndex(Connection conn, String schema, String table, String columnList) throws SQLException {
init(conn);
PreparedStatement prep = conn.prepareStatement("INSERT INTO "+SCHEMA+".INDEXES(SCHEMA, TABLE, COLUMNS) VALUES(?, ?, ?)");
prep.setString(1, schema);
prep.setString(2, table);
prep.setString(3, columnList);
prep.execute();
createTrigger(conn, schema, table);
indexExistingRows(conn, schema, table);
}
private static void createTrigger(Connection conn, String schema, String table) throws SQLException {
Statement stat = conn.createStatement();
String trigger = StringUtils.quoteIdentifier(schema) + "." + StringUtils.quoteIdentifier(TRIGGER_PREFIX + table);
stat.execute("DROP TRIGGER IF EXISTS " + trigger);
StringBuffer buff = new StringBuffer("CREATE TRIGGER IF NOT EXISTS ");
buff.append(trigger);
buff.append(" AFTER INSERT, UPDATE, DELETE ON ");
buff.append(StringUtils.quoteIdentifier(schema) + "." + StringUtils.quoteIdentifier(table));
buff.append(" FOR EACH ROW CALL \"");
buff.append(FullTextLucene.class.getName());
buff.append("\"");
stat.execute(buff.toString());
}
private static void indexExistingRows(Connection conn, String schema, String table) throws SQLException {
FullTextLucene existing = new FullTextLucene();
existing.init(conn, schema, null, table);
StringBuffer buff = new StringBuffer("SELECT * FROM ");
buff.append(StringUtils.quoteIdentifier(schema) + "." + StringUtils.quoteIdentifier(table));
ResultSet rs = conn.createStatement().executeQuery(buff.toString());
int columnCount = rs.getMetaData().getColumnCount();
while (rs.next()) {
Object[] row = new Object[columnCount];
for (int i = 0; i < columnCount; i++) {
row[i] = rs.getObject(i + 1);
}
existing.fire(conn, null, row);
}
}
/**
* Re-creates the full text index for this database
*
* @param conn the connection
*/
public static void reindex(Connection conn) throws SQLException {
init(conn);
removeAllTriggers(conn);
removeIndexFiles(conn);
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery("SELECT * FROM "+SCHEMA+".INDEXES");
while (rs.next()) {
String schema = rs.getString("SCHEMA");
String table = rs.getString("TABLE");
createTrigger(conn, schema, table);
indexExistingRows(conn, schema, table);
}
}
private static void removeAllTriggers(Connection conn) throws SQLException {
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery("SELECT * FROM INFORMATION_SCHEMA.TRIGGERS");
Statement stat2 = conn.createStatement();
while (rs.next()) {
String schema = rs.getString("TRIGGER_SCHEMA");
String name = rs.getString("TRIGGER_NAME");
if (name.startsWith(TRIGGER_PREFIX)) {
name = StringUtils.quoteIdentifier(schema) + "." + StringUtils.quoteIdentifier(name);
stat2.execute("DROP TRIGGER " + name);
}
}
}
private static void removeIndexFiles(Connection conn) throws SQLException {
String path = getIndexPath(conn);
IndexModifier index = (IndexModifier) indexers.get(path);
if(index != null) {
indexers.remove(path);
try {
index.flush();
index.close();
} catch (IOException e) {
throw convertException(e);
}
}
FileUtils.deleteRecursive(path);
}
/**
* Drops all full text indexes from the database.
*
* @param conn the connection
*/
public static void dropAll(Connection conn) throws SQLException {
Statement stat = conn.createStatement();
stat.execute("DROP SCHEMA IF EXISTS " + SCHEMA);
removeAllTriggers(conn);
removeIndexFiles(conn);
}
/**
* Initializes full text search functionality for this database. This adds the following Java functions to the
* database:
* <ul>
* <li>FTL_CREATE_INDEX(schemaNameString, tableNameString, columnListString)
* </li><li>FTL_SEARCH(queryString, limitInt, offsetInt): result set
* </li><li>FTL_REINDEX()
* </li><li>FTL_DROP_ALL()
* </li></ul>
* It also adds a schema FTL to the database where bookkeeping information is stored. This function may be
* called from a Java application, or by using the SQL statements:
*
* <pre>
* CREATE ALIAS IF NOT EXISTS FTL_INIT FOR &quot;org.h2.fulltext.FullTextLucene.init&quot;;
* CALL FTL_INIT();
* </pre>
*
* @param conn
*/
public static void init(Connection conn) throws SQLException {
Statement stat = conn.createStatement();
stat.execute("CREATE SCHEMA IF NOT EXISTS " + SCHEMA);
stat.execute("CREATE TABLE IF NOT EXISTS "+SCHEMA+".INDEXES(SCHEMA VARCHAR, TABLE VARCHAR, COLUMNS VARCHAR, PRIMARY KEY(SCHEMA, TABLE))");
stat.execute("CREATE ALIAS IF NOT EXISTS FTL_CREATE_INDEX FOR \"" + FullTextLucene.class.getName() + ".createIndex\"");
stat.execute("CREATE ALIAS IF NOT EXISTS FTL_SEARCH FOR \"" + FullTextLucene.class.getName() + ".search\"");
stat.execute("CREATE ALIAS IF NOT EXISTS FTL_REINDEX FOR \"" + FullTextLucene.class.getName() + ".reindex\"");
stat.execute("CREATE ALIAS IF NOT EXISTS FTL_DROP_ALL FOR \"" + FullTextLucene.class.getName() + ".dropAll\"");
}
private static IndexModifier getIndexModifier(Connection conn) throws SQLException {
try {
String path = getIndexPath(conn);
IndexModifier indexer;
synchronized (indexers) {
indexer = (IndexModifier) indexers.get(path);
if (indexer == null) {
// TODO: create flag = true means re-create
indexer = new IndexModifier(path, new StandardAnalyzer(), true);
indexers.put(path, indexer);
}
}
return indexer;
} catch (IOException e) {
throw convertException(e);
}
}
private static String getIndexPath(Connection conn) throws SQLException {
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery("CALL DATABASE_PATH()");
rs.next();
String path = rs.getString(1);
if (path == null) {
throw new SQLException("FULLTEXT", "Fulltext search for in-memory databases is not supported.");
}
rs.close();
return path;
}
/**
* INTERNAL
*/
public void init(Connection conn, String schemaName, String triggerName, String tableName) throws SQLException {
init(conn);
this.schemaName = schemaName;
this.tableName = tableName;
this.indexer = getIndexModifier(conn);
ArrayList keyList = new ArrayList();
DatabaseMetaData meta = conn.getMetaData();
ResultSet rs = meta.getColumns(null, schemaName, tableName, null);
ArrayList columnList = new ArrayList();
while (rs.next()) {
columnList.add(rs.getString("COLUMN_NAME"));
}
dataTypes = new int[columnList.size()];
columnNames = new String[columnList.size()];
columnList.toArray(columnNames);
rs = meta.getColumns(null, schemaName, tableName, null);
for (int i = 0; rs.next(); i++) {
dataTypes[i] = rs.getInt("DATA_TYPE");
}
if (keyList.size() == 0) {
rs = meta.getPrimaryKeys(null, schemaName, tableName);
while (rs.next()) {
keyList.add(rs.getString("COLUMN_NAME"));
}
}
if (keyList.size() == 0) {
throw new SQLException("No primary key for table " + tableName);
}
ArrayList indexList = new ArrayList();
PreparedStatement prep = conn.prepareStatement("SELECT COLUMNS FROM "+SCHEMA+".INDEXES WHERE SCHEMA=? AND TABLE=?");
prep.setString(1, schemaName);
prep.setString(2, tableName);
rs = prep.executeQuery();
if (rs.next()) {
String columns = rs.getString(1);
if (columns != null) {
String[] list = StringUtils.arraySplit(columns, ',', true);
for (int i = 0; i < list.length; i++) {
indexList.add(list[i]);
}
}
}
if (indexList.size() == 0) {
indexList.addAll(columnList);
}
keys = new int[keyList.size()];
setColumns(keys, keyList, columnList);
indexColumns = new int[indexList.size()];
setColumns(indexColumns, indexList, columnList);
}
private void setColumns(int[] index, ArrayList keys, ArrayList columns) throws SQLException {
for (int i = 0; i < keys.size(); i++) {
String key = (String) keys.get(i);
int found = -1;
for (int j = 0; found == -1 && j < columns.size(); j++) {
String column = (String) columns.get(j);
if (column.equals(key)) {
found = j;
}
}
if (found < 0) {
throw new SQLException("FULLTEXT", "Column not found: " + key);
}
index[i] = found;
}
}
/**
* INTERNAL
*/
public void fire(Connection conn, Object[] oldRow, Object[] newRow) throws SQLException {
if (oldRow != null) {
delete(oldRow);
}
if (newRow != null) {
insert(newRow);
}
}
private String getQuery(Object[] row) throws SQLException {
StringBuffer buff = new StringBuffer();
if (schemaName != null) {
buff.append(StringUtils.quoteIdentifier(schemaName));
buff.append(".");
}
buff.append(StringUtils.quoteIdentifier(tableName));
buff.append(" WHERE ");
for (int i = 0; i < keys.length; i++) {
if (i > 0) {
buff.append(" AND ");
}
int columnIndex = keys[i];
buff.append(StringUtils.quoteIdentifier(columnNames[columnIndex]));
Object o = row[columnIndex];
if (o == null) {
buff.append(" IS NULL");
} else {
buff.append("=");
buff.append(quoteSQL(o, dataTypes[columnIndex]));
}
}
String key = buff.toString();
return key;
}
private String quoteString(String data) {
if (data.indexOf('\'') < 0) {
return "'" + data + "'";
}
StringBuffer buff = new StringBuffer(data.length() + 2);
buff.append('\'');
for (int i = 0; i < data.length(); i++) {
char ch = data.charAt(i);
if (ch == '\'') {
buff.append(ch);
}
buff.append(ch);
}
buff.append('\'');
return buff.toString();
}
private String quoteBinary(byte[] data) {
return "'" + ByteUtils.convertBytesToString(data) + "'";
}
private String asString(Object data, int type) throws SQLException {
if (data == null) {
return "NULL";
}
switch (type) {
case Types.BIT:
case Types.BOOLEAN:
case Types.INTEGER:
case Types.BIGINT:
case Types.DECIMAL:
case Types.DOUBLE:
case Types.FLOAT:
case Types.NUMERIC:
case Types.REAL:
case Types.SMALLINT:
case Types.TINYINT:
case Types.DATE:
case Types.TIME:
case Types.TIMESTAMP:
case Types.LONGVARCHAR:
case Types.CHAR:
case Types.VARCHAR:
return data.toString();
case Types.VARBINARY:
case Types.LONGVARBINARY:
case Types.BINARY:
case Types.JAVA_OBJECT:
case Types.CLOB:
case Types.OTHER:
case Types.BLOB:
case Types.STRUCT:
case Types.REF:
case Types.NULL:
case Types.ARRAY:
case Types.DATALINK:
case Types.DISTINCT:
throw new SQLException("FULLTEXT", "Unsupported column data type: " + type);
}
return "";
}
private String quoteSQL(Object data, int type) throws SQLException {
if (data == null) {
return "NULL";
}
switch (type) {
case Types.BIT:
case Types.BOOLEAN:
case Types.INTEGER:
case Types.BIGINT:
case Types.DECIMAL:
case Types.DOUBLE:
case Types.FLOAT:
case Types.NUMERIC:
case Types.REAL:
case Types.SMALLINT:
case Types.TINYINT:
return data.toString();
case Types.DATE:
case Types.TIME:
case Types.TIMESTAMP:
case Types.LONGVARCHAR:
case Types.CHAR:
case Types.VARCHAR:
return quoteString(data.toString());
case Types.VARBINARY:
case Types.LONGVARBINARY:
case Types.BINARY:
return quoteBinary((byte[]) data);
case Types.JAVA_OBJECT:
case Types.CLOB:
case Types.OTHER:
case Types.BLOB:
case Types.STRUCT:
case Types.REF:
case Types.NULL:
case Types.ARRAY:
case Types.DATALINK:
case Types.DISTINCT:
throw new SQLException("FULLTEXT", "Unsupported key data type: " + type);
}
return "";
}
private void insert(Object[] row) throws SQLException {
String query = getQuery(row);
Document doc = new Document();
doc.add(new Field(FIELD_QUERY, query, Field.Store.YES, Field.Index.UN_TOKENIZED));
long time = System.currentTimeMillis();
doc.add(new Field("modified", DateTools.timeToString(time, DateTools.Resolution.SECOND), Field.Store.YES, Field.Index.UN_TOKENIZED));
StringBuffer allData = new StringBuffer();
for (int i = 0; i < indexColumns.length; i++) {
int index = indexColumns[i];
String columnName = columnNames[index];
String data = asString(row[index], dataTypes[index]);
doc.add(new Field(FIELD_COLUMN_PREFIX + columnName, data, Field.Store.NO, Field.Index.TOKENIZED));
if (i > 0) {
allData.append(" ");
}
allData.append(data);
}
doc.add(new Field(FIELD_DATA, allData.toString(), Field.Store.NO, Field.Index.TOKENIZED));
try {
indexer.addDocument(doc);
} catch (IOException e) {
throw convertException(e);
}
}
private void delete(Object[] row) throws SQLException {
String query = getQuery(row);
try {
Term term = new Term(FIELD_QUERY, query);
indexer.deleteDocuments(term);
} catch (IOException e) {
throw convertException(e);
}
}
private static SQLException convertException(Exception e) {
SQLException e2 = new SQLException("FULLTEXT", "Error while indexing document");
e2.initCause(e);
return e2;
}
/**
* Searches from the full text index for this database.
*
* @param conn the connection
* @param text the search query
* @param limit the maximum number of rows or 0 for no limit
* @param offset the offset or 0 for no offset
* @return the result set
*/
public static ResultSet search(Connection conn, String text, int limit, int offset) throws SQLException {
SimpleResultSet rs = new SimpleResultSet();
rs.addColumn(FIELD_QUERY, Types.VARCHAR, 0, 0);
if (text == null) {
// this is just to query the result set columns
return rs;
}
String path = getIndexPath(conn);
try {
IndexModifier indexer = getIndexModifier(conn);
indexer.flush();
IndexReader reader = IndexReader.open(path);
Analyzer analyzer = new StandardAnalyzer();
Searcher searcher = new IndexSearcher(reader);
QueryParser parser = new QueryParser(FIELD_DATA, analyzer);
Query query = parser.parse(text);
Hits hits = searcher.search(query);
int max = hits.length();
if (limit == 0) {
limit = max;
}
for (int i = 0; i < limit && i + offset < max; i++) {
Document doc = hits.doc(i + offset);
String q = doc.get(FIELD_QUERY);
rs.addRow(new Object[] { q });
}
// TODO keep it open if possible
reader.close();
} catch (Exception e) {
throw convertException(e);
}
return rs;
}
}
......@@ -2108,6 +2108,7 @@ INSTR(string, searchString, [, startInt]): int
","
Returns the location of a search string in a string (s).
If a start position is used, the characters before it are ignored.
If position is negative, the rightmost location is returned.
0 is returned if the search string is not found.
","
INSTR(EMAIL,'@')
......@@ -2154,6 +2155,7 @@ LOCATE(searchString, string [, startInt]): int
","
Returns the location of a search string in a string (s).
If a start position is used, the characters before it are ignored.
If position is negative, the rightmost location is returned.
0 is returned if the search string is not found.
","
LOCATE('.', NAME)
......
......@@ -5,7 +5,6 @@
package org.h2.server.pg;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;
......@@ -159,7 +158,7 @@ public class PgServer implements Service {
return false;
}
try {
Socket s = NetUtils.createSocket(InetAddress.getLocalHost(), serverSocket.getLocalPort(), false);
Socket s = NetUtils.createLoopbackSocket(serverSocket.getLocalPort(), false);
s.close();
return true;
} catch (Exception e) {
......
......@@ -34,11 +34,11 @@ public class FileLister {
* @throws SQLException
*/
public static ArrayList getDatabaseFiles(String dir, String db, boolean all) throws SQLException {
dir = FileUtils.normalize(dir);
ArrayList files = new ArrayList();
if (dir == null || dir.equals("")) {
dir = ".";
}
dir = FileUtils.normalize(dir);
ArrayList files = new ArrayList();
String start = db == null ? null : FileUtils.normalize(dir + "/" + db);
String[] list = FileUtils.listFiles(dir);
for (int i = 0; list != null && i < list.length; i++) {
......
......@@ -8,12 +8,12 @@ import java.io.IOException;
import java.io.InputStream;
public class FileInputStream extends InputStream {
public class FileObjectInputStream extends InputStream {
private FileObject file;
private byte[] buffer = new byte[1];
FileInputStream(FileObject file) {
FileObjectInputStream(FileObject file) {
this.file = file;
}
......
......@@ -7,12 +7,12 @@ package org.h2.store.fs;
import java.io.IOException;
import java.io.OutputStream;
public class FileOutputStream extends OutputStream {
public class FileObjectOutputStream extends OutputStream {
private FileObject file;
private byte[] buffer = new byte[1];
FileOutputStream(FileObject file, boolean append) throws IOException {
FileObjectOutputStream(FileObject file, boolean append) throws IOException {
this.file = file;
if (append) {
file.seek(file.length());
......
/*
* Copyright 2004-2007 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.store.fs;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class FileObjectZip implements FileObject {
private ZipFile file;
private ZipEntry entry;
private long pos;
private InputStream in;
private long inPos;
private long length;
public FileObjectZip(ZipFile file, ZipEntry entry) {
this.file = file;
this.entry = entry;
length = entry.getSize();
}
public void close() throws IOException {
}
public long getFilePointer() throws IOException {
return pos;
}
public long length() throws IOException {
return length;
}
public void readFully(byte[] b, int off, int len) throws IOException {
if (inPos > pos) {
if (in != null) {
in.close();
}
in = null;
}
if (in == null) {
in = file.getInputStream(entry);
inPos = 0;
}
if (inPos < pos) {
in.skip(pos - inPos);
inPos = pos;
}
in.read(b, off, len);
pos += len;
inPos += len;
}
public void seek(long pos) throws IOException {
this.pos = pos;
}
public void setLength(long newLength) throws IOException {
throw new IOException("File is read-only");
}
public void sync() throws IOException {
}
public void write(byte[] b, int off, int len) throws IOException {
throw new IOException("File is read-only");
}
}
......@@ -15,18 +15,21 @@ public abstract class FileSystem {
public static final String MEMORY_PREFIX = "memFS:";
public static final String MEMORY_PREFIX_LZF = "memLZF:";
public static final String DB_PREFIX = "jdbc:";
public static final String ZIP_PREFIX = "zip:";
public static FileSystem getInstance(String fileName) {
if (isInMemory(fileName)) {
return FileSystemMemory.getInstance();
} else if (fileName.startsWith(DB_PREFIX)) {
return FileSystemDatabase.getInstance(fileName);
} else if (fileName.startsWith(ZIP_PREFIX)) {
return FileSystemZip.getInstance();
}
return FileSystemDisk.getInstance();
}
private static boolean isInMemory(String fileName) {
return fileName != null && fileName.startsWith(MEMORY_PREFIX) || fileName.startsWith(MEMORY_PREFIX_LZF);
return fileName != null && (fileName.startsWith(MEMORY_PREFIX) || fileName.startsWith(MEMORY_PREFIX_LZF));
}
public abstract long length(String fileName);
......
......@@ -357,7 +357,7 @@ public class FileSystemDatabase extends FileSystem {
}
public InputStream openFileInputStream(String fileName) throws IOException {
return new FileInputStream(openFileObject(fileName, "r"));
return new FileObjectInputStream(openFileObject(fileName, "r"));
}
public FileObject openFileObject(String fileName, String mode) throws IOException {
......@@ -382,7 +382,7 @@ public class FileSystemDatabase extends FileSystem {
public OutputStream openFileOutputStream(String fileName, boolean append) throws SQLException {
try {
return new FileOutputStream(openFileObject(fileName, "rw"), append);
return new FileObjectOutputStream(openFileObject(fileName, "rw"), append);
} catch (IOException e) {
throw Message.convertIOException(e, fileName);
}
......
......@@ -148,14 +148,14 @@ public class FileSystemMemory extends FileSystem {
public OutputStream openFileOutputStream(String fileName, boolean append) throws SQLException {
try {
return new FileOutputStream(getMemoryFile(fileName), append);
return new FileObjectOutputStream(getMemoryFile(fileName), append);
} catch (IOException e) {
throw Message.convertIOException(e, fileName);
}
}
public InputStream openFileInputStream(String fileName) throws IOException {
return new FileInputStream(getMemoryFile(fileName));
return new FileObjectInputStream(getMemoryFile(fileName));
}
public FileObject openFileObject(String fileName, String mode) throws IOException {
......
/*
* Copyright 2004-2007 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.store.fs;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.h2.message.Message;
public class FileSystemZip extends FileSystem {
private static final FileSystemZip INSTANCE = new FileSystemZip();
private FileSystemZip() {
}
public static FileSystemZip getInstance() {
return INSTANCE;
}
public boolean canWrite(String fileName) {
return false;
}
public void copy(String original, String copy) throws SQLException {
throw Message.getUnsupportedException();
}
public void createDirs(String fileName) throws SQLException {
// ignore
}
public boolean createNewFile(String fileName) throws SQLException {
throw Message.getUnsupportedException();
}
public String createTempFile(String prefix, String suffix, boolean deleteOnExit, boolean inTempDir) throws IOException {
throw new IOException("File system is read-only");
}
public void delete(String fileName) throws SQLException {
throw Message.getUnsupportedException();
}
public void deleteRecursive(String fileName) throws SQLException {
throw Message.getUnsupportedException();
}
public boolean exists(String fileName) {
try {
String entryName = getEntryName(fileName);
if (entryName.length() == 0) {
return true;
}
ZipFile file = openZipFile(fileName);
return file.getEntry(entryName) != null;
} catch (IOException e) {
return false;
}
}
public boolean fileStartsWith(String fileName, String prefix) {
return fileName.startsWith(prefix);
}
public String getAbsolutePath(String fileName) {
return fileName;
}
public String getFileName(String name) throws SQLException {
name = getEntryName(name);
if (name.endsWith("/")) {
name = name.substring(0, name.length() - 1);
}
int idx = name.lastIndexOf('/');
if (idx >= 0) {
name = name.substring(idx + 1);
}
return name;
}
public long getLastModified(String fileName) {
return 0;
}
public String getParent(String fileName) {
int idx = fileName.lastIndexOf('/');
if (idx > 0) {
fileName = fileName.substring(0, idx);
}
return fileName;
}
public boolean isAbsolute(String fileName) {
return true;
}
public boolean isDirectory(String fileName) {
try {
String entryName = getEntryName(fileName);
if (entryName.length() == 0) {
return true;
}
ZipFile file = openZipFile(fileName);
Enumeration en = file.entries();
while (en.hasMoreElements()) {
ZipEntry entry = (ZipEntry) en.nextElement();
String n = entry.getName();
if (n.equals(entryName)) {
return entry.isDirectory();
} else if (n.startsWith(entryName)) {
if (n.length() == entryName.length() + 1) {
if (n.equals(entryName + "/")) {
return true;
}
}
}
}
return false;
} catch (IOException e) {
return false;
}
}
public boolean isReadOnly(String fileName) {
return true;
}
public long length(String fileName) {
try {
ZipFile file = openZipFile(fileName);
ZipEntry entry = file.getEntry(getEntryName(fileName));
return entry == null ? 0 : entry.getSize();
} catch (IOException e) {
return 0;
}
}
public String[] listFiles(String path) throws SQLException {
try {
if (!path.endsWith("/")) {
path += "/";
}
ZipFile file = openZipFile(path);
String dirName = getEntryName(path);
String prefix = path.substring(0, path.length() - dirName.length());
Enumeration en = file.entries();
ArrayList list = new ArrayList();
while (en.hasMoreElements()) {
ZipEntry entry = (ZipEntry) en.nextElement();
String name = entry.getName();
if (!name.startsWith(dirName)) {
continue;
}
if (name.length() <= dirName.length()) {
continue;
}
int idx = name.indexOf('/', dirName.length());
if (idx < 0 || idx >= name.length() - 1) {
list.add(prefix + name);
}
}
String[] result = new String[list.size()];
list.toArray(result);
return result;
} catch (IOException e) {
throw Message.convertIOException(e, "listFiles " + path);
}
}
public String normalize(String fileName) throws SQLException {
return fileName;
}
public InputStream openFileInputStream(String fileName) throws IOException {
FileObject file = openFileObject(fileName, "r");
return new FileObjectInputStream(file);
}
public FileObject openFileObject(String fileName, String mode) throws IOException {
ZipFile file = openZipFile(translateFileName(fileName));
ZipEntry entry = file.getEntry(getEntryName(fileName));
return new FileObjectZip(file, entry);
}
public OutputStream openFileOutputStream(String fileName, boolean append) throws SQLException {
throw Message.getUnsupportedException();
}
public void rename(String oldName, String newName) throws SQLException {
throw Message.getUnsupportedException();
}
public boolean tryDelete(String fileName) {
return false;
}
private String translateFileName(String fileName) {
if (fileName.startsWith(FileSystem.ZIP_PREFIX)) {
fileName = fileName.substring(FileSystem.ZIP_PREFIX.length());
}
int idx = fileName.indexOf('!');
if (idx >= 0) {
fileName = fileName.substring(0, idx);
}
return fileName;
}
private String getEntryName(String fileName) {
int idx = fileName.indexOf('!');
if (idx <= 0) {
fileName = "";
} else {
fileName = fileName.substring(idx + 1);
}
if (fileName.startsWith("/")) {
fileName = fileName.substring(1);
}
return fileName;
}
private ZipFile openZipFile(String fileName) throws IOException {
fileName = translateFileName(fileName);
return new ZipFile(fileName);
}
}
......@@ -316,11 +316,17 @@ public class TableData extends Table implements RecordReader {
if (lockExclusive == session) {
return;
}
if (exclusive) {
if (lockExclusive == null) {
if (!force && database.isMultiVersion()) {
// MVCC: update, delete, and insert use a shared lock
// but select doesn't lock
if (exclusive) {
exclusive = false;
} else {
return;
}
}
if (exclusive) {
if (lockExclusive == null) {
if (lockShared.isEmpty()) {
traceLock(session, exclusive, "added for");
session.addLock(this);
......@@ -333,11 +339,8 @@ public class TableData extends Table implements RecordReader {
}
}
} else {
if (!force && database.isMultiVersion()) {
return;
}
if (lockExclusive == null) {
if (lockMode == Constants.LOCK_MODE_READ_COMMITTED && !SysProperties.multiThreadedKernel) {
if (lockMode == Constants.LOCK_MODE_READ_COMMITTED && !SysProperties.multiThreadedKernel && !database.isMultiVersion()) {
// READ_COMMITTED read locks are acquired but they
// are released immediately
// when allowing only one thread, no read locks are
......
......@@ -17,18 +17,41 @@ import org.h2.security.SecureSocketFactory;
public class NetUtils {
public static Socket createLoopbackSocket(int port, boolean ssl) throws IOException, SQLException {
InetAddress address = InetAddress.getByName("127.0.0.1");
public static Socket createLoopbackSocket(int port, boolean ssl) throws SQLException {
return createSocket("127.0.0.1", port, ssl);
}
public static Socket createSocket(String server, int defaultPort, boolean ssl) throws SQLException {
int port = defaultPort;
// IPv6: RFC 2732 format is '[a:b:c:d:e:f:g:h]' or
// '[a:b:c:d:e:f:g:h]:port'
// RFC 2396 format is 'a.b.c.d' or 'a.b.c.d:port' or 'hostname' or
// 'hostname:port'
int startIndex = server.startsWith("[") ? server.indexOf(']') : 0;
int idx = server.indexOf(':', startIndex);
if (idx >= 0) {
port = MathUtils.decodeInt(server.substring(idx + 1));
server = server.substring(0, idx);
}
try {
InetAddress address = InetAddress.getByName(server);
return createSocket(address, port, ssl);
} catch (IOException e) {
throw Message.convert(e);
}
}
public static Socket createSocket(InetAddress address, int port, boolean ssl) throws IOException, SQLException {
public static Socket createSocket(InetAddress address, int port, boolean ssl) throws SQLException {
try {
if (ssl) {
SecureSocketFactory f = SecureSocketFactory.getInstance();
return f.createSocket(address, port);
} else {
return new Socket(address, port);
}
} catch (IOException e) {
throw Message.convert(e);
}
}
public static ServerSocket createServerSocket(int port, boolean ssl) throws SQLException {
......
......@@ -142,7 +142,17 @@ java org.h2.test.TestAll timer
/*
TODO history:
Math operations using unknown data types (for example -? and ?+?) are now interpreted as decimal.
INSTR, LOCATE: backward searching is not supported by using a negative start position
TODO doc:
MVCC still locks the table exclusively when adding or removing columns and when dropping the table.
Also, a shared lock is still added when inserting or removing rows.
replicating file system
background thread writing file system (all writes)
test DbStarter
......@@ -519,8 +529,8 @@ write tests using the PostgreSQL JDBC driver
System.out.println("test big:"+big+" net:"+networked+" cipher:"+cipher+" memory:"+memory+" log:"+logMode+" diskResult:"+diskResult + " mvcc:" + mvcc);
beforeTest();
// int testMvcc;
// mvcc = true;
// int testMvcc;
// mvcc = true;
// db
new TestScriptSimple().runTest(this);
......
......@@ -132,6 +132,13 @@ public class TestPreparedStatement extends TestBase {
} catch (SQLException e) {
// expected
}
PreparedStatement prep = conn.prepareStatement("SELECT -?");
prep.setInt(1, 1);
prep.execute();
prep = conn.prepareStatement("SELECT ?-?");
prep.setInt(1, 1);
prep.setInt(2, 2);
prep.execute();
}
private void testCancelReuse(Connection conn) throws Exception {
......
......@@ -47,6 +47,19 @@ public class TestMVCC extends TestBase {
c1.setAutoCommit(false);
c2.setAutoCommit(false);
s1.execute("create table test(id int primary key)");
s1.execute("insert into test values(1)");
try {
s2.execute("drop table test");
error("Unexpected success");
} catch (SQLException e) {
// lock timeout expected
checkNotGeneralException(e);
}
c1.rollback();
s2.execute("drop table test");
c2.rollback();
s1.execute("create table test(id int primary key, name varchar(255))");
s2.execute("insert into test values(4, 'Hello')");
c2.rollback();
......@@ -176,6 +189,7 @@ public class TestMVCC extends TestBase {
s1.execute("SELECT * FROM TEST ORDER BY ID");
s2.execute("SELECT * FROM TEST ORDER BY ID");
}
c2.rollback();
s1.execute("DROP TABLE TEST");
c1.commit();
c2.commit();
......@@ -215,6 +229,7 @@ public class TestMVCC extends TestBase {
s1.execute("SELECT * FROM TEST ORDER BY ID");
s2.execute("SELECT * FROM TEST ORDER BY ID");
}
c2.rollback();
s1.execute("DROP TABLE TEST");
c1.commit();
c2.commit();
......
select instr('asgisj','s', -1) from dual;
> 5;
CREATE TABLE TEST(ID INT);
INSERT INTO TEST VALUES(1), (2), (3);
create index idx_desc on test(id desc);
......
package org.h2.test.unit;
import java.sql.SQLException;
import org.h2.test.TestBase;
import org.h2.tools.Server;
public class TestFtp extends TestBase {
public void test() throws Exception {
test(baseDir);
}
private void test(String dir) throws SQLException {
Server server = Server.createFtpServer(new String[]{"-ftpDir", dir}).start();
server.stop();
}
}
package org.h2.tools.ftp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.sql.SQLException;
import java.util.ArrayList;
import org.h2.engine.Constants;
import org.h2.util.NetUtils;
public class FtpClient {
private Socket socket;
private BufferedReader reader;
private PrintWriter writer;
private int code;
private String message;
public static FtpClient open(String url) throws SQLException, IOException {
FtpClient client = new FtpClient();
client.connect(url);
return client;
}
private FtpClient() {
}
private void connect(String url) throws SQLException, IOException {
socket = NetUtils.createSocket(url, 21, false);
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
reader = new BufferedReader(new InputStreamReader(in));
writer = new PrintWriter(new OutputStreamWriter(out, Constants.UTF8));
readCode(220);
}
private void readLine() throws IOException {
message = reader.readLine();
int idx = message.indexOf(' ');
if (idx < 0) {
code = 0;
} else {
code = Integer.parseInt(message.substring(0, idx));
}
}
private void readCode(int expected) throws IOException {
readLine();
if (code != expected) {
throw new IOException("Expected: " + expected + " got: " + message);
}
}
private void send(String command) throws IOException {
writer.println(command);
writer.flush();
}
public void sendUser(String userName) throws IOException {
send("USER " + userName);
readCode(331);
}
public void sendQuit() throws IOException {
send("QUIT");
readCode(221);
}
public void sendPassword(String password) throws IOException {
send("PASS " + password);
readCode(230);
}
public void sendChangeWorkingDirectory(String dir) throws IOException {
send("CWD " + dir);
readCode(250);
}
public void sendChangeDirectoryUp() throws IOException {
send("CDUP");
readCode(250);
}
public void sendDelete(String fileName) throws IOException {
send("DELE " + fileName);
readCode(250);
}
public void sendMakeDirectory(String dir) throws IOException {
}
public void sendMode(String dir) throws IOException {
}
public void sendModifiedTime(String dir) throws IOException {
}
public void sendNameList(String dir) throws IOException {
}
public void sendRenameFrom(String dir) throws IOException {
}
public String[] sendList(String dir) throws IOException {
send("LIST " + dir);
readCode(250);
ArrayList list = new ArrayList();
String[] result = new String[list.size()];
list.toArray(result);
return result;
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论