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