Skip to content

Commit 8472a58

Browse files
authored
Feature: BlankNode::make_uuid (#444)
1 parent e5db74b commit 8472a58

3 files changed

Lines changed: 25 additions & 0 deletions

File tree

src/rdf4cpp/BlankNode.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
#include <cstring>
99

10+
#include <boost/uuid/random_generator.hpp>
11+
#include <boost/uuid/uuid.hpp>
12+
#include <boost/uuid/uuid_io.hpp>
13+
1014
namespace rdf4cpp {
1115

1216
namespace detail_bnode_inlining {
@@ -64,6 +68,12 @@ BlankNode BlankNode::make(std::string_view identifier, storage::DynNodeStoragePt
6468
return BlankNode{identifier, node_storage};
6569
}
6670

71+
BlankNode BlankNode::make_uuid(storage::DynNodeStoragePtr node_storage) {
72+
boost::uuids::random_generator_mt19937 gen{};
73+
boost::uuids::uuid const u = gen();
74+
return BlankNode{boost::uuids::to_string(u), node_storage};
75+
}
76+
6777
BlankNode BlankNode::make_unchecked(std::string_view identifier, storage::DynNodeStoragePtr node_storage) {
6878
auto const id = [&]() {
6979
if (auto const inlined = detail_bnode_inlining::try_into_inlined(identifier); !inlined.null()) {

src/rdf4cpp/BlankNode.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ struct BlankNode : Node {
3030
*/
3131
[[nodiscard]] static BlankNode make(std::string_view identifier, storage::DynNodeStoragePtr node_storage = storage::default_node_storage);
3232

33+
/**
34+
* creates a new BlankNode from a random UUID (Universally Unique IDentifier)
35+
* @return UUID IRI
36+
*/
37+
[[nodiscard]] static BlankNode make_uuid(storage::DynNodeStoragePtr node_storage = storage::default_node_storage);
38+
39+
3340
/**
3441
* Constructs a bnode from an identifier
3542
*/

tests/nodes/tests_Node.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,14 @@ TEST_CASE("IRI UUID") {
225225
CHECK(regex::Regex{"^urn:uuid:[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"}.regex_match(uuid.identifier()));
226226
}
227227

228+
TEST_CASE("BNODE UUID") {
229+
BlankNode uuid = BlankNode::make_uuid();
230+
BlankNode uuid2 = BlankNode::make_uuid();
231+
232+
CHECK(uuid != uuid2); // note: non-deterministic but should basically never fail
233+
CHECK(regex::Regex{"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"}.regex_match(uuid.identifier()));
234+
}
235+
228236
TEST_CASE("IRI fetch or serialize") {
229237
auto const x = IRI::make("https://example.com#some-iri");
230238
auto const expected = x.identifier();

0 commit comments

Comments
 (0)