Skip to content

Commit

Permalink
Merge pull request #112
Browse files Browse the repository at this point in the history
Use a random integer as the starting value for 'rev'
  • Loading branch information
uatuko authored Oct 20, 2024
2 parents 9a98d53 + 25c8df4 commit 96d61f7
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/db/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
add_library(db)
target_sources(db
PRIVATE
detail.cpp
pg.cpp
principals.cpp
tuples.cpp
Expand All @@ -9,13 +10,15 @@ target_sources(db
FILE_SET headers TYPE HEADERS
FILES
config.h
db.h
pg.h
principals.h
tuples.h
tuplets.h
PRIVATE
FILE_SET private_headers TYPE HEADERS
FILES
detail.h
common.h
)

Expand Down
14 changes: 14 additions & 0 deletions src/db/detail.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "detail.h"

#include <random>

namespace db {
namespace detail {
int rand() {
static std::random_device rd;
static std::mt19937 g(rd());

return g();
}
} // namespace detail
} // namespace db
7 changes: 7 additions & 0 deletions src/db/detail.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

namespace db {
namespace detail {
int rand();
} // namespace detail
} // namespace db
6 changes: 4 additions & 2 deletions src/db/principals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@

#include "err/errors.h"

#include "detail.h"

namespace db {
Principal::Principal(const Principal::Data &data) noexcept : _data(data), _rev(0) {
Principal::Principal(const Principal::Data &data) noexcept : _data(data), _rev(detail::rand()) {
if (_data.id.empty()) {
_data.id = xid::next();
}
}

Principal::Principal(Data &&data) noexcept : _data(std::move(data)), _rev(0) {
Principal::Principal(Data &&data) noexcept : _data(std::move(data)), _rev(detail::rand()) {
if (_data.id.empty()) {
_data.id = xid::next();
}
Expand Down
16 changes: 14 additions & 2 deletions src/db/principals_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,17 +235,29 @@ TEST_F(db_PrincipalsTest, retrieve) {
}

TEST_F(db_PrincipalsTest, rev) {
// Success: revision
{
const db::Principal::Data data{
.id = "id:db_PrincipalsTest.rev",
};

db::Principal pl(data), pr(data);
EXPECT_FALSE(pl == pr);
EXPECT_FALSE(pl.rev() == pr.rev());
EXPECT_EQ(pl.id(), pr.id());
}

// Success: revision increment
{
db::Principal principal({
.id = "id:db_PrincipalsTest.rev",
});

ASSERT_NO_THROW(principal.store());
EXPECT_EQ(0, principal.rev());

auto expected = principal.rev() + 1;
ASSERT_NO_THROW(principal.store());
EXPECT_EQ(1, principal.rev());
EXPECT_EQ(expected, principal.rev());
}

// Error: revision mismatch
Expand Down
5 changes: 3 additions & 2 deletions src/db/tuples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
#include "err/errors.h"

#include "common.h"
#include "detail.h"

namespace db {
Tuple::Tuple(const Tuple::Data &data) noexcept :
_data(data), _id(), _rev(0), _lHash(), _rHash(), _ridL(), _ridR() {
_data(data), _id(), _rev(detail::rand()), _lHash(), _rHash(), _ridL(), _ridR() {
sanitise();
}

Tuple::Tuple(Tuple::Data &&data) noexcept :
_data(std::move(data)), _id(), _rev(0), _lHash(), _rHash(), _ridL(), _ridR() {
_data(std::move(data)), _id(), _rev(detail::rand()), _lHash(), _rHash(), _ridL(), _ridR() {
sanitise();
}

Expand Down
18 changes: 16 additions & 2 deletions src/db/tuples_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,20 @@ TEST_F(db_TuplesTest, retrieve) {
}

TEST_F(db_TuplesTest, rev) {
// Success: revision
{
const db::Tuple::Data data{
.lEntityId = "left",
.lEntityType = "db_TuplesTest.rev",
.relation = "relation",
.rEntityId = "right",
.rEntityType = "db_TuplesTest.rev",
};

db::Tuple tl(data), tr(data);
EXPECT_FALSE(tl.rev() == tr.rev());
}

// Success: revision increment
{
db::Tuple tuple({
Expand All @@ -505,10 +519,10 @@ TEST_F(db_TuplesTest, rev) {
});

ASSERT_NO_THROW(tuple.store());
EXPECT_EQ(0, tuple.rev());

auto expected = tuple.rev() + 1;
ASSERT_NO_THROW(tuple.store());
EXPECT_EQ(1, tuple.rev());
EXPECT_EQ(expected, tuple.rev());
}

// Error: revision mismatch
Expand Down

0 comments on commit 96d61f7

Please sign in to comment.