Skip to content

Commit

Permalink
clear excel cache on reload
Browse files Browse the repository at this point in the history
  • Loading branch information
GregoryTravis committed Dec 13, 2024
1 parent 7998ed5 commit a9bc6a2
Show file tree
Hide file tree
Showing 3 changed files with 40 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;

public class ExcelConnectionPool {
public static final ExcelConnectionPool INSTANCE = new ExcelConnectionPool();
Expand All @@ -35,6 +36,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 @@ -208,6 +211,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
13 changes: 13 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,18 @@ 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 <| 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 <| 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 a9bc6a2

Please sign in to comment.