View Javadoc
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    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.commons.compress.harmony.unpack200;
18  
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.HashMap;
22  import java.util.IdentityHashMap;
23  import java.util.List;
24  
25  /**
26   * The SegmentConstantPool spends a lot of time searching through large arrays of Strings looking for matches. This can be sped up by caching the arrays in
27   * HashMaps so the String keys are looked up and resolve to positions in the array rather than iterating through the arrays each time.
28   *
29   * Because the arrays only grow (never shrink or change) we can use the last known size as a way to determine if the array has changed.
30   *
31   * Note that this cache must be synchronized externally if it is shared.
32   */
33  public class SegmentConstantPoolArrayCache {
34  
35      /**
36       * CachedArray keeps track of the last known size of an array as well as a HashMap that knows the mapping from element values to the indices of the array
37       * which contain that value.
38       */
39      protected class CachedArray {
40          String[] primaryArray;
41          int lastKnownSize;
42          HashMap<String, List<Integer>> primaryTable;
43  
44          public CachedArray(final String[] array) {
45              this.primaryArray = array;
46              this.lastKnownSize = array.length;
47              this.primaryTable = new HashMap<>(lastKnownSize);
48              cacheIndexes();
49          }
50  
51          /**
52           * Given a primaryArray, cache its values in a HashMap to provide a backwards mapping from element values to element indexes. For instance, a
53           * primaryArray of: {"Zero", "Foo", "Two", "Foo"} would yield a HashMap of: "Zero" -&gt; 0 "Foo" -&gt; 1, 3 "Two" -&gt; 2 which is then cached.
54           */
55          protected void cacheIndexes() {
56              for (int index = 0; index < primaryArray.length; index++) {
57                  final String key = primaryArray[index];
58                  primaryTable.computeIfAbsent(key, k -> new ArrayList<>()).add(Integer.valueOf(index));
59              }
60          }
61  
62          /**
63           * Given a particular key, answer a List of index locations in the array which contain that key.
64           *
65           * If no elements are found, answer an empty list.
66           *
67           * @param key String element of the array
68           * @return List of indexes containing that key in the array.
69           */
70          public List<Integer> indexesForKey(final String key) {
71              final List<Integer> list = primaryTable.get(key);
72              return list != null ? list : Collections.emptyList();
73          }
74  
75          /**
76           * Answer the last known size of the array cached. If the last known size is not the same as the current size, the array must have changed.
77           *
78           * @return int last known size of the cached array
79           */
80          public int lastKnownSize() {
81              return lastKnownSize;
82          }
83      }
84  
85      protected IdentityHashMap<String[], CachedArray> knownArrays = new IdentityHashMap<>(1000);
86      protected List<Integer> lastIndexes;
87      protected String[] lastArray;
88  
89      protected String lastKey;
90  
91      /**
92       * Given a String array, answer true if the array is correctly cached. Answer false if the array is not cached, or if the array cache is outdated.
93       *
94       * @param array of String
95       * @return boolean true if up-to-date cache, otherwise false.
96       */
97      protected boolean arrayIsCached(final String[] array) {
98          final CachedArray cachedArray = knownArrays.get(array);
99          return !(cachedArray == null || cachedArray.lastKnownSize() != array.length);
100     }
101 
102     /**
103      * Cache the array passed in as the argument
104      *
105      * @param array String[] to cache
106      */
107     protected void cacheArray(final String[] array) {
108         if (arrayIsCached(array)) {
109             throw new IllegalArgumentException("Trying to cache an array that already exists");
110         }
111         knownArrays.put(array, new CachedArray(array));
112         // Invalidate the cache-within-a-cache
113         lastArray = null;
114     }
115 
116     /**
117      * Answer the indices for the given key in the given array. If no such key exists in the cached array, answer -1.
118      *
119      * @param array String[] array to search for the value
120      * @param key   String value for which to search
121      * @return List collection of index positions in the array
122      */
123     public List<Integer> indexesForArrayKey(final String[] array, final String key) {
124         if (!arrayIsCached(array)) {
125             cacheArray(array);
126         }
127 
128         // If the search is one we've just done, don't even
129         // bother looking and return the last indices. This
130         // is a second cache within the cache. This is
131         // efficient because we are usually looking for
132         // several secondary elements with the same primary
133         // key.
134         if (lastArray == array && lastKey == key) {
135             return lastIndexes;
136         }
137 
138         // Remember the last thing we found.
139         lastArray = array;
140         lastKey = key;
141         lastIndexes = knownArrays.get(array).indexesForKey(key);
142 
143         return lastIndexes;
144     }
145 }