Changeset View
Changeset View
Standalone View
Standalone View
java/server/src/main/java/org/softwareheritage/graph/AllowedEdges.java
package org.softwareheritage.graph; | package org.softwareheritage.graph; | ||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import org.softwareheritage.graph.Graph; | import org.softwareheritage.graph.Graph; | ||||
import org.softwareheritage.graph.Node; | import org.softwareheritage.graph.Node; | ||||
/** | /** | ||||
* Edge restriction based on node types, used when visiting the graph. | * Edge restriction based on node types, used when visiting the graph. | ||||
* <p> | |||||
* <a href="https://docs.softwareheritage.org/devel/swh-model/data-model.html">Software Heritage | |||||
* graph</a> contains multiple node types (contents, directories, revisions, ...) and restricting | |||||
* the traversal to specific node types is necessary for many querying operations: <a | |||||
* href="https://docs.softwareheritage.org/devel/swh-graph/use-cases.html">use cases</a>. | |||||
* | * | ||||
* @author Thibault Allançon | * @author Thibault Allançon | ||||
* @version 0.0.1 | * @version 0.0.1 | ||||
* @since 0.0.1 | * @since 0.0.1 | ||||
*/ | */ | ||||
public class AllowedEdges { | public class AllowedEdges { | ||||
/** Graph on which edge restriction is performed */ | /** Graph on which edge restriction is performed */ | ||||
Graph graph; | Graph graph; | ||||
/** | /** | ||||
* 2D boolean matrix storing access rights for all combination of src/dst node types (first | * 2D boolean matrix storing access rights for all combination of src/dst node types (first | ||||
* dimension is source, second dimension is destination), when edge restriction is not enforced | * dimension is source, second dimension is destination), when edge restriction is not enforced | ||||
* this array is set to null for early bypass. | * this array is set to null for early bypass. | ||||
*/ | */ | ||||
public boolean[][] restrictedTo; | public boolean[][] restrictedTo; | ||||
/** | /** | ||||
* Constructor. | * Constructor. | ||||
* | * | ||||
* @param graph the graph on which to perform edge restriction | * @param graph the graph on which to perform edge restriction | ||||
* @param edgesFmt a formatted string describing allowed edges (TODO: link API doc) | * @param edgesFmt a formatted string describing <a | ||||
* href="https://docs.softwareheritage.org/devel/swh-graph/api.html#terminology">allowed edges</a> | |||||
*/ | */ | ||||
public AllowedEdges(Graph graph, String edgesFmt) { | public AllowedEdges(Graph graph, String edgesFmt) { | ||||
this.graph = graph; | this.graph = graph; | ||||
int nbNodeTypes = Node.Type.values().length; | int nbNodeTypes = Node.Type.values().length; | ||||
this.restrictedTo = new boolean[nbNodeTypes][nbNodeTypes]; | this.restrictedTo = new boolean[nbNodeTypes][nbNodeTypes]; | ||||
// Special values (null, empty, "*") | // Special values (null, empty, "*") | ||||
if (edgesFmt == null || edgesFmt.isEmpty()) { | if (edgesFmt == null || edgesFmt.isEmpty()) { | ||||
return; | return; | ||||
} | } | ||||
if (edgesFmt.equals("*")) { | if (edgesFmt.equals("*")) { | ||||
// Allows for quick bypass (with simple null check) when no edge restriction | // Allows for quick bypass (with simple null check) when no edge restriction | ||||
restrictedTo = null; | restrictedTo = null; | ||||
return; | return; | ||||
} | } | ||||
// Format: "src1:dst1,src2:dst2,[...]" | // Format: "src1:dst1,src2:dst2,[...]" | ||||
// TODO: link API doc | |||||
String[] edgeTypes = edgesFmt.split(","); | String[] edgeTypes = edgesFmt.split(","); | ||||
for (String edgeType : edgeTypes) { | for (String edgeType : edgeTypes) { | ||||
String[] nodeTypes = edgeType.split(":"); | String[] nodeTypes = edgeType.split(":"); | ||||
if (nodeTypes.length != 2) { | if (nodeTypes.length != 2) { | ||||
throw new IllegalArgumentException("Cannot parse edge type: " + edgeType); | throw new IllegalArgumentException("Cannot parse edge type: " + edgeType); | ||||
} | } | ||||
ArrayList<Node.Type> srcTypes = Node.Type.parse(nodeTypes[0]); | ArrayList<Node.Type> srcTypes = Node.Type.parse(nodeTypes[0]); | ||||
Show All 32 Lines |