data: removed Singleton from Dictionary and made it a member of SearchIndex

Dictionary never had a good reason of being a Singleton in the first place so this will avoid concurrency and
structural issues in the future.
This commit is contained in:
Eberhard Graether
2014-09-30 22:10:42 +02:00
parent 95ece9ef2e
commit 390df1db1f
13 changed files with 128 additions and 103 deletions
+15 -16
View File
@@ -5,44 +5,43 @@
class DictionaryTestSuite: public CxxTest::TestSuite
{
public:
void test_get_instance()
{
TS_ASSERT(Dictionary::getInstance());
}
void test_does_not_find_unsaved_word()
{
TS_ASSERT_EQUALS("", Dictionary::getInstance()->getWord(12));
Dictionary dictionary;
TS_ASSERT_EQUALS("", dictionary.getWord(12));
}
void test_can_save_word_and_retrieve_it_with_id()
{
Id id = Dictionary::getInstance()->getWordId("hello world!");
Dictionary dictionary;
Id id = dictionary.getWordId("hello world!");
TS_ASSERT_LESS_THAN(0, id);
TS_ASSERT_EQUALS("hello world!", Dictionary::getInstance()->getWord(id));
TS_ASSERT_EQUALS("hello world!", dictionary.getWord(id));
}
void test_can_save_multiple_words_and_retrieve_it_with_id()
{
std::deque<Id> ids = Dictionary::getInstance()->getWordIds("hello world!", " ");
Dictionary dictionary;
std::deque<Id> ids = dictionary.getWordIds("hello world!", " ");
TS_ASSERT_EQUALS(2, ids.size());
TS_ASSERT_EQUALS("hello", Dictionary::getInstance()->getWord(ids.front()));
TS_ASSERT_EQUALS("world!", Dictionary::getInstance()->getWord(ids.back()));
TS_ASSERT_EQUALS("hello world!", Dictionary::getInstance()->getWord(ids, " "));
TS_ASSERT_EQUALS("hello", dictionary.getWord(ids.front()));
TS_ASSERT_EQUALS("world!", dictionary.getWord(ids.back()));
TS_ASSERT_EQUALS("hello world!", dictionary.getWord(ids, " "));
}
void test_next_word_gets_different_id()
{
Id id = Dictionary::getInstance()->getWordId("hello world!");
Id id2 = Dictionary::getInstance()->getWordId("foobar");
Dictionary dictionary;
Id id = dictionary.getWordId("hello world!");
Id id2 = dictionary.getWordId("foobar");
TS_ASSERT_LESS_THAN(0, id);
TS_ASSERT_LESS_THAN(0, id2);
TS_ASSERT_LESS_THAN(id, id2);
TS_ASSERT_EQUALS("hello world!", Dictionary::getInstance()->getWord(id));
TS_ASSERT_EQUALS("foobar", Dictionary::getInstance()->getWord(id2));
TS_ASSERT_EQUALS("hello world!", dictionary.getWord(id));
TS_ASSERT_EQUALS("foobar", dictionary.getWord(id2));
}
};
+8 -7
View File
@@ -167,16 +167,16 @@ public:
void test_graph_saves_nodes_with_distinct_signatures()
{
TestStorageGraph 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");
Node* a1 = graph.createNodeHierarchyWithDistinctSignature(Node::NODE_FUNCTION, "A", 1);
Node* a2 = graph.createNodeHierarchyWithDistinctSignature(Node::NODE_FUNCTION, "A", 2);
Node* a3 = graph.createNodeHierarchyWithDistinctSignature(Node::NODE_FUNCTION, "A", 2);
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");
Node* c1 = graph.createNodeHierarchyWithDistinctSignature(Node::NODE_METHOD, "B::C", 3);
Node* c2 = graph.createNodeHierarchyWithDistinctSignature(Node::NODE_METHOD, "B::C", 4);
Node* c3 = graph.createNodeHierarchyWithDistinctSignature(Node::NODE_METHOD, "B::C", 4);
TS_ASSERT_DIFFERS(c1, c2);
TS_ASSERT_EQUALS(c2, c3);
@@ -300,9 +300,10 @@ private:
}
Node* createNodeHierarchyWithDistinctSignature(
Node::NodeType type, const std::string& name, const std::string& signature
Node::NodeType type, const std::string& name, Id signatureId
){
SearchIndex::SearchNode* searchNode = m_index.addNode(name);
std::shared_ptr<TokenComponentSignature> signature = std::make_shared<TokenComponentSignature>(signatureId);
return StorageGraph::createNodeHierarchyWithDistinctSignature(type, searchNode, signature);
}
+18 -5
View File
@@ -180,7 +180,7 @@ public:
TS_ASSERT_EQUALS(paramEdge->getTo()->getFullName(), "char");
TS_ASSERT(node->getComponent<TokenComponentSignature>());
TS_ASSERT_EQUALS(node->getComponent<TokenComponentSignature>()->getSignature(), "isTrue(char)");
TS_ASSERT_EQUALS(storage.getWord(node->getComponent<TokenComponentSignature>()->getWordId()), "isTrue(char)");
std::vector<TokenLocation*> locations = storage.getLocationsForId(id);
TS_ASSERT_EQUALS(locations.size(), 2);
@@ -214,7 +214,7 @@ public:
TS_ASSERT_EQUALS(paramEdge->getTo()->getFullName(), "bool");
TS_ASSERT(node->getComponent<TokenComponentSignature>());
TS_ASSERT_EQUALS(node->getComponent<TokenComponentSignature>()->getSignature(), "isMethod(bool)");
TS_ASSERT_EQUALS(storage.getWord(node->getComponent<TokenComponentSignature>()->getWordId()), "isMethod(bool)");
std::vector<TokenLocation*> locations = storage.getLocationsForId(id);
TS_ASSERT_EQUALS(locations.size(), 2);
@@ -511,17 +511,30 @@ private:
public:
Node* getNodeWithId(Id id) const
{
return dynamic_cast<Node*>(getTokenWithId(id));
return dynamic_cast<Node*>(getGraph().getTokenById(id));
}
Edge* getEdgeWithId(Id id) const
{
return dynamic_cast<Edge*>(getTokenWithId(id));
return dynamic_cast<Edge*>(getGraph().getTokenById(id));
}
std::vector<TokenLocation*> getLocationsForId(Id id) const
{
return getTokenLocationsForId(id);
const std::vector<Id>& locationIds = getGraph().getTokenById(id)->getLocationIds();
std::vector<TokenLocation*> result;
for (Id locationId : locationIds)
{
result.push_back(getTokenLocationCollection().findTokenLocationById(locationId));
}
return result;
}
const std::string& getWord(Id wordId) const
{
return getSearchIndex().getWord(wordId);
}
};