<commit_before>#include "stdafx.h"

#include <boost/graph/graphviz.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/depth_first_search.hpp>
#include <boost/graph/visitors.hpp>
#include "task_serializer.h"
#include "../../../You-DataStore/datastore.h"

#include "task_graph_controller.h"

namespace You {
namespace QueryEngine {
namespace Internal {

/// \cond Shorten names
namespace {
	using TGC = TaskGraphController;
	using Vertex = TaskGraph::Vertex;
	using VIterator = TaskGraph::VIterator;
}
/// \endcond

Task::ID TGC::loadFromFile(TaskGraph& graph) {
	Task::ID maxID = 0;
	auto serialized =
		You::DataStore::DataStore::get().getAllTasks();
	std::for_each(serialized.cbegin(), serialized.cend(),
		[&] (const TaskSerializer::STask task) {
		auto t = TaskSerializer::deserialize(task);
		addTask(graph, t);
		maxID = std::max(t.getID(), maxID);
	});
	return maxID;
}

bool TGC::isTaskExist(TaskGraph& graph, const Task::ID id) {
	bool isExist = true;
	try {
		graph.getTask(id);
	} catch (const Exception::TaskNotFoundException&) {
		isExist = false;
	}
	return isExist;
}

void TGC::addTask(TaskGraph& g, const Task& task) {
	boost::add_vertex(task.getID(), g.graph);
	g.taskTable.insert({ task.getID(), task });
	if (task.getDependencies().size() > 0) {
		addAllDependencies(g, task);
	}
}

void TGC::addAllDependencies(TaskGraph& g, const Task& task) {
	for (const auto& cid : task.getDependencies()) {
		 addDependency(g, task.getID(), cid);
	}
}

void TGC::addDependency(TaskGraph& g, const Task::ID pid, const Task::ID cid) {
	boost::add_edge(cid, pid, g.graph);
}

void TGC::deleteTask(TaskGraph& g, const Task::ID id) {
	VIterator begin;
	VIterator end;
	VIterator next;
	bool removed = false;
	boost::tie(begin, end) = boost::vertices(g.graph);
	for (next = begin; !removed && next != end; begin = next) {
		const Task::ID idInVertex = g.graph[*next];
		if (idInVertex == id) {
			removed = true;
			boost::remove_vertex(*begin, g.graph);
		}
		++next;
	}
	if (!removed) {
		throw Exception::TaskNotFoundException();
	} else {
		g.taskTable.erase(id);
	}
}

void TGC::updateTask(TaskGraph& g, const Task& task) {
	auto found = g.taskTable.find(task.getID());
	if (found != g.taskTable.end()) {
		bool dependencyIsChanged =
			task.getDependencies() != (found->second).getDependencies();
		if (dependencyIsChanged) {
			auto backup = found->second;
			found->second = task;
			bool hasCycle = false;
			CycleDetector detector(hasCycle);
			rebuildGraph(g);
			boost::depth_first_search(g.graph, boost::visitor(detector));
			if (hasCycle) {
				found->second = backup;
				rebuildGraph(g);
				throw Exception::CircularDependencyException();
			}
		} else {
			found->second = task;
		}
	} else {
		throw Exception::TaskNotFoundException();
	}
}

void TGC::rebuildGraph(TaskGraph& g) {
	g.graph = TaskGraph::Graph();
	for (auto pair = g.taskTable.cbegin(); pair != g.taskTable.cend();
		++pair) {
		Vertex v = boost::add_vertex(g.graph);
		g.graph[v] = pair->first;
	}
	for (auto pair = g.taskTable.cbegin(); pair != g.taskTable.cend();
		++pair) {
		for (const auto& cid : (pair->second).getDependencies()) {
			boost::add_edge(cid, pair->first, g.graph);
		}
	}
}

/// \endcond
}  // namespace Internal
}  // namespace QueryEngine
}  // namespace You
<commit_msg>Rebuild graph when deleting too<commit_after>#include "stdafx.h"

#include <boost/graph/graphviz.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/depth_first_search.hpp>
#include <boost/graph/visitors.hpp>
#include "task_serializer.h"
#include "../../../You-DataStore/datastore.h"

#include "task_graph_controller.h"

namespace You {
namespace QueryEngine {
namespace Internal {

/// \cond Shorten names
namespace {
	using TGC = TaskGraphController;
	using Vertex = TaskGraph::Vertex;
	using VIterator = TaskGraph::VIterator;
}
/// \endcond

Task::ID TGC::loadFromFile(TaskGraph& graph) {
	Task::ID maxID = 0;
	auto serialized =
		You::DataStore::DataStore::get().getAllTasks();
	std::for_each(serialized.cbegin(), serialized.cend(),
		[&] (const TaskSerializer::STask task) {
		auto t = TaskSerializer::deserialize(task);
		addTask(graph, t);
		maxID = std::max(t.getID(), maxID);
	});
	return maxID;
}

bool TGC::isTaskExist(TaskGraph& graph, const Task::ID id) {
	bool isExist = true;
	try {
		graph.getTask(id);
	} catch (const Exception::TaskNotFoundException&) {
		isExist = false;
	}
	return isExist;
}

void TGC::addTask(TaskGraph& g, const Task& task) {
	boost::add_vertex(task.getID(), g.graph);
	g.taskTable.insert({ task.getID(), task });
	if (task.getDependencies().size() > 0) {
		addAllDependencies(g, task);
	}
}

void TGC::addAllDependencies(TaskGraph& g, const Task& task) {
	for (const auto& cid : task.getDependencies()) {
		 addDependency(g, task.getID(), cid);
	}
}

void TGC::addDependency(TaskGraph& g, const Task::ID pid, const Task::ID cid) {
	boost::add_edge(cid, pid, g.graph);
}

void TGC::deleteTask(TaskGraph& g, const Task::ID id) {
	VIterator begin;
	VIterator end;
	VIterator next;
	bool removed = false;
	boost::tie(begin, end) = boost::vertices(g.graph);
	for (next = begin; !removed && next != end; begin = next) {
		const Task::ID idInVertex = g.graph[*next];
		if (idInVertex == id) {
			removed = true;
			boost::remove_vertex(*begin, g.graph);
		}
		++next;
	}
	if (!removed) {
		throw Exception::TaskNotFoundException();
	} else {
		g.taskTable.erase(id);
		rebuildGraph(g);
	}
}

void TGC::updateTask(TaskGraph& g, const Task& task) {
	auto found = g.taskTable.find(task.getID());
	if (found != g.taskTable.end()) {
		bool dependencyIsChanged =
			task.getDependencies() != (found->second).getDependencies();
		if (dependencyIsChanged) {
			auto backup = found->second;
			found->second = task;
			bool hasCycle = false;
			CycleDetector detector(hasCycle);
			rebuildGraph(g);
			boost::depth_first_search(g.graph, boost::visitor(detector));
			if (hasCycle) {
				found->second = backup;
				rebuildGraph(g);
				throw Exception::CircularDependencyException();
			}
		} else {
			found->second = task;
		}
	} else {
		throw Exception::TaskNotFoundException();
	}
}

void TGC::rebuildGraph(TaskGraph& g) {
	g.graph = TaskGraph::Graph();
	for (auto pair = g.taskTable.cbegin(); pair != g.taskTable.cend();
		++pair) {
		Vertex v = boost::add_vertex(g.graph);
		g.graph[v] = pair->first;
	}
	for (auto pair = g.taskTable.cbegin(); pair != g.taskTable.cend();
		++pair) {
		for (const auto& cid : (pair->second).getDependencies()) {
			boost::add_edge(cid, pair->first, g.graph);
		}
	}
}

/// \endcond
}  // namespace Internal
}  // namespace QueryEngine
}  // namespace You

{'fraction_non_alphanumeric': 0.16739490733765255, 'fraction_numerical': 0.0006026819346090101, 'mean_word_length': 4.467874794069193, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2141194', 'n_tokens_mistral': 2512, 'n_tokens_neox': 2375, 'n_words': 499}