Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear excel cache on reload #11872

Merged
merged 4 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would be good to also read xls_sheet one more time before the reload and see that it does not increase the counter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

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
Loading