You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EvaluationCache.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.ss.formula;
  16. import org.apache.poi.ss.formula.FormulaCellCache.IEntryOperation;
  17. import org.apache.poi.ss.formula.FormulaUsedBlankCellSet.BookSheetKey;
  18. import org.apache.poi.ss.formula.PlainCellCache.Loc;
  19. import org.apache.poi.ss.formula.eval.BlankEval;
  20. import org.apache.poi.ss.formula.eval.BoolEval;
  21. import org.apache.poi.ss.formula.eval.ErrorEval;
  22. import org.apache.poi.ss.formula.eval.NumberEval;
  23. import org.apache.poi.ss.formula.eval.StringEval;
  24. import org.apache.poi.ss.formula.eval.ValueEval;
  25. import org.apache.poi.ss.usermodel.CellType;
  26. /**
  27. * Performance optimisation for {@link org.apache.poi.ss.usermodel.FormulaEvaluator}.
  28. * This class stores previously calculated values of already visited cells,
  29. * to avoid unnecessary re-calculation when the same cells are referenced multiple times
  30. *
  31. * @author Josh Micich
  32. */
  33. final class EvaluationCache {
  34. private final PlainCellCache _plainCellCache;
  35. private final FormulaCellCache _formulaCellCache;
  36. /** only used for testing. <code>null</code> otherwise */
  37. final IEvaluationListener _evaluationListener;
  38. /* package */EvaluationCache(IEvaluationListener evaluationListener) {
  39. _evaluationListener = evaluationListener;
  40. _plainCellCache = new PlainCellCache();
  41. _formulaCellCache = new FormulaCellCache();
  42. }
  43. public void notifyUpdateCell(int bookIndex, int sheetIndex, EvaluationCell cell) {
  44. FormulaCellCacheEntry fcce = _formulaCellCache.get(cell);
  45. int rowIndex = cell.getRowIndex();
  46. int columnIndex = cell.getColumnIndex();
  47. Loc loc = new Loc(bookIndex, sheetIndex, rowIndex, columnIndex);
  48. PlainValueCellCacheEntry pcce = _plainCellCache.get(loc);
  49. if (cell.getCellType() == CellType.FORMULA) {
  50. if (fcce == null) {
  51. fcce = new FormulaCellCacheEntry();
  52. if (pcce == null) {
  53. if (_evaluationListener != null) {
  54. _evaluationListener.onChangeFromBlankValue(sheetIndex, rowIndex,
  55. columnIndex, cell, fcce);
  56. }
  57. updateAnyBlankReferencingFormulas(bookIndex, sheetIndex, rowIndex,
  58. columnIndex);
  59. }
  60. _formulaCellCache.put(cell, fcce);
  61. } else {
  62. fcce.recurseClearCachedFormulaResults(_evaluationListener);
  63. fcce.clearFormulaEntry();
  64. }
  65. if (pcce == null) {
  66. // was formula cell before - no change of type
  67. } else {
  68. // changing from plain cell to formula cell
  69. pcce.recurseClearCachedFormulaResults(_evaluationListener);
  70. _plainCellCache.remove(loc);
  71. }
  72. } else {
  73. ValueEval value = WorkbookEvaluator.getValueFromNonFormulaCell(cell);
  74. if (pcce == null) {
  75. if (value != BlankEval.instance) {
  76. // only cache non-blank values in the plain cell cache
  77. // (dependencies on blank cells are managed by
  78. // FormulaCellCacheEntry._usedBlankCellGroup)
  79. pcce = new PlainValueCellCacheEntry(value);
  80. if (fcce == null) {
  81. if (_evaluationListener != null) {
  82. _evaluationListener.onChangeFromBlankValue(sheetIndex, rowIndex, columnIndex, cell, pcce);
  83. }
  84. updateAnyBlankReferencingFormulas(bookIndex, sheetIndex,
  85. rowIndex, columnIndex);
  86. }
  87. _plainCellCache.put(loc, pcce);
  88. }
  89. } else {
  90. if (pcce.updateValue(value)) {
  91. pcce.recurseClearCachedFormulaResults(_evaluationListener);
  92. }
  93. if (value == BlankEval.instance) {
  94. _plainCellCache.remove(loc);
  95. }
  96. }
  97. if (fcce == null) {
  98. // was plain cell before - no change of type
  99. } else {
  100. // was formula cell before - now a plain value
  101. _formulaCellCache.remove(cell);
  102. fcce.setSensitiveInputCells(null);
  103. fcce.recurseClearCachedFormulaResults(_evaluationListener);
  104. }
  105. }
  106. }
  107. private void updateAnyBlankReferencingFormulas(int bookIndex, int sheetIndex,
  108. final int rowIndex, final int columnIndex) {
  109. final BookSheetKey bsk = new BookSheetKey(bookIndex, sheetIndex);
  110. _formulaCellCache.applyOperation(new IEntryOperation() {
  111. public void processEntry(FormulaCellCacheEntry entry) {
  112. entry.notifyUpdatedBlankCell(bsk, rowIndex, columnIndex, _evaluationListener);
  113. }
  114. });
  115. }
  116. public PlainValueCellCacheEntry getPlainValueEntry(int bookIndex, int sheetIndex,
  117. int rowIndex, int columnIndex, ValueEval value) {
  118. Loc loc = new Loc(bookIndex, sheetIndex, rowIndex, columnIndex);
  119. PlainValueCellCacheEntry result = _plainCellCache.get(loc);
  120. if (result == null) {
  121. result = new PlainValueCellCacheEntry(value);
  122. _plainCellCache.put(loc, result);
  123. if (_evaluationListener != null) {
  124. _evaluationListener.onReadPlainValue(sheetIndex, rowIndex, columnIndex, result);
  125. }
  126. } else {
  127. // TODO - if we are confident that this sanity check is not required, we can remove 'value' from plain value cache entry
  128. if (!areValuesEqual(result.getValue(), value)) {
  129. throw new IllegalStateException("value changed");
  130. }
  131. if (_evaluationListener != null) {
  132. _evaluationListener.onCacheHit(sheetIndex, rowIndex, columnIndex, value);
  133. }
  134. }
  135. return result;
  136. }
  137. private boolean areValuesEqual(ValueEval a, ValueEval b) {
  138. if (a == null) {
  139. return false;
  140. }
  141. Class<?> cls = a.getClass();
  142. if (cls != b.getClass()) {
  143. // value type is changing
  144. return false;
  145. }
  146. if (a == BlankEval.instance) {
  147. return b == a;
  148. }
  149. if (cls == NumberEval.class) {
  150. return ((NumberEval)a).getNumberValue() == ((NumberEval)b).getNumberValue();
  151. }
  152. if (cls == StringEval.class) {
  153. return ((StringEval)a).getStringValue().equals(((StringEval)b).getStringValue());
  154. }
  155. if (cls == BoolEval.class) {
  156. return ((BoolEval)a).getBooleanValue() == ((BoolEval)b).getBooleanValue();
  157. }
  158. if (cls == ErrorEval.class) {
  159. return ((ErrorEval)a).getErrorCode() == ((ErrorEval)b).getErrorCode();
  160. }
  161. throw new IllegalStateException("Unexpected value class (" + cls.getName() + ")");
  162. }
  163. public FormulaCellCacheEntry getOrCreateFormulaCellEntry(EvaluationCell cell) {
  164. FormulaCellCacheEntry result = _formulaCellCache.get(cell);
  165. if (result == null) {
  166. result = new FormulaCellCacheEntry();
  167. _formulaCellCache.put(cell, result);
  168. }
  169. return result;
  170. }
  171. /**
  172. * Should be called whenever there are changes to input cells in the evaluated workbook.
  173. */
  174. public void clear() {
  175. if(_evaluationListener != null) {
  176. _evaluationListener.onClearWholeCache();
  177. }
  178. _plainCellCache.clear();
  179. _formulaCellCache.clear();
  180. }
  181. public void notifyDeleteCell(int bookIndex, int sheetIndex, EvaluationCell cell) {
  182. if (cell.getCellType() == CellType.FORMULA) {
  183. FormulaCellCacheEntry fcce = _formulaCellCache.remove(cell);
  184. if (fcce == null) {
  185. // formula cell has not been evaluated yet
  186. } else {
  187. fcce.setSensitiveInputCells(null);
  188. fcce.recurseClearCachedFormulaResults(_evaluationListener);
  189. }
  190. } else {
  191. Loc loc = new Loc(bookIndex, sheetIndex, cell.getRowIndex(), cell.getColumnIndex());
  192. PlainValueCellCacheEntry pcce = _plainCellCache.get(loc);
  193. if (pcce == null) {
  194. // cache entry doesn't exist. nothing to do
  195. } else {
  196. pcce.recurseClearCachedFormulaResults(_evaluationListener);
  197. }
  198. }
  199. }
  200. }