From d7ae5e69ce6e82cd01c28ac7c4528af5996b9689 Mon Sep 17 00:00:00 2001 From: Marko Budiselic Date: Tue, 15 Dec 2015 23:35:09 +0100 Subject: [PATCH] RecordProxy work in progres... Initial test case for VertexProxy --- api/resources/include.hpp.template | 5 +-- api/resources/node.hpp | 9 ++++-- api/resources/relationship.hpp | 5 +-- api/restful/resource.hpp | 5 +-- api/restful/restful_resource.hpp | 5 +-- storage/model/properties/jsonwriter.hpp | 5 --- storage/record_proxy.hpp | 41 ++++++++++++++++++++++++- storage/vertex_proxy.hpp | 5 +-- storage/vertices.hpp | 7 +++-- 9 files changed, 58 insertions(+), 29 deletions(-) diff --git a/api/resources/include.hpp.template b/api/resources/include.hpp.template index c400bf923..cff903fa7 100644 --- a/api/resources/include.hpp.template +++ b/api/resources/include.hpp.template @@ -8,8 +8,7 @@ * well, only if you're editing the template file, that's ok :) */ -#ifndef MEMGRAPH_API_RESOURCES_INCLUDE_HPP -#define MEMGRAPH_API_RESOURCES_INCLUDE_HPP +#pragma once #include #include @@ -47,5 +46,3 @@ void init(ioc::Container& container) // insert("/node/{id:\\d+}"); } - -#endif diff --git a/api/resources/node.hpp b/api/resources/node.hpp index 028d4f96b..4bb0dc265 100644 --- a/api/resources/node.hpp +++ b/api/resources/node.hpp @@ -19,18 +19,21 @@ public: auto& transaction = db->tx_engine.begin(); // insert a new vertex - auto vertex = db->graph.vertices.insert(transaction); + auto vertex_proxy = db->graph.vertices.insert(transaction); // map fields for(auto it = req.json.MemberBegin(); it != req.json.MemberEnd(); ++it) { - vertex->data.props.set(it->name.GetString(), it->value.GetString()); + vertex_proxy.property( + it->name.GetString(), + it->value.GetString() + ); } // commit the transaction transaction.commit(); - return vertex; + return vertex_proxy.version(); }, [&req, &res](Vertex* node) { return res.send(properties_to_string(node)); diff --git a/api/resources/relationship.hpp b/api/resources/relationship.hpp index 80576bf84..5fa8389c2 100644 --- a/api/resources/relationship.hpp +++ b/api/resources/relationship.hpp @@ -1,5 +1,4 @@ -#ifndef MEMGRAPH_API_RESOURCES_RELATIONSHIP_HPP -#define MEMGRAPH_API_RESOURCES_RELATIONSHIP_HPP +#pragma once #include "api/restful/resource.hpp" @@ -27,5 +26,3 @@ public: return res.send("GET /db/data/relationship"); } }; - -#endif diff --git a/api/restful/resource.hpp b/api/restful/resource.hpp index ce9145247..80a00fbce 100644 --- a/api/restful/resource.hpp +++ b/api/restful/resource.hpp @@ -1,5 +1,4 @@ -#ifndef MEMGRAPH_API_RESTFUL_RESOURCE_HPP -#define MEMGRAPH_API_RESTFUL_RESOURCE_HPP +#pragma once #include "api/restful/restful_resource.hpp" @@ -49,5 +48,3 @@ protected: private: std::once_flag once_flag; }; - -#endif diff --git a/api/restful/restful_resource.hpp b/api/restful/restful_resource.hpp index 0f09e5fa1..72eed6639 100644 --- a/api/restful/restful_resource.hpp +++ b/api/restful/restful_resource.hpp @@ -1,5 +1,4 @@ -#ifndef MEMGRAPH_API_RESTFUL_RESTFUL_RESOURCE_HPP -#define MEMGRAPH_API_RESTFUL_RESTFUL_RESOURCE_HPP +#pragma once #include @@ -155,5 +154,3 @@ public: Restful(T& resource, sp::Speedy& app, const std::string& path) : detail::Methods(resource, app, path) {} }; - -#endif diff --git a/storage/model/properties/jsonwriter.hpp b/storage/model/properties/jsonwriter.hpp index 56f628e5c..179b64bf1 100644 --- a/storage/model/properties/jsonwriter.hpp +++ b/storage/model/properties/jsonwriter.hpp @@ -20,11 +20,6 @@ public: value.accept(*this); } - void handle(Null&) - { - buffer << "NULL"; - } - void handle(Bool& b) { buffer << (b.value() ? "true" : "false"); diff --git a/storage/record_proxy.hpp b/storage/record_proxy.hpp index fdc9d3ed9..59926566b 100644 --- a/storage/record_proxy.hpp +++ b/storage/record_proxy.hpp @@ -2,11 +2,32 @@ #include "transactions/transaction.hpp" #include "mvcc/version_list.hpp" +#include "storage/model/properties/property.hpp" -template +template class RecordProxy { public: + RecordProxy(T* record, Store *store, mvcc::VersionList *version_list) : + record(record), store(store), version_list(version_list) + { + } + + RecordProxy(const RecordProxy& record_proxy) = delete; + + RecordProxy(RecordProxy&& other) : + record(other.record), store(other.store), version_list(other.version_list) + { + other.record = nullptr; + other.store = nullptr; + other.version_list = nullptr; + } + + ~RecordProxy() + { + // TODO: implementation + } + Derived update(tx::Transaction& transaction) const { // TODO: implementation @@ -20,6 +41,24 @@ public: transaction.commit(); } + template + Property* property(const K& key) const + { + return record->data.props.find(key); + } + + template + void property(const K& key, const V& value) + { + record->data.props.template set(key, value); + // TODO: update the index + } + + T* version() + { + return record; + } + private: T* record; Store *store; diff --git a/storage/vertex_proxy.hpp b/storage/vertex_proxy.hpp index 258659158..bf7831ed4 100644 --- a/storage/vertex_proxy.hpp +++ b/storage/vertex_proxy.hpp @@ -1,9 +1,10 @@ #pragma once #include "record_proxy.hpp" -#include "vertices.hpp" + +class Vertices; class VertexProxy : public RecordProxy { - // TODO: implementation + using RecordProxy::RecordProxy; }; diff --git a/storage/vertices.hpp b/storage/vertices.hpp index 48a42a4d6..cd29c1d95 100644 --- a/storage/vertices.hpp +++ b/storage/vertices.hpp @@ -2,6 +2,7 @@ #include "vertex.hpp" #include "common.hpp" +#include "vertex_proxy.hpp" class Vertices { @@ -22,7 +23,7 @@ public: return vertex; } - Vertex* insert(tx::Transaction& transaction) + VertexProxy insert(tx::Transaction& transaction) { // get next vertex id auto next = counter.next(std::memory_order_acquire); @@ -39,7 +40,9 @@ public: auto vertex_accessor = inserted_vertex_record->second.access(transaction); auto vertex = vertex_accessor.insert(); - return vertex; + VertexProxy vertex_proxy(vertex, this, &inserted_vertex_record->second); + + return vertex_proxy; } Vertex* update(tx::Transaction& transaction, const Id& id)