zhao
2021-07-07 2fdf959ac739edd6de84aa8053b8b9683dce8e8b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* ====================================================================
   Licensed To the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for Additional information regarding copyright ownership.
   The ASF licenses this file To You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at
 
       http://www.apache.org/licenses/LICENSE-2.0
 
   Unless required by applicable law or agreed To in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */
 
namespace HH.WMS.Utils.NPOI.SS.Formula
{
 
    using System;
 
    using HH.WMS.Utils.NPOI.SS.Formula.Eval;
    
 
    /**
     * Performance optimisation for {@link HSSFFormulaEvaluator}. This class stores previously
     * calculated values of already visited cells, To avoid unnecessary re-calculation when the 
     * same cells are referenced multiple times
     * 
     * 
     * @author Josh Micich
     */
    public class EvaluationCache
    {
 
        private PlainCellCache _plainCellCache;
        private FormulaCellCache _formulaCellCache;
        /** only used for testing. <c>null</c> otherwise */
        IEvaluationListener _evaluationListener;
 
        /* package */
        public EvaluationCache(IEvaluationListener evaluationListener)
        {
            _evaluationListener = evaluationListener;
            _plainCellCache = new PlainCellCache();
            _formulaCellCache = new FormulaCellCache();
        }
 
        public void NotifyUpdateCell(int bookIndex, int sheetIndex, IEvaluationCell cell)
        {
            FormulaCellCacheEntry fcce = _formulaCellCache.Get(cell);
 
            int rowIndex = cell.RowIndex;
            int columnIndex = cell.ColumnIndex;
            Loc loc = new Loc(bookIndex, sheetIndex, cell.RowIndex, cell.ColumnIndex);
            PlainValueCellCacheEntry pcce = _plainCellCache.Get(loc);
 
            if (cell.CellType == HH.WMS.Utils.NPOI.SS.UserModel.CellType.FORMULA)
            {
                if (fcce == null)
                {
                    fcce = new FormulaCellCacheEntry();
 
                    if (pcce == null)
                    {
                        if (_evaluationListener != null)
                        {
                            _evaluationListener.OnChangeFromBlankValue(sheetIndex, rowIndex,
                                    columnIndex, cell, fcce);
                        }
                        UpdateAnyBlankReferencingFormulas(bookIndex, sheetIndex, rowIndex, columnIndex);
                    }
                    _formulaCellCache.Put(cell, fcce);
                }
                else
                {
                    fcce.RecurseClearCachedFormulaResults(_evaluationListener);
                    fcce.ClearFormulaEntry();
                }
                if (pcce == null)
                {
                    // was formula cell before - no Change of type
                }
                else
                {
                    // changing from plain cell To formula cell
                    pcce.RecurseClearCachedFormulaResults(_evaluationListener);
                    _plainCellCache.Remove(loc);
                }
            }
            else
            {
                ValueEval value = WorkbookEvaluator.GetValueFromNonFormulaCell(cell);
                if (pcce == null)
                {
                    if (value != BlankEval.instance)
                    {
                        pcce = new PlainValueCellCacheEntry(value);
                        if (fcce == null)
                        {
                            if (_evaluationListener != null)
                            {
                                _evaluationListener.OnChangeFromBlankValue(sheetIndex, rowIndex,
                                        columnIndex, cell, pcce);
                            }
                            UpdateAnyBlankReferencingFormulas(bookIndex, sheetIndex, rowIndex, columnIndex);
                        }
                        _plainCellCache.Put(loc, pcce);
                    }
                }
                else
                {
                    if (pcce.UpdateValue(value))
                    {
                        pcce.RecurseClearCachedFormulaResults(_evaluationListener);
                    }
                    if (value == BlankEval.instance)
                    {
                        _plainCellCache.Remove(loc);
                    }
                }
                if (fcce == null)
                {
                    // was plain cell before - no Change of type
                }
                else
                {
                    // was formula cell before - now a plain value
                    _formulaCellCache.Remove(cell);
                    fcce.SetSensitiveInputCells(null);
                    fcce.RecurseClearCachedFormulaResults(_evaluationListener);
                }
            }
        }
 
        public class EntryOperation : IEntryOperation
        {
            BookSheetKey bsk;
            int rowIndex, columnIndex;
            IEvaluationListener evaluationListener;
 
            public EntryOperation(BookSheetKey bsk,
                int rowIndex, int columnIndex, IEvaluationListener evaluationListener)
            {
                this.bsk = bsk;
                this.evaluationListener = evaluationListener;
                this.rowIndex = rowIndex;
                this.columnIndex = columnIndex;
            }
 
            public void ProcessEntry(FormulaCellCacheEntry entry)
            {
                entry.NotifyUpdatedBlankCell(bsk, rowIndex, columnIndex, evaluationListener);
            }
        }
 
        private void UpdateAnyBlankReferencingFormulas(int bookIndex, int sheetIndex,
                int rowIndex, int columnIndex)
        {
            BookSheetKey bsk = new BookSheetKey(bookIndex, sheetIndex);
            _formulaCellCache.ApplyOperation(new EntryOperation(bsk,rowIndex,columnIndex,_evaluationListener));
        }
 
        public PlainValueCellCacheEntry GetPlainValueEntry(int bookIndex, int sheetIndex,
                int rowIndex, int columnIndex, ValueEval value)
        {
 
            Loc loc = new Loc(bookIndex, sheetIndex, rowIndex, columnIndex);
            PlainValueCellCacheEntry result = _plainCellCache.Get(loc);
            if (result == null)
            {
                result = new PlainValueCellCacheEntry(value);
                _plainCellCache.Put(loc, result);
                if (_evaluationListener != null)
                {
                    _evaluationListener.OnReadPlainValue(sheetIndex, rowIndex, columnIndex, result);
                }
            }
            else
            {
                // TODO - if we are confident that this sanity check is not required, we can Remove 'value' from plain value cache entry  
                if (!AreValuesEqual(result.GetValue(), value))
                {
                    throw new InvalidOperationException("value changed");
                }
                if (_evaluationListener != null)
                {
                    _evaluationListener.OnCacheHit(sheetIndex, rowIndex, columnIndex, value);
                }
            }
            return result;
        }
        private bool AreValuesEqual(ValueEval a, ValueEval b)
        {
            if (a == null)
            {
                return false;
            }
            Type cls = a.GetType();
            if (cls != b.GetType())
            {
                // value type is changing
                return false;
            }
            if (a == BlankEval.instance)
            {
                return b == a;
            }
            if (cls == typeof(NumberEval))
            {
                return ((NumberEval)a).NumberValue == ((NumberEval)b).NumberValue;
            }
            if (cls == typeof(StringEval))
            {
                return ((StringEval)a).StringValue.Equals(((StringEval)b).StringValue);
            }
            if (cls == typeof(BoolEval))
            {
                return ((BoolEval)a).BooleanValue == ((BoolEval)b).BooleanValue;
            }
            if (cls == typeof(ErrorEval))
            {
                return ((ErrorEval)a).ErrorCode == ((ErrorEval)b).ErrorCode;
            }
            throw new InvalidOperationException("Unexpected value class (" + cls.Name + ")");
        }
 
        public FormulaCellCacheEntry GetOrCreateFormulaCellEntry(IEvaluationCell cell)
        {            
            FormulaCellCacheEntry result = _formulaCellCache.Get(cell);
            if (result == null)
            {
 
                result = new FormulaCellCacheEntry();
                _formulaCellCache.Put(cell, result);
            }
            return result;
        }
 
        /**
         * Should be called whenever there are Changes To input cells in the evaluated workbook.
         */
        public void Clear()
        {
            if (_evaluationListener != null)
            {
                _evaluationListener.OnClearWholeCache();
            }
            _plainCellCache.Clear();
            _formulaCellCache.Clear();
        }
        public void NotifyDeleteCell(int bookIndex, int sheetIndex, IEvaluationCell cell)
        {
 
            if (cell.CellType == HH.WMS.Utils.NPOI.SS.UserModel.CellType.FORMULA)
            {
                FormulaCellCacheEntry fcce = _formulaCellCache.Remove(cell);
                if (fcce == null)
                {
                    // formula cell Has not been evaluated yet 
                }
                else
                {
                    fcce.SetSensitiveInputCells(null);
                    fcce.RecurseClearCachedFormulaResults(_evaluationListener);
                }
            }
            else
            {
                Loc loc = new Loc(bookIndex, sheetIndex, cell.RowIndex, cell.ColumnIndex);
                PlainValueCellCacheEntry pcce = _plainCellCache.Get(loc);
 
                if (pcce == null)
                {
                    // cache entry doesn't exist. nothing To do
                }
                else
                {
                    pcce.RecurseClearCachedFormulaResults(_evaluationListener);
                }
            }
        }
    }
}