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.compressors.lz77support;
20  
21  /**
22   * Parameters of the {@link LZ77Compressor compressor}.
23   */
24  public final class Parameters {
25  
26      /**
27       * Builder for {@link Parameters} instances.
28       */
29      public static class Builder {
30  
31          private final int windowSize;
32          private int minBackReferenceLength;
33          private int maxBackReferenceLength;
34          private int maxOffset;
35          private int maxLiteralLength;
36          private Integer niceBackReferenceLength;
37          private Integer maxCandidates;
38          private Integer lazyThreshold;
39          private Boolean lazyMatches;
40  
41          private Builder(final int windowSize) {
42              if (windowSize < 2 || !isPowerOfTwo(windowSize)) {
43                  throw new IllegalArgumentException("windowSize must be a power of two");
44              }
45              this.windowSize = windowSize;
46              minBackReferenceLength = TRUE_MIN_BACK_REFERENCE_LENGTH;
47              maxBackReferenceLength = windowSize - 1;
48              maxOffset = windowSize - 1;
49              maxLiteralLength = windowSize;
50          }
51  
52          /**
53           * Creates the {@link Parameters} instance.
54           *
55           * @return the configured {@link Parameters} instance.
56           */
57          public Parameters build() {
58              // default settings tuned for a compromise of good compression and acceptable speed
59              final int niceLen = niceBackReferenceLength != null ? niceBackReferenceLength : Math.max(minBackReferenceLength, maxBackReferenceLength / 2);
60              final int candidates = maxCandidates != null ? maxCandidates : Math.max(256, windowSize / 128);
61              final boolean lazy = lazyMatches == null || lazyMatches;
62              final int threshold = lazy ? lazyThreshold != null ? lazyThreshold : niceLen : minBackReferenceLength;
63  
64              return new Parameters(windowSize, minBackReferenceLength, maxBackReferenceLength, maxOffset, maxLiteralLength, niceLen, candidates, lazy,
65                      threshold);
66          }
67  
68          /**
69           * Changes the default setting for "nice back-reference length" and "maximum number of candidates" for improved compression ratio at the cost of
70           * compression speed.
71           * <p>
72           * Use this method after configuring "maximum back-reference length".
73           * </p>
74           *
75           * @return the builder
76           */
77          public Builder tunedForCompressionRatio() {
78              niceBackReferenceLength = lazyThreshold = maxBackReferenceLength;
79              maxCandidates = Math.max(32, windowSize / 16);
80              lazyMatches = true;
81              return this;
82          }
83  
84          /**
85           * Changes the default setting for "nice back-reference length" and "maximum number of candidates" for improved compression speed at the cost of
86           * compression ratio.
87           * <p>
88           * Use this method after configuring "maximum back-reference length".
89           * </p>
90           *
91           * @return the builder
92           */
93          public Builder tunedForSpeed() {
94              niceBackReferenceLength = Math.max(minBackReferenceLength, maxBackReferenceLength / 8);
95              maxCandidates = Math.max(32, windowSize / 1024);
96              lazyMatches = false;
97              lazyThreshold = minBackReferenceLength;
98              return this;
99          }
100 
101         /**
102          * Sets whether lazy matching should be performed.
103          * <p>
104          * Lazy matching means that after a back-reference for a certain position has been found the compressor will try to find a longer match for the next
105          * position.
106          * </p>
107          * <p>
108          * Lazy matching is enabled by default and disabled when tuning for speed.
109          * </p>
110          *
111          * @param lazy whether lazy matching should be performed
112          * @return the builder
113          */
114         public Builder withLazyMatching(final boolean lazy) {
115             lazyMatches = lazy;
116             return this;
117         }
118 
119         /**
120          * Sets the threshold for lazy matching.
121          * <p>
122          * Even if lazy matching is enabled it will not be performed if the length of the back-reference found for the current position is longer than this
123          * value.
124          * </p>
125          *
126          * @param threshold the threshold for lazy matching
127          * @return the builder
128          */
129         public Builder withLazyThreshold(final int threshold) {
130             lazyThreshold = threshold;
131             return this;
132         }
133 
134         /**
135          * Sets the maximal length of a back-reference.
136          * <p>
137          * It is recommended to not use this method directly but rather tune a pre-configured builder created by a format specific factory like
138          * {@link org.apache.commons.compress.compressors.snappy.SnappyCompressorOutputStream#createParameterBuilder}.
139          * </p>
140          *
141          * @param maxBackReferenceLength maximal length of a back-reference found. A value smaller than {@code minBackReferenceLength} is interpreted as
142          *                               {@code minBackReferenceLength}. {@code maxBackReferenceLength} is capped at {@code windowSize - 1}.
143          * @return the builder
144          */
145         public Builder withMaxBackReferenceLength(final int maxBackReferenceLength) {
146             this.maxBackReferenceLength = maxBackReferenceLength < minBackReferenceLength ? minBackReferenceLength
147                     : Math.min(maxBackReferenceLength, windowSize - 1);
148             return this;
149         }
150 
151         /**
152          * Sets the maximal length of a literal block.
153          * <p>
154          * It is recommended to not use this method directly but rather tune a pre-configured builder created by a format specific factory like
155          * {@link org.apache.commons.compress.compressors.snappy.SnappyCompressorOutputStream#createParameterBuilder}.
156          * </p>
157          *
158          * @param maxLiteralLength maximal length of a literal block. Negative numbers and 0 as well as values bigger than {@code windowSize} are interpreted as
159          *                         {@code windowSize}.
160          * @return the builder
161          */
162         public Builder withMaxLiteralLength(final int maxLiteralLength) {
163             this.maxLiteralLength = maxLiteralLength < 1 ? windowSize : Math.min(maxLiteralLength, windowSize);
164             return this;
165         }
166 
167         /**
168          * Sets the maximum number of back-reference candidates that should be consulted.
169          * <p>
170          * This settings can be used to tune the tradeoff between compression speed and compression ratio.
171          * </p>
172          *
173          * @param maxCandidates maximum number of back-reference candidates
174          * @return the builder
175          */
176         public Builder withMaxNumberOfCandidates(final int maxCandidates) {
177             this.maxCandidates = maxCandidates;
178             return this;
179         }
180 
181         /**
182          * Sets the maximal offset of a back-reference.
183          * <p>
184          * It is recommended to not use this method directly but rather tune a pre-configured builder created by a format specific factory like
185          * {@link org.apache.commons.compress.compressors.snappy.SnappyCompressorOutputStream#createParameterBuilder}.
186          * </p>
187          *
188          * @param maxOffset maximal offset of a back-reference. A non-positive value as well as values bigger than {@code windowSize - 1} are interpreted as
189          *                  {@code windowSize * - 1}.
190          * @return the builder
191          */
192         public Builder withMaxOffset(final int maxOffset) {
193             this.maxOffset = maxOffset < 1 ? windowSize - 1 : Math.min(maxOffset, windowSize - 1);
194             return this;
195         }
196 
197         /**
198          * Sets the minimal length of a back-reference.
199          * <p>
200          * Ensures {@code maxBackReferenceLength} is not smaller than {@code minBackReferenceLength}.
201          * </p>
202          * <p>
203          * It is recommended to not use this method directly but rather tune a pre-configured builder created by a format specific factory like
204          * {@link org.apache.commons.compress.compressors.snappy.SnappyCompressorOutputStream#createParameterBuilder}.
205          * </p>
206          *
207          * @param minBackReferenceLength the minimal length of a back-reference found. A true minimum of 3 is hard-coded inside of this implementation but
208          *                               bigger lengths can be configured.
209          * @throws IllegalArgumentException if {@code windowSize} is smaller than {@code minBackReferenceLength}.
210          * @return the builder
211          */
212         public Builder withMinBackReferenceLength(final int minBackReferenceLength) {
213             this.minBackReferenceLength = Math.max(TRUE_MIN_BACK_REFERENCE_LENGTH, minBackReferenceLength);
214             if (windowSize < this.minBackReferenceLength) {
215                 throw new IllegalArgumentException("minBackReferenceLength can't be bigger than windowSize");
216             }
217             if (maxBackReferenceLength < this.minBackReferenceLength) {
218                 maxBackReferenceLength = this.minBackReferenceLength;
219             }
220             return this;
221         }
222 
223         /**
224          * Sets the "nice length" of a back-reference.
225          * <p>
226          * When a back-references if this size has been found, stop searching for longer back-references.
227          * </p>
228          * <p>
229          * This settings can be used to tune the tradeoff between compression speed and compression ratio.
230          * </p>
231          *
232          * @param niceLen the "nice length" of a back-reference
233          * @return the builder
234          */
235         public Builder withNiceBackReferenceLength(final int niceLen) {
236             niceBackReferenceLength = niceLen;
237             return this;
238         }
239     }
240 
241     /**
242      * The hard-coded absolute minimal length of a back-reference.
243      */
244     public static final int TRUE_MIN_BACK_REFERENCE_LENGTH = LZ77Compressor.NUMBER_OF_BYTES_IN_HASH;
245 
246     /**
247      * Initializes the builder for the compressor's parameters with a {@code minBackReferenceLength} of 3 and {@code max*Length} equal to
248      * {@code windowSize - 1}.
249      * <p>
250      * It is recommended to not use this method directly but rather tune a pre-configured builder created by a format specific factory like
251      * {@link org.apache.commons.compress.compressors.snappy.SnappyCompressorOutputStream#createParameterBuilder}.
252      * </p>
253      *
254      * @param windowSize the size of the sliding window - this determines the maximum offset a back-reference can take. Must be a power of two.
255      * @throws IllegalArgumentException if windowSize is not a power of two.
256      * @return a builder configured for the given window size
257      */
258     public static Builder builder(final int windowSize) {
259         return new Builder(windowSize);
260     }
261 
262     private static boolean isPowerOfTwo(final int x) {
263         // pre-condition: x > 0
264         return (x & x - 1) == 0;
265     }
266 
267     private final int windowSize;
268     private final int minBackReferenceLength;
269     private final int maxBackReferenceLength;
270     private final int maxOffset;
271     private final int maxLiteralLength;
272     private final int niceBackReferenceLength;
273     private final int maxCandidates;
274     private final int lazyThreshold;
275 
276     private final boolean lazyMatching;
277 
278     private Parameters(final int windowSize, final int minBackReferenceLength, final int maxBackReferenceLength, final int maxOffset,
279             final int maxLiteralLength, final int niceBackReferenceLength, final int maxCandidates, final boolean lazyMatching, final int lazyThreshold) {
280         this.windowSize = windowSize;
281         this.minBackReferenceLength = minBackReferenceLength;
282         this.maxBackReferenceLength = maxBackReferenceLength;
283         this.maxOffset = maxOffset;
284         this.maxLiteralLength = maxLiteralLength;
285         this.niceBackReferenceLength = niceBackReferenceLength;
286         this.maxCandidates = maxCandidates;
287         this.lazyMatching = lazyMatching;
288         this.lazyThreshold = lazyThreshold;
289     }
290 
291     /**
292      * Gets whether to perform lazy matching.
293      *
294      * @return whether to perform lazy matching
295      */
296     public boolean getLazyMatching() {
297         return lazyMatching;
298     }
299 
300     /**
301      * Gets the threshold for lazy matching.
302      *
303      * @return the threshold for lazy matching
304      */
305     public int getLazyMatchingThreshold() {
306         return lazyThreshold;
307     }
308 
309     /**
310      * Gets the maximal length of a back-reference found.
311      *
312      * @return the maximal length of a back-reference found
313      */
314     public int getMaxBackReferenceLength() {
315         return maxBackReferenceLength;
316     }
317 
318     /**
319      * Gets the maximum number of back-reference candidates to consider.
320      *
321      * @return the maximum number of back-reference candidates to consider
322      */
323     public int getMaxCandidates() {
324         return maxCandidates;
325     }
326 
327     /**
328      * Gets the maximal length of a literal block.
329      *
330      * @return the maximal length of a literal block
331      */
332     public int getMaxLiteralLength() {
333         return maxLiteralLength;
334     }
335 
336     /**
337      * Gets the maximal offset of a back-reference found.
338      *
339      * @return the maximal offset of a back-reference found
340      */
341     public int getMaxOffset() {
342         return maxOffset;
343     }
344 
345     /**
346      * Gets the minimal length of a back-reference found.
347      *
348      * @return the minimal length of a back-reference found
349      */
350     public int getMinBackReferenceLength() {
351         return minBackReferenceLength;
352     }
353 
354     /**
355      * Gets the length of a back-reference that is considered nice enough to stop searching for longer ones.
356      *
357      * @return the length of a back-reference that is considered nice enough to stop searching
358      */
359     public int getNiceBackReferenceLength() {
360         return niceBackReferenceLength;
361     }
362 
363     /**
364      * Gets the size of the sliding window - this determines the maximum offset a back-reference can take.
365      *
366      * @return the size of the sliding window
367      */
368     public int getWindowSize() {
369         return windowSize;
370     }
371 }