Skip to content

Commit

Permalink
Clear excel cache on reload (#11872)
Browse files Browse the repository at this point in the history
(cherry picked from commit 7d3293c)
  • Loading branch information
GregoryTravis authored and jdunkerley committed Dec 18, 2024
1 parent a855497 commit 23cf258
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private void resetEnsoReloadDetector() {
"Standard.Base.Network.Reload_Detector", "create_reload_detector");
}

void simulateReloadTestOnly() {
public void simulateReloadTestOnly() {
EnsoMeta.callStaticModuleMethod(
"Standard.Base.Network.Reload_Detector", "simulate_reload_test_only", ensoReloadDetector);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.enso.base.cache.ReloadDetector;
import org.enso.table.excel.xssfreader.XSSFReaderWorkbook;

public class ExcelConnectionPool {
Expand All @@ -36,6 +37,8 @@ public ReadOnlyExcelConnection openReadOnlyConnection(File file, ExcelFileFormat
+ "written to. This is a bug in the Table library.");
}

clearOnReload();

if (!file.exists()) {
throw new FileNotFoundException(file.toString());
}
Expand Down Expand Up @@ -209,6 +212,29 @@ void release(ReadOnlyExcelConnection excelConnection) throws IOException {
private final HashMap<String, ConnectionRecord> records = new HashMap<>();
private boolean isCurrentlyWriting = false;

/** Used to clear the ConnectionRecord on reload. */
private final ReloadDetector reloadDetector = new ReloadDetector();

/** If a reload has just happened, clear the ConnectionRecord cache. */
private void clearOnReload() throws IOException {
if (reloadDetector.hasReloadOccurred()) {
for (var record : records.values()) {
record.close();
}
records.clear();
}
}

/** Public for testing. */
public int getConnectionRecordCount() {
return records.size();
}

/** Public for testing. */
public void simulateReloadTestOnly() {
reloadDetector.simulateReloadTestOnly();
}

static class ConnectionRecord {
private int refCount;
private File file;
Expand Down
21 changes: 21 additions & 0 deletions test/Table_Tests/src/IO/Excel_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ from project.Common_Table_Operations.Util import within_table
from project.IO.Read_Many_Spec import with_temp_dir

polyglot java import org.enso.table_test_helpers.RandomHelpers
polyglot java import org.enso.table.excel.ExcelConnectionPool

spec_fmt suite_builder header file read_method sheet_count=5 =
suite_builder.group header group_builder->
Expand Down Expand Up @@ -1151,6 +1152,26 @@ add_specs suite_builder =
r3.at "Value" . at 0 . should_be_a Table
r3.at "Value" . at 1 . should_be_a Excel_Workbook

group_builder.specify "should clear cache on simulated reload" <|
ExcelConnectionPool.INSTANCE.simulateReloadTestOnly
check_workbook <| xlsx_sheet.read
ExcelConnectionPool.INSTANCE.getConnectionRecordCount . should_equal 1
check_workbook <| xlsx_sheet.read
ExcelConnectionPool.INSTANCE.getConnectionRecordCount . should_equal 1
check_workbook <| xls_sheet.read
ExcelConnectionPool.INSTANCE.getConnectionRecordCount . should_equal 2
check_workbook <| xls_sheet.read
ExcelConnectionPool.INSTANCE.getConnectionRecordCount . should_equal 2
ExcelConnectionPool.INSTANCE.simulateReloadTestOnly
check_workbook <| xlsx_sheet.read
ExcelConnectionPool.INSTANCE.getConnectionRecordCount . should_equal 1
check_workbook <| xlsx_sheet.read
ExcelConnectionPool.INSTANCE.getConnectionRecordCount . should_equal 1
check_workbook <| xls_sheet.read
ExcelConnectionPool.INSTANCE.getConnectionRecordCount . should_equal 2
check_workbook <| xls_sheet.read
ExcelConnectionPool.INSTANCE.getConnectionRecordCount . should_equal 2

suite_builder.group "Problems" group_builder->
group_builder.specify "should report a user-friendly error message when format is missing a required argument" <|
r = xlsx_sheet.read (..Range)
Expand Down

0 comments on commit 23cf258

Please sign in to comment.