View Javadoc

1   // BSD License (http://www.galagosearch.org/license)
2   package org.galagosearch.tupleflow;
3   
4   import java.io.IOException;
5   
6   /***
7    * This class holds helper static methods that help with delta encoding for compression.
8    * 
9    * <p>
10   * To encode 'a', with a previous value of 'b', write:
11   *    <pre>encode(output, a, b);</pre>
12   * and decode as:
13   *    <pre>a = decode(input, b);</pre>
14   * </p>
15   * 
16   * <p>
17   * For integers, the order of the integers matters.  If the values are ascending,
18   * meaning that (last &lt;= current), use encodeAscending/decodeAscending.  If the
19   * values are descending, meaning that (last &gt;= current), use encodeDescending/decodeAscending.
20   * </p>
21   * 
22   * @author trevor
23   */
24  public class Delta {
25      public static void encodeAscending(ArrayOutput output, String current, String last) throws IOException {
26          encode(output, current, last);
27      }
28  
29      public static void encodeDescending(ArrayOutput output, String current, String last) throws IOException {
30          encode(output, current, last);
31      }
32  
33      public static void encode(ArrayOutput output, String current, String last) throws IOException {
34          int maximum = Math.min(current.length(), last.length());
35          int i;
36  
37          for (i = 0; i < maximum; i++) {
38              if (current.charAt(i) != last.charAt(i)) {
39                  break;
40              }
41          }
42  
43          int overlap = i;
44  
45          output.writeInt(overlap);
46          output.writeString(last.substring(overlap));
47      }
48  
49      public static String decodeAscending(ArrayInput input, String last) throws IOException {
50          return decode(input, last);
51      }
52  
53      public static String decodeDescending(ArrayInput input, String last) throws IOException {
54          return decode(input, last);
55      }
56  
57      public static String decode(ArrayInput input, String last) throws IOException {
58          int overlap = input.readInt();
59          String suffix = input.readString();
60  
61          return last.substring(0, overlap) + suffix;
62      }
63  
64      public static void encodeAscending(ArrayOutput output, long current, long last) throws IOException {
65          output.writeLong(current - last);
66      }
67  
68      public static void encodeDescending(ArrayOutput output, long current, long last) throws IOException {
69          output.writeLong(last - current);
70      }
71  
72      public static long decodeAscending(ArrayInput input, long last) throws IOException {
73          return last + input.readLong();
74      }
75  
76      public static int decodeAscending(ArrayInput input, int last) throws IOException {
77          return last + input.readInt();
78      }
79  
80      public static long decodeDescending(ArrayInput input, long last) throws IOException {
81          return last - input.readLong();
82      }
83  
84      public static int decodeDescending(ArrayInput input, int last) throws IOException {
85          return last - input.readInt();
86      }
87  
88      public static String resetString() {
89          return "";
90      }
91  
92      public static int resetInt() {
93          return 0;
94      }
95  
96      public static long resetLong() {
97          return 0;
98      }
99  }