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

LOB objects are now deleted (and the empty space is reused) when the transaction is committed.

上级 fbcfd32b
...@@ -140,7 +140,7 @@ public class LobStorage { ...@@ -140,7 +140,7 @@ public class LobStorage {
prep.setInt(1, tableId); prep.setInt(1, tableId);
ResultSet rs = prep.executeQuery(); ResultSet rs = prep.executeQuery();
while (rs.next()) { while (rs.next()) {
deleteLob(rs.getLong(1)); removeLob(rs.getLong(1));
} }
reuse(sql, prep); reuse(sql, prep);
} catch (SQLException e) { } catch (SQLException e) {
...@@ -298,42 +298,54 @@ public class LobStorage { ...@@ -298,42 +298,54 @@ public class LobStorage {
prepared.put(sql, prep); prepared.put(sql, prep);
} }
private void deleteLob(long lob) throws SQLException { /**
synchronized (handler) { * Delete a LOB from the database.
String sql = "SELECT BLOCK, HASH FROM " + LOB_MAP + " D WHERE D.LOB = ? " + *
"AND NOT EXISTS(SELECT 1 FROM " + LOB_MAP + " O " + * @param lob the lob id
"WHERE O.BLOCK = D.BLOCK AND O.LOB <> ?)"; */
PreparedStatement prep = prepare(sql); public void removeLob(long lob) {
prep.setLong(1, lob); try {
prep.setLong(2, lob); synchronized (handler) {
ResultSet rs = prep.executeQuery(); if (conn == null) {
ArrayList<Long> blocks = New.arrayList(); return;
while (rs.next()) { }
blocks.add(rs.getLong(1)); String sql = "SELECT BLOCK, HASH FROM " + LOB_MAP + " D WHERE D.LOB = ? " +
int hash = rs.getInt(2); "AND NOT EXISTS(SELECT 1 FROM " + LOB_MAP + " O " +
setHashCacheBlock(hash, -1); "WHERE O.BLOCK = D.BLOCK AND O.LOB <> ?)";
} PreparedStatement prep = prepare(sql);
reuse(sql, prep); prep.setLong(1, lob);
prep.setLong(2, lob);
ResultSet rs = prep.executeQuery();
ArrayList<Long> blocks = New.arrayList();
while (rs.next()) {
blocks.add(rs.getLong(1));
int hash = rs.getInt(2);
setHashCacheBlock(hash, -1);
}
reuse(sql, prep);
sql = "DELETE FROM " + LOB_MAP + " WHERE LOB = ?"; sql = "DELETE FROM " + LOB_MAP + " WHERE LOB = ?";
prep = prepare(sql); prep = prepare(sql);
prep.setLong(1, lob); prep.setLong(1, lob);
prep.execute(); prep.execute();
reuse(sql, prep); reuse(sql, prep);
sql = "DELETE FROM " + LOB_DATA + " WHERE BLOCK = ?"; sql = "DELETE FROM " + LOB_DATA + " WHERE BLOCK = ?";
prep = prepare(sql); prep = prepare(sql);
for (long block : blocks) { for (long block : blocks) {
prep.setLong(1, block); prep.setLong(1, block);
prep.execute();
}
reuse(sql, prep);
sql = "DELETE FROM " + LOBS + " WHERE ID = ?";
prep = prepare(sql);
prep.setLong(1, lob);
prep.execute(); prep.execute();
reuse(sql, prep);
} }
reuse(sql, prep); } catch (SQLException e) {
throw DbException.convert(e);
sql = "DELETE FROM " + LOBS + " WHERE ID = ?";
prep = prepare(sql);
prep.setLong(1, lob);
prep.execute();
reuse(sql, prep);
} }
} }
...@@ -416,7 +428,7 @@ public class LobStorage { ...@@ -416,7 +428,7 @@ public class LobStorage {
return registerLob(type, lobId, TABLE_TEMP, length); return registerLob(type, lobId, TABLE_TEMP, length);
} catch (IOException e) { } catch (IOException e) {
if (lobId != -1) { if (lobId != -1) {
deleteLob(lobId); removeLob(lobId);
} }
throw DbException.convertIOException(e, "adding blob"); throw DbException.convertIOException(e, "adding blob");
} }
......
...@@ -120,15 +120,15 @@ public class ValueLobDb extends Value implements Value.ValueClob, Value.ValueBlo ...@@ -120,15 +120,15 @@ public class ValueLobDb extends Value implements Value.ValueClob, Value.ValueBlo
if (tempFile != null) { if (tempFile != null) {
tempFile.stopAutoDelete(); tempFile.stopAutoDelete();
} }
deleteFile(handler, fileName); // synchronize on the database, to avoid concurrent temp file creation /
// deletion / backup
synchronized (handler.getLobSyncObject()) {
IOUtils.delete(fileName);
}
} }
} if (lobStorage != null) {
lobStorage.removeLob(lobId);
private static synchronized void deleteFile(DataHandler handler, String fileName) { lobStorage = null;
// synchronize on the database, to avoid concurrent temp file creation /
// deletion / backup
synchronized (handler.getLobSyncObject()) {
IOUtils.delete(fileName);
} }
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论