#pragma once #include "database/db_transaction.hpp" #include "storage/model/properties/property_family.hpp" #include "utils/border.hpp" #include "utils/option.hpp" namespace tx { class Transaction; } class Label; class EdgeType; using EdgePropertyFamily = PropertyFamily; using VertexPropertyFamily = PropertyFamily; /* * DbAccessor * -Guarantees that access to Vertex and Edge is possible only through * VertexAccessor and EdgeAccessor. * -Guarantees that changing Vertex and Edge is possible only using * VertexAccessor returned by vertex_insert() method and * EdgeAccessor returned by edge_insert() method. * -Offers CRUD for Vertex and Edge except iterating over all edges. * * VertexAccessor * By default Vertex::accessor is empty. Caller has to call fill() method * to fetch valid data and check it's return value. fill() method returns * true if there is valid data for current transaction false otherwise. * Only exception to this rule is vertex_insert() method in DbAccessor * which returns by default filled VertexAccessor. * * EdgeAccessor * By default Edge::accessor is empty. Caller has to call fill() method * to * fetch valid data and check it's return value. fill() method returns * true * if there is valid data for current transaction false otherwise. * Only exception to this rule is edge_insert() method in DbAccessor * which * returns by default filled EdgeAccessor. */ class DbAccessor { public: DbAccessor(Db &db); DbAccessor(Db &db, tx::Transaction &t); //*******************VERTEX METHODS auto vertex_access(); Option vertex_find(const Id &id); // Creates new Vertex and returns filled VertexAccessor. VertexAccessor vertex_insert(); // ******************* EDGE METHODS Option edge_find(const Id &id); // Creates new Edge and returns filled EdgeAccessor. EdgeAccessor edge_insert(VertexAccessor const &from, VertexAccessor const &to); // ******************* LABEL METHODS const Label &label_find_or_create(const char *name); bool label_contains(const char *name); // ******************** TYPE METHODS const EdgeType &type_find_or_create(const char *name); bool type_contains(const char *name); // ******************** PROPERTY METHODS VertexPropertyFamily &vertex_property_family_get(const std::string &name); EdgePropertyFamily &edge_property_family_get(const std::string &name); // ******************** PROPERTY HELPER METHODS VertexPropertyFamily::PropertyType::PropertyFamilyKey vertex_property_key(const std::string &name, Type type); EdgePropertyFamily::PropertyType::PropertyFamilyKey edge_property_key(const std::string &name, Type type); template VertexPropertyFamily::PropertyType::PropertyTypeKey vertex_property_key(const std::string &name); template EdgePropertyFamily::PropertyType::PropertyTypeKey edge_property_key(const std::string &name); // ******************** TRANSACTION METHODS // True if commit was successful, or false if transaction was aborted. bool commit(); void abort(); private: // TODO: make this friend generic for all indexes. template friend class NonUniqueUnorderedIndex; template friend class UniqueOrderedIndex; DbTransaction db_transaction; }; // ********************** CONVENIENT FUNCTIONS template bool option_fill(Option &o) { return o.is_present() && o.get().fill(); }