提交 a674f921 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Merge branch 'master' into generated_keys

......@@ -120,7 +120,7 @@ public class AlterTableAddConstraint extends SchemaCommand {
ArrayList<Constraint> constraints = table.getConstraints();
for (int i = 0; constraints != null && i < constraints.size(); i++) {
Constraint c = constraints.get(i);
if (Constraint.PRIMARY_KEY.equals(c.getConstraintType())) {
if (Constraint.Type.PRIMARY_KEY == c.getConstraintType()) {
throw DbException.get(ErrorCode.SECOND_PRIMARY_KEY);
}
}
......
......@@ -57,7 +57,7 @@ public class DropIndex extends SchemaCommand {
Constraint cons = constraints.get(i);
if (cons.usesIndex(index)) {
// can drop primary key index (for compatibility)
if (Constraint.PRIMARY_KEY.equals(cons.getConstraintType())) {
if (Constraint.Type.PRIMARY_KEY == cons.getConstraintType()) {
pkConstraint = cons;
} else {
throw DbException.get(
......
......@@ -64,40 +64,40 @@ public class BackupCommand extends Prepared {
}
String name = db.getName();
name = FileUtils.getName(name);
OutputStream zip = FileUtils.newOutputStream(fileName, false);
ZipOutputStream out = new ZipOutputStream(zip);
db.flush();
if (db.getPageStore() != null) {
String fn = db.getName() + Constants.SUFFIX_PAGE_FILE;
backupPageStore(out, fn, db.getPageStore());
}
// synchronize on the database, to avoid concurrent temp file
// creation / deletion / backup
String base = FileUtils.getParent(db.getName());
synchronized (db.getLobSyncObject()) {
String prefix = db.getDatabasePath();
String dir = FileUtils.getParent(prefix);
dir = FileLister.getDir(dir);
ArrayList<String> fileList = FileLister.getDatabaseFiles(dir, name, true);
for (String n : fileList) {
if (n.endsWith(Constants.SUFFIX_LOB_FILE)) {
backupFile(out, base, n);
}
if (n.endsWith(Constants.SUFFIX_MV_FILE) && mvStore != null) {
MVStore s = mvStore.getStore();
boolean before = s.getReuseSpace();
s.setReuseSpace(false);
try {
InputStream in = mvStore.getInputStream();
backupFile(out, base, n, in);
} finally {
s.setReuseSpace(before);
try (OutputStream zip = FileUtils.newOutputStream(fileName, false)) {
ZipOutputStream out = new ZipOutputStream(zip);
db.flush();
if (db.getPageStore() != null) {
String fn = db.getName() + Constants.SUFFIX_PAGE_FILE;
backupPageStore(out, fn, db.getPageStore());
}
// synchronize on the database, to avoid concurrent temp file
// creation / deletion / backup
String base = FileUtils.getParent(db.getName());
synchronized (db.getLobSyncObject()) {
String prefix = db.getDatabasePath();
String dir = FileUtils.getParent(prefix);
dir = FileLister.getDir(dir);
ArrayList<String> fileList = FileLister.getDatabaseFiles(dir, name, true);
for (String n : fileList) {
if (n.endsWith(Constants.SUFFIX_LOB_FILE)) {
backupFile(out, base, n);
}
if (n.endsWith(Constants.SUFFIX_MV_FILE) && mvStore != null) {
MVStore s = mvStore.getStore();
boolean before = s.getReuseSpace();
s.setReuseSpace(false);
try {
InputStream in = mvStore.getInputStream();
backupFile(out, base, n, in);
} finally {
s.setReuseSpace(before);
}
}
}
}
out.close();
}
out.close();
zip.close();
} catch (IOException e) {
throw DbException.convertIOException(e, fileName);
}
......
......@@ -284,8 +284,7 @@ public class ScriptCommand extends ScriptBase {
final ArrayList<Constraint> constraints = table.getConstraints();
if (constraints != null) {
for (Constraint constraint : constraints) {
if (Constraint.PRIMARY_KEY.equals(
constraint.getConstraintType())) {
if (Constraint.Type.PRIMARY_KEY == constraint.getConstraintType()) {
add(constraint.getCreateSQLWithoutIndexes(), false);
}
}
......@@ -336,7 +335,7 @@ public class ScriptCommand extends ScriptBase {
if (constraint.getTable().isHidden()) {
continue;
}
if (!Constraint.PRIMARY_KEY.equals(constraint.getConstraintType())) {
if (Constraint.Type.PRIMARY_KEY != constraint.getConstraintType()) {
add(constraint.getCreateSQLWithoutIndexes(), false);
}
}
......
......@@ -24,25 +24,24 @@ import org.h2.table.Table;
public abstract class Constraint extends SchemaObjectBase implements
Comparable<Constraint> {
/**
* The constraint type name for check constraints.
*/
public static final String CHECK = "CHECK";
/**
* The constraint type name for referential constraints.
*/
public static final String REFERENTIAL = "REFERENTIAL";
/**
* The constraint type name for unique constraints.
*/
public static final String UNIQUE = "UNIQUE";
/**
* The constraint type name for primary key constraints.
*/
public static final String PRIMARY_KEY = "PRIMARY KEY";
public enum Type {
/**
* The constraint type for check constraints.
*/
CHECK,
/**
* The constraint type for primary key constraints.
*/
PRIMARY_KEY,
/**
* The constraint type for unique constraints.
*/
UNIQUE,
/**
* The constraint type for referential constraints.
*/
REFERENTIAL
}
/**
* The table for which this constraint is defined.
......@@ -60,7 +59,7 @@ public abstract class Constraint extends SchemaObjectBase implements
*
* @return the name
*/
public abstract String getConstraintType();
public abstract Type getConstraintType();
/**
* Check if this row fulfils the constraint.
......@@ -155,29 +154,12 @@ public abstract class Constraint extends SchemaObjectBase implements
return null;
}
private int getConstraintTypeOrder() {
String constraintType = getConstraintType();
if (CHECK.equals(constraintType)) {
return 0;
} else if (PRIMARY_KEY.equals(constraintType)) {
return 1;
} else if (UNIQUE.equals(constraintType)) {
return 2;
} else if (REFERENTIAL.equals(constraintType)) {
return 3;
} else {
throw DbException.throwInternalError("type: " + constraintType);
}
}
@Override
public int compareTo(Constraint other) {
if (this == other) {
return 0;
}
int thisType = getConstraintTypeOrder();
int otherType = other.getConstraintTypeOrder();
return thisType - otherType;
return Integer.compare(getConstraintType().ordinal(), other.getConstraintType().ordinal());
}
@Override
......
......@@ -36,8 +36,8 @@ public class ConstraintCheck extends Constraint {
}
@Override
public String getConstraintType() {
return Constraint.CHECK;
public Type getConstraintType() {
return Constraint.Type.CHECK;
}
public void setTableFilter(TableFilter filter) {
......
......@@ -50,8 +50,8 @@ public class ConstraintReferential extends Constraint {
}
@Override
public String getConstraintType() {
return Constraint.REFERENTIAL;
public Type getConstraintType() {
return Constraint.Type.REFERENTIAL;
}
private static void appendAction(StatementBuilder buff, ConstraintActionType action) {
......
......@@ -34,8 +34,8 @@ public class ConstraintUnique extends Constraint {
}
@Override
public String getConstraintType() {
return primaryKey ? Constraint.PRIMARY_KEY : Constraint.UNIQUE;
public Type getConstraintType() {
return primaryKey ? Constraint.Type.PRIMARY_KEY : Constraint.Type.UNIQUE;
}
@Override
......
......@@ -467,7 +467,7 @@ public class MVTable extends TableBase {
if (constraints != null) {
for (int i = 0, size = constraints.size(); i < size; i++) {
Constraint c = constraints.get(i);
if (!(c.getConstraintType().equals(Constraint.REFERENTIAL))) {
if (c.getConstraintType() != Constraint.Type.REFERENTIAL) {
continue;
}
ConstraintReferential ref = (ConstraintReferential) c;
......
......@@ -868,8 +868,7 @@ public class MetaTable extends Table {
Constraint constraint = constraints.get(k);
if (constraint.usesIndex(index)) {
if (index.getIndexType().isPrimaryKey()) {
if (constraint.getConstraintType().equals(
Constraint.PRIMARY_KEY)) {
if (constraint.getConstraintType() == Constraint.Type.PRIMARY_KEY) {
constraintName = constraint.getName();
}
} else {
......@@ -1544,7 +1543,7 @@ public class MetaTable extends Table {
for (SchemaObject obj : database.getAllSchemaObjects(
DbObject.CONSTRAINT)) {
Constraint constraint = (Constraint) obj;
if (!(constraint.getConstraintType().equals(Constraint.REFERENTIAL))) {
if (constraint.getConstraintType() != Constraint.Type.REFERENTIAL) {
continue;
}
ConstraintReferential ref = (ConstraintReferential) constraint;
......@@ -1597,7 +1596,7 @@ public class MetaTable extends Table {
for (SchemaObject obj : database.getAllSchemaObjects(
DbObject.CONSTRAINT)) {
Constraint constraint = (Constraint) obj;
String constraintType = constraint.getConstraintType();
Constraint.Type constraintType = constraint.getConstraintType();
String checkExpression = null;
IndexColumn[] indexColumns = null;
Table table = constraint.getTable();
......@@ -1613,12 +1612,12 @@ public class MetaTable extends Table {
if (!checkIndex(session, tableName, indexFrom, indexTo)) {
continue;
}
if (constraintType.equals(Constraint.CHECK)) {
if (constraintType == Constraint.Type.CHECK) {
checkExpression = ((ConstraintCheck) constraint).getExpression().getSQL();
} else if (constraintType.equals(Constraint.UNIQUE) ||
constraintType.equals(Constraint.PRIMARY_KEY)) {
} else if (constraintType == Constraint.Type.UNIQUE ||
constraintType == Constraint.Type.PRIMARY_KEY) {
indexColumns = ((ConstraintUnique) constraint).getColumns();
} else if (constraintType.equals(Constraint.REFERENTIAL)) {
} else if (constraintType == Constraint.Type.REFERENTIAL) {
indexColumns = ((ConstraintReferential) constraint).getColumns();
}
String columnList = null;
......@@ -1638,7 +1637,7 @@ public class MetaTable extends Table {
// CONSTRAINT_NAME
identifier(constraint.getName()),
// CONSTRAINT_TYPE
constraintType,
constraintType.toString(),
// TABLE_CATALOG
catalog,
// TABLE_SCHEMA
......
......@@ -746,7 +746,7 @@ public class RegularTable extends TableBase {
if (constraints != null) {
for (int i = 0, size = constraints.size(); i < size; i++) {
Constraint c = constraints.get(i);
if (!(c.getConstraintType().equals(Constraint.REFERENTIAL))) {
if (c.getConstraintType() != Constraint.Type.REFERENTIAL) {
continue;
}
ConstraintReferential ref = (ConstraintReferential) c;
......
......@@ -128,47 +128,47 @@ public class Backup extends Tool {
OutputStream fileOut = null;
try {
fileOut = FileUtils.newOutputStream(zipFileName, false);
ZipOutputStream zipOut = new ZipOutputStream(fileOut);
String base = "";
for (String fileName : list) {
if (allFiles ||
fileName.endsWith(Constants.SUFFIX_PAGE_FILE) ||
fileName.endsWith(Constants.SUFFIX_MV_FILE)) {
base = FileUtils.getParent(fileName);
break;
try (ZipOutputStream zipOut = new ZipOutputStream(fileOut)) {
String base = "";
for (String fileName : list) {
if (allFiles ||
fileName.endsWith(Constants.SUFFIX_PAGE_FILE) ||
fileName.endsWith(Constants.SUFFIX_MV_FILE)) {
base = FileUtils.getParent(fileName);
break;
}
}
}
for (String fileName : list) {
String f = FileUtils.toRealPath(fileName);
if (!f.startsWith(base)) {
DbException.throwInternalError(f + " does not start with " + base);
}
if (f.endsWith(zipFileName)) {
continue;
}
if (FileUtils.isDirectory(fileName)) {
continue;
}
f = f.substring(base.length());
f = BackupCommand.correctFileName(f);
ZipEntry entry = new ZipEntry(f);
zipOut.putNextEntry(entry);
InputStream in = null;
try {
in = FileUtils.newInputStream(fileName);
IOUtils.copyAndCloseInput(in, zipOut);
} catch (FileNotFoundException e) {
// the file could have been deleted in the meantime
// ignore this (in this case an empty file is created)
} finally {
IOUtils.closeSilently(in);
}
zipOut.closeEntry();
if (!quiet) {
out.println("Processed: " + fileName);
for (String fileName : list) {
String f = FileUtils.toRealPath(fileName);
if (!f.startsWith(base)) {
DbException.throwInternalError(f + " does not start with " + base);
}
if (f.endsWith(zipFileName)) {
continue;
}
if (FileUtils.isDirectory(fileName)) {
continue;
}
f = f.substring(base.length());
f = BackupCommand.correctFileName(f);
ZipEntry entry = new ZipEntry(f);
zipOut.putNextEntry(entry);
InputStream in = null;
try {
in = FileUtils.newInputStream(fileName);
IOUtils.copyAndCloseInput(in, zipOut);
} catch (FileNotFoundException e) {
// the file could have been deleted in the meantime
// ignore this (in this case an empty file is created)
} finally {
IOUtils.closeSilently(in);
}
zipOut.closeEntry();
if (!quiet) {
out.println("Processed: " + fileName);
}
}
}
zipOut.close();
} catch (IOException e) {
throw DbException.convertIOException(e, zipFileName);
} finally {
......
......@@ -220,46 +220,40 @@ public class ChangeFileEncryption extends Tool {
if (FileUtils.isDirectory(fileName)) {
return;
}
FileChannel fileIn = FilePath.get(fileName).open("r");
FileChannel fileOut = null;
String temp = directory + "/temp.db";
try {
if (decryptKey != null) {
fileIn = new FilePathEncrypt.FileEncrypt(fileName, decryptKey, fileIn);
}
InputStream inStream = new FileChannelInputStream(fileIn, true);
FileUtils.delete(temp);
fileOut = FilePath.get(temp).open("rw");
if (encryptKey != null) {
fileOut = new FilePathEncrypt.FileEncrypt(temp, encryptKey, fileOut);
}
OutputStream outStream = new FileChannelOutputStream(fileOut, true);
byte[] buffer = new byte[4 * 1024];
long remaining = fileIn.size();
long total = remaining;
long time = System.nanoTime();
while (remaining > 0) {
if (!quiet && System.nanoTime() - time > TimeUnit.SECONDS.toNanos(1)) {
out.println(fileName + ": " + (100 - 100 * remaining / total) + "%");
time = System.nanoTime();
try (FileChannel fileIn = getFileChannel(fileName, "r", decryptKey)){
try(InputStream inStream = new FileChannelInputStream(fileIn, true)) {
FileUtils.delete(temp);
try (OutputStream outStream = new FileChannelOutputStream(getFileChannel(temp, "rw", encryptKey), true)) {
byte[] buffer = new byte[4 * 1024];
long remaining = fileIn.size();
long total = remaining;
long time = System.nanoTime();
while (remaining > 0) {
if (!quiet && System.nanoTime() - time > TimeUnit.SECONDS.toNanos(1)) {
out.println(fileName + ": " + (100 - 100 * remaining / total) + "%");
time = System.nanoTime();
}
int len = (int) Math.min(buffer.length, remaining);
len = inStream.read(buffer, 0, len);
outStream.write(buffer, 0, len);
remaining -= len;
}
}
int len = (int) Math.min(buffer.length, remaining);
len = inStream.read(buffer, 0, len);
outStream.write(buffer, 0, len);
remaining -= len;
}
inStream.close();
outStream.close();
} finally {
fileIn.close();
if (fileOut != null) {
fileOut.close();
}
}
FileUtils.delete(fileName);
FileUtils.move(temp, fileName);
}
private FileChannel getFileChannel(String fileName, String r, byte[] decryptKey) throws IOException {
FileChannel fileIn = FilePath.get(fileName).open(r);
if (decryptKey != null) {
fileIn = new FilePathEncrypt.FileEncrypt(fileName, decryptKey, fileIn);
}
return fileIn;
}
private void copy(String fileName, FileStore in, byte[] key, boolean quiet) {
if (FileUtils.isDirectory(fileName)) {
return;
......
......@@ -155,41 +155,41 @@ public class Restore extends Tool {
originalDbLen = originalDbName.length();
}
in = FileUtils.newInputStream(zipFileName);
ZipInputStream zipIn = new ZipInputStream(in);
while (true) {
ZipEntry entry = zipIn.getNextEntry();
if (entry == null) {
break;
}
String fileName = entry.getName();
// restoring windows backups on linux and vice versa
fileName = fileName.replace('\\', SysProperties.FILE_SEPARATOR.charAt(0));
fileName = fileName.replace('/', SysProperties.FILE_SEPARATOR.charAt(0));
if (fileName.startsWith(SysProperties.FILE_SEPARATOR)) {
fileName = fileName.substring(1);
}
boolean copy = false;
if (db == null) {
copy = true;
} else if (fileName.startsWith(originalDbName + ".")) {
fileName = db + fileName.substring(originalDbLen);
copy = true;
}
if (copy) {
OutputStream o = null;
try {
o = FileUtils.newOutputStream(
directory + SysProperties.FILE_SEPARATOR + fileName, false);
IOUtils.copy(zipIn, o);
o.close();
} finally {
IOUtils.closeSilently(o);
try (ZipInputStream zipIn = new ZipInputStream(in)) {
while (true) {
ZipEntry entry = zipIn.getNextEntry();
if (entry == null) {
break;
}
String fileName = entry.getName();
// restoring windows backups on linux and vice versa
fileName = fileName.replace('\\', SysProperties.FILE_SEPARATOR.charAt(0));
fileName = fileName.replace('/', SysProperties.FILE_SEPARATOR.charAt(0));
if (fileName.startsWith(SysProperties.FILE_SEPARATOR)) {
fileName = fileName.substring(1);
}
boolean copy = false;
if (db == null) {
copy = true;
} else if (fileName.startsWith(originalDbName + ".")) {
fileName = db + fileName.substring(originalDbLen);
copy = true;
}
if (copy) {
OutputStream o = null;
try {
o = FileUtils.newOutputStream(
directory + SysProperties.FILE_SEPARATOR + fileName, false);
IOUtils.copy(zipIn, o);
o.close();
} finally {
IOUtils.closeSilently(o);
}
}
zipIn.closeEntry();
}
zipIn.closeEntry();
}
zipIn.closeEntry();
zipIn.close();
} catch (IOException e) {
throw DbException.convertIOException(e, zipFileName);
} finally {
......
......@@ -167,25 +167,21 @@ public class Profiler implements Runnable {
continue;
}
String file = arg;
Reader reader;
LineNumberReader r;
reader = new InputStreamReader(
new FileInputStream(file), "CP1252");
r = new LineNumberReader(reader);
while (true) {
String line = r.readLine();
if (line == null) {
break;
} else if (line.startsWith("Full thread dump")) {
threadDumps++;
try (Reader reader = new InputStreamReader(new FileInputStream(file), "CP1252")) {
LineNumberReader r = new LineNumberReader(reader);
while (true) {
String line = r.readLine();
if (line == null) {
break;
} else if (line.startsWith("Full thread dump")) {
threadDumps++;
}
}
}
reader.close();
reader = new InputStreamReader(
new FileInputStream(file), "CP1252");
r = new LineNumberReader(reader);
processList(readStackTrace(r));
reader.close();
try (Reader reader = new InputStreamReader(new FileInputStream(file), "CP1252")) {
LineNumberReader r = new LineNumberReader(reader);
processList(readStackTrace(r));
}
}
System.out.println(getTopTraces(5));
} catch (IOException e) {
......
......@@ -112,17 +112,17 @@ public class SortedProperties extends Properties {
} catch (Exception e) {
throw new IOException(e.toString(), e);
}
PrintWriter writer = new PrintWriter(new BufferedWriter(w));
while (true) {
String line = r.readLine();
if (line == null) {
break;
}
if (!line.startsWith("#")) {
writer.print(line + "\n");
try (PrintWriter writer = new PrintWriter(new BufferedWriter(w))) {
while (true) {
String line = r.readLine();
if (line == null) {
break;
}
if (!line.startsWith("#")) {
writer.print(line + "\n");
}
}
}
writer.close();
}
/**
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论