From 025937ad30cd98108b9e25c7881f07e4c910cd35 Mon Sep 17 00:00:00 2001 From: honorwu Date: Wed, 30 Mar 2022 16:52:49 +0800 Subject: [PATCH] fix delete & rename failed in logTruncate on Windows --- src/java/simpledb/common/Database.java | 3 +++ src/java/simpledb/storage/LogFile.java | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/src/java/simpledb/common/Database.java b/src/java/simpledb/common/Database.java index b4ceed61..fa209871 100644 --- a/src/java/simpledb/common/Database.java +++ b/src/java/simpledb/common/Database.java @@ -72,6 +72,9 @@ public static BufferPool resetBufferPool(int pages) { // reset the database, used for unit tests only. public static void reset() { + if (_instance != null) { + _instance.get()._logfile.close(); + } _instance.set(new Database()); } diff --git a/src/java/simpledb/storage/LogFile.java b/src/java/simpledb/storage/LogFile.java index e2dff98b..b1c364ba 100644 --- a/src/java/simpledb/storage/LogFile.java +++ b/src/java/simpledb/storage/LogFile.java @@ -437,6 +437,7 @@ public synchronized void logTruncate() throws IOException { raf.close(); logFile.delete(); + logNew.close(); newFile.renameTo(logFile); raf = new RandomAccessFile(logFile, "rw"); raf.seek(raf.length()); @@ -571,4 +572,11 @@ public synchronized void force() throws IOException { raf.getChannel().force(true); } + public void close() { + try { + raf.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } }