Compare commits

...

4 Commits

Author SHA1 Message Date
niko4299
91fd608a7f Removed extra fixes 2022-10-31 16:23:20 +01:00
niko4299
d294760550 FindEdge by edge_id and vertex_id 2022-10-31 15:42:35 +01:00
niko4299
45d9cd1656 Restore FindVertex 2022-10-28 16:28:41 +02:00
niko4299
3e07a367db Intial solution 2022-10-28 16:14:17 +02:00
4 changed files with 52 additions and 0 deletions

View File

@ -314,6 +314,12 @@ class DbAccessor final {
return std::nullopt;
}
std::optional<EdgeAccessor> FindEdge(storage::Gid edge_id, storage::Gid vertex_id) {
auto maybe_edge = accessor_->FindEdge(edge_id, vertex_id);
if (maybe_edge) return EdgeAccessor(*maybe_edge);
return std::nullopt;
}
void FinalizeTransaction() { accessor_->FinalizeTransaction(); }
VerticesIterable Vertices(storage::View view) { return VerticesIterable(accessor_->Vertices(view)); }
@ -483,6 +489,8 @@ class SubgraphDbAccessor final {
std::optional<VertexAccessor> FindVertex(storage::Gid gid, storage::View view);
std::optional<EdgeAccessor> FindEdge(storage::Gid gid);
Graph *getGraph();
};

View File

@ -650,6 +650,19 @@ TypedValue Labels(const TypedValue *args, int64_t nargs, const FunctionContext &
return TypedValue(std::move(labels));
}
TypedValue GetEdgeById(const TypedValue *args, int64_t nargs, const FunctionContext &ctx) {
FType<Or<Null, Integer>, Or<Null, Integer>>("edge_id", args, nargs);
auto *dba = ctx.db_accessor;
if (args[0].IsNull() || args[1].IsNull()) return TypedValue(ctx.memory);
auto edge_id = args[0].ValueInt();
auto vertex_id = args[1].ValueInt();
auto maybe_edge = dba->FindEdge(storage::Gid::FromUint(edge_id), storage::Gid::FromUint(vertex_id));
if (!maybe_edge) throw query::QueryRuntimeException("Edge doesn't exist.");
return TypedValue(*maybe_edge, ctx.memory);
}
TypedValue Nodes(const TypedValue *args, int64_t nargs, const FunctionContext &ctx) {
FType<Or<Null, Path>>("nodes", args, nargs);
if (args[0].IsNull()) return TypedValue(ctx.memory);
@ -1271,6 +1284,8 @@ std::function<TypedValue(const TypedValue *, int64_t, const FunctionContext &ctx
if (function_name == "RELATIONSHIPS") return Relationships;
if (function_name == "TAIL") return Tail;
if (function_name == "UNIFORMSAMPLE") return UniformSample;
if (function_name == "GET_EDGE_BY_ID") return GetEdgeById;
// if (function_name == "GET_EDGES_BY_ID") return GetEdgesById;
// Mathematical functions - numeric
if (function_name == "ABS") return Abs;

View File

@ -14,6 +14,8 @@
#include <atomic>
#include <memory>
#include <mutex>
#include <optional>
#include <ranges>
#include <variant>
#include <gflags/gflags.h>
@ -26,6 +28,7 @@
#include "storage/v2/durability/snapshot.hpp"
#include "storage/v2/durability/wal.hpp"
#include "storage/v2/edge_accessor.hpp"
#include "storage/v2/edge_ref.hpp"
#include "storage/v2/indices.hpp"
#include "storage/v2/mvcc.hpp"
#include "storage/v2/replication/config.hpp"
@ -524,6 +527,30 @@ std::optional<VertexAccessor> Storage::Accessor::FindVertex(Gid gid, View view)
return VertexAccessor::Create(&*it, &transaction_, &storage_->indices_, &storage_->constraints_, config_, view);
}
std::optional<EdgeAccessor> Storage::Accessor::FindEdge(Gid edge_id, Gid vertex_id) {
auto vertex_acc = storage_->vertices_.access();
auto vertex = &*vertex_acc.find(vertex_id);
auto it_in_edges =
std::ranges::find_if(vertex->in_edges.begin(), vertex->in_edges.end(), [edge_id](const auto &item) {
return (get<2>(item).ptr && get<2>(item).ptr->gid == edge_id) || get<2>(item).gid == edge_id;
});
if (it_in_edges != vertex->in_edges.end())
return EdgeAccessor{get<2>(*it_in_edges), get<0>(*it_in_edges), get<1>(*it_in_edges), vertex,
&transaction_, &storage_->indices_, &storage_->constraints_, config_};
auto it_out_edges =
std::ranges::find_if(vertex->out_edges.begin(), vertex->out_edges.end(), [edge_id](const auto &item) {
return (get<2>(item).ptr && get<2>(item).ptr->gid == edge_id) || get<2>(item).gid == edge_id;
});
if (it_out_edges != vertex->out_edges.end())
return EdgeAccessor{get<2>(*it_out_edges), get<0>(*it_out_edges), vertex, get<1>(*it_out_edges), &transaction_,
&storage_->indices_, &storage_->constraints_, config_};
return std::nullopt;
}
Result<std::optional<VertexAccessor>> Storage::Accessor::DeleteVertex(VertexAccessor *vertex) {
MG_ASSERT(vertex->transaction_ == &transaction_,
"VertexAccessor must be from the same transaction as the storage "

View File

@ -216,6 +216,8 @@ class Storage final {
std::optional<VertexAccessor> FindVertex(Gid gid, View view);
std::optional<EdgeAccessor> FindEdge(Gid edge_id, Gid vertex_id);
VerticesIterable Vertices(View view) {
return VerticesIterable(AllVerticesIterable(storage_->vertices_.access(), &transaction_, view,
&storage_->indices_, &storage_->constraints_,