package com.scaleunlimited.wikiwords.tools;

import java.io.BufferedReader;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.log4j.Logger;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;

import com.scaleunlimited.maps.StringMap;

public class RedirectMapTool {
    private static final Logger LOGGER = Logger.getLogger(RedirectMapTool.class);

    public RedirectMapTool() {
        // TODO Auto-generated constructor stub
    }

    public void run(RedirectMapOptions options) throws IOException {
        
        // We'll get lines of text <from article title> <tab> <to article title>
        // Build a map from the <from article> hash to the <to article> title
        // And a separate map from <to article title> to offset into byte array of this title.
        StringMap articleMap = new StringMap();
        
        int curLine = 0;
        File inputFile = new File(options.getInputFilename());
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile), "UTF-8"))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] pieces = line.split("\t");
                if (pieces.length != 2) {
                    LOGGER.error("Invalid redirect line: " + line);
                    continue;
                }
                
                if (articleMap.containsKey(pieces[0])) {
                    LOGGER.error(String.format("Existing entry for %s", pieces[0]));
                } else {
                    articleMap.put(pieces[0], pieces[1]);
                }
                
                curLine += 1;
            }
        }
        
        LOGGER.info("Number of lines: " + curLine);
        
        File outputFile = new File(options.getOutputFilename());
        File outputDir = outputFile.getParentFile();
        outputDir.mkdirs();
        FileOutputStream fos = new FileOutputStream(outputFile);
        DataOutput out = new DataOutputStream(fos);
        articleMap.write(out);
        fos.close();
        
        LOGGER.info("Wrote redirect map to " + outputFile);
    }

    public static void main(String[] args) {
        RedirectMapOptions options = new RedirectMapOptions();
        CmdLineParser parser = new CmdLineParser(options);

        try {
            parser.parseArgument(args);
        } catch (CmdLineException e) {
            System.err.println(e.getMessage());
            printUsageAndExit(parser);
        }

        RedirectMapTool tool = new RedirectMapTool();
        
        try {
            tool.run(options);
        } catch (IllegalArgumentException e) {
            System.err.println(e.getMessage());
            printUsageAndExit(parser);
        } catch (Throwable t) {
            System.err.println("Exception running tool: " + t.getMessage());
            t.printStackTrace(System.err);
            System.exit(-1);
        }
    }

    private static void printUsageAndExit(CmdLineParser parser) {
        parser.printUsage(System.err);
        System.exit(-1);
    }

    public static class RedirectMapOptions {
        private String _inputFilename;
        private String _outputFilename;
        
        @Option(name = "-inputfile", usage = "path to redirect data generated by WikiDumpTool", required = true)
        public void setInputFilename(String inputFilename) {
            _inputFilename = inputFilename;
        }

        public String getInputFilename() {
            return _inputFilename;
        }

        @Option(name = "-outputfile", usage = "path to file for generated map", required = true)
        public void setOutputFilename(String outputFilename) {
            _outputFilename = outputFilename;
        }

        public String getOutputFilename() {
            return _outputFilename;
        }

    }

}

Add tool to build compact map for redirects