data: added SearchIndex for fuzzy name search and rewrote name handling in the Storage
This change stores each Token name in the separate singleton class Dictionary. The Dictionary gives each saved word an Id and thereby avoids duplicated names. E.g if the constructor method "Graph::Graph" is stored then the word "Graph" only appears once in memory. The class SearchIndex is now responsible for the name hierarchy and is instantiated by the Storage. The SearchIndex builds the name hierarchy using SearchNodes, each holding a Dictionary string reference of the name it holds. E.g if the names "math::ceil" and "math::floor" are added to the SearchIndex then 3 nodes get created, the SearchNode "math" will hold the two childs "ceil" and "floor". The hierarchical graph creation functionality got split off from Graph into the new subclass StorageGraph. The StorageGraph creates nodes with a passed SearchNode pointer of the name it represents in the SearchIndex. Thereby the StorageGraph reuses the hierarchical information in the SearchIndex and can create nodes much quicker by avoiding node searches and name comparisions. The name information is now stored in the Nodes via the TokenComponentName class, which is subclassed into TokenComponentNameReferenced and TokenComponentNameCached. The StorageClass creates nodes with the component TokenComponentNameReferenced, which holds a pointer to the SearchNode instance holding the name. This allows for retrieving the full name of the node, without using other Nodes int the Graph, which might not be present. If the Node is copied then the component changes to a TokenComponentNameCached, which holds the full name as a string, so the memory in the Storage doesn't have to be accessed anymore. TokenComponentSignature is now only holding an Id of the signature string saved in the Dictionary, which speeds up the signature comparison. A follow-up will change saving the whole signature as string to reusing the wordIds it is consisting of. Lastly the SearchIndex holds basic fuzzy search functionality. A passed query gets compared down the SearchNode hierarchy as long as matches for each letter are found. Matches must contain all letters of the query. The search is case-insensitive. If letters are found in front positions, next to each other or written in uppercase they are weighed higher in the match ranking. The character ':' is also interpreted and found, although the '::' delimiter is not stored. E.g. the query "m:l" used on the example above will return both "math::floor" and "math::ceil", but "floor" is ranked higher because the 'l' appears closer to the start.
This commit is contained in:
+26
-279
@@ -246,289 +246,51 @@ public:
|
||||
|
||||
void test_graph_saves_nodes()
|
||||
{
|
||||
TestGraph graph;
|
||||
Node* a = graph.createNodeHierarchy("A");
|
||||
Node* b = graph.createNodeHierarchy("B");
|
||||
Graph graph;
|
||||
Node a(Node::NODE_UNDEFINED, "A");
|
||||
Node b(Node::NODE_UNDEFINED, "B");
|
||||
|
||||
graph.addNode(&a);
|
||||
graph.addNode(&b);
|
||||
|
||||
TS_ASSERT_EQUALS(2, graph.getNodeCount());
|
||||
TS_ASSERT_EQUALS(0, graph.getEdgeCount());
|
||||
|
||||
TS_ASSERT_EQUALS(a, graph.getNode("A"));
|
||||
TS_ASSERT_EQUALS("A", graph.getNode("A")->getName());
|
||||
TS_ASSERT(graph.getNodeById(a.getId()));
|
||||
TS_ASSERT_EQUALS("A", graph.getNodeById(a.getId())->getName());
|
||||
|
||||
TS_ASSERT_EQUALS(b, graph.getNode("B"));
|
||||
TS_ASSERT_EQUALS("B", graph.getNode("B")->getName());
|
||||
TS_ASSERT(graph.getNodeById(b.getId()));
|
||||
TS_ASSERT_EQUALS("B", graph.getNodeById(b.getId())->getName());
|
||||
|
||||
TS_ASSERT(!graph.getNode("C"));
|
||||
TS_ASSERT(!graph.getNodeById(0));
|
||||
}
|
||||
|
||||
void test_graph_saves_edges()
|
||||
{
|
||||
TestGraph graph;
|
||||
Node* a = graph.createNodeHierarchy("A");
|
||||
Node* b = graph.createNodeHierarchy("B");
|
||||
Edge* e = graph.createEdge(Edge::EDGE_TYPE_OF, a, b);
|
||||
Graph graph;
|
||||
|
||||
TS_ASSERT_EQUALS(e, graph.getEdge(Edge::EDGE_TYPE_OF, a, b));
|
||||
TS_ASSERT(!graph.getEdge(Edge::EDGE_CALL, a, b));
|
||||
}
|
||||
Node a(Node::NODE_FUNCTION, "A");
|
||||
Node b(Node::NODE_FUNCTION, "B");
|
||||
|
||||
void test_graph_finds_nodes_and_edges_by_id()
|
||||
{
|
||||
TestGraph graph;
|
||||
Node* a = graph.createNodeHierarchy("A");
|
||||
Node* b = graph.createNodeHierarchy("B");
|
||||
Edge* e = graph.createEdge(Edge::EDGE_TYPE_OF, a, b);
|
||||
Edge e(Edge::EDGE_CALL, &a, &b);
|
||||
|
||||
TS_ASSERT(!graph.getEdgeById(a->getId()));
|
||||
TS_ASSERT_EQUALS(a, graph.getNodeById(a->getId()));
|
||||
TS_ASSERT_EQUALS(a, graph.getTokenById(a->getId()));
|
||||
graph.addEdge(&e);
|
||||
|
||||
TS_ASSERT(!graph.getNodeById(e->getId()));
|
||||
TS_ASSERT_EQUALS(e, graph.getEdgeById(e->getId()));
|
||||
TS_ASSERT_EQUALS(e, graph.getTokenById(e->getId()));
|
||||
}
|
||||
TS_ASSERT_EQUALS(0, graph.getNodeCount());
|
||||
TS_ASSERT_EQUALS(0, graph.getEdgeCount());
|
||||
|
||||
void test_graph_creates_child_edges()
|
||||
{
|
||||
TestGraph graph;
|
||||
Node* a = graph.createNodeHierarchy("A");
|
||||
Node* ab = graph.createNodeHierarchy("A::B");
|
||||
TS_ASSERT(!graph.getEdgeById(e.getId()));
|
||||
|
||||
graph.addNode(&a);
|
||||
graph.addNode(&b);
|
||||
|
||||
graph.addEdge(&e);
|
||||
|
||||
TS_ASSERT_EQUALS(2, graph.getNodeCount());
|
||||
TS_ASSERT_EQUALS(1, graph.getEdgeCount());
|
||||
|
||||
TS_ASSERT(ab->getMemberEdge());
|
||||
TS_ASSERT(graph.getEdge(Edge::EDGE_MEMBER, a, ab));
|
||||
TS_ASSERT_EQUALS(ab->getMemberEdge(), graph.getEdge(Edge::EDGE_MEMBER, a, ab));
|
||||
TS_ASSERT_EQUALS(ab->getMemberEdge()->getFrom(), a);
|
||||
TS_ASSERT_EQUALS(ab->getMemberEdge()->getTo(), ab);
|
||||
TS_ASSERT_EQUALS(ab->getParentNode(), a);
|
||||
}
|
||||
|
||||
void test_graph_removes_nodes()
|
||||
{
|
||||
TestGraph graph;
|
||||
Node* a = graph.createNodeHierarchy("A");
|
||||
Node* b = graph.createNodeHierarchy("B");
|
||||
graph.createNodeHierarchy("A::C");
|
||||
graph.createNodeHierarchy("A::C::D");
|
||||
graph.createEdge(Edge::EDGE_TYPE_OF, a, b);
|
||||
|
||||
graph.removeNode(a);
|
||||
|
||||
TS_ASSERT_EQUALS(1, graph.getNodeCount());
|
||||
TS_ASSERT_EQUALS(0, graph.getEdgeCount());
|
||||
|
||||
TS_ASSERT(!graph.getNode("A"));
|
||||
TS_ASSERT(!graph.getNode("A::C"));
|
||||
TS_ASSERT(graph.getNode("B"));
|
||||
}
|
||||
|
||||
void test_graph_removes_edge()
|
||||
{
|
||||
TestGraph graph;
|
||||
Node* a = graph.createNodeHierarchy("A");
|
||||
Node* b = graph.createNodeHierarchy("B");
|
||||
Edge* e = graph.createEdge(Edge::EDGE_TYPE_OF, a, b);
|
||||
|
||||
graph.removeEdge(e);
|
||||
|
||||
TS_ASSERT_EQUALS(2, graph.getNodeCount());
|
||||
TS_ASSERT_EQUALS(0, graph.getEdgeCount());
|
||||
|
||||
TS_ASSERT(graph.getNode("A"));
|
||||
TS_ASSERT(graph.getNode("B"));
|
||||
}
|
||||
|
||||
void test_graph_can_not_remove_member_edge()
|
||||
{
|
||||
TestGraph graph;
|
||||
Node* a = graph.createNodeHierarchy("A");
|
||||
Node* b = graph.createNodeHierarchy("B");
|
||||
Node* c = graph.createNodeHierarchy("A::C");
|
||||
graph.createEdge(Edge::EDGE_TYPE_OF, a, b);
|
||||
|
||||
graph.removeEdge(c->getMemberEdge());
|
||||
|
||||
TS_ASSERT_EQUALS(3, graph.getNodeCount());
|
||||
TS_ASSERT_EQUALS(2, graph.getEdgeCount());
|
||||
|
||||
TS_ASSERT(graph.getNode("A"));
|
||||
TS_ASSERT(graph.getNode("B"));
|
||||
TS_ASSERT(graph.getNode("A::C"));
|
||||
}
|
||||
|
||||
void test_node_in_graph_finds_child_node()
|
||||
{
|
||||
TestGraph graph;
|
||||
Node* a = graph.createNodeHierarchy("A");
|
||||
Node* b = graph.createNodeHierarchy("A::B");
|
||||
Node* c = graph.createNodeHierarchy("A::C");
|
||||
|
||||
Node* x = a->findChildNode(
|
||||
[](Node* n)
|
||||
{
|
||||
return n->getName() == "C";
|
||||
}
|
||||
);
|
||||
|
||||
TS_ASSERT_EQUALS(x, c);
|
||||
TS_ASSERT_DIFFERS(x, b);
|
||||
}
|
||||
|
||||
void test_node_has_name_and_full_name()
|
||||
{
|
||||
TestGraph graph;
|
||||
Node* n = graph.createNodeHierarchy("A::B::C");
|
||||
|
||||
TS_ASSERT_EQUALS(n->getName(), "C");
|
||||
TS_ASSERT_EQUALS(n->getFullName(), "A::B::C");
|
||||
}
|
||||
|
||||
void test_edge_has_name()
|
||||
{
|
||||
TestGraph graph;
|
||||
Node* a = graph.createNodeHierarchy(Node::NODE_FUNCTION, "A");
|
||||
Node* b = graph.createNodeHierarchy(Node::NODE_FUNCTION, "B");
|
||||
Edge* e = graph.createEdge(Edge::EDGE_CALL, a, b);
|
||||
|
||||
TS_ASSERT_EQUALS(e->getName(), "call:A->B");
|
||||
}
|
||||
|
||||
void test_graph_saves_nodes_with_distinct_signatures()
|
||||
{
|
||||
TestGraph graph;
|
||||
Node* a1 = graph.createNodeHierarchyWithDistinctSignature(Node::NODE_FUNCTION, "A", "A1");
|
||||
Node* a2 = graph.createNodeHierarchyWithDistinctSignature(Node::NODE_FUNCTION, "A", "A2");
|
||||
Node* a3 = graph.createNodeHierarchyWithDistinctSignature(Node::NODE_FUNCTION, "A", "A2");
|
||||
|
||||
TS_ASSERT_DIFFERS(a1, a2);
|
||||
TS_ASSERT_EQUALS(a2, a3);
|
||||
|
||||
Node* c1 = graph.createNodeHierarchyWithDistinctSignature(Node::NODE_METHOD, "B::C", "C1");
|
||||
Node* c2 = graph.createNodeHierarchyWithDistinctSignature(Node::NODE_METHOD, "B::C", "C2");
|
||||
Node* c3 = graph.createNodeHierarchyWithDistinctSignature(Node::NODE_METHOD, "B::C", "C2");
|
||||
|
||||
TS_ASSERT_DIFFERS(c1, c2);
|
||||
TS_ASSERT_EQUALS(c2, c3);
|
||||
}
|
||||
|
||||
void test_graph_saves_nodes_as_undefined_function_when_using_signatures()
|
||||
{
|
||||
TestGraph graph;
|
||||
Node* a1 = graph.createNodeHierarchyWithDistinctSignature("A", "A1");
|
||||
Node* a2 = graph.createNodeHierarchyWithDistinctSignature("A", "A2");
|
||||
|
||||
TS_ASSERT_DIFFERS(a1, a2);
|
||||
TS_ASSERT_EQUALS(a1->getType(), Node::NODE_UNDEFINED_FUNCTION);
|
||||
TS_ASSERT_EQUALS(a2->getType(), Node::NODE_UNDEFINED_FUNCTION);
|
||||
}
|
||||
|
||||
void test_graph_creates_multiple_nodes_as_undefined_nodes()
|
||||
{
|
||||
TestGraph graph;
|
||||
Node* abc = graph.createNodeHierarchy("A::B::C");
|
||||
|
||||
TS_ASSERT_EQUALS(3, graph.getNodeCount());
|
||||
TS_ASSERT_EQUALS(2, graph.getEdgeCount());
|
||||
|
||||
TS_ASSERT_EQUALS("A", graph.getNode("A")->getName());
|
||||
TS_ASSERT_EQUALS(Node::NODE_UNDEFINED, graph.getNode("A")->getType());
|
||||
|
||||
TS_ASSERT_EQUALS("A::B", graph.getNode("A::B")->getFullName());
|
||||
TS_ASSERT_EQUALS(Node::NODE_UNDEFINED, graph.getNode("A::B")->getType());
|
||||
|
||||
TS_ASSERT_EQUALS(abc, graph.getNode("A::B::C"));
|
||||
TS_ASSERT_EQUALS("A::B::C", graph.getNode("A::B::C")->getFullName());
|
||||
TS_ASSERT_EQUALS(Node::NODE_UNDEFINED, graph.getNode("A::B::C")->getType());
|
||||
|
||||
Node* abcde = graph.createNodeHierarchy("A::B::C::D::E");
|
||||
|
||||
TS_ASSERT_EQUALS(5, graph.getNodeCount());
|
||||
TS_ASSERT_EQUALS(4, graph.getEdgeCount());
|
||||
|
||||
TS_ASSERT_EQUALS("A::B::C::D", graph.getNode("A::B::C::D")->getFullName());
|
||||
TS_ASSERT_EQUALS(Node::NODE_UNDEFINED, graph.getNode("A::B::C::D")->getType());
|
||||
|
||||
TS_ASSERT_EQUALS(abcde, graph.getNode("A::B::C::D::E"));
|
||||
TS_ASSERT_EQUALS("A::B::C::D::E", graph.getNode("A::B::C::D::E")->getFullName());
|
||||
TS_ASSERT_EQUALS(Node::NODE_UNDEFINED, graph.getNode("A::B::C::D::E")->getType());
|
||||
}
|
||||
|
||||
void test_visit_each_token_on_graph()
|
||||
{
|
||||
TestGraph graph;
|
||||
Node* a = graph.createNodeHierarchy("A");
|
||||
Node* b = graph.createNodeHierarchy("B");
|
||||
Node* c = graph.createNodeHierarchy("C");
|
||||
Edge* e = graph.createEdge(Edge::EDGE_TYPE_OF, a, b);
|
||||
Edge* f = graph.createEdge(Edge::EDGE_TYPE_OF, b, c);
|
||||
|
||||
unsigned long idSum = a->getId() + b->getId() + c->getId() + e->getId() + f->getId();
|
||||
unsigned long checkSum = 0;
|
||||
|
||||
graph.forEachToken(
|
||||
[&checkSum](Token* t)
|
||||
{
|
||||
checkSum += t->getId();
|
||||
}
|
||||
);
|
||||
|
||||
TS_ASSERT_EQUALS(idSum, checkSum);
|
||||
}
|
||||
|
||||
void test_visit_each_edge_of_type_on_node()
|
||||
{
|
||||
TestGraph graph;
|
||||
Node* a = graph.createNodeHierarchy("A");
|
||||
Node* ab = graph.createNodeHierarchy("A::B");
|
||||
Node* ac = graph.createNodeHierarchy("A::C");
|
||||
|
||||
graph.createEdge(Edge::EDGE_TYPE_OF, a, ab);
|
||||
graph.createEdge(Edge::EDGE_CALL, a, ac);
|
||||
|
||||
unsigned int sum = 0;
|
||||
a->forEachEdgeOfType(Edge::EDGE_MEMBER, [&sum](Edge* e)
|
||||
{
|
||||
TS_ASSERT_EQUALS(Edge::EDGE_MEMBER, e->getType());
|
||||
sum++;
|
||||
});
|
||||
|
||||
TS_ASSERT_EQUALS(sum, 2);
|
||||
}
|
||||
|
||||
void test_creating_plain_copy_of_graph_part()
|
||||
{
|
||||
TestGraph graph;
|
||||
Node* b = graph.createNodeHierarchy("A::B");
|
||||
graph.createNodeHierarchy("A::B::C");
|
||||
Node* d = graph.createNodeHierarchy("D");
|
||||
Node* e = graph.createNodeHierarchy("E");
|
||||
graph.createEdge(Edge::EDGE_TYPE_OF, d, b);
|
||||
graph.createEdge(Edge::EDGE_TYPE_OF, d, e);
|
||||
|
||||
TestGraph plainGraph;
|
||||
Node* x = graph.getNode("A::B");
|
||||
plainGraph.addNodeAsPlainCopy(x);
|
||||
|
||||
x->forEachEdge(
|
||||
[&plainGraph](Edge* e)
|
||||
{
|
||||
plainGraph.addNodeAsPlainCopy(e->getFrom());
|
||||
plainGraph.addNodeAsPlainCopy(e->getTo());
|
||||
plainGraph.addEdgeAsPlainCopy(e);
|
||||
}
|
||||
);
|
||||
|
||||
TS_ASSERT_EQUALS(4, plainGraph.getNodeCount());
|
||||
TS_ASSERT_EQUALS(3, plainGraph.getEdgeCount());
|
||||
|
||||
TS_ASSERT(plainGraph.getNode("A"));
|
||||
TS_ASSERT(plainGraph.getNode("A::B"));
|
||||
TS_ASSERT(plainGraph.getNode("A::B::C"));
|
||||
TS_ASSERT(plainGraph.getNode("D"));
|
||||
TS_ASSERT(!plainGraph.getNode("E"));
|
||||
TS_ASSERT(graph.getEdgeById(e.getId()));
|
||||
TS_ASSERT_EQUALS(Edge::EDGE_CALL, graph.getEdgeById(e.getId())->getType());
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -583,19 +345,4 @@ private:
|
||||
return std::make_shared<Test2Component>(*this);
|
||||
}
|
||||
};
|
||||
|
||||
class TestGraph: public Graph
|
||||
{
|
||||
public:
|
||||
size_t getNodeCount() const
|
||||
{
|
||||
return getNodes().size();
|
||||
}
|
||||
|
||||
size_t getEdgeCount() const
|
||||
{
|
||||
return getEdges().size();
|
||||
}
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user