First runnable version of insert with the VertexProxy object
This commit is contained in:
parent
d7ae5e69ce
commit
7bfc610124
1
.gitignore
vendored
1
.gitignore
vendored
@ -8,3 +8,4 @@
|
||||
*.dSYM/
|
||||
memgraph
|
||||
*~
|
||||
*.pyc
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include "api/restful/resource.hpp"
|
||||
#include "mvcc/version_list.hpp"
|
||||
#include "debug/log.hpp"
|
||||
#include "api/response_json.hpp"
|
||||
|
||||
#pragma url /node
|
||||
class Nodes : public Resource<Nodes, POST>
|
||||
@ -33,10 +34,10 @@ public:
|
||||
// commit the transaction
|
||||
transaction.commit();
|
||||
|
||||
return vertex_proxy.version();
|
||||
return std::move(vertex_proxy);
|
||||
},
|
||||
[&req, &res](Vertex* node) {
|
||||
return res.send(properties_to_string(node));
|
||||
[&req, &res](VertexProxy&& vertex_proxy) {
|
||||
return res.send(http::Status::Created, vertex_create_response(vertex_proxy));
|
||||
});
|
||||
}
|
||||
};
|
||||
@ -64,11 +65,11 @@ public:
|
||||
|
||||
return vertex;
|
||||
},
|
||||
[&req, &res](const Vertex* node) {
|
||||
if (node == nullptr) {
|
||||
[&req, &res](const Vertex* vertex) {
|
||||
if (vertex == nullptr) {
|
||||
return res.send(http::Status::NotFound, "The node was not found");
|
||||
}
|
||||
return res.send(properties_to_string(node));
|
||||
return res.send(vertex_props_to_string(vertex));
|
||||
});
|
||||
}
|
||||
|
||||
@ -98,11 +99,11 @@ public:
|
||||
|
||||
return vertex;
|
||||
},
|
||||
[&req, &res](Vertex* node) {
|
||||
if (node == nullptr) {
|
||||
[&req, &res](Vertex* vertex) {
|
||||
if (vertex == nullptr) {
|
||||
return res.send(http::Status::NotFound, "The node was not found");
|
||||
}
|
||||
return res.send(properties_to_string(node));
|
||||
return res.send(vertex_props_to_string(vertex));
|
||||
});
|
||||
}
|
||||
|
||||
|
56
api/response_json.hpp
Normal file
56
api/response_json.hpp
Normal file
@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
#include "storage/vertex.hpp"
|
||||
#include "storage/vertex_proxy.hpp"
|
||||
#include "storage/writer/rapidjson_stringwriter.hpp"
|
||||
|
||||
StringBuffer vertex_props_to_buffer(const Vertex* vertex)
|
||||
{
|
||||
// make a string buffer
|
||||
StringBuffer buffer;
|
||||
JsonWriter<StringBuffer> writer(buffer);
|
||||
|
||||
// dump properties in this buffer
|
||||
vertex->data.props.accept(writer);
|
||||
writer.finish();
|
||||
|
||||
// respond to the use with the buffer
|
||||
return buffer;
|
||||
}
|
||||
|
||||
std::string vertex_props_to_string(const Vertex* vertex)
|
||||
{
|
||||
auto buffer = vertex_props_to_buffer(vertex);
|
||||
return std::move(buffer.str());
|
||||
}
|
||||
|
||||
// TODO: clear up naming
|
||||
using RJStringBuffer = rapidjson::StringBuffer;
|
||||
using RJStringWriter = rapidjson::Writer<RJStringBuffer>;
|
||||
using ptr_RJStringWriter = std::shared_ptr<RJStringWriter>;
|
||||
|
||||
std::string vertex_create_response(const VertexProxy& vertex_proxy)
|
||||
{
|
||||
// make a string buffer
|
||||
RJStringBuffer buffer;
|
||||
ptr_RJStringWriter writer = std::make_shared<RJStringWriter>(buffer);
|
||||
|
||||
writer->StartObject();
|
||||
writer->String("metadata");
|
||||
|
||||
writer->StartObject();
|
||||
writer->String("id");
|
||||
writer->Int64(vertex_proxy.record_id());
|
||||
writer->EndObject();
|
||||
|
||||
writer->String("data");
|
||||
writer->StartObject();
|
||||
RapidJsonStringWriter dataBuffer(writer);
|
||||
auto vertex = vertex_proxy.record_version();
|
||||
vertex->data.props.accept(dataBuffer);
|
||||
writer->EndObject();
|
||||
|
||||
writer->EndObject();
|
||||
|
||||
return std::move(buffer.GetString());
|
||||
}
|
@ -8,17 +8,24 @@ template <typename T, typename Store, typename Derived>
|
||||
class RecordProxy
|
||||
{
|
||||
public:
|
||||
RecordProxy(T* record, Store *store, mvcc::VersionList<T> *version_list) :
|
||||
record(record), store(store), version_list(version_list)
|
||||
RecordProxy(
|
||||
const Id& id,
|
||||
T* version,
|
||||
Store *store,
|
||||
mvcc::VersionList<T> *version_list) :
|
||||
id(id), version(version), 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)
|
||||
id(other.id), version(other.version), store(other.store),
|
||||
version_list(other.version_list)
|
||||
{
|
||||
other.record = nullptr;
|
||||
other.id = 0; // TODO: not very good idea because
|
||||
// replace with something else
|
||||
other.version = nullptr;
|
||||
other.store = nullptr;
|
||||
other.version_list = nullptr;
|
||||
}
|
||||
@ -44,23 +51,29 @@ public:
|
||||
template<typename K>
|
||||
Property* property(const K& key) const
|
||||
{
|
||||
return record->data.props.find(key);
|
||||
return version->data.props.find(key);
|
||||
}
|
||||
|
||||
template<typename K, typename V>
|
||||
void property(const K& key, const V& value)
|
||||
{
|
||||
record->data.props.template set<String>(key, value);
|
||||
version->data.props.template set<String>(key, value);
|
||||
// TODO: update the index
|
||||
}
|
||||
|
||||
T* version()
|
||||
Id record_id() const
|
||||
{
|
||||
return record;
|
||||
return id;
|
||||
}
|
||||
|
||||
T* record_version() const
|
||||
{
|
||||
return version;
|
||||
}
|
||||
|
||||
private:
|
||||
T* record;
|
||||
Id id;
|
||||
T* version;
|
||||
Store *store;
|
||||
mvcc::VersionList<T> *version_list;
|
||||
};
|
||||
|
@ -34,18 +34,3 @@ inline std::ostream& operator<<(std::ostream& stream, const Vertex& record)
|
||||
<< ", exp = " << record.tx.exp()
|
||||
<< "): " << buffer.str();
|
||||
}
|
||||
|
||||
// TODO: find more appropriate place for this
|
||||
inline std::string properties_to_string(const Vertex* vertex)
|
||||
{
|
||||
// make a string buffer
|
||||
StringBuffer buffer;
|
||||
JsonWriter<StringBuffer> writer(buffer);
|
||||
|
||||
// dump properties in this buffer
|
||||
vertex->data.props.accept(writer);
|
||||
writer.finish();
|
||||
|
||||
// respond to the use with the buffer
|
||||
return std::move(buffer.str());
|
||||
}
|
||||
|
@ -40,7 +40,7 @@ public:
|
||||
auto vertex_accessor = inserted_vertex_record->second.access(transaction);
|
||||
auto vertex = vertex_accessor.insert();
|
||||
|
||||
VertexProxy vertex_proxy(vertex, this, &inserted_vertex_record->second);
|
||||
VertexProxy vertex_proxy(next, vertex, this, &inserted_vertex_record->second);
|
||||
|
||||
return vertex_proxy;
|
||||
}
|
||||
|
58
storage/writer/rapidjson_stringwriter.hpp
Normal file
58
storage/writer/rapidjson_stringwriter.hpp
Normal file
@ -0,0 +1,58 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "rapidjson/writer.h"
|
||||
#include "rapidjson/stringbuffer.h"
|
||||
#include "storage/model/properties/properties.hpp"
|
||||
|
||||
class RapidJsonStringWriter
|
||||
{
|
||||
public:
|
||||
// TODO: clear up naming
|
||||
using sptr = std::shared_ptr<rapidjson::Writer<rapidjson::StringBuffer>>;
|
||||
|
||||
RapidJsonStringWriter(const sptr& writer) :
|
||||
writer(writer)
|
||||
{
|
||||
}
|
||||
|
||||
void handle(const std::string& key, Property& value, bool first)
|
||||
{
|
||||
writer->String(key.c_str());
|
||||
value.accept(*this);
|
||||
}
|
||||
|
||||
void handle(Bool& b)
|
||||
{
|
||||
writer->String(b.value() ? "true" : "false");
|
||||
}
|
||||
|
||||
void handle(String& s)
|
||||
{
|
||||
writer->String(s.value.c_str());
|
||||
}
|
||||
|
||||
void handle(Int32& int32)
|
||||
{
|
||||
writer->String(std::to_string(int32.value).c_str());
|
||||
}
|
||||
|
||||
void handle(Int64& int64)
|
||||
{
|
||||
writer->String(std::to_string(int64.value).c_str());
|
||||
}
|
||||
|
||||
void handle(Float& f)
|
||||
{
|
||||
writer->String(std::to_string(f.value).c_str());
|
||||
}
|
||||
|
||||
void handle(Double& d)
|
||||
{
|
||||
writer->String(std::to_string(d.value).c_str());
|
||||
}
|
||||
|
||||
private:
|
||||
sptr writer;
|
||||
};
|
3
test/unit/http_api/run.sh
Executable file
3
test/unit/http_api/run.sh
Executable file
@ -0,0 +1,3 @@
|
||||
#!/bin/sh
|
||||
|
||||
python -m unittest discover -v -s ./ -p *_test.py
|
26
test/unit/http_api/vertex_crud_test.py
Normal file
26
test/unit/http_api/vertex_crud_test.py
Normal file
@ -0,0 +1,26 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import unittest
|
||||
import requests
|
||||
|
||||
|
||||
class VertexCrudTest(unittest.TestCase):
|
||||
|
||||
def test_crud(self):
|
||||
'''
|
||||
create -> read -> update -> read -> delete -> read
|
||||
'''
|
||||
endpoint = 'http://localhost:7474/db/data/node'
|
||||
payload = { "foo1": "bar1", "foo2": "bar2" }
|
||||
r = requests.post(endpoint, json = payload)
|
||||
self.assertEqual(r.status_code, 201)
|
||||
response = r.json()
|
||||
self.assertIn("metadata", response)
|
||||
metadata = response["metadata"]
|
||||
self.assertIn("id", metadata)
|
||||
identifier = metadata["id"]
|
||||
self.assertIn("data", response)
|
||||
data = response["data"]
|
||||
self.assertDictEqual(payload, data)
|
||||
|
||||
# TODO: read -> update -> read -> delete -> read
|
Loading…
Reference in New Issue
Block a user