Changeset View
Changeset View
Standalone View
Standalone View
java/src/main/java/org/softwareheritage/graph/maps/NodeIdMap.java
Show All 27 Lines | public class NodeIdMap { | ||||
/** Fixed length of binary SWHID buffer */ | /** Fixed length of binary SWHID buffer */ | ||||
public static final int SWHID_BIN_SIZE = 22; | public static final int SWHID_BIN_SIZE = 22; | ||||
/** File extension for the long node id to SWHID map */ | /** File extension for the long node id to SWHID map */ | ||||
public static final String NODE_TO_SWHID = ".node2swhid.bin"; | public static final String NODE_TO_SWHID = ".node2swhid.bin"; | ||||
/** Graph path and basename */ | /** Graph path and basename */ | ||||
String graphPath; | String graphPath; | ||||
/** Number of ids to map */ | |||||
long nbIds; | |||||
/** mmap()-ed NODE_TO_SWHID file */ | /** mmap()-ed NODE_TO_SWHID file */ | ||||
MapFile nodeToSwhMap; | MapFile nodeToSwhMap; | ||||
/** Minimal perfect hash (MPH) function SWHID -> initial order */ | /** Minimal perfect hash (MPH) function SWHID -> initial order */ | ||||
Object2LongFunction<byte[]> mph; | Object2LongFunction<byte[]> mph; | ||||
/** mmap()-ed long list with the permutation initial order -> graph order */ | /** mmap()-ed long list with the permutation initial order -> graph order */ | ||||
LongBigList orderMap; | LongBigList orderMap; | ||||
/** FileInputStream containing the permutation */ | /** FileInputStream containing the permutation */ | ||||
FileInputStream orderInputStream; | FileInputStream orderInputStream; | ||||
/** | /** | ||||
* Constructor. | * Constructor. | ||||
* | * | ||||
* @param graphPath full graph path | * @param graphPath full graph path | ||||
* @param nbNodes number of nodes in the graph | |||||
*/ | */ | ||||
public NodeIdMap(String graphPath, long nbNodes) throws IOException { | public NodeIdMap(String graphPath) throws IOException { | ||||
this.graphPath = graphPath; | this.graphPath = graphPath; | ||||
this.nbIds = nbNodes; | |||||
// node -> SWHID | // node -> SWHID | ||||
this.nodeToSwhMap = new MapFile(graphPath + NODE_TO_SWHID, SWHID_BIN_SIZE); | this.nodeToSwhMap = new MapFile(graphPath + NODE_TO_SWHID, SWHID_BIN_SIZE); | ||||
// SWHID -> node | // SWHID -> node | ||||
this.mph = loadMph(graphPath + ".mph"); | this.mph = loadMph(graphPath + ".mph"); | ||||
this.orderInputStream = new FileInputStream(graphPath + ".order"); | this.orderInputStream = new FileInputStream(graphPath + ".order"); | ||||
this.orderMap = ByteBufferLongBigList.map(orderInputStream.getChannel()); | this.orderMap = ByteBufferLongBigList.map(orderInputStream.getChannel()); | ||||
▲ Show 20 Lines • Show All 98 Lines • ▼ Show 20 Lines | public class NodeIdMap { | ||||
* @return corresponding node as a {@link SWHID} | * @return corresponding node as a {@link SWHID} | ||||
* @see SWHID | * @see SWHID | ||||
*/ | */ | ||||
public SWHID getSWHID(long nodeId) { | public SWHID getSWHID(long nodeId) { | ||||
/* | /* | ||||
* Each line in NODE_TO_SWHID is formatted as: swhid The file is ordered by nodeId, meaning node0's | * Each line in NODE_TO_SWHID is formatted as: swhid The file is ordered by nodeId, meaning node0's | ||||
* swhid is at line 0, hence we can read the nodeId-th line to get corresponding swhid | * swhid is at line 0, hence we can read the nodeId-th line to get corresponding swhid | ||||
*/ | */ | ||||
if (nodeId < 0 || nodeId >= nbIds) { | if (nodeId < 0 || nodeId >= nodeToSwhMap.size()) { | ||||
throw new IllegalArgumentException("Node id " + nodeId + " should be between 0 and " + nbIds); | throw new IllegalArgumentException("Node id " + nodeId + " should be between 0 and " + nodeToSwhMap.size()); | ||||
} | } | ||||
return SWHID.fromBytes(nodeToSwhMap.readAtLine(nodeId)); | return SWHID.fromBytes(nodeToSwhMap.readAtLine(nodeId)); | ||||
} | } | ||||
/** | /** | ||||
* Closes the mapping files. | * Closes the mapping files. | ||||
*/ | */ | ||||
public void close() throws IOException { | public void close() throws IOException { | ||||
orderInputStream.close(); | orderInputStream.close(); | ||||
nodeToSwhMap.close(); | nodeToSwhMap.close(); | ||||
} | } | ||||
} | } |