|
| 1 | +package org.fastfilter.tools; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileReader; |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.LineNumberReader; |
| 7 | +import java.io.RandomAccessFile; |
| 8 | +import java.nio.charset.Charset; |
| 9 | +import java.util.ArrayList; |
| 10 | + |
| 11 | +import org.fastfilter.utils.StringUtils; |
| 12 | +import org.fastfilter.xorplus.XorPlus8; |
| 13 | + |
| 14 | +public class BuildFilterFile { |
| 15 | + |
| 16 | + public static final int SEGMENT_BITS = 4; |
| 17 | + |
| 18 | + public static void main(String... args) throws IOException { |
| 19 | + if (args.length != 1) { |
| 20 | + System.out.println("Usage: java " + BuildFilterFile.class.getName() + " <textFile>\n" |
| 21 | + + "Builds a .filter file from a text file that contains SHA-1 hashes and counts."); |
| 22 | + return; |
| 23 | + } |
| 24 | + String textFile = args[0]; |
| 25 | + String filterFileName = textFile + ".filter"; |
| 26 | + long start = System.nanoTime(); |
| 27 | + LineNumberReader lineReader = new LineNumberReader(new FileReader(textFile, Charset.forName("LATIN1"))); |
| 28 | + new File(filterFileName).delete(); |
| 29 | + RandomAccessFile out = new RandomAccessFile(filterFileName, "rw"); |
| 30 | + int lines = 0; |
| 31 | + // header |
| 32 | + out.write(new byte[8 << SEGMENT_BITS]); |
| 33 | + int currentSegment = 0; |
| 34 | + long lastHash = 0; |
| 35 | + ArrayList<Long> keys = new ArrayList<Long>(); |
| 36 | + while (true) { |
| 37 | + String line = lineReader.readLine(); |
| 38 | + if (line == null) { |
| 39 | + break; |
| 40 | + } |
| 41 | + lines++; |
| 42 | + long hash = 0; |
| 43 | + for (int i = 0; i < 16; i++) { |
| 44 | + hash <<= 4; |
| 45 | + hash |= StringUtils.getHex(line.charAt(i)); |
| 46 | + } |
| 47 | + if (lastHash == hash) { |
| 48 | + System.out.println("Warning: duplicate hash detected, ignoring: " + line); |
| 49 | + continue; |
| 50 | + } |
| 51 | + lastHash = hash; |
| 52 | + int dot = line.lastIndexOf(':'); |
| 53 | + int count = Integer.parseInt(line, dot + 1, line.length(), 10); |
| 54 | + // set the lowest bit to 0 |
| 55 | + long key = hash ^ (hash & 1); |
| 56 | + // if common, set the lowest bit |
| 57 | + if (count > 9) { |
| 58 | + key |= 1; |
| 59 | + } |
| 60 | + int segment = (int) (key >>> (64 - SEGMENT_BITS)); |
| 61 | + if (segment != currentSegment) { |
| 62 | + writeSegment(keys, currentSegment, out); |
| 63 | + long time = System.nanoTime() - start; |
| 64 | + System.out.println("Lines processed: " + lines + " " + (time / lines) + " ns/line"); |
| 65 | + currentSegment = segment; |
| 66 | + } |
| 67 | + keys.add(key); |
| 68 | + } |
| 69 | + writeSegment(keys, currentSegment, out); |
| 70 | + lineReader.close(); |
| 71 | + out.close(); |
| 72 | + } |
| 73 | + |
| 74 | + private static void writeSegment(ArrayList<Long> keys, int segment, RandomAccessFile out) throws IOException { |
| 75 | + long[] array = new long[keys.size()]; |
| 76 | + for(int i=0; i<keys.size(); i++) { |
| 77 | + array[i] = keys.get(i); |
| 78 | + } |
| 79 | + long start = out.length(); |
| 80 | + out.seek(segment * 8); |
| 81 | + out.writeLong(start); |
| 82 | + out.seek(start); |
| 83 | + XorPlus8 filter = XorPlus8.construct(array); |
| 84 | + out.write(filter.getData()); |
| 85 | + keys.clear(); |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments