?? recover.java
字號:
}
storageId = s.readInt();
if (storageId < 0) {
writeDataError(writer, "storageId<0", s.getBytes(), blockCount);
continue;
}
int page = block / DiskFile.BLOCKS_PER_PAGE;
if (pageOwners[page] != 0 && pageOwners[page] != storageId) {
writeDataError(writer, "double allocation, previous=" + pageOwners[page] + " now=" + storageId, s
.getBytes(), blockCount);
} else {
pageOwners[page] = storageId;
}
writer.println("// [" + block + "] page:" + page + " blocks:" + blockCount + " storage:" + storageId);
}
writer.close();
} catch (Throwable e) {
writeError(writer, e);
} finally {
IOUtils.closeSilently(writer);
closeSilently(store);
}
}
private void dumpData(String fileName) throws SQLException {
PrintWriter writer = null;
FileStore store = null;
try {
setDatabaseName(fileName.substring(0, fileName.length() - Constants.SUFFIX_DATA_FILE.length()));
writer = getWriter(fileName, ".sql");
writer.println("CREATE ALIAS IF NOT EXISTS READ_CLOB FOR \"" + this.getClass().getName() + ".readClob\";");
writer.println("CREATE ALIAS IF NOT EXISTS READ_BLOB FOR \"" + this.getClass().getName() + ".readBlob\";");
ObjectArray schema = new ObjectArray();
HashSet objectIdSet = new HashSet();
HashMap tableMap = new HashMap();
textStorage = Database.isTextStorage(fileName, false);
byte[] magic = Database.getMagic(textStorage);
store = FileStore.open(null, fileName, "r", magic);
long length = store.length();
int offset = FileStore.HEADER_LENGTH;
int blockSize = DiskFile.BLOCK_SIZE;
int blocks = (int) (length / blockSize);
blockCount = 1;
int[] pageOwners = new int[blocks / DiskFile.BLOCKS_PER_PAGE];
for (int block = 0; block < blocks; block += blockCount) {
store.seek(offset + (long) block * blockSize);
byte[] buff = new byte[blockSize];
DataPage s = DataPage.create(this, buff);
store.readFully(buff, 0, blockSize);
blockCount = s.readInt();
storageId = -1;
recordLength = -1;
valueId = -1;
if (blockCount == 0) {
// free block
blockCount = 1;
continue;
} else if (blockCount < 0) {
writeDataError(writer, "blockCount<0", s.getBytes(), 1);
blockCount = 1;
continue;
} else if ((blockCount * blockSize) >= Integer.MAX_VALUE / 4) {
writeDataError(writer, "blockCount=" + blockCount, s.getBytes(), 1);
blockCount = 1;
continue;
}
writer.println("-- block " + block + " - " + (block + blockCount - 1));
try {
s.checkCapacity(blockCount * blockSize);
} catch (OutOfMemoryError e) {
writeDataError(writer, "out of memory", s.getBytes(), 1);
blockCount = 1;
continue;
}
if (blockCount > 1) {
if ((blockCount * blockSize) < 0) {
writeDataError(writer, "wrong blockCount", s.getBytes(), 1);
blockCount = 1;
} else {
store.readFully(s.getBytes(), blockSize, blockCount * blockSize - blockSize);
}
}
try {
s.check(blockCount * blockSize);
} catch (SQLException e) {
writeDataError(writer, "wrong checksum", s.getBytes(), 1);
blockCount = 1;
continue;
}
storageId = s.readInt();
if (storageId < 0) {
writeDataError(writer, "storageId<0", s.getBytes(), blockCount);
continue;
}
int page = block / DiskFile.BLOCKS_PER_PAGE;
if (pageOwners[page] != 0 && pageOwners[page] != storageId) {
writeDataError(writer, "double allocation, previous=" + pageOwners[page] + " now=" + storageId, s
.getBytes(), blockCount);
} else {
pageOwners[page] = storageId;
}
recordLength = s.readInt();
if (recordLength <= 0) {
writeDataError(writer, "recordLength<0", s.getBytes(), blockCount);
continue;
}
Value[] data;
try {
data = new Value[recordLength];
} catch (OutOfMemoryError e) {
writeDataError(writer, "out of memory", s.getBytes(), blockCount);
continue;
}
if (!objectIdSet.contains(ObjectUtils.getInteger(storageId))) {
objectIdSet.add(ObjectUtils.getInteger(storageId));
StringBuffer sb = new StringBuffer();
sb.append("CREATE TABLE O_" + storageId + "(");
for (int i = 0; i < recordLength; i++) {
if (i > 0) {
sb.append(", ");
}
sb.append("C");
sb.append(i);
sb.append(" VARCHAR");
}
sb.append(");");
writer.println(sb.toString());
writer.flush();
}
StringBuffer sb = new StringBuffer();
sb.append("INSERT INTO O_" + storageId + " VALUES(");
for (valueId = 0; valueId < recordLength; valueId++) {
try {
Value v = s.readValue();
data[valueId] = v;
if (valueId > 0) {
sb.append(", ");
}
sb.append(getSQL(v));
} catch (Exception e) {
writeDataError(writer, "exception " + e, s.getBytes(), blockCount);
continue;
} catch (OutOfMemoryError e) {
writeDataError(writer, "out of memory", s.getBytes(), blockCount);
continue;
}
}
sb.append(");");
writer.println(sb.toString());
writer.flush();
if (storageId == 0) {
try {
SimpleRow r = new SimpleRow(data);
MetaRecord meta = new MetaRecord(r);
schema.add(meta);
if (meta.getObjectType() == DbObject.TABLE_OR_VIEW) {
String sql = data[3].getString();
int end = sql.indexOf('(');
if (end >= 0) {
int start = sql.lastIndexOf(' ', end);
String name = sql.substring(start, end).trim();
tableMap.put(ObjectUtils.getInteger(meta.getId()), name);
}
}
} catch (Throwable t) {
writeError(writer, t);
}
}
}
MetaRecord.sort(schema);
for (int i = 0; i < schema.size(); i++) {
MetaRecord m = (MetaRecord) schema.get(i);
writer.println(m.getSQL() + ";");
}
for (Iterator it = tableMap.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Entry) it.next();
Integer objectId = (Integer) entry.getKey();
String name = (String) entry.getValue();
writer.println("INSERT INTO " + name + " SELECT * FROM O_" + objectId + ";");
}
for (Iterator it = objectIdSet.iterator(); it.hasNext();) {
Integer objectId = (Integer) it.next();
writer.println("DROP TABLE O_" + objectId + ";");
}
writer.println("DROP ALIAS READ_CLOB;");
writer.println("DROP ALIAS READ_BLOB;");
writer.close();
} catch (Throwable e) {
writeError(writer, e);
} finally {
IOUtils.closeSilently(writer);
closeSilently(store);
}
}
private void closeSilently(FileStore store) {
if (store != null) {
store.closeSilently();
store = null;
}
}
private void writeError(PrintWriter writer, Throwable e) {
if (writer != null) {
writer.println("// error: " + e);
}
logError("Error", e);
}
/**
* INTERNAL
*/
public boolean getTextStorage() {
return textStorage;
}
/**
* INTERNAL
*/
public String getDatabasePath() {
return databaseName;
}
/**
* INTERNAL
*/
public FileStore openFile(String name, String mode, boolean mustExist) throws SQLException {
return FileStore.open(this, name, "rw", Constants.MAGIC_FILE_HEADER.getBytes());
}
/**
* INTERNAL
*/
public int getChecksum(byte[] data, int start, int end) {
int x = 0;
while (start < end) {
x += data[start++];
}
return x;
}
/**
* INTERNAL
*/
public void checkPowerOff() throws SQLException {
}
/**
* INTERNAL
*/
public void checkWritingAllowed() throws SQLException {
}
/**
* INTERNAL
*/
public void freeUpDiskSpace() throws SQLException {
}
/**
* INTERNAL
*/
public void handleInvalidChecksum() throws SQLException {
throw new SQLException("Invalid Checksum");
}
/**
* INTERNAL
*/
public int compareTypeSave(Value a, Value b) throws SQLException {
throw Message.getInternalError();
}
/**
* INTERNAL
*/
public int getMaxLengthInplaceLob() {
throw Message.getInternalError();
}
/**
* INTERNAL
*/
public int allocateObjectId(boolean b, boolean c) {
throw Message.getInternalError();
}
/**
* INTERNAL
*/
public String createTempFile() throws SQLException {
throw Message.getInternalError();
}
/**
* INTERNAL
*/
public String getLobCompressionAlgorithm(int type) {
return null;
}
/**
* INTERNAL
*/
public Object getLobSyncObject() {
return this;
}
/**
* INTERNAL
*/
public boolean getLobFilesInDirectories() {
return lobFilesInDirectories;
}
/**
* INTERNAL
*/
public SmallLRUCache getLobFileListCache() {
return null;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -