Coverage Report - org.galagosearch.tupleflow.Delta
 
Classes in this File Line Coverage Branch Coverage Complexity
Delta
0%
0/29
0%
0/4
1.133
 
 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  0
 public class Delta {
 25  
     public static void encodeAscending(ArrayOutput output, String current, String last) throws IOException {
 26  0
         encode(output, current, last);
 27  0
     }
 28  
 
 29  
     public static void encodeDescending(ArrayOutput output, String current, String last) throws IOException {
 30  0
         encode(output, current, last);
 31  0
     }
 32  
 
 33  
     public static void encode(ArrayOutput output, String current, String last) throws IOException {
 34  0
         int maximum = Math.min(current.length(), last.length());
 35  
         int i;
 36  
 
 37  0
         for (i = 0; i < maximum; i++) {
 38  0
             if (current.charAt(i) != last.charAt(i)) {
 39  0
                 break;
 40  
             }
 41  
         }
 42  
 
 43  0
         int overlap = i;
 44  
 
 45  0
         output.writeInt(overlap);
 46  0
         output.writeString(last.substring(overlap));
 47  0
     }
 48  
 
 49  
     public static String decodeAscending(ArrayInput input, String last) throws IOException {
 50  0
         return decode(input, last);
 51  
     }
 52  
 
 53  
     public static String decodeDescending(ArrayInput input, String last) throws IOException {
 54  0
         return decode(input, last);
 55  
     }
 56  
 
 57  
     public static String decode(ArrayInput input, String last) throws IOException {
 58  0
         int overlap = input.readInt();
 59  0
         String suffix = input.readString();
 60  
 
 61  0
         return last.substring(0, overlap) + suffix;
 62  
     }
 63  
 
 64  
     public static void encodeAscending(ArrayOutput output, long current, long last) throws IOException {
 65  0
         output.writeLong(current - last);
 66  0
     }
 67  
 
 68  
     public static void encodeDescending(ArrayOutput output, long current, long last) throws IOException {
 69  0
         output.writeLong(last - current);
 70  0
     }
 71  
 
 72  
     public static long decodeAscending(ArrayInput input, long last) throws IOException {
 73  0
         return last + input.readLong();
 74  
     }
 75  
 
 76  
     public static int decodeAscending(ArrayInput input, int last) throws IOException {
 77  0
         return last + input.readInt();
 78  
     }
 79  
 
 80  
     public static long decodeDescending(ArrayInput input, long last) throws IOException {
 81  0
         return last - input.readLong();
 82  
     }
 83  
 
 84  
     public static int decodeDescending(ArrayInput input, int last) throws IOException {
 85  0
         return last - input.readInt();
 86  
     }
 87  
 
 88  
     public static String resetString() {
 89  0
         return "";
 90  
     }
 91  
 
 92  
     public static int resetInt() {
 93  0
         return 0;
 94  
     }
 95  
 
 96  
     public static long resetLong() {
 97  0
         return 0;
 98  
     }
 99  
 }