nodes could now again be created and found (skiplist is now in usage)

This commit is contained in:
Marko Budiselic 2015-12-08 20:49:45 +01:00
parent 62fcffffa5
commit 9877ff6834
5 changed files with 76 additions and 35 deletions

View File

@ -1,5 +1,4 @@
#ifndef MEMGRAPH_API_RESOURCES_NODE_HPP
#define MEMGRAPH_API_RESOURCES_NODE_HPP
#pragma once
#include <random>
@ -16,29 +15,24 @@ public:
void post(sp::Request& req, sp::Response& res)
{
task->run([this, &req]() {
// create transaction
auto& transaction = db->tx_engine.begin();
auto index_accessor = db->graph.vertices.access();
auto vertex_record = std::make_unique<VertexRecord>();
auto vertex_accessor = vertex_record->access(transaction);
auto vertex = vertex_accessor.insert();
// for(key, value in body)
// node->properties[key] = value;
// insert a new vertex
auto vertex = db->graph.vertex_store.insert(transaction);
// map fields
for(auto it = req.json.MemberBegin(); it != req.json.MemberEnd(); ++it)
{
vertex->data.props.set<String>(it->name.GetString(), it->value.GetString());
}
auto result = index_accessor.insert_unique(0, std::move(vertex_record));
transaction.commit();
// return result.first;
return nullptr;
return vertex;
},
[&req, &res](Vertex* node) {
return res.send("TODO");
// return res.send(properties_to_string(node));
return res.send(properties_to_string(node));
});
}
};
@ -51,14 +45,21 @@ public:
void get(sp::Request& req, sp::Response& res)
{
task->run([this, &req]() -> Vertex* {
// // read id param
// Id id(std::stoull(req.params[0]));
// // TODO: transaction?
// return db->graph.find_vertex(id);
return nullptr;
task->run([this, &req]() {
// create transaction
auto& transaction = db->tx_engine.begin();
// read id param
Id id(std::stoull(req.params[0]));
// find node
auto vertex = db->graph.vertex_store.find(transaction, id);
transaction.commit();
return vertex;
},
[&req, &res](Vertex* node) {
[&req, &res](const Vertex* node) {
if (node == nullptr) {
return res.send(http::Status::NotFound, "The node was not found");
}
@ -76,5 +77,3 @@ public:
return res.send("TODO");
}
};
#endif

View File

@ -74,7 +74,7 @@ public:
*/
VersionList(VersionList&& other)
{
this->head = other.head;
this->head = other.head.load();
other.head = nullptr;
}

View File

@ -1,17 +1,9 @@
#pragma once
#include <list>
#include "mvcc/atom.hpp"
#include "mvcc/store.hpp"
#include "mvcc/mvcc_error.hpp"
#include "mvcc/version_list.hpp"
#include "storage/vertex_store.hpp"
#include "data_structures/skiplist/skiplist.hpp"
#include "vertex.hpp"
#include "edge.hpp"
using VertexStore = SkipList<uint64_t, VertexRecord::uptr>;
using EdgeStore = SkipList<uint64_t, EdgeRecord::uptr>;
class Graph
@ -27,6 +19,6 @@ public:
// TODO: find vertex by Id
VertexStore vertices;
VertexStore vertex_store;
EdgeStore edges;
};

View File

@ -36,7 +36,7 @@ inline std::ostream& operator<<(std::ostream& stream, const Vertex& record)
}
// TODO: find more appropriate place for this
inline std::string properties_to_string(Vertex* vertex)
inline std::string properties_to_string(const Vertex* vertex)
{
// make a string buffer
StringBuffer buffer;

View File

@ -1,2 +1,52 @@
#pragma once
#include "vertex.hpp"
#include "transactions/transaction.hpp"
#include "mvcc/version_list.hpp"
#include "data_structures/skiplist/skiplist.hpp"
#include "utils/counters/atomic_counter.hpp"
class VertexStore
{
public:
Vertex* insert(tx::Transaction& transaction)
{
// get next vertex id
auto next = counter.next(std::memory_order_acquire);
// create new vertex record
VertexRecord vertex_record;
// insert the new vertex record into the vertex store
auto store_accessor = vertex_store.access();
auto result = store_accessor.insert_unique(next, std::move(vertex_record));
// create new vertex
auto inserted_vertex_record = result.first;
auto vertex_accessor = inserted_vertex_record->second.access(transaction);
auto vertex = vertex_accessor.insert();
return vertex;
}
const Vertex* find(tx::Transaction& transaction, const Id& id)
{
// find vertex record
auto store_accessor = vertex_store.access();
auto vertex_record = store_accessor.find(id);
if (vertex_record == store_accessor.end())
return nullptr;
// find vertex
auto vertex_accessor = vertex_record->second.access(transaction);
auto vertex = vertex_accessor.find();
return vertex;
}
private:
SkipList<uint64_t, VertexRecord> vertex_store;
AtomicCounter<uint64_t> counter;
};