-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathLiteral.cpp
More file actions
2705 lines (2159 loc) · 106 KB
/
Literal.cpp
File metadata and controls
2705 lines (2159 loc) · 106 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Literal.hpp"
#include <algorithm>
#include <execution>
#include <random>
#include <ranges>
#include <sstream>
#include <array>
#include <boost/uuid/random_generator.hpp>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <uni_algo/all.h>
#include <rdf4cpp/writer/BufWriter.hpp>
#include <rdf4cpp/writer/TryWrite.hpp>
#include <rdf4cpp/IRI.hpp>
#include <rdf4cpp/datatypes/registry/util/DateTimeUtils.hpp>
#include <rdf4cpp/util/CaseInsensitiveCharTraits.hpp>
#include <rdf4cpp/writer/Prefixes.hpp>
#include <rdf4cpp/util/CharMatcher.hpp>
#include <openssl/evp.h>
namespace rdf4cpp {
bool Literal::lexical_form_needs_escape(std::string_view const lexical_form) noexcept {
static constexpr datatypes::registry::util::ConstexprString pattern = "\"\\\n\r";
return util::char_matcher_detail::contains_any(lexical_form, pattern);
}
template<typename T, typename S>
static std::string run_serialize(S serialize, T const &value) {
return writer::StringWriter::oneshot([&serialize, &value](writer::StringWriter &w) noexcept {
return std::invoke(serialize, value, w);
});
}
Literal::Literal(storage::identifier::NodeBackendHandle handle) noexcept : Node{handle} {
}
Literal::Literal() noexcept : Node{storage::identifier::NodeBackendHandle{}} {
}
Literal Literal::make_null() noexcept {
return Literal{};
}
Literal Literal::make_simple_unchecked(std::string_view lexical_form, bool needs_escape, storage::DynNodeStoragePtr node_storage) {
return Literal{storage::identifier::NodeBackendHandle{node_storage.find_or_make_id(storage::view::LexicalFormLiteralBackendView{
.datatype_id = storage::identifier::NodeBackendID::xsd_string_iri.first,
.lexical_form = lexical_form,
.language_tag = "",
.needs_escape = needs_escape}),
node_storage}};
}
Literal Literal::make_noninlined_typed_unchecked(std::string_view lexical_form, bool needs_escape, IRI const &datatype, storage::DynNodeStoragePtr node_storage) {
return Literal{storage::identifier::NodeBackendHandle{node_storage.find_or_make_id(storage::view::LexicalFormLiteralBackendView{
.datatype_id = datatype.to_node_storage(node_storage).backend_handle().id(),
.lexical_form = lexical_form,
.language_tag = "",
.needs_escape = needs_escape}),
node_storage}};
}
Literal Literal::make_noninlined_special_unchecked(std::any &&value, storage::identifier::LiteralType fixed_id, storage::DynNodeStoragePtr node_storage) {
return Literal{storage::identifier::NodeBackendHandle{node_storage.find_or_make_id(storage::view::ValueLiteralBackendView{
.datatype = fixed_id,
.value = std::move(value)}),
node_storage}};
}
Literal Literal::make_lang_tagged_unchecked(std::string_view lexical_form, bool needs_escape, std::string_view lang, storage::DynNodeStoragePtr node_storage) {
auto node_id = node_storage.find_or_make_id(storage::view::LexicalFormLiteralBackendView{
.datatype_id = storage::identifier::NodeBackendID::rdf_langstring_iri.first,
.lexical_form = lexical_form,
.language_tag = lang,
.needs_escape = needs_escape});
return make_lang_tagged_unchecked_from_node_id(lang, node_storage, node_id);
}
Literal Literal::make_lang_tagged_unchecked_from_node_id(std::string_view lang, storage::DynNodeStoragePtr node_storage, storage::identifier::NodeBackendID node_id) noexcept {
using namespace storage::identifier;
auto const lang_tag_i = datatypes::registry::DatatypeRegistry::LangTagInlines::try_tag_to_inlined(lang); // check if the lang_tag can be inlined
if (!lang_tag_i.has_value()) {
return Literal{NodeBackendHandle{node_id, node_storage}};
}
auto const inlined_id = datatypes::registry::DatatypeRegistry::LangTagInlines::try_into_inlined(node_id.node_id().literal_id(), *lang_tag_i); // check if we have enough space#
if (!inlined_id.has_value()) {
return Literal{NodeBackendHandle{node_id, node_storage}};
}
return Literal{NodeBackendHandle{NodeBackendID{NodeID{*inlined_id, node_id.node_id().literal_type()},
RDFNodeType::Literal,
true,
node_id.free_tagging_bits()},
node_storage}};
}
Literal Literal::make_inlined_typed_unchecked(storage::identifier::LiteralID inlined_value, storage::identifier::LiteralType fixed_id, storage::DynNodeStoragePtr node_storage) noexcept {
using namespace storage::identifier;
RDF4CPP_ASSERT(fixed_id != LiteralType::other());
return Literal{NodeBackendHandle{NodeID{inlined_value, fixed_id},
RDFNodeType::Literal,
node_storage,
true}};
}
Literal Literal::make_typed_unchecked(std::any &&value, datatypes::registry::DatatypeIDView datatype, datatypes::registry::DatatypeRegistry::DatatypeEntry const &entry, storage::DynNodeStoragePtr node_storage) {
if (entry.inlining_ops.has_value()) {
if (auto const maybe_inlined = entry.inlining_ops->try_into_inlined_fptr(value); maybe_inlined.has_value()) {
return Literal::make_inlined_typed_unchecked(*maybe_inlined, datatype.get_fixed(), node_storage);
}
}
if (datatype.is_fixed()) {
if (auto const fixed_id = datatype.get_fixed(); node_storage.has_specialized_storage_for(fixed_id)) {
return Literal::make_noninlined_special_unchecked(std::move(value), fixed_id, node_storage);
}
}
auto const lex = run_serialize(entry.serialize_canonical_string_fptr, value);
return Literal::make_noninlined_typed_unchecked(lex,
false,
IRI{datatype, node_storage},
node_storage);
}
Literal Literal::make_string_like_copy_lang_tag(std::string_view str, Literal const &lang_tag_src, storage::DynNodeStoragePtr node_storage) {
auto const needs_escape = lexical_form_needs_escape(str);
if (lang_tag_src.datatype_eq<datatypes::rdf::LangString>()) {
return Literal::make_lang_tagged_unchecked(str, needs_escape, lang_tag_src.language_tag(), node_storage);
}
RDF4CPP_ASSERT(lang_tag_src.datatype_eq<datatypes::xsd::String>());
return Literal::make_simple_unchecked(str, needs_escape, node_storage);
}
Literal Literal::lang_tagged_get_de_inlined() const noexcept {
auto [_, id] = datatypes::registry::DatatypeRegistry::LangTagInlines::from_inlined(handle_.node_id().literal_id());
return Literal{storage::identifier::NodeBackendHandle{storage::identifier::NodeID{id, this->handle_.node_id().literal_type()},
handle_.type(),
handle_.storage(),
false,
handle_.free_tagging_bits()}};
}
bool Literal::dynamic_datatype_eq_impl(std::string_view datatype) const noexcept {
RDF4CPP_ASSERT(!this->is_fixed());
return this->datatype().identifier() == datatype;
}
Literal Literal::make_simple(std::string_view lexical_form, storage::DynNodeStoragePtr node_storage) {
if (!una::is_valid_utf8(lexical_form)) {
throw InvalidNode{"Invalid UTF-8 in lexical form of literal"};
}
auto const needs_escape = lexical_form_needs_escape(lexical_form);
return Literal::make_simple_unchecked(lexical_form, needs_escape, node_storage);
}
Literal Literal::make_simple_normalize(std::string_view lexical_form, storage::DynNodeStoragePtr node_storage) {
auto const lex = una::norm::to_nfc_utf8(lexical_form);
auto const needs_escape = lexical_form_needs_escape(lex);
return Literal::make_simple_unchecked(lex, needs_escape, node_storage);
}
Literal Literal::make_lang_tagged(std::string_view lexical_form, std::string_view lang_tag,
storage::DynNodeStoragePtr node_storage) {
if (!una::is_valid_utf8(lexical_form)) [[unlikely]] {
throw InvalidNode{"Invalid UTF-8 in lexical form of literal"};
}
auto const lowercase_lang_tag = una::cases::to_lowercase_utf8(lang_tag);
auto const needs_escape = lexical_form_needs_escape(lexical_form);
return Literal::make_lang_tagged_unchecked(lexical_form, needs_escape, lowercase_lang_tag, node_storage);
}
Literal Literal::make_lang_tagged_normalize(std::string_view lexical_form, std::string_view lang_tag,
storage::DynNodeStoragePtr node_storage) {
auto const lowercase_lang_tag = una::cases::to_lowercase_utf8(lang_tag);
auto const lex = una::norm::to_nfc_utf8(lexical_form);
auto const needs_escape = lexical_form_needs_escape(lex);
return Literal::make_lang_tagged_unchecked(lex, needs_escape, lowercase_lang_tag, node_storage);
}
Literal Literal::make_typed(std::string_view lexical_form, IRI const &datatype, storage::DynNodeStoragePtr node_storage) {
using namespace datatypes::registry;
DatatypeIDView const datatype_identifier{datatype};
if (datatype_identifier == datatypes::rdf::LangString::datatype_id) {
// see: https://www.w3.org/TR/rdf11-concepts/#section-Graph-Literal
throw InvalidNode{"cannot construct rdf:langString without a language tag, please call one of the other factory functions"};
}
if (datatype_identifier == datatypes::xsd::String::datatype_id) {
return Literal::make_simple(lexical_form, node_storage);
}
if (auto const *entry = DatatypeRegistry::get_entry(datatype_identifier); entry != nullptr) {
// exists => canonize
auto cpp_value = entry->factory_fptr(lexical_form);
return Literal::make_typed_unchecked(std::move(cpp_value), datatype_identifier, *entry, node_storage);
} else {
// doesn't exist in the registry no way to canonicalize
auto const needs_escape = lexical_form_needs_escape(lexical_form);
return Literal::make_noninlined_typed_unchecked(lexical_form, needs_escape, datatype, node_storage);
}
}
Literal Literal::make_boolean(TriBool const b, storage::DynNodeStoragePtr node_storage) noexcept {
if (b == TriBool::Err) {
return Literal{};
}
return Literal::make_typed_from_value<datatypes::xsd::Boolean>(b == TriBool::True, node_storage);
}
Literal Literal::make_string_uuid(storage::DynNodeStoragePtr node_storage) {
boost::uuids::random_generator_mt19937 gen{};
boost::uuids::uuid u = gen();
std::string s = boost::uuids::to_string(u);
return make_simple(s, node_storage);
}
Literal Literal::generate_random_double(storage::DynNodeStoragePtr node_storage) {
static thread_local std::default_random_engine rng{std::random_device{}()};
return Literal::generate_random_double(rng, node_storage);
}
Literal Literal::to_node_storage(storage::DynNodeStoragePtr node_storage) const {
using datatypes::registry::DatatypeRegistry;
node_storage = select_node_storage(node_storage);
if (handle_.storage() == node_storage || null()) {
return *this;
}
if (this->is_inlined()) {
if (this->datatype_eq<datatypes::rdf::LangString>()) {
auto const data = this->lang_tagged_get_de_inlined().backend_handle().literal_backend().get_lexical();
return Literal::make_lang_tagged_unchecked(data.lexical_form,
data.needs_escape,
data.language_tag,
node_storage);
}
auto const node_id = this->handle_.node_id();
return Literal::make_inlined_typed_unchecked(node_id.literal_id(),
node_id.literal_type(),
node_storage);
}
auto literal_view = handle_.literal_backend();
auto node_id = literal_view.visit(
[&](storage::view::LexicalFormLiteralBackendView &lexical_backend) {
if (auto const dt_id = storage::identifier::iri_node_id_to_literal_type(lexical_backend.datatype_id);
dt_id.is_fixed() && node_storage.has_specialized_storage_for(dt_id)) {
// This node storage doesn't have specialized storage for the given type but the target node storage does.
// Need to send value over.
// This doesn't work for rdf:langString, but it shouldn't have a specialized storage anyway.
RDF4CPP_ASSERT(!this->datatype_eq<datatypes::rdf::LangString>());
auto const from_string = DatatypeRegistry::get_factory(dt_id);
auto value = from_string(lexical_backend.lexical_form);
return node_storage.find_or_make_id(storage::view::ValueLiteralBackendView{.datatype = dt_id,
.value = std::move(value)});
}
// send over IRI corresponding to this datatype
auto const dtype_iri_view = handle_.storage().find_iri_backend(lexical_backend.datatype_id);
lexical_backend.datatype_id = node_storage.find_or_make_id(dtype_iri_view);
// find or make the requested node
return node_storage.find_or_make_id(literal_view);
},
[&node_storage, &literal_view](storage::view::ValueLiteralBackendView const &value_backend) {
// no need to send over datatype IRI, as this having a specialized storage requires
// that the datatype is fixed, so it must already be present
if (!node_storage.has_specialized_storage_for(value_backend.datatype)) {
// target node storage is not specialized for this datatype, need to convert to lexical form
auto const serialize = DatatypeRegistry::get_serialize_canonical_string(datatypes::registry::DatatypeIDView{value_backend.datatype});
RDF4CPP_ASSERT(serialize != nullptr);
auto const lex = run_serialize(serialize, value_backend.value);
return node_storage.find_or_make_id(storage::view::LexicalFormLiteralBackendView{
.datatype_id = storage::identifier::literal_type_to_iri_node_id(value_backend.datatype),
.lexical_form = lex,
.language_tag = "",
.needs_escape = false});
}
// target node storage is also specialized for this datatype, directly send it over
return node_storage.find_or_make_id(literal_view);
});
return Literal{storage::identifier::NodeBackendHandle{node_id, node_storage}};
}
Literal Literal::try_get_in_node_storage(storage::DynNodeStoragePtr node_storage) const noexcept {
using datatypes::registry::DatatypeRegistry;
node_storage = select_node_storage(node_storage);
if (handle_.storage() == node_storage || null()) {
return *this;
}
if (this->is_inlined()) {
if (this->datatype_eq<datatypes::rdf::LangString>()) {
// special case for rdf:langString because part of it is in the backend
auto const literal_view = this->lang_tagged_get_de_inlined().handle_.literal_backend();
auto const tmp_id = node_storage.find_id(literal_view);
if (tmp_id.null()) {
return Literal{};
}
auto const lang_tag_i = DatatypeRegistry::LangTagInlines::try_tag_to_inlined(literal_view.get_lexical().language_tag);
if (!lang_tag_i.has_value()) {
return Literal{storage::identifier::NodeBackendHandle{tmp_id, node_storage}};
}
auto const inlined_id = DatatypeRegistry::LangTagInlines::try_into_inlined(tmp_id.node_id().literal_id(), *lang_tag_i);
if (!inlined_id.has_value()) {
return Literal{storage::identifier::NodeBackendHandle{tmp_id, node_storage}};
}
return Literal{storage::identifier::NodeBackendHandle{storage::identifier::NodeBackendID{storage::identifier::NodeID{*inlined_id, tmp_id.node_id().literal_type()},
storage::identifier::RDFNodeType::Literal,
true,
tmp_id.free_tagging_bits()},
node_storage}};
}
auto const node_id = this->handle_.node_id();
return Literal::make_inlined_typed_unchecked(node_id.literal_id(),
node_id.literal_type(),
node_storage);
}
auto literal_view = handle_.literal_backend();
auto const node_id = literal_view.visit(
[&](storage::view::LexicalFormLiteralBackendView &lexical_backend) noexcept {
if (auto const dt_id = storage::identifier::iri_node_id_to_literal_type(lexical_backend.datatype_id);
dt_id.is_fixed() && node_storage.has_specialized_storage_for(dt_id)) {
// This node storage doesn't have specialized storage for the given type but the target node storage does.
// Need to send value over.
// This doesn't work for rdf:langString, but it shouldn't have a specialized storage anyway.
RDF4CPP_ASSERT(!this->datatype_eq<datatypes::rdf::LangString>());
auto const from_string = DatatypeRegistry::get_factory(dt_id);
auto value = from_string(lexical_backend.lexical_form);
return node_storage.find_id(storage::view::ValueLiteralBackendView{.datatype = dt_id,
.value = std::move(value)});
}
// Default case.
// This node storage doesn't have specialized storage for the value and the new one also doesn't
auto src_node_storage = handle_.storage();
auto const dtype_iri_view = src_node_storage.find_iri_backend(lexical_backend.datatype_id);
lexical_backend.datatype_id = node_storage.find_id(dtype_iri_view);
if (lexical_backend.datatype_id.null()) {
// datatype IRI not present, therefore literal cannot be present
return storage::identifier::NodeBackendID{};
}
return node_storage.find_id(literal_view);
},
[&node_storage, &literal_view](storage::view::ValueLiteralBackendView const &value_backend) noexcept {
// no need to send over datatype IRI, as this having a specialized storage requires
// that the datatype is fixed, so it must already be present
if (!node_storage.has_specialized_storage_for(value_backend.datatype)) {
// target node storage is not specialized for this datatype, need to convert to lexical form
auto const serialize_canonical = DatatypeRegistry::get_serialize_canonical_string(datatypes::registry::DatatypeIDView{value_backend.datatype});
RDF4CPP_ASSERT(serialize_canonical != nullptr);
auto const lex = run_serialize(serialize_canonical, value_backend.value);
return node_storage.find_id(storage::view::LexicalFormLiteralBackendView{
.datatype_id = storage::identifier::literal_type_to_iri_node_id(value_backend.datatype),
.lexical_form = lex,
.language_tag = "",
.needs_escape = false});
}
// target node storage is also specialized for this datatype, directly try to get it
return node_storage.find_id(literal_view);
});
if (node_id.null()) {
return Literal{};
}
return Literal{storage::identifier::NodeBackendHandle{node_id, node_storage}};
}
storage::identifier::NodeBackendID Literal::find_datatype_iri(datatypes::registry::DatatypeIDView id, storage::DynNodeStoragePtr node_storage) noexcept {
auto nid = IRI::find(id, node_storage);
return nid.backend_handle().id();
}
Literal Literal::find_simple(std::string_view lexical_form, storage::DynNodeStoragePtr node_storage) noexcept {
auto esc = lexical_form_needs_escape(lexical_form);
auto nid = node_storage.find_id(storage::view::LexicalFormLiteralBackendView{
storage::identifier::NodeBackendID::xsd_string_iri.first, lexical_form, "", esc});
if (nid.null())
return Literal{};
return Literal{storage::identifier::NodeBackendHandle{nid, node_storage}};
}
Literal Literal::find_lang_tagged(std::string_view lexical_form, std::string_view lang_tag, storage::DynNodeStoragePtr node_storage) noexcept {
auto esc = lexical_form_needs_escape(lexical_form);
auto nid = node_storage.find_id(storage::view::LexicalFormLiteralBackendView{
storage::identifier::NodeBackendID::rdf_langstring_iri.first, lexical_form, lang_tag, esc});
if (nid.null())
return Literal{};
return make_lang_tagged_unchecked_from_node_id(lang_tag, node_storage, nid);
}
bool Literal::datatype_eq(IRI const &datatype) const noexcept {
return this->datatype_id() == datatypes::registry::DatatypeIDView{datatype};
}
bool Literal::datatype_eq(Literal const &other) const noexcept {
if (auto const this_type = this->handle_.node_id().literal_type(); this_type.is_fixed()) {
if (auto const other_type = other.handle_.node_id().literal_type(); other_type.is_fixed()) {
return this_type == other_type;
}
return false;
}
return this->datatype() == other.datatype();
}
Literal Literal::as_datatype_eq(IRI const &datatype, storage::DynNodeStoragePtr node_storage) const noexcept {
if (this->null()) {
return Literal{};
}
return Literal::make_boolean(this->datatype_eq(datatype), select_node_storage(node_storage));
}
Literal Literal::as_datatype_eq(Literal const &other, storage::DynNodeStoragePtr node_storage) const noexcept {
if (this->null() || other.null()) {
return Literal{};
}
return Literal::make_boolean(this->datatype_eq(other), select_node_storage(node_storage));
}
IRI Literal::datatype() const noexcept {
if (null()) {
return IRI::make_null();
}
if (this->is_fixed()) {
return IRI{storage::identifier::datatype_iri_handle_for_fixed_lit_handle(handle_)};
}
auto const lexical = handle_.literal_backend().get_lexical();
return IRI{storage::identifier::NodeBackendHandle{lexical.datatype_id,
handle_.storage()}};
}
template<bool simplified, typename C>
auto Literal::serialize_lexical_form_impl(C &&consume) const noexcept {
if (this->is_inlined()) {
if (this->datatype_eq<datatypes::rdf::LangString>()) {
auto const lex = this->lang_tagged_get_de_inlined()
.handle_
.literal_backend()
.get_lexical()
.lexical_form;
return std::invoke(std::forward<C>(consume), lex);
}
auto const *entry = datatypes::registry::DatatypeRegistry::get_entry(this->datatype_id());
RDF4CPP_ASSERT(entry != nullptr);
RDF4CPP_ASSERT(entry->inlining_ops.has_value());
auto const inlined_value = this->handle_.node_id().literal_id();
auto const value = entry->inlining_ops->from_inlined_fptr(inlined_value);
if constexpr (simplified) {
return std::invoke(std::forward<C>(consume), value, entry->serialize_simplified_string_fptr);
} else {
return std::invoke(std::forward<C>(consume), value, entry->serialize_canonical_string_fptr);
}
}
return handle_.literal_backend().visit(
[this, &consume](storage::view::LexicalFormLiteralBackendView const &lexical_backend) noexcept {
if constexpr (simplified) {
auto const *entry = datatypes::registry::DatatypeRegistry::get_entry(this->datatype_id());
if (entry != nullptr) {
auto const value = entry->factory_fptr(lexical_backend.lexical_form);
return std::invoke(std::forward<C>(consume), value, entry->serialize_simplified_string_fptr);
}
} else {
(void) this; // silence unused capture warning
}
return std::invoke(std::forward<C>(consume), lexical_backend.lexical_form);
},
[&consume](storage::view::ValueLiteralBackendView const &value_backend) noexcept {
if constexpr (simplified) {
auto const serialize = datatypes::registry::DatatypeRegistry::get_serialize_simplified_string(value_backend.datatype);
return std::invoke(std::forward<C>(consume), value_backend.value, serialize);
} else {
auto const serialize = datatypes::registry::DatatypeRegistry::get_serialize_canonical_string(value_backend.datatype);
return std::invoke(std::forward<C>(consume), value_backend.value, serialize);
}
});
}
// Using structs to ensure static-dispatch
struct ConsumeSafe {
[[nodiscard]] CowString operator()(std::string_view lexical) const noexcept {
return CowString{CowString::borrowed, lexical};
}
[[nodiscard]] CowString operator()(std::any const &value, datatypes::registry::DatatypeRegistry::serialize_fptr_t serialize) const noexcept {
auto s = writer::StringWriter::oneshot([&value, serialize](writer::StringWriter &w) noexcept {
return serialize(value, w);
});
return CowString{CowString::owned, std::move(s)};
}
};
struct ConsumeMaybeSerialize {
std::string_view *const out_lex_form;
writer::BufWriterParts const writer;
[[nodiscard]] FetchOrSerializeResult operator()(std::string_view const lexical) const noexcept {
*out_lex_form = lexical;
return FetchOrSerializeResult::Fetched;
}
[[nodiscard]] FetchOrSerializeResult operator()(std::any const &value, datatypes::registry::DatatypeRegistry::serialize_fptr_t const serialize) const noexcept {
if (!serialize(value, writer)) {
return FetchOrSerializeResult::SerializationFailed;
}
return FetchOrSerializeResult::Serialized;
}
};
struct ConsumeSerialize {
writer::BufWriterParts const writer;
bool operator()(std::string_view const lexical_form) const noexcept {
return writer::write_str(lexical_form, writer);
}
bool operator()(std::any const &value, datatypes::registry::DatatypeRegistry::serialize_fptr_t const serialize) const noexcept {
return serialize(value, writer);
}
};
CowString Literal::lexical_form() const noexcept {
return serialize_lexical_form_impl<false>(ConsumeSafe{});
}
FetchOrSerializeResult Literal::fetch_or_serialize_lexical_form(std::string_view &out_lex_form, writer::BufWriterParts const writer) const noexcept {
return serialize_lexical_form_impl<false>(ConsumeMaybeSerialize{&out_lex_form, writer});
}
Literal Literal::as_lexical_form(storage::DynNodeStoragePtr node_storage) const {
if (this->null()) {
return Literal{};
}
auto const lex = this->lexical_form();
auto const needs_escape = lexical_form_needs_escape(lex);
return Literal::make_simple_unchecked(lex, needs_escape, select_node_storage(node_storage));
}
CowString Literal::simplified_lexical_form() const noexcept {
return serialize_lexical_form_impl<true>(ConsumeSafe{});
}
FetchOrSerializeResult Literal::fetch_or_serialize_simplified_lexical_form(std::string_view &out_lex_form, writer::BufWriterParts const writer) const noexcept {
return serialize_lexical_form_impl<true>(ConsumeMaybeSerialize{&out_lex_form, writer});
}
Literal Literal::as_simplified_lexical_form(storage::DynNodeStoragePtr node_storage) const {
if (this->null()) {
return Literal{};
}
auto const lex = this->simplified_lexical_form();
auto const needs_escape = lexical_form_needs_escape(lex);
return Literal::make_simple_unchecked(lex, needs_escape, select_node_storage(node_storage));
}
std::string_view Literal::language_tag() const noexcept {
if (this->datatype_eq<datatypes::rdf::LangString>()) {
if (this->is_inlined()) {
auto [tag, _] = rdf4cpp::datatypes::registry::DatatypeRegistry::LangTagInlines::from_inlined(this->handle_.node_id().literal_id());
return rdf4cpp::datatypes::registry::DatatypeRegistry::LangTagInlines::inlined_to_tag(tag);
}
return handle_.literal_backend().get_lexical().language_tag;
}
return "";
}
Literal Literal::as_language_tag(storage::DynNodeStoragePtr node_storage) const {
if (this->null()) {
return Literal{};
}
return Literal::make_simple_unchecked(this->language_tag(), false, select_node_storage(node_storage));
}
TriBool Literal::language_tag_eq(std::string_view const lang_tag) const noexcept {
if (!this->datatype_eq<datatypes::rdf::LangString>()) {
return TriBool::Err;
}
return this->language_tag() == lang_tag;
}
TriBool Literal::language_tag_eq(Literal const &other) const noexcept {
if (!this->datatype_eq<datatypes::rdf::LangString>() || !other.datatype_eq<datatypes::rdf::LangString>()) {
return TriBool::Err;
}
return this->language_tag() == other.language_tag();
}
Literal Literal::as_language_tag_eq(std::string_view const lang_tag, storage::DynNodeStoragePtr node_storage) const noexcept {
if (this->null()) {
return Literal{};
}
return Literal::make_boolean(this->language_tag_eq(lang_tag), select_node_storage(node_storage));
}
Literal Literal::as_language_tag_eq(Literal const &other, storage::DynNodeStoragePtr node_storage) const noexcept {
if (this->null() || other.null()) {
return Literal{};
}
return Literal::make_boolean(this->language_tag_eq(other), select_node_storage(node_storage));
}
// https://www.w3.org/TR/n-triples/#grammar-production-STRING_LITERAL_QUOTE
#define RDF4CPP_DETAIL_TRY_SER_QUOTED_LEXICAL(lexical) \
RDF4CPP_DETAIL_TRY_WRITE_STR("\""); \
for (char const ch : lexical) { \
switch (ch) { \
case '"': { \
RDF4CPP_DETAIL_TRY_WRITE_STR(R"(\")"); \
break; \
} \
case '\\': { \
RDF4CPP_DETAIL_TRY_WRITE_STR(R"(\\)"); \
break; \
} \
case '\n': { \
RDF4CPP_DETAIL_TRY_WRITE_STR(R"(\n)"); \
break; \
} \
case '\r': { \
RDF4CPP_DETAIL_TRY_WRITE_STR(R"(\r)"); \
break; \
} \
[[likely]] default: { \
RDF4CPP_DETAIL_TRY_WRITE_STR(&ch, 1); \
break; \
} \
} \
} \
RDF4CPP_DETAIL_TRY_WRITE_STR("\"");
bool Literal::serialize(writer::BufWriterParts const writer, NodeSerializationOpts opts) const noexcept {
if (this->null()) {
RDF4CPP_DETAIL_TRY_WRITE_STR("null");
return true;
}
if (this->datatype_eq<datatypes::xsd::String>()) {
auto const value = this->backend_handle().literal_backend().get_lexical();
if (value.needs_escape) [[unlikely]] {
RDF4CPP_DETAIL_TRY_SER_QUOTED_LEXICAL(value.lexical_form);
} else {
RDF4CPP_DETAIL_TRY_WRITE_STR("\"");
RDF4CPP_DETAIL_TRY_WRITE_STR(value.lexical_form);
RDF4CPP_DETAIL_TRY_WRITE_STR("\"");
}
return true;
} else if (this->datatype_eq<datatypes::rdf::LangString>()) {
auto const value = this->lang_tagged_get_de_inlined().backend_handle().literal_backend().get_lexical();
if (value.needs_escape) [[unlikely]] {
RDF4CPP_DETAIL_TRY_SER_QUOTED_LEXICAL(value.lexical_form);
} else {
RDF4CPP_DETAIL_TRY_WRITE_STR("\"");
RDF4CPP_DETAIL_TRY_WRITE_STR(value.lexical_form);
RDF4CPP_DETAIL_TRY_WRITE_STR("\"");
}
RDF4CPP_DETAIL_TRY_WRITE_STR("@");
RDF4CPP_DETAIL_TRY_WRITE_STR(value.language_tag);
return true;
} else if (opts.use_short_form && (this->datatype_eq<datatypes::xsd::Integer>()
|| this->datatype_eq<datatypes::xsd::Decimal>()
|| this->datatype_eq<datatypes::xsd::Boolean>()
|| (this->datatype_eq<datatypes::xsd::Double>() && std::isfinite(this->value<datatypes::xsd::Double>())))) {
return this->serialize_lexical_form(writer);
} else if (this->is_inlined()) {
RDF4CPP_ASSERT(!this->datatype_eq<datatypes::rdf::LangString>());
// Notes:
// 1. inlined values are assumed to not require escaping
// 2. This is a known datatype because it is inlined => the registry contains the datatype IRI
auto const *entry = datatypes::registry::DatatypeRegistry::get_entry(this->datatype_id());
RDF4CPP_ASSERT(entry != nullptr);
RDF4CPP_ASSERT(entry->inlining_ops.has_value());
RDF4CPP_DETAIL_TRY_WRITE_STR("\"");
auto const inlined_value = this->backend_handle().node_id().literal_id();
if (!entry->serialize_canonical_string_fptr(entry->inlining_ops->from_inlined_fptr(inlined_value), writer)) {
return false;
}
RDF4CPP_DETAIL_TRY_WRITE_STR("\"^^");
return writer::write_fixed_type_iri(this->backend_handle().node_id().literal_type(), writer, opts.use_prefixes);
} else {
using storage::NodeStorage;
using storage::identifier::NodeBackendHandle;
return this->backend_handle().literal_backend().visit(
[&](storage::view::LexicalFormLiteralBackendView const &lexical_backend) noexcept {
auto const dtype_iri = handle_.storage().find_iri_backend(lexical_backend.datatype_id);
if (lexical_backend.needs_escape) [[unlikely]] {
RDF4CPP_DETAIL_TRY_SER_QUOTED_LEXICAL(lexical_backend.lexical_form);
} else {
RDF4CPP_DETAIL_TRY_WRITE_STR("\"");
RDF4CPP_DETAIL_TRY_WRITE_STR(lexical_backend.lexical_form);
RDF4CPP_DETAIL_TRY_WRITE_STR("\"");
}
RDF4CPP_DETAIL_TRY_WRITE_STR("^^<");
RDF4CPP_DETAIL_TRY_WRITE_STR(dtype_iri.identifier);
RDF4CPP_DETAIL_TRY_WRITE_STR(">");
return true;
},
[&](storage::view::ValueLiteralBackendView const &value_backend) noexcept {
// Notes:
// 1. non-string storage values are assumed to not require escaping
// 2. This is a known datatype because it is stored in a value backend => the registry contains the datatype IRI
auto const *entry = datatypes::registry::DatatypeRegistry::get_entry(this->datatype_id());
RDF4CPP_ASSERT(entry != nullptr);
RDF4CPP_DETAIL_TRY_WRITE_STR("\"");
if (!entry->serialize_canonical_string_fptr(value_backend.value, writer)) {
return false;
}
RDF4CPP_DETAIL_TRY_WRITE_STR("\"^^");
return writer::write_fixed_type_iri(this->backend_handle().node_id().literal_type(), writer, opts.use_prefixes);
});
}
}
#undef RDF4CPP_DETAIL_TRY_SER_QUOTED_LEXICAL
bool Literal::serialize_lexical_form(writer::BufWriterParts const writer) const noexcept {
return serialize_lexical_form_impl<false>(ConsumeSerialize{writer});
}
bool Literal::serialize_simplified_lexical_form(writer::BufWriterParts const writer) const noexcept {
return serialize_lexical_form_impl<true>(ConsumeSerialize{writer});
}
Literal::operator std::string() const noexcept {
return writer::StringWriter::oneshot([this](auto &w) noexcept {
return this->serialize(w);
});
}
bool Literal::is_numeric() const noexcept {
if (auto const is_num = this->handle_.node_id().literal_type().is_numeric(); is_num != TriBool::Err) {
return is_num;
}
return !this->null() && datatypes::registry::DatatypeRegistry::get_numerical_ops(this->datatype_id()) != nullptr;
}
bool Literal::is_timepoint() const noexcept {
if (auto const is_tp = this->handle_.node_id().literal_type().is_timepoint(); is_tp != TriBool::Err) {
return is_tp;
}
return !this->null() && datatypes::registry::DatatypeRegistry::get_timepoint_ops(this->datatype_id()) == nullptr;
}
bool Literal::is_duration() const noexcept {
if (auto const is_dur = this->handle_.node_id().literal_type().is_duration(); is_dur != TriBool::Err) {
return is_dur;
}
return !this->null() && datatypes::registry::DatatypeRegistry::get_duration_ops(this->datatype_id()) == nullptr;
}
std::ostream &operator<<(std::ostream &os, Literal const &literal) {
writer::BufOStreamWriter w{os};
literal.serialize(w);
w.finalize();
return os;
}
std::any Literal::value() const noexcept {
using namespace datatypes;
auto const datatype = this->datatype_id();
if (this->is_inlined()) {
if (datatype == rdf::LangString::datatype_id) {
return this->lang_tagged_get_de_inlined().value();
}
auto const ops = registry::DatatypeRegistry::get_inlining_ops(datatype);
RDF4CPP_ASSERT(ops != nullptr);
auto const inlined_value = this->handle_.node_id().literal_id();
return ops->from_inlined_fptr(inlined_value);
}
auto const backend = handle_.literal_backend();
if (datatype == rdf::LangString::datatype_id) {
auto const &lex = backend.get_lexical();
return std::any{registry::LangStringRepr{
.lexical_form = lex.lexical_form,
.language_tag = lex.language_tag}};
}
if (datatype == xsd::String::datatype_id) {
auto const &lex = backend.get_lexical();
return std::any{lex.lexical_form};
}
return backend.visit(
[&datatype](storage::view::LexicalFormLiteralBackendView const &lexical_backend) noexcept {
if (auto const factory = registry::DatatypeRegistry::get_factory(datatype); factory != nullptr) {
return factory(lexical_backend.lexical_form);
}
return std::any{};
},
[&datatype](storage::view::ValueLiteralBackendView const &value_backend) noexcept {
RDF4CPP_ASSERT(value_backend.datatype == datatype);
(void)datatype;
return value_backend.value;
});
}
Literal Literal::cast(IRI const &target, storage::DynNodeStoragePtr node_storage) const {
using namespace datatypes::registry;
using namespace datatypes::xsd;
if (this->null()) {
return Literal{};
}
node_storage = select_node_storage(node_storage);
auto const this_dtid = this->datatype_id();
DatatypeIDView const target_dtid{target};
if (this_dtid == target_dtid) {
return this->to_node_storage(node_storage);
}
if (this_dtid == String::datatype_id) {
// string -> any
try {
auto const lex = this->lexical_form();
return Literal::make_typed(lex, target, node_storage);
} catch (...) {
return Literal{};
}
}
if (target_dtid == String::datatype_id) {
// any -> string
return this->as_simplified_lexical_form(node_storage);
}
if (target_dtid == Boolean::datatype_id) {
// any -> bool
return this->as_ebv(node_storage);
}
auto const *target_e = DatatypeRegistry::get_entry(target_dtid);
if (target_e == nullptr) {
// target not registered
return Literal{};
}
if (this_dtid == Boolean::datatype_id && target_e->numeric_ops.has_value()) {
// bool -> numeric
if (target_e->numeric_ops->is_impl()) {
auto value = this->template value<Boolean>() ? target_e->numeric_ops->get_impl().one_value_fptr()
: target_e->numeric_ops->get_impl().zero_value_fptr();
return Literal::make_typed_unchecked(std::move(value), target_dtid, *target_e, node_storage);
} else {
auto const &impl_converter = DatatypeRegistry::get_numeric_op_impl_conversion(*target_e);
auto const *target_num_impl = DatatypeRegistry::get_numerical_ops(impl_converter.target_type_id);
RDF4CPP_ASSERT(target_num_impl != nullptr);
// perform conversion as impl numeric type
auto const value = this->template value<Boolean>() ? target_num_impl->get_impl().one_value_fptr()
: target_num_impl->get_impl().zero_value_fptr();
// downcast to target
auto target_value = impl_converter.inverted_convert(value);
if (!target_value.has_value()) {
// not representable as target type
return Literal{};
}
return Literal::make_typed_unchecked(std::move(*target_value), target_dtid, *target_e, node_storage);
}
}
auto const *this_e = DatatypeRegistry::get_entry(this_dtid);
if (this_e == nullptr) {
// this datatype not registered
return Literal{};
}
if (auto const common_conversion = DatatypeRegistry::get_common_type_conversion(this_e->conversion_table, target_e->conversion_table); common_conversion.has_value()) {
// general cast
// TODO: if performance is bad split into separate cases for up-, down- and cross-casting to avoid one set of std::any wrapping and unwrapping for the former 2
auto const common_type_value = common_conversion->convert_lhs(this->value()); // upcast to common
if (!common_type_value.has_value()) {
// upcast failed
return Literal{};
}
auto target_value = common_conversion->inverted_convert_rhs(*common_type_value); // downcast to target
if (!target_value.has_value()) {
// downcast failed
return Literal{};
}
return Literal::make_typed_unchecked(std::move(*target_value), target_dtid, *target_e, node_storage);
}
// no conversion found
return Literal{};
}
template<typename OpSelect>
requires std::is_nothrow_invocable_r_v<datatypes::registry::DatatypeRegistry::binop_fptr_t, OpSelect, datatypes::registry::DatatypeRegistry::NumericOpsImpl const &>
Literal Literal::numeric_binop_impl(OpSelect op_select, Literal const &other, storage::DynNodeStoragePtr node_storage) const {
using namespace datatypes::registry;
RDF4CPP_ASSERT(!this->null() && !other.null());
if (this->is_fixed_not_numeric() || other.is_fixed_not_numeric()) {
return Literal{};