2017-02-04 16:01:15 +08:00
|
|
|
#pragma once
|
|
|
|
|
2017-11-29 23:03:42 +08:00
|
|
|
#include <memory>
|
|
|
|
|
LabelProperty index.
Summary:
Add return values.
After merge.
Inital working version. Still missing comments.
Update documentation.
Add checking for previous vlist and value equality.
After merge.
Remove functor, add boolean ffunction.
Build index.
More functionality. Start implementing tests.
Add tests.
Reviewers: matej.gradicek, mislav.bradac, mferencevic, buda, florijan
Reviewed By: mislav.bradac, buda, florijan
Subscribers: lion, florijan, teon.banek, buda, pullbot
Differential Revision: https://phabricator.memgraph.io/D355
2017-05-16 20:47:03 +08:00
|
|
|
#include "cppitertools/filter.hpp"
|
|
|
|
#include "cppitertools/imap.hpp"
|
|
|
|
|
2017-09-13 23:09:04 +08:00
|
|
|
#include "data_structures/concurrent/concurrent_map.hpp"
|
2017-02-18 18:54:37 +08:00
|
|
|
#include "data_structures/concurrent/concurrent_set.hpp"
|
2017-02-04 16:01:15 +08:00
|
|
|
#include "data_structures/concurrent/skiplist.hpp"
|
2017-03-29 18:37:58 +08:00
|
|
|
#include "database/graph_db_datatypes.hpp"
|
2017-04-05 23:23:00 +08:00
|
|
|
#include "database/indexes/key_index.hpp"
|
LabelProperty index.
Summary:
Add return values.
After merge.
Inital working version. Still missing comments.
Update documentation.
Add checking for previous vlist and value equality.
After merge.
Remove functor, add boolean ffunction.
Build index.
More functionality. Start implementing tests.
Add tests.
Reviewers: matej.gradicek, mislav.bradac, mferencevic, buda, florijan
Reviewed By: mislav.bradac, buda, florijan
Subscribers: lion, florijan, teon.banek, buda, pullbot
Differential Revision: https://phabricator.memgraph.io/D355
2017-05-16 20:47:03 +08:00
|
|
|
#include "database/indexes/label_property_index.hpp"
|
2017-11-13 16:50:49 +08:00
|
|
|
#include "durability/wal.hpp"
|
2017-02-04 16:01:15 +08:00
|
|
|
#include "mvcc/version_list.hpp"
|
2017-11-23 23:36:54 +08:00
|
|
|
#include "storage/concurrent_id_mapper.hpp"
|
2017-04-14 23:32:59 +08:00
|
|
|
#include "storage/deferred_deleter.hpp"
|
2017-03-29 18:37:58 +08:00
|
|
|
#include "storage/edge.hpp"
|
|
|
|
#include "storage/garbage_collector.hpp"
|
|
|
|
#include "storage/vertex.hpp"
|
2017-02-18 18:54:37 +08:00
|
|
|
#include "transactions/engine.hpp"
|
2017-04-10 21:44:36 +08:00
|
|
|
#include "utils/scheduler.hpp"
|
2017-02-04 16:01:15 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Main class which represents Database concept in code.
|
2017-02-16 22:47:55 +08:00
|
|
|
* This class is essentially a data structure. It exposes
|
|
|
|
* all the data publicly, and should therefore not be directly
|
|
|
|
* exposed to client functions. The GraphDbAccessor is used for that.
|
2017-10-30 17:43:25 +08:00
|
|
|
*
|
|
|
|
* Always be sure that GraphDb object is destructed before main exits, i. e.
|
|
|
|
* GraphDb object shouldn't be part of global/static variable, except if its
|
|
|
|
* destructor is explicitly called before main exits. Consider code:
|
|
|
|
*
|
|
|
|
* GraphDb db; // KeyIndex is created as a part of db.
|
|
|
|
* int main() {
|
|
|
|
* GraphDbAccessor dba(db);
|
|
|
|
* auto v = dba.InsertVertex();
|
|
|
|
* v.add_label(dba.Label(
|
|
|
|
* "Start")); // New SkipList is created in KeyIndex for LabelIndex.
|
|
|
|
* // That SkipList creates SkipListGc which
|
|
|
|
* // initialises static Executor object.
|
|
|
|
* return 0;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* After main exits: 1. Executor is destructed, 2. KeyIndex is destructed.
|
|
|
|
* Destructor of KeyIndex calls delete on created SkipLists which destroy
|
|
|
|
* SkipListGc that tries to use Excutioner object that doesn't exist anymore.
|
|
|
|
* -> CRASH
|
2017-02-04 16:01:15 +08:00
|
|
|
*/
|
2017-06-21 17:29:13 +08:00
|
|
|
class GraphDb {
|
2017-02-18 18:54:37 +08:00
|
|
|
public:
|
2017-11-20 18:58:05 +08:00
|
|
|
/// GraphDb configuration. Initialized from flags, but modifiable.
|
|
|
|
struct Config {
|
|
|
|
Config();
|
|
|
|
// Durability flags.
|
|
|
|
bool durability_enabled;
|
|
|
|
std::string durability_directory;
|
|
|
|
bool db_recover_on_startup;
|
|
|
|
int snapshot_cycle_sec;
|
|
|
|
int snapshot_max_retained;
|
|
|
|
int snapshot_on_exit;
|
|
|
|
|
|
|
|
// Misc flags.
|
|
|
|
int gc_cycle_sec;
|
|
|
|
int query_execution_time_sec;
|
|
|
|
};
|
|
|
|
|
|
|
|
explicit GraphDb(Config config = Config{});
|
2017-10-30 17:43:25 +08:00
|
|
|
/** Delete all vertices and edges and free all deferred deleters. */
|
2017-04-14 23:32:59 +08:00
|
|
|
~GraphDb();
|
2017-02-04 16:01:15 +08:00
|
|
|
|
2017-10-30 17:43:25 +08:00
|
|
|
/** Database object can't be copied. */
|
2017-02-04 16:01:15 +08:00
|
|
|
GraphDb(const GraphDb &db) = delete;
|
2017-06-27 00:43:07 +08:00
|
|
|
GraphDb(GraphDb &&other) = default;
|
|
|
|
GraphDb &operator=(const GraphDb &other) = default;
|
|
|
|
GraphDb &operator=(GraphDb &&other) = default;
|
2017-02-04 16:01:15 +08:00
|
|
|
|
2017-10-30 17:43:25 +08:00
|
|
|
/** Stop all transactions and set is_accepting_transactions_ to false. */
|
|
|
|
void Shutdown();
|
|
|
|
|
|
|
|
void CollectGarbage();
|
|
|
|
|
|
|
|
/** When this is false, no new transactions should be created. */
|
|
|
|
std::atomic<bool> is_accepting_transactions_{true};
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class GraphDbAccessor;
|
|
|
|
|
2017-06-06 23:04:49 +08:00
|
|
|
void StartSnapshooting();
|
|
|
|
|
2017-11-20 18:58:05 +08:00
|
|
|
Config config_;
|
|
|
|
|
2017-11-29 23:03:42 +08:00
|
|
|
/** Transaction engine related to this database. Master instance if this
|
|
|
|
* GraphDb is a single-node deployment, or the master in a distributed system.
|
|
|
|
* Otherwise a WorkerEngine instance. */
|
|
|
|
std::unique_ptr<tx::Engine> tx_engine_;
|
2017-02-04 16:01:15 +08:00
|
|
|
|
2017-11-13 16:50:49 +08:00
|
|
|
std::atomic<int64_t> next_vertex_id_{0};
|
2017-11-29 23:03:42 +08:00
|
|
|
std::atomic<int64_t> next_edge_id_{0};
|
2017-11-13 16:50:49 +08:00
|
|
|
|
2017-02-04 16:01:15 +08:00
|
|
|
// main storage for the graph
|
2017-11-13 16:50:49 +08:00
|
|
|
ConcurrentMap<int64_t, mvcc::VersionList<Vertex> *> vertices_;
|
|
|
|
ConcurrentMap<int64_t, mvcc::VersionList<Edge> *> edges_;
|
2017-04-14 23:32:59 +08:00
|
|
|
|
|
|
|
// Garbage collectors
|
2017-11-13 16:50:49 +08:00
|
|
|
GarbageCollector<ConcurrentMap<int64_t, mvcc::VersionList<Vertex> *>, Vertex>
|
|
|
|
gc_vertices_;
|
|
|
|
GarbageCollector<ConcurrentMap<int64_t, mvcc::VersionList<Edge> *>, Edge>
|
|
|
|
gc_edges_;
|
2017-02-04 16:01:15 +08:00
|
|
|
|
2017-04-14 23:32:59 +08:00
|
|
|
// Deleters for not relevant records
|
|
|
|
DeferredDeleter<Vertex> vertex_record_deleter_;
|
|
|
|
DeferredDeleter<Edge> edge_record_deleter_;
|
|
|
|
|
|
|
|
// Deleters for not relevant version_lists
|
|
|
|
DeferredDeleter<mvcc::VersionList<Vertex>> vertex_version_list_deleter_;
|
|
|
|
DeferredDeleter<mvcc::VersionList<Edge>> edge_version_list_deleter_;
|
|
|
|
|
2017-02-06 19:40:55 +08:00
|
|
|
// unique object stores
|
2017-03-29 18:37:58 +08:00
|
|
|
// TODO this should be also garbage collected
|
2017-11-23 23:36:54 +08:00
|
|
|
ConcurrentIdMapper<GraphDbTypes::Label, GraphDbTypes::Label::StorageT,
|
|
|
|
std::string>
|
|
|
|
labels_;
|
|
|
|
ConcurrentIdMapper<GraphDbTypes::EdgeType, GraphDbTypes::EdgeType::StorageT,
|
|
|
|
std::string>
|
|
|
|
edge_types_;
|
|
|
|
ConcurrentIdMapper<GraphDbTypes::Property, GraphDbTypes::Property::StorageT,
|
|
|
|
std::string>
|
|
|
|
properties_;
|
2017-05-30 16:26:09 +08:00
|
|
|
|
2017-04-03 17:26:32 +08:00
|
|
|
// indexes
|
2017-04-05 23:23:00 +08:00
|
|
|
KeyIndex<GraphDbTypes::Label, Vertex> labels_index_;
|
LabelProperty index.
Summary:
Add return values.
After merge.
Inital working version. Still missing comments.
Update documentation.
Add checking for previous vlist and value equality.
After merge.
Remove functor, add boolean ffunction.
Build index.
More functionality. Start implementing tests.
Add tests.
Reviewers: matej.gradicek, mislav.bradac, mferencevic, buda, florijan
Reviewed By: mislav.bradac, buda, florijan
Subscribers: lion, florijan, teon.banek, buda, pullbot
Differential Revision: https://phabricator.memgraph.io/D355
2017-05-16 20:47:03 +08:00
|
|
|
LabelPropertyIndex label_property_index_;
|
2017-04-10 21:44:36 +08:00
|
|
|
|
2017-10-30 17:43:25 +08:00
|
|
|
/**
|
2017-11-28 20:02:58 +08:00
|
|
|
* Set of transactions ids which are building indexes currently
|
2017-10-30 17:43:25 +08:00
|
|
|
*/
|
2017-11-28 20:02:58 +08:00
|
|
|
ConcurrentSet<tx::transaction_id_t> index_build_tx_in_progress_;
|
2017-08-11 15:48:13 +08:00
|
|
|
|
2017-11-13 16:50:49 +08:00
|
|
|
durability::WriteAheadLog wal_;
|
2017-05-17 16:08:57 +08:00
|
|
|
|
2017-04-10 21:44:36 +08:00
|
|
|
// Schedulers
|
2017-08-17 15:19:58 +08:00
|
|
|
Scheduler gc_scheduler_;
|
|
|
|
Scheduler snapshot_creator_;
|
2017-07-14 19:58:25 +08:00
|
|
|
// Periodically wakes up and hints to transactions that are running for a long
|
|
|
|
// time to stop their execution.
|
2017-08-17 15:19:58 +08:00
|
|
|
Scheduler transaction_killer_;
|
2017-09-13 23:09:04 +08:00
|
|
|
|
2017-10-30 17:43:25 +08:00
|
|
|
/** DB level global counters, used in the "counter" function. */
|
2017-09-13 23:09:04 +08:00
|
|
|
ConcurrentMap<std::string, std::atomic<int64_t>> counters_;
|
2017-02-04 16:01:15 +08:00
|
|
|
};
|