bug 57840: move XSSFTable cache from XSSFWorkbook to XSSFEvaluationWorkbook where staleness is less likely to cause problems for users

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/xssf_structured_references@1747615 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Javen O'Neal 2016-06-10 01:02:05 +00:00
parent 588c19e9d3
commit 5b983791aa
3 changed files with 52 additions and 8 deletions

View File

@ -28,7 +28,7 @@ import org.apache.poi.xssf.usermodel.BaseXSSFEvaluationWorkbook;
* SXSSF wrapper around the SXSSF and XSSF workbooks * SXSSF wrapper around the SXSSF and XSSF workbooks
*/ */
public final class SXSSFEvaluationWorkbook extends BaseXSSFEvaluationWorkbook { public final class SXSSFEvaluationWorkbook extends BaseXSSFEvaluationWorkbook {
private SXSSFWorkbook _uBook; private final SXSSFWorkbook _uBook;
public static SXSSFEvaluationWorkbook create(SXSSFWorkbook book) { public static SXSSFEvaluationWorkbook create(SXSSFWorkbook book) {
if (book == null) { if (book == null) {
@ -42,15 +42,18 @@ public final class SXSSFEvaluationWorkbook extends BaseXSSFEvaluationWorkbook {
_uBook = book; _uBook = book;
} }
@Override
public int getSheetIndex(EvaluationSheet evalSheet) { public int getSheetIndex(EvaluationSheet evalSheet) {
SXSSFSheet sheet = ((SXSSFEvaluationSheet)evalSheet).getSXSSFSheet(); SXSSFSheet sheet = ((SXSSFEvaluationSheet)evalSheet).getSXSSFSheet();
return _uBook.getSheetIndex(sheet); return _uBook.getSheetIndex(sheet);
} }
@Override
public EvaluationSheet getSheet(int sheetIndex) { public EvaluationSheet getSheet(int sheetIndex) {
return new SXSSFEvaluationSheet(_uBook.getSheetAt(sheetIndex)); return new SXSSFEvaluationSheet(_uBook.getSheetAt(sheetIndex));
} }
@Override
public Ptg[] getFormulaTokens(EvaluationCell evalCell) { public Ptg[] getFormulaTokens(EvaluationCell evalCell) {
SXSSFCell cell = ((SXSSFEvaluationCell)evalCell).getSXSSFCell(); SXSSFCell cell = ((SXSSFEvaluationCell)evalCell).getSXSSFCell();
SXSSFEvaluationWorkbook frBook = SXSSFEvaluationWorkbook.create(_uBook); SXSSFEvaluationWorkbook frBook = SXSSFEvaluationWorkbook.create(_uBook);

View File

@ -17,7 +17,10 @@
package org.apache.poi.xssf.usermodel; package org.apache.poi.xssf.usermodel;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.formula.EvaluationName; import org.apache.poi.ss.formula.EvaluationName;
@ -37,6 +40,7 @@ import org.apache.poi.ss.formula.ptg.Ref3DPxg;
import org.apache.poi.ss.formula.udf.IndexedUDFFinder; import org.apache.poi.ss.formula.udf.IndexedUDFFinder;
import org.apache.poi.ss.formula.udf.UDFFinder; import org.apache.poi.ss.formula.udf.UDFFinder;
import org.apache.poi.ss.usermodel.Table; import org.apache.poi.ss.usermodel.Table;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.AreaReference; import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellReference; import org.apache.poi.ss.util.CellReference;
import org.apache.poi.util.NotImplemented; import org.apache.poi.util.NotImplemented;
@ -311,8 +315,46 @@ public abstract class BaseXSSFEvaluationWorkbook implements FormulaRenderingWork
return _uBook.createName(); return _uBook.createName();
} }
public Table getTable(String name) { /*
return _uBook.getTable(name); * TODO: data tables are stored at the workbook level in XSSF, but are bound to a single sheet.
* The current code structure has them hanging off XSSFSheet, but formulas reference them
* only by name (names are global, and case insensitive).
* This map stores names as lower case for case-insensitive lookups.
*
* FIXME: Caching tables by name here for fast formula lookup means the map is out of date if
* a table is renamed or added/removed to a sheet after the map is created.
*
* Perhaps tables can be managed similar to PivotTable references above?
*/
private Map<String, XSSFTable> _tableCache = null;
private Map<String, XSSFTable> getTableCache() {
if ( _tableCache != null ) {
return _tableCache;
}
// FIXME: use org.apache.commons.collections.map.CaseInsensitiveMap
_tableCache = new HashMap<String, XSSFTable>();
for (Sheet sheet : _uBook) {
for (XSSFTable tbl : ((XSSFSheet)sheet).getTables()) {
String lname = tbl.getName().toLowerCase(Locale.ROOT);
_tableCache.put(lname, tbl);
}
}
return _tableCache;
}
/**
* Returns the data table with the given name (case insensitive).
* Tables are cached for performance (formula evaluation looks them up by name repeatedly).
* After the first table lookup, adding or removing a table from the document structure will cause trouble.
* This is meant to be used on documents whose structure is essentially static at the point formulas are evaluated.
*
* @param name the data table name (case-insensitive)
* @return The Data table in the workbook named <tt>name</tt>, or <tt>null</tt> if no table is named <tt>name</tt>.
* @since 3.15 beta 2
*/
public XSSFTable getTable(String name) {
return getTableCache().get(name.toLowerCase(Locale.ROOT));
} }
public UDFFinder getUDFFinder(){ public UDFFinder getUDFFinder(){

View File

@ -27,7 +27,6 @@ import org.apache.poi.ss.formula.ptg.Ptg;
* Internal POI use only * Internal POI use only
*/ */
public final class XSSFEvaluationWorkbook extends BaseXSSFEvaluationWorkbook { public final class XSSFEvaluationWorkbook extends BaseXSSFEvaluationWorkbook {
@Override
public static XSSFEvaluationWorkbook create(XSSFWorkbook book) { public static XSSFEvaluationWorkbook create(XSSFWorkbook book) {
if (book == null) { if (book == null) {
return null; return null;