Remove rand from dba
Reviewers: florijan, buda Reviewed By: florijan Subscribers: pullbot Differential Revision: https://phabricator.memgraph.io/D721
This commit is contained in:
parent
30ff78bf93
commit
9a39a125b2
@ -9,9 +9,7 @@
|
|||||||
#include "utils/on_scope_exit.hpp"
|
#include "utils/on_scope_exit.hpp"
|
||||||
|
|
||||||
GraphDbAccessor::GraphDbAccessor(GraphDb &db)
|
GraphDbAccessor::GraphDbAccessor(GraphDb &db)
|
||||||
: db_(db), transaction_(db.tx_engine_.Begin()) {
|
: db_(db), transaction_(db.tx_engine_.Begin()) {}
|
||||||
pseudo_rand_gen_.seed(std::random_device()());
|
|
||||||
}
|
|
||||||
|
|
||||||
GraphDbAccessor::~GraphDbAccessor() {
|
GraphDbAccessor::~GraphDbAccessor() {
|
||||||
if (!commited_ && !aborted_) {
|
if (!commited_ && !aborted_) {
|
||||||
@ -334,5 +332,3 @@ const std::string &GraphDbAccessor::PropertyName(
|
|||||||
debug_assert(!commited_ && !aborted_, "Accessor committed or aborted");
|
debug_assert(!commited_ && !aborted_, "Accessor committed or aborted");
|
||||||
return *property;
|
return *property;
|
||||||
}
|
}
|
||||||
|
|
||||||
double GraphDbAccessor::Rand() { return rand_dist_(pseudo_rand_gen_); }
|
|
||||||
|
@ -553,11 +553,6 @@ class GraphDbAccessor {
|
|||||||
if (!accessor.new_) accessor.new_ = accessor.vlist_->update(*transaction_);
|
if (!accessor.new_) accessor.new_ = accessor.vlist_->update(*transaction_);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a uniformly random-generated number from the [0, 1) interval.
|
|
||||||
*/
|
|
||||||
double Rand();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/**
|
/**
|
||||||
* Insert this vertex into corresponding label and label+property (if it
|
* Insert this vertex into corresponding label and label+property (if it
|
||||||
@ -598,8 +593,4 @@ class GraphDbAccessor {
|
|||||||
|
|
||||||
bool commited_{false};
|
bool commited_{false};
|
||||||
bool aborted_{false};
|
bool aborted_{false};
|
||||||
|
|
||||||
// Random number generation stuff.
|
|
||||||
std::mt19937 pseudo_rand_gen_;
|
|
||||||
std::uniform_real_distribution<> rand_dist_{0, 1};
|
|
||||||
};
|
};
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <random>
|
||||||
|
|
||||||
#include "query/exceptions.hpp"
|
#include "query/exceptions.hpp"
|
||||||
#include "utils/string.hpp"
|
#include "utils/string.hpp"
|
||||||
@ -453,11 +454,13 @@ TypedValue Pi(const std::vector<TypedValue> &args, GraphDbAccessor &) {
|
|||||||
return M_PI;
|
return M_PI;
|
||||||
}
|
}
|
||||||
|
|
||||||
TypedValue Rand(const std::vector<TypedValue> &args, GraphDbAccessor &dba) {
|
TypedValue Rand(const std::vector<TypedValue> &args, GraphDbAccessor &) {
|
||||||
|
static thread_local std::mt19937 pseudo_rand_gen_{std::random_device{}()};
|
||||||
|
static thread_local std::uniform_real_distribution<> rand_dist_{0, 1};
|
||||||
if (args.size() != 0U) {
|
if (args.size() != 0U) {
|
||||||
throw QueryRuntimeException("rand shouldn't be called with arguments");
|
throw QueryRuntimeException("rand shouldn't be called with arguments");
|
||||||
}
|
}
|
||||||
return dba.Rand();
|
return rand_dist_(pseudo_rand_gen_);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <bool (*Predicate)(const std::string &s1, const std::string &s2)>
|
template <bool (*Predicate)(const std::string &s1, const std::string &s2)>
|
||||||
|
@ -352,19 +352,6 @@ TEST(GraphDbAccessorTest, Transfer) {
|
|||||||
EXPECT_EQ(dba3->Transfer(e12)->PropsAt(prop).Value<int64_t>(), 12);
|
EXPECT_EQ(dba3->Transfer(e12)->PropsAt(prop).Value<int64_t>(), 12);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(GraphDbAccessorTest, Rand) {
|
|
||||||
Dbms dbms;
|
|
||||||
auto dba = dbms.active();
|
|
||||||
|
|
||||||
double a = dba->Rand();
|
|
||||||
EXPECT_GE(a, 0.0);
|
|
||||||
EXPECT_LT(a, 1.0);
|
|
||||||
double b = dba->Rand();
|
|
||||||
EXPECT_GE(b, 0.0);
|
|
||||||
EXPECT_LT(b, 1.0);
|
|
||||||
EXPECT_NE(a, b);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
::testing::InitGoogleTest(&argc, argv);
|
::testing::InitGoogleTest(&argc, argv);
|
||||||
// ::testing::GTEST_FLAG(filter) = "*.DetachRemoveVertex";
|
// ::testing::GTEST_FLAG(filter) = "*.DetachRemoveVertex";
|
||||||
|
Loading…
Reference in New Issue
Block a user