2017-04-10 21:44:36 +08:00
|
|
|
#include <functional>
|
|
|
|
|
2017-03-29 18:37:58 +08:00
|
|
|
#include "config/config.hpp"
|
2017-02-04 16:01:15 +08:00
|
|
|
#include "database/creation_exception.hpp"
|
2017-04-10 21:44:36 +08:00
|
|
|
#include "database/graph_db.hpp"
|
2017-03-29 18:37:58 +08:00
|
|
|
#include "logging/logger.hpp"
|
|
|
|
#include "storage/edge.hpp"
|
|
|
|
#include "storage/garbage_collector.hpp"
|
2017-02-15 21:10:16 +08:00
|
|
|
//#include "snapshot/snapshoter.hpp"
|
2017-02-04 16:01:15 +08:00
|
|
|
|
2017-03-29 18:37:58 +08:00
|
|
|
const int DEFAULT_CLEANING_CYCLE_SEC = 30; // 30 seconds
|
|
|
|
|
|
|
|
GraphDb::GraphDb(const std::string &name, bool import_snapshot)
|
|
|
|
: name_(name),
|
2017-04-14 23:32:59 +08:00
|
|
|
gc_vertices_(vertices_, vertex_record_deleter_,
|
|
|
|
vertex_version_list_deleter_),
|
|
|
|
gc_edges_(edges_, edge_record_deleter_, edge_version_list_deleter_) {
|
2017-04-10 21:44:36 +08:00
|
|
|
const std::string time_str = CONFIG(config::CLEANING_CYCLE_SEC);
|
2017-03-29 18:37:58 +08:00
|
|
|
int pause = DEFAULT_CLEANING_CYCLE_SEC;
|
2017-04-10 21:44:36 +08:00
|
|
|
if (!time_str.empty()) pause = CONFIG_INTEGER(config::CLEANING_CYCLE_SEC);
|
|
|
|
// Pause of -1 means we shouldn't run the GC.
|
|
|
|
if (pause != -1) {
|
2017-04-14 23:32:59 +08:00
|
|
|
gc_scheduler_.Run(std::chrono::seconds(pause), [this]() {
|
|
|
|
// main garbage collection logic
|
|
|
|
// see wiki documentation for logic explanation
|
|
|
|
const auto next_id = this->tx_engine.count() + 1;
|
|
|
|
const auto id = this->tx_engine.oldest_active().get_or(next_id);
|
|
|
|
{
|
|
|
|
// This can be run concurrently
|
|
|
|
this->gc_vertices_.Run(id, this->tx_engine);
|
|
|
|
this->gc_edges_.Run(id, this->tx_engine);
|
|
|
|
}
|
|
|
|
// This has to be run sequentially after gc because gc modifies
|
|
|
|
// version_lists and changes the oldest visible record, on which Refresh
|
|
|
|
// depends.
|
|
|
|
{
|
|
|
|
// This can be run concurrently
|
|
|
|
this->labels_index_.Refresh(id, this->tx_engine);
|
|
|
|
this->edge_types_index_.Refresh(id, this->tx_engine);
|
|
|
|
}
|
|
|
|
this->edge_record_deleter_.FreeExpiredObjects(id);
|
|
|
|
this->vertex_record_deleter_.FreeExpiredObjects(id);
|
|
|
|
this->edge_version_list_deleter_.FreeExpiredObjects(id);
|
|
|
|
this->vertex_version_list_deleter_.FreeExpiredObjects(id);
|
|
|
|
});
|
2017-04-10 21:44:36 +08:00
|
|
|
}
|
2017-02-18 18:54:37 +08:00
|
|
|
// if (import_snapshot)
|
|
|
|
// snap_engine.import();
|
2017-02-04 16:01:15 +08:00
|
|
|
}
|
2017-04-14 23:32:59 +08:00
|
|
|
|
|
|
|
GraphDb::~GraphDb() {
|
|
|
|
// Stop the gc scheduler to not run into race conditions for deletions.
|
|
|
|
gc_scheduler_.Stop();
|
|
|
|
|
|
|
|
// Delete vertices and edges which weren't collected before, also deletes
|
|
|
|
// records inside version list
|
|
|
|
for (auto &vertex : this->vertices_.access()) delete vertex;
|
|
|
|
for (auto &edge : this->edges_.access()) delete edge;
|
|
|
|
|
|
|
|
// Free expired records with the maximal possible id from all the deleters.
|
|
|
|
this->edge_record_deleter_.FreeExpiredObjects(Id::MaximalId());
|
|
|
|
this->vertex_record_deleter_.FreeExpiredObjects(Id::MaximalId());
|
|
|
|
this->edge_version_list_deleter_.FreeExpiredObjects(Id::MaximalId());
|
|
|
|
this->vertex_version_list_deleter_.FreeExpiredObjects(Id::MaximalId());
|
|
|
|
}
|