Changeset View
Changeset View
Standalone View
Standalone View
java/src/main/java/org/softwareheritage/graph/NodesFiltering.java
- This file was added.
package org.softwareheritage.graph; | ||||||||||
import java.util.ArrayList; | ||||||||||
/** | ||||||||||
* class that manages the filtering of nodes that have been returned after a visit of the graph. | ||||||||||
* parameterized by a string that represents either no filtering (*) or a set of node types | ||||||||||
* | ||||||||||
* Exemples of query : | ||||||||||
* | ||||||||||
vlorentz: What about overloading the constructor with a variant with no argument, instead of a magic… | ||||||||||
* graph/visit/nodes/swh:1:rel:0000000000000000000000000000000000000010 return_types==rev will | ||||||||||
* return all the nodes without those have 'rev' type. | ||||||||||
* | ||||||||||
*/ | ||||||||||
public class NodesFiltering { | ||||||||||
boolean restricted; | ||||||||||
ArrayList<Node.Type> restrictedNodesTypes; | ||||||||||
/** | ||||||||||
* Default constructor, in order to handle the * case (all types of nodes are allowed to be | ||||||||||
* returned) | ||||||||||
* | ||||||||||
*/ | ||||||||||
public NodesFiltering() { | ||||||||||
restricted = false; | ||||||||||
Not Done Inline ActionsWouldn't this work? for (String type : types) { restrictedNodesTypes.add(Node.Type.fromStr(type)); } or even this? Collections.addAll(restrictedNodesTypes, types) vlorentz: Wouldn't this work?
```
for (String type : types) {… | ||||||||||
restrictedNodesTypes = Node.Type.parse("*"); | ||||||||||
} | ||||||||||
public NodesFiltering(String t) { | ||||||||||
restricted = true; | ||||||||||
restrictedNodesTypes = new ArrayList<Node.Type>(); | ||||||||||
String[] types = t.split(","); | ||||||||||
for (String type : types) { | ||||||||||
restrictedNodesTypes.add(Node.Type.fromStr(type)); | ||||||||||
} | ||||||||||
} | ||||||||||
public boolean typeIsAllowed(Node.Type typ) { | ||||||||||
return !this.restrictedNodesTypes.contains(typ); | ||||||||||
Not Done Inline ActionsDoesn't this return a value *and* modify the argument? this could be confusing to use. What about creating a new ArrayList and adding matching elements to it? vlorentz: Doesn't this return a value *and* modify the argument? this could be confusing to use.
What… | ||||||||||
Not Done Inline ActionsAnd I'm not very familiar with Java, but isn't ArrayList backed by a single dynamic array? This means every remove operation needs to copy all elements on the right, so filterByNodeTypes runs in quadratic time. vlorentz: And I'm not very familiar with Java, but isn't ArrayList backed by a single dynamic array? This… | ||||||||||
} | ||||||||||
/** | ||||||||||
* the function that filters the nodes returned, we browse the list of nodes found after a visit and | ||||||||||
* we create a new list with only the nodes that have a type that is not contained in the list of | ||||||||||
* not allowed types (restrictedNodesTypes) | ||||||||||
* | ||||||||||
* We need the graph in order to find the types of nodes. | ||||||||||
*/ | ||||||||||
public ArrayList<Long> filterByNodeTypes(ArrayList<Long> nodeIds, Graph g) { | ||||||||||
ArrayList<Long> filteredNodes = new ArrayList<Long>(); | ||||||||||
for (Long node : nodeIds) { | ||||||||||
if (this.typeIsAllowed(g.getNodeType(node))) { | ||||||||||
filteredNodes.add(node); | ||||||||||
} | ||||||||||
} | ||||||||||
return filteredNodes; | ||||||||||
} | ||||||||||
} | ||||||||||
Not Done Inline Actions
vlorentz: |
What about overloading the constructor with a variant with no argument, instead of a magic value *?