Changeset View
Changeset View
Standalone View
Standalone View
java/src/main/java/org/softwareheritage/graph/utils/MPHTranslate.java
package org.softwareheritage.graph.utils; | package org.softwareheritage.graph.utils; | ||||
import com.martiansoftware.jsap.*; | import com.martiansoftware.jsap.*; | ||||
import it.unimi.dsi.fastutil.io.BinIO; | |||||
import it.unimi.dsi.fastutil.objects.Object2LongFunction; | import it.unimi.dsi.fastutil.objects.Object2LongFunction; | ||||
import it.unimi.dsi.io.FastBufferedReader; | import it.unimi.dsi.io.FastBufferedReader; | ||||
import it.unimi.dsi.io.LineIterator; | import it.unimi.dsi.io.LineIterator; | ||||
import org.softwareheritage.graph.maps.NodeIdMap; | |||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.io.InputStreamReader; | import java.io.InputStreamReader; | ||||
import java.nio.charset.StandardCharsets; | import java.nio.charset.StandardCharsets; | ||||
public class MPHTranslate { | public class MPHTranslate { | ||||
private static JSAPResult parse_args(String[] args) { | private static JSAPResult parse_args(String[] args) { | ||||
JSAPResult config = null; | JSAPResult config = null; | ||||
try { | try { | ||||
SimpleJSAP jsap = new SimpleJSAP(MPHTranslate.class.getName(), "", | SimpleJSAP jsap = new SimpleJSAP(MPHTranslate.class.getName(), "", | ||||
new Parameter[]{new UnflaggedOption("function", JSAP.STRING_PARSER, JSAP.REQUIRED, | new Parameter[]{new UnflaggedOption("function", JSAP.STRING_PARSER, JSAP.REQUIRED, | ||||
"Filename of the serialized MPH"),}); | "Filename of the serialized MPH"),}); | ||||
config = jsap.parse(args); | config = jsap.parse(args); | ||||
if (jsap.messagePrinted()) { | if (jsap.messagePrinted()) { | ||||
System.exit(1); | System.exit(1); | ||||
} | } | ||||
} catch (JSAPException e) { | } catch (JSAPException e) { | ||||
e.printStackTrace(); | e.printStackTrace(); | ||||
} | } | ||||
return config; | return config; | ||||
} | } | ||||
@SuppressWarnings("unchecked") // Suppress warning for Object2LongFunction cast | |||||
static Object2LongFunction<byte[]> loadMPH(String mphPath) throws IOException, ClassNotFoundException { | |||||
return (Object2LongFunction<byte[]>) BinIO.loadObject(mphPath); | |||||
} | |||||
public static void main(String[] args) throws IOException, ClassNotFoundException { | public static void main(String[] args) throws IOException, ClassNotFoundException { | ||||
JSAPResult config = parse_args(args); | JSAPResult config = parse_args(args); | ||||
String mphPath = config.getString("function"); | String mphPath = config.getString("function"); | ||||
Object2LongFunction<byte[]> mphMap = loadMPH(mphPath); | Object2LongFunction<byte[]> mphMap = NodeIdMap.loadMph(mphPath); | ||||
// TODO: wasteful to convert to/from bytes | // TODO: wasteful to convert to/from bytes | ||||
FastBufferedReader buffer = new FastBufferedReader(new InputStreamReader(System.in, StandardCharsets.US_ASCII)); | FastBufferedReader buffer = new FastBufferedReader(new InputStreamReader(System.in, StandardCharsets.US_ASCII)); | ||||
LineIterator lineIterator = new LineIterator(buffer); | LineIterator lineIterator = new LineIterator(buffer); | ||||
while (lineIterator.hasNext()) { | while (lineIterator.hasNext()) { | ||||
String line = lineIterator.next().toString(); | String line = lineIterator.next().toString(); | ||||
System.out.println(mphMap.getLong(line.getBytes(StandardCharsets.US_ASCII))); | System.out.println(mphMap.getLong(line.getBytes(StandardCharsets.US_ASCII))); | ||||
} | } | ||||
} | } | ||||
} | } |