File: rdf_core.c

package info (click to toggle)
virtuoso-opensource 6.1.6+dfsg2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 260,992 kB
  • ctags: 125,220
  • sloc: ansic: 652,748; sql: 458,419; xml: 282,834; java: 61,031; sh: 40,031; cpp: 36,890; cs: 25,240; php: 12,692; yacc: 9,523; lex: 7,018; makefile: 6,157; jsp: 4,484; awk: 1,643; perl: 1,013; ruby: 1,003; python: 326
file content (3613 lines) | stat: -rw-r--r-- 112,911 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
/*
 *  $Id$
 *
 *  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
 *  project.
 *
 *  Copyright (C) 1998-2012 OpenLink Software
 *
 *  This project is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; only version 2 of the License, dated June 1991.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */

#include "../Dk/Dkhash64.h"
#include "libutil.h"
#include "sqlnode.h"
#include "sqlbif.h"
#include "sqlparext.h"
#include "bif_text.h"
#include "bif_xper.h"
#include "xmlparser.h"
#include "xmltree.h"
#include "numeric.h"
#include "sqlcmps.h"
#include "rdf_core.h"
#include "security.h" /* for sec_proc_check() */
#include "http.h"
#ifdef __cplusplus
extern "C" {
#endif
#include "turtle_p.h"
#ifdef __cplusplus
}
#endif

#ifdef RDF_DEBUG
#define rdf_dbg_printf(x) printf(x)
#else
#define rdf_dbg_printf(x)
#endif

#ifdef _SSL
#include <openssl/crypto.h>
#include <openssl/ssl.h>
#define MD5Init MD5_Init
#define MD5Update MD5_Update
#define MD5Final MD5_Final
#else
#include "../util/md5.h"
#endif /* _SSL */

int uriqa_dynamic_local = 0;

caddr_t
uriqa_get_host_for_dynamic_local (client_connection_t *cli, int * is_https)
{
  caddr_t res = NULL;
  if (cli && NULL != cli->cli_ws)
    {
      ws_connection_t *ws = cli->cli_ws;
      res = ws_mime_header_field (ws->ws_lines, "Host", NULL, 0);
      if (NULL != is_https)
#ifdef _SSL
	*is_https = (NULL != tcpses_get_ssl (ws->ws_session->dks_session));
#else
	*is_https = 0;
#endif
    }
  if (NULL == res)
    {
      const char *regname = "URIQADefaultHost";
      caddr_t * place;
      IN_TXN;
      ASSERT_IN_TXN;
      place = (caddr_t *) id_hash_get (registry, (caddr_t) & regname);
      LEAVE_TXN;
      if (place)
        res = box_copy (place[0]);
      if (NULL != is_https)
	*is_https = 0; /* default host scheme is considered to be http: */
    }
  return res;
}

int
uriqa_iri_is_local (query_instance_t *qi, const char *iri)
{
  const char *regname = "URIQADefaultHost";
  caddr_t * place;
  if (!uriqa_dynamic_local)
    return 0;
/*                   01234567 */
  if (strncmp (iri, "http://", 7))
    return 0;
  if (qi && NULL != qi->qi_client->cli_http_ses)
    {
      ws_connection_t *ws = qi->qi_client->cli_ws;
      caddr_t host = ws_mime_header_field (ws->ws_lines, "Host", NULL, 0);
      if (NULL != host)
        {
          int host_len = strlen (host);
          if (!strncmp (iri+7, host, host_len) && ('/' == iri[7+host_len]))
	    {
	      dk_free_box (host);
              return 7 + host_len;
            }
	  dk_free_box (host);
        }
    }
  IN_TXN;
  ASSERT_IN_TXN;
  place = (caddr_t *) id_hash_get (registry, (caddr_t) & regname);
  LEAVE_TXN;
  if (NULL != place)
    {
      int host_len = strlen (place[0]);
      if (!strncmp (iri+7, place[0], host_len))
        return 7 + host_len;
    }
  return 0;
}

caddr_t
uriqa_get_default_for_connvar (query_instance_t *qi, const char *varname)
{
  if (!strcmp ("URIQADefaultHost", varname))
    {
      const char *regname = "URIQADefaultHost";
      caddr_t * place;
      IN_TXN;
      place = (caddr_t *) id_hash_get (registry, (caddr_t) & regname);
      LEAVE_TXN;
      if (place)
        return box_copy (place[0]);
      return NULL;
    }
  if (!strcmp ("WSHost", varname))
    return uriqa_get_host_for_dynamic_local (qi->qi_client, NULL);
  if (!strcmp ("WSHostName", varname))
    {
      caddr_t host = uriqa_get_host_for_dynamic_local (qi->qi_client, NULL);
      const char *colon;
      caddr_t res;
      if (NULL == host)
        return NULL;
      colon = strchr (host, ':');
      if (NULL == colon)
        return host;
      res = box_dv_short_nchars (host, colon - host);
      dk_free_box (host);
      return res;
    }
  if (!strcmp ("WSHostPort", varname))
    {
      caddr_t host = uriqa_get_host_for_dynamic_local (qi->qi_client, NULL);
      const char *colon;
      caddr_t res;
      if (NULL == host)
        return box_dv_short_string ("80");
      colon = strchr (host, ':');
      if (NULL == colon)
        {
          dk_free_box (host);
          return box_dv_short_string ("80");
        }
      res = box_dv_short_string (colon + 1);
      dk_free_box (host);
      return res;
    }
  return NULL;
}

triple_feed_t *
tf_alloc (void)
{
  NEW_VARZ (triple_feed_t, tf);
  tf->tf_blank_node_ids = id_hash_allocate (1021, sizeof (caddr_t), sizeof (caddr_t), strhash, strhashcmp);
  id_hash_set_rehash_pct (tf->tf_blank_node_ids, 220);
  return tf;
}


void
tf_free (triple_feed_t *tf)
{
  id_hash_t *dict;			/*!< Current dictionary to be zapped */
  id_hash_iterator_t dict_hit;		/*!< Iterator to zap dictionary */
  char **dict_key, **dict_val;		/*!< Current key to zap */
  dict = tf->tf_blank_node_ids;
  for( id_hash_iterator (&dict_hit,dict);
    hit_next(&dict_hit, (char **)(&dict_key), (char **)(&dict_val));
    /*no step*/ )
    {
      dk_free_box (dict_key[0]);
      dk_free_tree (dict_val[0]);
    }
  id_hash_free (tf->tf_blank_node_ids);
  if (tf->tf_current_graph_uri != tf->tf_default_graph_uri)
    dk_free_tree (tf->tf_current_graph_uri);
  dk_free_tree (tf->tf_default_graph_iid);
  dk_free_tree (tf->tf_current_graph_iid);
  dk_free_tree (tf->tf_boxed_input_name);
  dk_free_tree (tf->tf_base_uri);
  dk_free_tree (tf->tf_default_graph_uri);
  dk_free (tf, sizeof (triple_feed_t));
}

void
sqlr_set_cbk_name_and_proc (client_connection_t *cli, const char *cbk_name, const char* cbk_param_types, const char *funname,
  char **full_name_ret, query_t **proc_ret, caddr_t *err_ret )
{
  const char *param_type_iter;
  proc_ret[0] = NULL;
  err_ret[0] = NULL;
  full_name_ret[0] = sch_full_proc_name (wi_inst.wi_schema, cbk_name, cli_qual (cli), CLI_OWNER (cli));
  if (NULL != full_name_ret[0])
    proc_ret[0] = sch_proc_def (wi_inst.wi_schema, full_name_ret[0]);
  if (NULL == proc_ret[0])
    {
      err_ret[0] = srv_make_new_error ("42001", "SR574",
        "Undefined procedure name \"%.100s\" is passed as callback parameter to %.100s()", cbk_name, funname );
      return;
    }
  if (proc_ret[0]->qr_to_recompile)
    {
      proc_ret[0] = qr_recompile (proc_ret[0], err_ret);
      if (err_ret[0])
        return;
    }
  if (NULL != cli->cli_user && !sec_proc_check (proc_ret[0], cli->cli_user->usr_id, cli->cli_user->usr_g_id))
    {
      err_ret[0] = srv_make_new_error ("42000", "SR575",
        "No permission to execute %.300s as callback of %.100s()", full_name_ret[0], funname );
      return;
    }
  if (strlen (cbk_param_types) != dk_set_length (proc_ret[0]->qr_parms))
    {
      err_ret[0] = srv_make_new_error ("42000", "SR576",
        "The callback %.300s[] of %.100s() declaration contains %d arguments, not %d as expected",
        full_name_ret[0], funname, (int)(dk_set_length (proc_ret[0]->qr_parms)), (int)(strlen (cbk_param_types)) );
      return;
    }
  param_type_iter = cbk_param_types;
  DO_SET (state_slot_t *, ssl, &(proc_ret[0]->qr_parms))
    {
      const char *problem = NULL;
      switch (param_type_iter[0])
        {
          case 'R':
            if (SSL_REF_PARAMETER != ssl->ssl_type)
	      problem = "should be declared as inout";
            break;
          case 'I':
            if (SSL_REF_PARAMETER == ssl->ssl_type)
	      problem = "should be declared as in";
            break;
#ifndef NDEBUG
          default:
            GPF_T1("bad param type decl string");
#endif
        }
      if (NULL != problem)
        {
          err_ret[0] = srv_make_new_error ("42000", "SR577",
            "The argument #%ld (%.100s) of callback %.300s[] of %.100s() %.100s",
            (long) (param_type_iter - cbk_param_types) + 1, ssl->ssl_name,
            full_name_ret[0], funname, problem );
	  return;
	}
      param_type_iter++;
    }
  END_DO_SET();
}

static const char *tf_cbk_param_types[COUNTOF__TRIPLE_FEED__ALL] = {
  "RRR",	/* e.g., DB.DBA.TTLP_EV_NEW_GRAPH(?,?) */
  "RRR",	/* e.g., DB.DBA.TTLP_EV_NEW_BLANK(?,?, ?); there was 'select DB.DBA.TTLP_EV_NEW_BLANK(?,?)' */
  "RRRR",	/* e.g., DB.DBA.TTLP_EV_GET_IID(?,?,?, ?); there was 'select DB.DBA.TTLP_EV_GET_IID(?,?,?)'  */
  "RRRRR",	/* e.g., DB.DBA.TTLP_EV_TRIPLE(?, ?, ?, ?, ?) */
  "RRRRRRR",	/* e.g., DB.DBA.TTLP_EV_TRIPLE_L(?, ?, ?, ?,?,?, ?) */
  "RR",		/* e.g., DB.DBA.TTLP_EV_COMMIT(?,?) */
  "RRRRRRRRRRR",	/* e.g., DB.DBA.TTLP_EV_REPORT_DEFAULT(?,?,?,?,?,?,?,?,?,?,?) */
  "RRR" };		/* e.g., DB.DBA.TTLP_EV_NEW_BASE(?,?,?) */


void
tf_set_cbk_names (triple_feed_t *tf, ccaddr_t *cbk_names)
{
  int cbk_count = BOX_ELEMENTS (cbk_names);
  int ctr;
  for (ctr = 0; ctr < COUNTOF__TRIPLE_FEED__ALL; ctr++)
    {
      caddr_t err = NULL;
      if ((ctr >= cbk_count) || ('\0' == cbk_names[ctr][0]))
        {
          tf->tf_cbk_names[ctr] = NULL;
          tf->tf_cbk_qrs[ctr] = NULL;
          continue;
        }
      if ('!' == cbk_names[ctr][0])
        {
          tf->tf_cbk_names[ctr] = cbk_names[ctr];
          tf->tf_cbk_qrs[ctr] = NULL;
          continue;
        }
      sqlr_set_cbk_name_and_proc (tf->tf_qi->qi_client, cbk_names[ctr], tf_cbk_param_types[ctr], tf->tf_creator,
        (caddr_t *) (tf->tf_cbk_names + ctr), tf->tf_cbk_qrs + ctr, &err );
      if (NULL != err)
        sqlr_resignal (err);
    }
}

int32 tf_rnd_seed;

caddr_t
tf_get_iid (triple_feed_t *tf, caddr_t uri)
{
  dtp_t uri_dtp = DV_TYPE_OF (uri);
  caddr_t res;
  caddr_t err = NULL;
  query_t *cbk_qr;
  if ((DV_STRING != uri_dtp) && (DV_UNAME != uri_dtp))
    return box_copy_tree (uri);
  cbk_qr = tf->tf_cbk_qrs[TRIPLE_FEED_GET_IID];
  if (NULL == cbk_qr)
    { /* Now two meaningful cases are "!iri_to_id" and "!cached_iri_to_id" */
      const char *cbk_name = tf->tf_cbk_names[TRIPLE_FEED_GET_IID];
      if (NULL == cbk_name)
        res = box_copy_tree (uri);
      else if (!strcmp (cbk_name, "!iri_to_id"))
        res = iri_to_id ((caddr_t *)(tf->tf_qi), uri, IRI_TO_ID_WITH_CREATE, &err);
      else if (!strcmp (cbk_name, "!cached_iri_to_id"))
        res = iri_to_id ((caddr_t *)(tf->tf_qi), uri, IRI_TO_ID_IF_CACHED, &err);
      else
        sqlr_new_error ("22023", ".....", "Unsupported callback %.100s in RDF parser", cbk_name);
    }
  else
    {
      char params_buf [BOX_AUTO_OVERHEAD + sizeof (caddr_t) * 4];
      void **params;
      BOX_AUTO_TYPED (void **, params, params_buf, sizeof (caddr_t) * 4, DV_ARRAY_OF_POINTER);
      res = NULL;
      params[0] = &uri;
      params[1] = TF_GRAPH_ARG(tf);
      params[2] = &(tf->tf_app_env);
      params[3] = &res;
      err = qr_exec (tf->tf_qi->qi_client, cbk_qr, tf->tf_qi, NULL, NULL, NULL, (caddr_t *)params, NULL, 0);
      BOX_DONE (params, params_buf);
    }
  if (NULL != err)
    sqlr_resignal (err);
  return res;
}


void
tf_new_graph (triple_feed_t *tf, caddr_t uri)
{
  query_t *cbk_qr = tf->tf_cbk_qrs[TRIPLE_FEED_NEW_GRAPH];
  char params_buf [BOX_AUTO_OVERHEAD + sizeof (caddr_t) * 3];
  void **params;
  caddr_t err = NULL;
  if (NULL == cbk_qr)
    return;
  BOX_AUTO_TYPED (void **, params, params_buf, sizeof (caddr_t) * 3, DV_ARRAY_OF_POINTER);
  params[0] = &uri;
  params[1] = TF_GRAPH_ARG(tf);
  params[2] = &(tf->tf_app_env);
  err = qr_exec (tf->tf_qi->qi_client, cbk_qr, tf->tf_qi, NULL, NULL, NULL, (caddr_t *)params, NULL, 0);
  BOX_DONE (params, params_buf);
  if (NULL != err)
    sqlr_resignal (err);
}


void
tf_commit (triple_feed_t *tf)
{
  char params_buf [BOX_AUTO_OVERHEAD + sizeof (caddr_t) * 2];
  void **params;
  caddr_t err;
  query_t *cbk_qr = tf->tf_cbk_qrs[TRIPLE_FEED_COMMIT];
  if (NULL == cbk_qr)
    return;
  BOX_AUTO_TYPED (void **, params, params_buf, sizeof (caddr_t) * 2, DV_ARRAY_OF_POINTER);
  params[0] = TF_GRAPH_ARG(tf);
  params[1] = &(tf->tf_app_env);
  err = qr_exec (tf->tf_qi->qi_client, cbk_qr, tf->tf_qi, NULL, NULL, NULL, (caddr_t *)params, NULL, 0);
  BOX_DONE (params, params_buf);
  if (NULL != err)
    sqlr_resignal (err);
}

void
tf_report (triple_feed_t *tf, char msg_type, const char *sqlstate, const char *sqlmore, const char *descr)
{
  /* caddr_t res; */
  caddr_t err = NULL;
  query_t *cbk_qr;
  char params_buf [BOX_AUTO_OVERHEAD + sizeof (caddr_t) * 11];
  void **params;
  caddr_t msg_no_box, msg_type_box, inp_name_box, line_no_box, triple_no_box, sqlstate_box, sqlmore_box, descr_box;
  cbk_qr = tf->tf_cbk_qrs[TRIPLE_FEED_MESSAGE];
  if (NULL == cbk_qr)
    return;
  BOX_AUTO_TYPED (void **, params, params_buf, sizeof (caddr_t) * 11, DV_ARRAY_OF_POINTER);
  msg_no_box = box_num (tf->tf_message_count++);
  msg_type_box = box_dv_short_nchars (&msg_type, 1);
  inp_name_box = box_copy (tf->tf_boxed_input_name);
  line_no_box = ((NULL != tf->tf_line_no_ptr) ? box_num (tf->tf_line_no_ptr[0]) : NULL);
  triple_no_box = box_num (tf->tf_triple_count);
  sqlstate_box = box_dv_short_string (sqlstate);
  sqlmore_box = box_dv_short_string (sqlmore);
  descr_box = box_dv_short_string (descr);
  /* res = NULL; */
  params[0] = &msg_no_box;
  params[1] = &msg_type_box;
  params[2] = &inp_name_box;
  params[3] = &(tf->tf_base_uri);
  params[4] = &(tf->tf_current_graph_uri);
  params[5] = &line_no_box;
  params[6] = &triple_no_box;
  params[7] = &sqlstate_box;
  params[8] = &sqlmore_box;
  params[9] = &descr_box;
  params[10] = &(tf->tf_app_env);
  /*params[11] = &res;*/
  err = qr_exec (tf->tf_qi->qi_client, cbk_qr, tf->tf_qi, NULL, NULL, NULL, (caddr_t *)params, NULL, 0);
#ifdef DEBUG
  if (NULL != err)
    {
      printf ("tf_report callback error ");
      dbg_print_box (err, stdout);
      printf (" for arguments ");
      dbg_print_box ((caddr_t)params, stdout);
      printf ("\n");
    }
#endif
  dk_free_tree (msg_no_box);
  dk_free_tree (msg_type_box);
  dk_free_tree (inp_name_box);
  dk_free_tree (line_no_box);
  dk_free_tree (triple_no_box);
  dk_free_tree (sqlstate_box);
  dk_free_tree (sqlmore_box);
  dk_free_tree (descr_box);
  BOX_DONE (params, params_buf);
  if (NULL != err)
    sqlr_resignal (err);
#if 0
  if (NULL != res)
    {
      printf ("tf_report callback returns ");
      dbg_print_box (res, stdout);
      printf ("\n");
      dk_free_tree (res);
    }
#endif
}


void
tf_new_base (triple_feed_t *tf, caddr_t new_base)
{
  if ((NULL != tf->tf_cbk_names[TRIPLE_FEED_NEW_GRAPH]) && (NULL != tf->tf_current_graph_uri))
    tf_commit (tf);
  if (tf->tf_current_graph_uri != tf->tf_default_graph_uri)
    {
      dk_free_tree (tf->tf_current_graph_uri);
      tf->tf_current_graph_uri = NULL;
    }
  tf->tf_current_graph_uri = tf->tf_default_graph_uri;
  if (NULL != tf->tf_base_uri)
    {
      dk_free_box (new_base);
      sqlr_new_error ("37000", "SP029", "Multiple base URI declarations are not supported");
    }
  dk_free_box (tf->tf_base_uri); tf->tf_base_uri = box_dv_short_string (new_base);
  do {
      query_t *cbk_qr = tf->tf_cbk_qrs[TRIPLE_FEED_NEW_BASE];
      char params_buf [BOX_AUTO_OVERHEAD + sizeof (caddr_t) * 3];
      void **params;
      caddr_t err = NULL;
      if (NULL == cbk_qr)
        GPF_T;
      BOX_AUTO_TYPED (void **, params, params_buf, sizeof (caddr_t) * 3, DV_ARRAY_OF_POINTER);
      params[0] = &(tf->tf_base_uri);
      params[1] = &(tf->tf_default_graph_uri);
      params[2] = &(tf->tf_app_env);
      err = qr_exec (tf->tf_qi->qi_client, cbk_qr, tf->tf_qi, NULL, NULL, NULL, (caddr_t *)params, NULL, 0);
      BOX_DONE (params, params_buf);
      if (NULL != err)
        sqlr_resignal (err);
    } while (0);
  if (NULL != tf->tf_cbk_names[TRIPLE_FEED_NEW_GRAPH]) {
      dk_free_tree (tf->tf_current_graph_iid);
      if (TF_ONE_GRAPH_AT_TIME (tf)) {
          tf->tf_current_graph_iid = NULL; /* to avoid double free in case of error in tf_get_iid() below */
          tf->tf_default_graph_iid = tf_get_iid (tf, tf->tf_default_graph_uri); }
      tf->tf_current_graph_iid = box_copy (tf->tf_default_graph_iid);
      tf_new_graph (tf, tf->tf_current_graph_uri); }
}


caddr_t
bif_rdf_load_rdfxml_impl (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args, int mode_bits, const char *bifname)
{
  caddr_t text_arg;
  dtp_t dtp_of_text_arg;
  int arg_is_wide = 0;
  char * volatile enc = NULL;
  lang_handler_t *volatile lh = server_default_lh;
  /*caddr_t volatile dtd_config = NULL;*/
  caddr_t base_uri = NULL;
  caddr_t err = NULL;
  /*xml_ns_2dict_t ns_2dict;*/
  caddr_t graph_uri;
  ccaddr_t *cbk_names;
  caddr_t *app_env;
  int n_args = BOX_ELEMENTS (args);
  /*wcharset_t * volatile charset = QST_CHARSET (qst) ? QST_CHARSET (qst) : default_charset;*/
  text_arg = bif_arg (qst, args, 0, bifname);
  mode_bits |= bif_long_arg (qst, args, 1, bifname);
  graph_uri = bif_string_or_wide_or_uname_arg (qst, args, 2, bifname);
  cbk_names = (ccaddr_t *)bif_strict_type_array_arg (DV_STRING, qst, args, 3, bifname);
  app_env = (caddr_t *) bif_arg (qst, args, 4, bifname);
  if ((COUNTOF__TRIPLE_FEED__REQUIRED > BOX_ELEMENTS (cbk_names)) || (COUNTOF__TRIPLE_FEED__ALL < BOX_ELEMENTS (cbk_names)))
    sqlr_new_error ("22023", "RDF01",
      "The argument #4 of %.200s() should be a vector of %d to %d names of stored procedures",
      bifname, COUNTOF__TRIPLE_FEED__REQUIRED, COUNTOF__TRIPLE_FEED__ALL );
  dtp_of_text_arg = DV_TYPE_OF (text_arg);
  /*ns_2dict.xn2_size = 0;*/
  do
    {
      if ((dtp_of_text_arg == DV_SHORT_STRING) ||
	  (dtp_of_text_arg == DV_LONG_STRING) ||
	  (dtp_of_text_arg == DV_C_STRING) )
	{ /* Note DV_TIMESTAMP_OBJ is not enumerated in if(...), unlike bif_string_arg)_ */
	  break;
	}
      if (IS_WIDE_STRING_DTP (dtp_of_text_arg))
	{
	  arg_is_wide = 1;
	  break;
	}
      if (dtp_of_text_arg == DV_STRING_SESSION)
	{
	  int ses_sort = looks_like_serialized_xml (((query_instance_t *)(qst)), text_arg);
	  if (XE_XPER_SERIALIZATION == ses_sort)
	    sqlr_error ("42000",
	      "Function %.200s() does not support loading from string session with persistent XML data", bifname );
	  if (XE_XPACK_SERIALIZATION == ses_sort)
	    {
#if 1
	    sqlr_error ("42000",
	      "Function %.200s() does not support loading from string session with packed XML data", bifname );
#else
	      caddr_t *tree_tmp = NULL; /* Solely to avoid dummy warning C4090: 'function' : different 'volatile' qualifiers */
	      xte_deserialize_packed ((dk_session_t *)text_arg, &tree_tmp, dtd_ptr);
	      tree = (caddr_t)tree_tmp;
	      if (NULL != dtd)
	        dtd_addref (dtd, 0);
	      if ((NULL == tree) && (DEAD_HTML != (parser_mode & ~(FINE_XSLT | GE_XML | WEBIMPORT_HTML | FINE_XML_SRCPOS))))
		sqlr_error ("42000", "The BLOB passed to a function rdf_load_rdfxml() contains corrupted packed XML serialization data");
	      goto tree_complete; /* see below */
#endif
	    }
	  break;
	}
      if (dtp_of_text_arg == DV_BLOB_XPER_HANDLE)
	sqlr_error ("42000",
	  "Function %.200s() does not support loading from persistent XML objects", bifname );
      if ((DV_BLOB_HANDLE == dtp_of_text_arg) || (DV_BLOB_WIDE_HANDLE == dtp_of_text_arg))
	{
	  int blob_sort = looks_like_serialized_xml (((query_instance_t *)(qst)), text_arg);
	  if (XE_XPER_SERIALIZATION == blob_sort)
	    sqlr_error ("42000",
	      "Function %.200s() does not support loading from BLOBs with persistent XML data", bifname );
	  if (XE_XPACK_SERIALIZATION == blob_sort)
	    {
#if 1
	    sqlr_error ("42000",
	      "Function %.200s() does not support loading from BLOBs with packed XML data", bifname );
#else
	      caddr_t *tree_tmp = NULL; /* Solely to avoid dummy warning C4090: 'function' : different 'volatile' qualifiers */
	      dk_session_t *ses = blob_to_string_output (((query_instance_t *)(qst))->qi_trx, text_arg);
	      xte_deserialize_packed (ses, &tree_tmp, dtd_ptr);
	      tree = (caddr_t)tree_tmp;
	      if (NULL != dtd)
	        dtd_addref (dtd, 0);
	      strses_free (ses);
	      if ((NULL == tree) && (DEAD_HTML != (parser_mode & ~(FINE_XSLT | GE_XML | WEBIMPORT_HTML | FINE_XML_SRCPOS))))
		sqlr_error ("42000", "The BLOB passed to a function %.200s() contains corrupted packed XML serialization data", bifname);
	      goto tree_complete; /* see below */
#endif
	    }
	  arg_is_wide = (DV_BLOB_WIDE_HANDLE == dtp_of_text_arg) ? 1 : 0;
	  break;
	}
      sqlr_error ("42000",
	"Function %.200s() needs a string or string session or BLOB as argument 1, not an arg of type %s (%d), bifname",
	bifname, dv_type_title (dtp_of_text_arg), dtp_of_text_arg);
    } while (0);
  /* Now we have \c text ready to process */

/*
  if (n_args < 3)
    enc = CHARSET_NAME (charset, NULL);
*/
  switch (n_args)
    {
    default:
/*    case 9:
      dtd_config = bif_array_or_null_arg (qst, args, 8, bifname);*/
    case 8:
      lh = lh_get_handler (bif_string_arg (qst, args, 7, bifname));
    case 7:
      enc = bif_string_arg (qst, args, 6, bifname);
    case 6:
      base_uri = bif_string_or_uname_or_wide_or_null_arg (qst, args, 5, bifname);
    case 5:
    case 4:
    case 3:
    case 2:
    case 1:
	  ;
    }
  rdfxml_parse ((query_instance_t *) qst, text_arg, (caddr_t *)&err, mode_bits,
    NULL /* source name is unknown */, base_uri, graph_uri, cbk_names, app_env, enc, lh
    /*, caddr_t dtd_config, dtd_t **ret_dtd, id_hash_t **ret_id_cache, &ns2dict*/ );
  if (NULL != err)
    sqlr_resignal (err);
  return NULL;
}


caddr_t
bif_rdf_load_rdfxml (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
  return bif_rdf_load_rdfxml_impl (qst, err_ret, args, 0, "rdf_load_rdfxml");
}


caddr_t
bif_rdf_load_rdfa (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
  return bif_rdf_load_rdfxml_impl (qst, err_ret, args, RDFXML_IN_ATTRIBUTES, "rdf_load_rdfa");
}


caddr_t
bif_rdf_load_microdata (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
  return bif_rdf_load_rdfxml_impl (qst, err_ret, args, RDFXML_IN_MDATA, "rdf_load_microdata");
}


ttlp_t *
ttlp_alloc (void)
{
  ttlp_t *ttlp;
  ttlp = (ttlp_t *)dk_alloc (sizeof (ttlp_t));
  memset (ttlp, 0, sizeof (ttlp_t));
  ttlp->ttlp_lexlineno = 1;
  ttlp->ttlp_tf = tf_alloc ();
  ttlp->ttlp_tf->tf_creator = "rdf_load_turtle";
  return ttlp;
}

void
ttlp_enter_trig_group (ttlp_t *ttlp)
{
  ttlp->ttlp_default_ns_uri_saved = ttlp->ttlp_default_ns_uri;
  ttlp->ttlp_base_uri_saved = ttlp->ttlp_base_uri;
  ttlp->ttlp_in_trig_graph = 1;
}

void
ttlp_leave_trig_group (ttlp_t *ttlp)
{
  if (ttlp->ttlp_default_ns_uri_saved != ttlp->ttlp_default_ns_uri)
    {
      dk_free_box (ttlp->ttlp_default_ns_uri);
      ttlp->ttlp_default_ns_uri = ttlp->ttlp_default_ns_uri_saved;
      ttlp->ttlp_default_ns_uri_saved = NULL;
    }
  if (ttlp->ttlp_base_uri_saved != ttlp->ttlp_base_uri)
    {
      dk_free_box (ttlp->ttlp_base_uri);
      ttlp->ttlp_base_uri = ttlp->ttlp_base_uri_saved;
      ttlp->ttlp_base_uri_saved = NULL;
    }
  dk_free_box ((caddr_t)(ttlp->ttlp_inner_namespaces_prefix2iri));
  ttlp->ttlp_inner_namespaces_prefix2iri = NULL;
  ttlp->ttlp_in_trig_graph = 0;
}

void
ttlp_free (ttlp_t *ttlp)
{
  if (ttlp->ttlp_in_trig_graph)
    {
      dk_free_box ((caddr_t)(ttlp->ttlp_inner_namespaces_prefix2iri));
      if (ttlp->ttlp_default_ns_uri_saved != ttlp->ttlp_default_ns_uri)
        dk_free_box (ttlp->ttlp_default_ns_uri_saved);
      if (ttlp->ttlp_base_uri_saved != ttlp->ttlp_base_uri)
        dk_free_box (ttlp->ttlp_base_uri_saved);
    }
  dk_free_box (ttlp->ttlp_default_ns_uri);
  dk_free_box ((caddr_t)(ttlp->ttlp_namespaces_prefix2iri));
  while (NULL != ttlp->ttlp_saved_uris)
    dk_free_tree ((box_t) dk_set_pop (&(ttlp->ttlp_saved_uris)));
  while (NULL != ttlp->ttlp_unused_seq_bnodes)
    dk_free_tree ((box_t) dk_set_pop (&(ttlp->ttlp_unused_seq_bnodes)));
  dk_free_tree (ttlp->ttlp_last_complete_uri);
  dk_free_tree (ttlp->ttlp_subj_uri);
  dk_free_tree (ttlp->ttlp_pred_uri);
  dk_free_tree (ttlp->ttlp_obj);
  dk_free_tree (ttlp->ttlp_obj_type);
  dk_free_tree (ttlp->ttlp_obj_lang);
  dk_free_tree (ttlp->ttlp_formula_iid);
  tf_free (ttlp->ttlp_tf);
  dk_free (ttlp, sizeof (ttlp_t));
}

void
ttlyyerror_impl (ttlp_t *ttlp_arg, const char *raw_text, const char *strg)
{
  int lineno = ttlp_arg[0].ttlp_lexlineno;
  if (NULL == raw_text)
    raw_text = ttlp_arg[0].ttlp_raw_text;
  tf_report (ttlp_arg[0].ttlp_tf, 'F', "37000", "RDF29", strg);
  sqlr_new_error ("37000", "SP029",
      "%.400s, line %d: %.500s%.5s%.1000s",
      ttlp_arg[0].ttlp_err_hdr,
      lineno,
      strg,
      ((NULL == raw_text) ? "" : " at "),
      ((NULL == raw_text) ? "" : raw_text));
}


void
ttlyyerror_impl_1 (ttlp_t *ttlp_arg, const char *raw_text, int yystate, short *yyssa, short *yyssp, const char *strg)
{
  int sm2, sm1, sp1;
  int lineno = ttlp_arg[0].ttlp_lexlineno;
  if (NULL == raw_text)
    raw_text = ttlp_arg[0].ttlp_raw_text;
  tf_report (ttlp_arg[0].ttlp_tf, 'F', "37000", "RDF30", strg);
  sp1 = yyssp[1];
  sm1 = yyssp[-1];
  sm2 = ((sm1 > 0) ? yyssp[-2] : 0);
  sqlr_new_error ("37000", "RDF30",
     /*errlen,*/ "%.400s, line %d: %.500s [%d-%d-(%d)-%d]%.5s%.1000s%.5s",
      ttlp_arg[0].ttlp_err_hdr,
      lineno,
      strg,
      sm2,
      sm1,
      yystate,
      ((sp1 & ~0x7FF) ? -1 : sp1) /* stub to avoid printing random garbage in logs */ ,
      ((NULL == raw_text) ? "" : " at '"),
      ((NULL == raw_text) ? "" : raw_text),
      ((NULL == raw_text) ? "" : "'")
      );
}


caddr_t ttlp_strliteral (ttlp_t *ttlp_arg, const char *strg, int mode, char delimiter)
{
  caddr_t tmp_buf;
  caddr_t res;
  const char *err_msg;
  const char *src_tail, *src_end;
  char *tgt_tail;
  int strg_is_long = (TTLP_STRLITERAL_QUOT_AT < mode);
  src_tail = strg + (strg_is_long ? 3 : 1);
  src_end = strg + strlen (strg) - (mode>>4);
  tgt_tail = tmp_buf = dk_alloc_box ((src_end - src_tail) + 1, DV_SHORT_STRING);
  while (src_tail < src_end)
    {
      switch (src_tail[0])
	{
	case '\\':
          {
	    const char *bs_src		= "abfnrtv\\\'\">uU\n\r";
	    const char *bs_trans	= "\a\b\f\n\r\t\v\\\'\">\0\0\0\0";
            const char *bs_lengths	= "\2\2\2\2\2\2\2\2\2\2\2\6\012\2\2";
	    const char *hit = strchr (bs_src, src_tail[1]);
	    char bs_len, bs_tran;
	    const char *nextchr;
	    if (NULL == hit)
	      {
		err_msg = "Unsupported escape sequence after '\'";
		goto err;
	      }
            bs_len = bs_lengths [hit - bs_src];
            bs_tran = bs_trans [hit - bs_src];
	    nextchr = src_tail + bs_len;
	    if ((src_tail + bs_len) > src_end)
	      {
	        err_msg = "There is no place for escape sequence between '\' and the end of string";
	        goto err;
	      }
            if ('\0' != bs_tran)
              (tgt_tail++)[0] = bs_tran;
	    else if (('\n' == src_tail[1]) || ('\r' == src_tail[1]))
              {
                (tgt_tail++)[0] = '\n';
                while (('\n' == nextchr[0]) || ('\r' == nextchr[0]))
                  nextchr++;
              }
            else
	      {
		unichar acc = 0;
		for (src_tail += 2; src_tail < nextchr; src_tail++)
		  {
		    int dgt = src_tail[0];
		    if ((dgt >= '0') && (dgt <= '9'))
		      dgt = dgt - '0';
		    else if ((dgt >= 'A') && (dgt <= 'F'))
		      dgt = 10 + dgt - 'A';
		    else if ((dgt >= 'a') && (dgt <= 'f'))
		      dgt = 10 + dgt - 'a';
		    else
		      {
		        err_msg = "Invalid hexadecimal digit in escape sequence";
			goto err;
		      }
		    acc = acc * 16 + dgt;
		  }
		if (acc < 0)
		  {
		    err_msg = "The \\U escape sequence represents invalid Unicode char";
		    goto err;
		  }
		tgt_tail = eh_encode_char__UTF8 (acc, tgt_tail, tgt_tail + MAX_UTF8_CHAR);
	      }
	    src_tail = nextchr;
            continue;
	  }
	default: (tgt_tail++)[0] = (src_tail++)[0];
	}
    }
  res = box_dv_short_nchars (tmp_buf, tgt_tail - tmp_buf);
  box_flags (res) = BF_UTF8;
  dk_free_box (tmp_buf);
  return res;

err:
  dk_free_box (tmp_buf);
  ttlyyerror_impl (ttlp_arg, NULL, err_msg);
  return NULL;
}

ptrlong
ttlp_bit_of_special_qname (caddr_t qname)
{
  if (!strcmp (qname, "a"))	return TTLP_ALLOW_QNAME_A;
  if (!strcmp (qname, "has"))	return TTLP_ALLOW_QNAME_HAS;
  if (!strcmp (qname, "is"))	return TTLP_ALLOW_QNAME_IS;
  if (!strcmp (qname, "of"))	return TTLP_ALLOW_QNAME_OF;
  if (!strcmp (qname, "this"))	return TTLP_ALLOW_QNAME_THIS;
  return 0;
}

#undef ttlp_expand_qname_prefix
caddr_t DBG_NAME (ttlp_expand_qname_prefix) (DBG_PARAMS ttlp_t *ttlp_arg, caddr_t qname)
{
  char *lname = strchr (qname, ':');
  id_hash_t *ns_dict;
  caddr_t ns_pref, ns_uri, *ns_uri_ptr, res;
  int ns_uri_len, local_len, res_len;
  if (NULL == lname)
    {
      lname = qname;
      ns_uri = ttlp_arg[0].ttlp_default_ns_uri;
      if (NULL == ns_uri)
        {
          ns_uri = "#";
          ns_uri_len = 1;
          goto ns_uri_found; /* see below */
        }
      ns_uri_len = box_length (ns_uri) - 1;
      goto ns_uri_found; /* see below */
    }
  if (qname == lname)
    {
      lname = qname + 1;
      ns_uri = ttlp_arg[0].ttlp_default_ns_uri;
      if (NULL == ns_uri)
        {
/* TimBL's sample:
The empty prefix "" is by default , bound to the empty URI "".
this means that <#foo> can be written :foo and using @keywords one can reduce that to foo
*/
#if 0
          res = box_dv_short_nchars (qname + 1, box_length (qname) - 2);
          dk_free_box (qname);
          return res;
#else
          if (DV_STRING == DV_TYPE_OF (qname))
            {
	      qname[0] = '#';
	      return qname;
            }
          ns_uri = "#";
          ns_uri_len = 1;
          goto ns_uri_found; /* see below */
#endif
        }
      ns_uri_len = box_length (ns_uri) - 1;
      goto ns_uri_found; /* see below */
    }
  lname++;
  ns_pref = box_dv_short_nchars (qname, lname - qname);
  do {
      if (ttlp_arg[0].ttlp_in_trig_graph)
        {
          ns_dict = ttlp_arg[0].ttlp_inner_namespaces_prefix2iri;
          ns_uri_ptr = ((NULL == ns_dict) ? NULL : (caddr_t *) id_hash_get (ns_dict, (caddr_t)(&ns_pref)));
          if (NULL != ns_uri_ptr)
            { ns_uri = ns_uri_ptr[0]; break; }
        }
      ns_dict = ttlp_arg[0].ttlp_namespaces_prefix2iri;
      ns_uri_ptr = ((NULL == ns_dict) ? NULL : (caddr_t *) id_hash_get (ns_dict, (caddr_t)(&ns_pref)));
      if (NULL != ns_uri_ptr)
        { ns_uri = ns_uri_ptr[0]; break; }
      if (!strcmp (ns_pref, "rdf:"))
        { ns_uri = uname_rdf_ns_uri; break; }
      if (!strcmp (ns_pref, "xsd:"))
        { ns_uri = uname_xmlschema_ns_uri_hash; break; }
      if (!strcmp (ns_pref, "virtrdf:"))
        { ns_uri = uname_virtrdf_ns_uri; break; }
      dk_free_box (ns_pref);
      ttlyyerror_impl (ttlp_arg, qname, "Undefined namespace prefix");
    } while (0);
  dk_free_box (ns_pref);
  ns_uri_len = box_length (ns_uri) - 1;

ns_uri_found:
  local_len = strlen (lname);
  res_len = ns_uri_len + local_len;
#if 1
  res = DBG_NAME (dk_alloc_box) (DBG_ARGS res_len+1, DV_STRING);
  memcpy (res, ns_uri, ns_uri_len);
  memcpy (res + ns_uri_len, lname, local_len);
  res[res_len] = '\0';
  dk_free_box (qname);
  return res;
#else
  res = box_dv_ubuf (res_len);
  memcpy (res, ns_uri, ns_uri_len);
  memcpy (res + ns_uri_len, lname, local_len);
  res[res_len] = '\0';
  dk_free_box (qname);
  return box_dv_uname_from_ubuf (res);
#endif
}

caddr_t
ttlp_uri_resolve (ttlp_t *ttlp_arg, caddr_t qname)
{
  /*query_instance_t *qi = ttlp_arg[0].ttlp_tf->tf_qi;*/
  caddr_t res, err = NULL;
  res = rfc1808_expand_uri (/*qi,*/ ttlp_arg[0].ttlp_tf->tf_base_uri, qname, "UTF-8", 1 /* ??? */, "UTF-8", "UTF-8", &err);
  if (res != qname)
    dk_free_box (qname);
  if (NULL != err)
    sqlr_resignal (err);
  if (res == ttlp_arg[0].ttlp_tf->tf_base_uri)
    return box_copy (res);
  return res;
}

void
ttlp_triple_and_inf (ttlp_t *ttlp_arg, caddr_t o_uri)
{
  triple_feed_t *tf = ttlp_arg[0].ttlp_tf;
  caddr_t s = ttlp_arg[0].ttlp_subj_uri;
  caddr_t p = ttlp_arg[0].ttlp_pred_uri;
  caddr_t o = o_uri;
  if ((NULL == s) || (NULL == p))
    return;
  if (ttlp_arg[0].ttlp_pred_is_reverse)
    {
      caddr_t swap = o;
      o = s;
      s = swap;
    }
  if (ttlp_arg[0].ttlp_formula_iid)
    {
      caddr_t stmt = tf_bnode_iid (tf, NULL);
      tf_triple (tf, stmt, uname_rdf_ns_uri_subject, s);
      tf_triple (tf, stmt, uname_rdf_ns_uri_predicate, p);
      tf_triple (tf, stmt, uname_rdf_ns_uri_object, o);
      tf_triple (tf, stmt, uname_rdf_ns_uri_type, uname_rdf_ns_uri_Statement);
      tf_triple (tf, ttlp_arg[0].ttlp_formula_iid, uname_swap_reify_ns_uri_statement, stmt);
    }
  tf_triple (tf, s, p, o);
}

void
ttlp_triple_l_and_inf (ttlp_t *ttlp_arg, caddr_t o_sqlval, caddr_t o_dt, caddr_t o_lang)
{
  triple_feed_t *tf = ttlp_arg[0].ttlp_tf;
  caddr_t s = ttlp_arg[0].ttlp_subj_uri;
  caddr_t p = ttlp_arg[0].ttlp_pred_uri;
  if ((NULL == s) || (NULL == p))
    return;
  if (ttlp_arg[0].ttlp_pred_is_reverse)
    {
      if (!(ttlp_arg[0].ttlp_flags & TTLP_SKIP_LITERAL_SUBJECTS))
        ttlyyerror_impl (ttlp_arg, "", "Virtuoso does not support literal subjects");
      if (ttlp_arg[0].ttlp_formula_iid)
        {
          caddr_t stmt = tf_bnode_iid (tf, NULL);
          tf_triple_l (tf, stmt, uname_rdf_ns_uri_subject, o_sqlval, o_dt, o_lang);
          tf_triple (tf, stmt, uname_rdf_ns_uri_predicate, p);
          tf_triple (tf, stmt, uname_rdf_ns_uri_object, s);
          tf_triple (tf, stmt, uname_rdf_ns_uri_type, uname_rdf_ns_uri_Statement);
          tf_triple (tf, ttlp_arg[0].ttlp_formula_iid, uname_swap_reify_ns_uri_statement, stmt);
        }
      return;
    }
  if (ttlp_arg[0].ttlp_formula_iid)
    {
      caddr_t stmt = tf_bnode_iid (tf, NULL);
      tf_triple (tf, stmt, uname_rdf_ns_uri_subject, s);
      tf_triple (tf, stmt, uname_rdf_ns_uri_predicate, p);
      tf_triple_l (tf, stmt, uname_rdf_ns_uri_object, o_sqlval, o_dt, o_lang);
      tf_triple (tf, stmt, uname_rdf_ns_uri_type, uname_rdf_ns_uri_Statement);
      tf_triple (tf, ttlp_arg[0].ttlp_formula_iid, uname_swap_reify_ns_uri_statement, stmt);
    }
  tf_triple_l (ttlp_arg[0].ttlp_tf, s, p, o_sqlval, o_dt, o_lang);
}

void
ttlp_triples_for_bnodes_debug (ttlp_t *ttlp_arg, caddr_t bnode_iid, int lineno, caddr_t label)
{
  triple_feed_t *tf = ttlp_arg[0].ttlp_tf;
  if (NULL != ttlp_arg->ttlp_base_uri)
    tf_triple_l (tf, bnode_iid, uname_virtrdf_ns_uri_bnode_base, ttlp_arg->ttlp_base_uri, NULL, NULL);
  tf_triple_l (tf, bnode_iid, uname_virtrdf_ns_uri_bnode_row, box_num_nonull (lineno), NULL, NULL);
  if (NULL != label)
    tf_triple_l (tf, bnode_iid, uname_virtrdf_ns_uri_bnode_label, label, NULL, NULL);
}


#ifdef DEBUG
#define TF_TRIPLE_PROGRESS_MESSAGE_MOD 25
#else
#define TF_TRIPLE_PROGRESS_MESSAGE_MOD 10000000
#endif

void
tf_triple (triple_feed_t *tf, caddr_t s_uri, caddr_t p_uri, caddr_t o_uri)
{
  char params_buf [BOX_AUTO_OVERHEAD + sizeof (caddr_t) * 5];
  void **params;
  caddr_t err;
  caddr_t replica_of = box_copy (tf->tf_qi->qi_trx->lt_replica_of);
  query_t *cbk_qr = tf->tf_cbk_qrs[TRIPLE_FEED_TRIPLE];
  if (NULL == cbk_qr)
    return;
#ifdef DEBUG
  switch (DV_TYPE_OF (o_uri))
    {
    case DV_LONG_INT:
      rdf_dbg_printf (("\ntf_triple (%ld)", (long)(o_uri)));
    case DV_STRING: case DV_UNAME:
      rdf_dbg_printf (("\ntf_triple (%s)", o_uri));
    }
#endif
  BOX_AUTO_TYPED (void **, params, params_buf, sizeof (caddr_t) * 5, DV_ARRAY_OF_POINTER);
  params[0] = TF_GRAPH_ARG(tf);
  params[1] = &s_uri;
  params[2] = &p_uri;
  params[3] = &o_uri;
  params[4] = &(tf->tf_app_env);
  err = qr_exec (tf->tf_qi->qi_client, cbk_qr, tf->tf_qi, NULL, NULL, NULL, (caddr_t *)params, NULL, 0);
  BOX_DONE (params, params_buf);
  tf->tf_triple_count++;
  if (!tf->tf_qi->qi_trx->lt_replica_of)
    tf->tf_qi->qi_trx->lt_replica_of = replica_of;
  else
    dk_free_box (replica_of);
  if (!(tf->tf_triple_count % TF_TRIPLE_PROGRESS_MESSAGE_MOD))
    tf_report (tf, 'P', NULL, NULL, "Loading is in progress");
  if (NULL != err)
    sqlr_resignal (err);
}

void tf_triple_l (triple_feed_t *tf, caddr_t s_uri, caddr_t p_uri, caddr_t obj_sqlval, caddr_t obj_datatype, caddr_t obj_language)
{
  char params_buf [BOX_AUTO_OVERHEAD + sizeof (caddr_t) * 7];
  void **params;
  caddr_t err;
  caddr_t replica_of = box_copy (tf->tf_qi->qi_trx->lt_replica_of);
  query_t *cbk_qr = tf->tf_cbk_qrs[TRIPLE_FEED_TRIPLE_L];
  if (NULL == cbk_qr)
    return;
#ifdef DEBUG
  switch (DV_TYPE_OF (obj_sqlval))
    {
    case DV_LONG_INT:
      rdf_dbg_printf (("\ntf_triple_l (%ld)", (long)(obj_sqlval))); break;
    case DV_STRING: case DV_UNAME:
      rdf_dbg_printf (("\ntf_triple_l (%s, %s, %s)", obj_sqlval, obj_datatype, obj_language)); break;
    default:
      rdf_dbg_printf (("\ntf_triple_l (..., %s, %s)", obj_datatype, obj_language)); break;
    }
#endif
  BOX_AUTO_TYPED (void **, params, params_buf, sizeof (caddr_t) * 7, DV_ARRAY_OF_POINTER);
  params[0] = TF_GRAPH_ARG(tf);
  params[1] = &s_uri;
  params[2] = &p_uri;
  params[3] = &obj_sqlval;
  params[4] = &obj_datatype;
  params[5] = &obj_language;
  params[6] = &(tf->tf_app_env);
  err = qr_exec (tf->tf_qi->qi_client, cbk_qr, tf->tf_qi, NULL, NULL, NULL, (caddr_t *)params, NULL, 0);
  BOX_DONE (params, params_buf);
  tf->tf_triple_count++;
  if (!tf->tf_qi->qi_trx->lt_replica_of)
    tf->tf_qi->qi_trx->lt_replica_of = replica_of;
  else
    dk_free_box (replica_of);
  if (!(tf->tf_triple_count % TF_TRIPLE_PROGRESS_MESSAGE_MOD))
    tf_report (tf, 'P', NULL, NULL, "Loading is in progress");
  if (NULL != err)
    sqlr_resignal (err);
}

caddr_t
bif_rdf_load_turtle (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
  caddr_t str = bif_string_or_wide_or_null_or_strses_arg (qst, args, 0, "rdf_load_turtle");
  caddr_t base_uri = bif_string_or_uname_arg (qst, args, 1, "rdf_load_turtle");
  caddr_t graph_uri = bif_string_or_uname_or_wide_or_null_arg (qst, args, 2, "rdf_load_turtle");
  long flags = bif_long_arg (qst, args, 3, "rdf_load_turtle");
  caddr_t *cbk_names = bif_strict_type_array_arg (DV_STRING, qst, args, 4, "rdf_load_turtle");
  caddr_t *app_env = (caddr_t *) bif_arg (qst, args, 5, "rdf_load_turtle");
  caddr_t err = NULL;
  caddr_t res;
  if ((COUNTOF__TRIPLE_FEED__REQUIRED > BOX_ELEMENTS (cbk_names)) || (COUNTOF__TRIPLE_FEED__ALL < BOX_ELEMENTS (cbk_names)))
    sqlr_new_error ("22023", "RDF01",
      "The argument #4 of rdf_load_turtle() should be a vector of %d to %d names of stored procedures",
      COUNTOF__TRIPLE_FEED__REQUIRED, COUNTOF__TRIPLE_FEED__ALL );
  res = rdf_load_turtle (str, 0, base_uri, graph_uri, flags,
    (ccaddr_t *) cbk_names, app_env,
    (query_instance_t *)qst, QST_CHARSET(qst), &err );
  if (NULL != err)
    {
      dk_free_tree (res);
      sqlr_resignal (err);
    }
  return res;
}

caddr_t
bif_rdf_load_turtle_local_file (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
  caddr_t str = bif_string_arg (qst, args, 0, "rdf_load_turtle_local_file");
  caddr_t base_uri = bif_string_or_uname_arg (qst, args, 1, "rdf_load_turtle_local_file");
  caddr_t graph_uri = bif_string_or_uname_or_wide_or_null_arg (qst, args, 2, "rdf_load_turtle_local_file");
  long flags = bif_long_arg (qst, args, 3, "rdf_load_turtle_local_file");
  caddr_t *cbk_names = bif_strict_type_array_arg (DV_STRING, qst, args, 4, "rdf_load_turtle_local_file");
  caddr_t *app_env = (caddr_t *) bif_arg (qst, args, 5, "rdf_load_turtle_local_file");
  caddr_t err = NULL;
  caddr_t res;
  if ((COUNTOF__TRIPLE_FEED__REQUIRED > BOX_ELEMENTS (cbk_names)) || (COUNTOF__TRIPLE_FEED__ALL < BOX_ELEMENTS (cbk_names)))
    sqlr_new_error ("22023", "RDF01",
      "The argument #4 of rdf_load_turtle() should be a vector of %d to %d names of stored procedures",
      COUNTOF__TRIPLE_FEED__REQUIRED, COUNTOF__TRIPLE_FEED__ALL );
  file_path_assert (str, NULL, 0);
  res = rdf_load_turtle (str, 1, base_uri, graph_uri, flags,
    (ccaddr_t *) cbk_names, app_env,
    (query_instance_t *)qst, QST_CHARSET(qst), &err );
  if (NULL != err)
    {
      dk_free_tree (res);
      sqlr_resignal (err);
    }
  return res;
}

caddr_t
bif_turtle_lex_analyze (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
  caddr_t str = bif_string_arg (qst, args, 0, "turtle_lex_analyze");
  int mode_flags = ((1 < BOX_ELEMENTS(args)) ? bif_long_arg (qst, args, 1, "turtle_lex_analyze") : 0);
  return ttl_lex_analyze (str, mode_flags, QST_CHARSET(qst));
}

#ifdef DEBUG

typedef struct ttl_lexem_descr_s
{
  int ld_val;
  const char *ld_yname;
  char ld_fmttype;
  const char * ld_fmt;
  caddr_t *ld_tests;
} ttl_lexem_descr_t;

ttl_lexem_descr_t ttl_lexem_descrs[__TTL_NONPUNCT_END+1];

#define LEX_PROPS ttl_lex_props
#define PUNCT(x) 'P', (x)
#define LITERAL(x) 'L', (x)
#define FAKE(x) 'F', (x)
#define TTL "s"
#define TRIG "t"

#define LAST(x) "L", (x)
#define LAST1(x) "K", (x)
#define MISS(x) "M", (x)
#define ERR(x)  "E", (x)

#define PUNCT_TTL_LAST(x) PUNCT(x), TTL, LAST(x)
#define PUNCT_TRIG_LAST(x) PUNCT(x), TRIG, LAST(x)


static void ttl_lex_props (int val, const char *yname, char fmttype, const char *fmt, ...)
{
  va_list tail;
  const char *cmd;
  dk_set_t tests = NULL;
  ttl_lexem_descr_t *ld = ttl_lexem_descrs + val;
  if (0 != ld->ld_val)
    GPF_T;
  ld->ld_val = val;
  ld->ld_yname = yname;
  ld->ld_fmttype = fmttype;
  ld->ld_fmt = fmt;
  va_start (tail, fmt);
  for (;;)
    {
      cmd = va_arg (tail, const char *);
      if (NULL == cmd)
	break;
      dk_set_push (&tests, box_dv_short_string (cmd));
    }
  va_end (tail);
  ld->ld_tests = (caddr_t *)revlist_to_array (tests);
}

static void ttl_lexem_descrs_fill (void)
{
  static int first_run = 1;
  if (!first_run)
    return;
  first_run = 0;
#include "turtle_lex_props.c"
}

caddr_t
bif_turtle_lex_test (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
  dk_set_t report = NULL;
  int tested_lex_val = 0;
  ttl_lexem_descrs_fill ();
  for (tested_lex_val = 0; tested_lex_val < __TTL_NONPUNCT_END; tested_lex_val++)
    {
      char cmd;
      caddr_t **lexems;
      unsigned lex_count;
      unsigned cmd_idx = 0;
      int mode_bits = 0, last_lval, last1_lval;
      ttl_lexem_descr_t *ld = ttl_lexem_descrs + tested_lex_val;
      if (0 == ld->ld_val)
	continue;
      dk_set_push (&report, box_dv_short_string (""));
      dk_set_push (&report,
        box_sprintf (0x100, "#define % 25s %d /* '%s' (%c) */",
	  ld->ld_yname, ld->ld_val, ld->ld_fmt, ld->ld_fmttype ) );
      for (cmd_idx = 0; cmd_idx < BOX_ELEMENTS(ld->ld_tests); cmd_idx++)
	{
	  cmd = ld->ld_tests[cmd_idx][0];
	  switch (cmd)
	    {
	    case 's': mode_bits = 0; break;
	    case 't': mode_bits = TTLP_ALLOW_TRIG; break;
	    case 'K': case 'L': case 'M': case 'E':
	      cmd_idx++;
	      lexems = (caddr_t **) ttl_lex_analyze (ld->ld_tests[cmd_idx], mode_bits, QST_CHARSET(qst));
	      dk_set_push (&report, box_dv_short_string (ld->ld_tests[cmd_idx]));
	      lex_count = BOX_ELEMENTS (lexems);
	      if (0 == lex_count)
		{
		  dk_set_push (&report, box_dv_short_string ("FAILED: no lexems parsed and no error reported!"));
		  goto end_of_test;
		}
	      { char buf[0x1000]; char *buf_tail = buf;
	        unsigned lctr = 0;
		for (lctr = 0; lctr < lex_count && (5 == BOX_ELEMENTS(lexems[lctr])); lctr++)
		  {
		    ptrlong *ldata = ((ptrlong *)(lexems[lctr]));
		    int lval = ldata[3];
		    ttl_lexem_descr_t *ld = ttl_lexem_descrs + lval;
		    if (ld->ld_val)
		      buf_tail += sprintf (buf_tail, " %s", ld->ld_yname);
		    else if (lval < 0x100)
		      buf_tail += sprintf (buf_tail, " '%c'", lval);
		    else GPF_T;
		    buf_tail += sprintf (buf_tail, " %ld ", (long)(ldata[4]));
		  }
	        buf_tail[0] = '\0';
		dk_set_push (&report, box_dv_short_string (buf));
	      }
	      if (3 == BOX_ELEMENTS(lexems[lex_count-1])) /* lexical error */
		{
		  dk_set_push (&report,
		    box_sprintf (0x1000, "%s: ERROR %s",
		      ('E' == cmd) ? "PASSED": "FAILED", lexems[lex_count-1][2] ) );
		  goto end_of_test;
		}
/*
	      if (END_OF_TURTLE_TEXT != ((ptrlong *)(lexems[lex_count-1]))[3])
		{
		  dk_set_push (&report, box_dv_short_string ("FAILED: end of source is not reached and no error reported!"));
		  goto end_of_test;
		}
*/
	      if (0 /*1*/ == lex_count)
		{
		  dk_set_push (&report, box_dv_short_string ("FAILED: no lexems parsed and only end of source has found!"));
		  goto end_of_test;
		}
	      last_lval = ((ptrlong *)(lexems[lex_count-/*2*/1]))[3];
	      if ('E' == cmd)
		{
		  dk_set_push (&report,
		    box_sprintf (0x1000, "FAILED: %d lexems found, last lexem is %d, must be error",
		      lex_count, last_lval) );
		  goto end_of_test;
		}
	      if ('K' == cmd)
		{
		  if (/*4*/2 > lex_count)
		    {
		      dk_set_push (&report,
			box_sprintf (0x1000, "FAILED: %d lexems found, the number of actual lexems is less than two",
			  lex_count ) );
		      goto end_of_test;
		    }
		  last1_lval = ((ptrlong *)(lexems[lex_count-/*3*/2]))[3];
		  dk_set_push (&report,
		    box_sprintf (0x1000, "%s: %d lexems found, one-before-last lexem is %d, must be %d",
		      (last1_lval == tested_lex_val) ? "PASSED": "FAILED", lex_count, last1_lval, tested_lex_val) );
		  goto end_of_test;
		}
	      if ('L' == cmd)
		{
		  dk_set_push (&report,
		    box_sprintf (0x1000, "%s: %d lexems found, last lexem is %d, must be %d",
		      (last_lval == tested_lex_val) ? "PASSED": "FAILED", lex_count, last_lval, tested_lex_val) );
		  goto end_of_test;
		}
	      if ('M' == cmd)
		{
		  unsigned lctr;
		  for (lctr = 0; lctr < lex_count; lctr++)
		    {
		      int lval = ((ptrlong *)(lexems[lctr]))[3];
		      if (lval == tested_lex_val)
			{
			  dk_set_push (&report,
			    box_sprintf (0x1000, "FAILED: %d lexems found, lexem %d is found but it should not occur",
			      lex_count, tested_lex_val) );
			  goto end_of_test;
			}
		    }
		  dk_set_push (&report,
		    box_sprintf (0x1000, "PASSED: %d lexems found, lexem %d is not found and it should not occur",
		      lex_count, tested_lex_val) );
		  goto end_of_test;
		}
	      GPF_T;
end_of_test:
	      dk_free_tree (lexems);
	      break;
	    default: GPF_T;
	    }
	  }
    }
  return revlist_to_array (report);
}
#endif

void
nic_set (name_id_cache_t * nic, caddr_t name, boxint id)
{
  caddr_t name_box = NULL;
  caddr_t * place;
  mutex_enter (nic->nic_mtx);
  place = (caddr_t*) id_hash_get (nic->nic_name_to_id, (caddr_t)&name);
  if(place)
    {
      boxint old_id = *(boxint*)place;
      name_box = ((caddr_t*)place) [-1];
      *(boxint*) place = id;
      remhash_64 (old_id, nic->nic_id_to_name);
      sethash_64 (id, nic->nic_id_to_name,  (boxint)((ptrlong)(name_box)));
    }
  else
    {
      while (nic->nic_id_to_name->ht_count > nic->nic_size)
	{
	  caddr_t key = NULL;
	  boxint id;
	  int32 rnd  = sqlbif_rnd (&tf_rnd_seed);
	  if (id_hash_remove_rnd (nic->nic_name_to_id, rnd, (caddr_t)&key, (caddr_t)&id))
	    {
	      remhash_64 ( id, nic->nic_id_to_name);
	      dk_free_box (key);
	    }
	}
      name_box = treehash == nic->nic_name_to_id->ht_hash_func  ? box_copy (name) :  box_dv_short_string (name);
      id_hash_set (nic->nic_name_to_id, (caddr_t)&name_box, (caddr_t)&id);
      sethash_64 (id, nic->nic_id_to_name, (boxint)((ptrlong)(name_box)));
    }
  mutex_leave (nic->nic_mtx);
}


boxint
nic_name_id (name_id_cache_t * nic, char * name)
{
  boxint * place, res = 0;
  mutex_enter (nic->nic_mtx);
  place = (boxint*) id_hash_get (nic->nic_name_to_id, (caddr_t) &name);
  if (place)
    res = *place;
  mutex_leave (nic->nic_mtx);
  return res;
}


void
nic_merge (name_id_cache_t * to, name_id_cache_t * from)
{
  id_hash_iterator_t hit;
  caddr_t * pn;
  boxint * pid;
  id_hash_iterator (&hit, from->nic_name_to_id);
  while (hit_next (&hit, (caddr_t*)&pn, (caddr_t*)&pid))
    {
      nic_set (to, *pn, *pid);
      dk_free_box (*pn);
    }
  id_hash_clear (from->nic_name_to_id);
  id_hash_clear (from->nic_id_to_name);
}


resource_t * iri_nic_rc;
resource_t * prefix_nic_rc;


void
nic_clear (name_id_cache_t * nic)
{
  id_hash_iterator_t hit;
  caddr_t * pn;
  boxint * pid;
  id_hash_iterator (&hit, nic->nic_name_to_id);
  while (hit_next (&hit, (caddr_t*)&pn, (caddr_t*)&pid))
    {
      dk_free_box (*pn);
    }
  id_hash_clear (nic->nic_name_to_id);
  id_hash_clear (nic->nic_id_to_name);
}

void
nic_free (name_id_cache_t * nic)
{
  nic_clear (nic);
  id_hash_free (nic->nic_name_to_id);
  id_hash_free (nic->nic_id_to_name);
  mutex_free (nic->nic_mtx);
  dk_free ((caddr_t)nic, sizeof (name_id_cache_t));
}


void
nic_done (resource_t * rc, name_id_cache_t * nic)
{
  if (11 == nic->nic_name_to_id->ht_buckets)
    resource_store (rc, (void*) nic);
  else
    nic_free (nic);
}


void
lt_nic_commit_hook (lock_trx_t * lt)
{
  if (lt->lt_rdf_prefix)
    {
      nic_merge (iri_prefix_cache, lt->lt_rdf_prefix);
      nic_done (prefix_nic_rc, lt->lt_rdf_prefix);
      lt->lt_rdf_prefix = NULL;
    }
  if (lt->lt_rdf_iri)
    {
      nic_merge (iri_name_cache, lt->lt_rdf_iri);
      nic_done (iri_nic_rc, lt->lt_rdf_iri);
      lt->lt_rdf_iri = NULL;
    }
}


void
lt_nic_rollback_hook (lock_trx_t * lt)
{
  if (lt->lt_rdf_prefix)
    {
      nic_done (prefix_nic_rc, lt->lt_rdf_prefix);
      lt->lt_rdf_prefix = NULL;
    }
  if (lt->lt_rdf_iri)
    {
      nic_done (iri_nic_rc, lt->lt_rdf_iri);
      lt->lt_rdf_iri = NULL;
    }
}


caddr_t
DBG_NAME(nic_id_name) (DBG_PARAMS name_id_cache_t * nic, boxint id)
{
  caddr_t ret;
  boxint r;
  mutex_enter (nic->nic_mtx);
  gethash_64 (r, id, nic->nic_id_to_name);
  ret = r ? DBG_NAME (box_copy) (DBG_ARGS (caddr_t) (ptrlong)r) : NULL;
  /* read the value inside the mtx because cache replacement may del it before the copy is made if not in the mtx */
  mutex_leave(nic->nic_mtx);
  return ret;
}


name_id_cache_t *
nic_allocate (unsigned long sz, int is_box, int ht_init_sz)
{
  NEW_VARZ (name_id_cache_t, nic);
  nic->nic_size = sz;
  if (ht_init_sz)
    sz = ht_init_sz * 2;
  if (!is_box)
    nic->nic_name_to_id = id_hash_allocate (sz / 2, sizeof (caddr_t), sizeof (boxint), strhash, strhashcmp);
  else
    nic->nic_name_to_id = id_hash_allocate (sz / 2, sizeof (caddr_t), sizeof (boxint), treehash, treehashcmp);
  nic->nic_id_to_name = hash_table_allocate_64 (sz);
  id_hash_set_rehash_pct (nic->nic_id_to_name, 220);
  id_hash_set_rehash_pct (nic->nic_name_to_id, 220);
  nic->nic_mtx =mutex_allocate ();
  mutex_option (nic->nic_mtx, is_box ? "NICB" : "NIC", NULL, NULL);
  return nic;
}

void
nic_flush (name_id_cache_t * nic)
{
  int bucket_ctr = 0;
  mutex_enter (nic->nic_mtx);
  for (bucket_ctr = nic->nic_name_to_id->ht_buckets; bucket_ctr--; /* no step */)
    {
      caddr_t key;
      boxint id;
      while (id_hash_remove_rnd (nic->nic_name_to_id, bucket_ctr, (caddr_t)&key, (caddr_t)&id))
        {
          remhash_64 ( id, nic->nic_id_to_name);
          dk_free_box (key);
        }
    }
  mutex_leave (nic->nic_mtx);
}


void
lt_nic_set (lock_trx_t * lt, name_id_cache_t * nic, caddr_t name, boxint id)
{
  if (iri_name_cache == nic)
    {
      if (lt && !lt->lt_client->cli_row_autocommit)
	{
	  if (!lt->lt_rdf_iri)
	    {
	      lt->lt_rdf_iri = (name_id_cache_t*) resource_get (iri_nic_rc);
	      if (!lt->lt_rdf_iri)
		lt->lt_rdf_iri = nic_allocate (140000, 1, 11);
	      lt->lt_commit_hook = lt_nic_commit_hook;
	      lt->lt_rollback_hook = lt_nic_rollback_hook;
	    }
	  nic_set (lt->lt_rdf_iri, name, id);
	}
      else
	nic_set (iri_name_cache, name, id);
    }
  else
    {
      if (lt && !lt->lt_client->cli_row_autocommit)
	{
	  if (!lt->lt_rdf_prefix)
	    {
	      lt->lt_rdf_prefix = (name_id_cache_t*) resource_get (prefix_nic_rc);
	      if (!lt->lt_rdf_prefix)
		lt->lt_rdf_prefix = nic_allocate (4000, 0, 11);
	      lt->lt_commit_hook = lt_nic_commit_hook;
	      lt->lt_rollback_hook = lt_nic_rollback_hook;
	    }
	  nic_set (lt->lt_rdf_prefix, name, id);
	}
      else
	nic_set (iri_prefix_cache, name, id);
    }
}


boxint
lt_nic_name_id (lock_trx_t * lt, name_id_cache_t * nic, caddr_t name)
{
  name_id_cache_t * lt_nic = lt->lt_client->cli_row_autocommit ? NULL
    : nic == iri_name_cache ? lt->lt_rdf_iri : lt->lt_rdf_prefix;
  if (lt_nic)
    {
      boxint id = nic_name_id (lt_nic, name);
      if (id)
	return id;
    }
  return nic_name_id (nic, name);
}


caddr_t
lt_nic_id_name (lock_trx_t * lt, name_id_cache_t * nic, boxint id)
{
  name_id_cache_t * lt_nic = lt->lt_client->cli_row_autocommit ? NULL
    : nic == iri_name_cache ? lt->lt_rdf_iri : lt->lt_rdf_prefix;
  if (lt_nic)
    {
      caddr_t name = nic_id_name (lt_nic, id);
      if (name)
	return name;
    }
  return nic_id_name (nic, id);
}



#define IS_INT_LIKE(x) ((x) == DV_LONG_INT || (x) == DV_INT64 || (x) == DV_IRI_ID || (x) == DV_IRI_ID_8)


int
tb_string_and_id_check (dbe_table_t * tb, dbe_column_t ** str_col, dbe_column_t ** id_col)
{
  /* true if tb has string pk and int dependent and another key with the reverse */
  dbe_key_t * pk = tb->tb_primary_key;
  dbe_column_t * col1 = (dbe_column_t *) pk->key_parts->data;
  dbe_column_t * col2 = pk->key_parts->next ? (dbe_column_t *) pk->key_parts->next->data : NULL;
  if (!col2
      || col1->col_sqt.sqt_dtp != DV_STRING
      || (! IS_INT_LIKE (col2->col_sqt.sqt_dtp))
      || !tb->tb_keys->next
      || tb->tb_keys->next->next)
  return 0;
  *str_col = col1;
  *id_col = col2;
  return 1;
}

#define N_IRI_SEQS 19
#define IRI_RANGE_SZ 10000

extern dk_mutex_t * log_write_mtx;


boxint
rdf_new_iri_id (lock_trx_t * lt, char ** value_seq_ret, int nth, query_instance_t * qi)
{
  int rc;
  caddr_t log_array, * old_repl;
  static caddr_t iri_seq[N_IRI_SEQS];
  static caddr_t iri_seq_max[N_IRI_SEQS];
  static caddr_t range_seq;
  boxint id, id_max;
  if (-1 == nth)
    {
      du_thread_t * self = THREAD_CURRENT_THREAD;
      nth = (((uptrlong)self) ^ (((uptrlong)self) >> 11))
      % N_IRI_SEQS;
    }
  else
    nth = ((unsigned int)nth) % N_IRI_SEQS;
  if (!range_seq)
    {
      int inx;
      for (inx = 0; inx < N_IRI_SEQS; inx++)
	{
	  char name[20];
	  sprintf (name, "__IRI%d", inx);
	  iri_seq[inx] = box_dv_short_string (name);
	  sprintf (name, "__IRI_MAX%d", inx);
	  iri_seq_max[inx] = box_dv_short_string (name);
	}
      range_seq = box_dv_short_string ("RDF_URL_IID_NAMED");
    }
  IN_TXN;
  id = sequence_next_inc (iri_seq[nth], INSIDE_MAP, 1);
  id_max = sequence_set (iri_seq_max[nth], 0, SEQUENCE_GET, INSIDE_MAP);
  if (id < id_max)
    {
      LEAVE_TXN;
      *value_seq_ret = iri_seq[nth];
      return id;
    }
    id = sequence_next_inc (range_seq, INSIDE_MAP, IRI_RANGE_SZ);
  if (!id)
    sequence_set (range_seq, IRI_RANGE_SZ, SET_ALWAYS, INSIDE_MAP);
  sequence_set (iri_seq[nth], id + 1, SET_ALWAYS, INSIDE_MAP);
  sequence_set (iri_seq_max[nth], id + IRI_RANGE_SZ, SET_ALWAYS, INSIDE_MAP);
  LEAVE_TXN;
  if (!in_srv_global_init)
    {
      log_array = list (5, box_string ("DB.DBA.ID_RANGE_REPLAY (?, ?, ?, ?)"),
	  box_dv_short_string (iri_seq[nth]), box_dv_short_string (iri_seq_max[nth]),
	  box_num (id), box_num (id + IRI_RANGE_SZ));
      mutex_enter (log_write_mtx);
      old_repl = lt->lt_replicate;
      lt->lt_replicate = REPL_LOG;
      rc = log_text_array_sync (lt, log_array);
      lt->lt_replicate = old_repl;
      mutex_leave (log_write_mtx);
      dk_free_tree (log_array);
      if (rc != LTE_OK)
	{
	  static caddr_t details = NULL;
	  if (NULL == details)
	    details = box_dv_short_string ("while writing new IRI_ID range allocation to log file");
	  /*      if (lt->lt_client != bootstrap_cli) */
	  sqlr_resignal (srv_make_trx_error (rc, details));
	}
    }

  *value_seq_ret = iri_seq[nth];
  return id;
}



caddr_t
tb_new_id_and_name (lock_trx_t * lt, it_cursor_t * itc, dbe_table_t * tb, caddr_t name, char * value_seq_name)
{
  int rc;
  caddr_t log_array;
  dbe_key_t * id_key = (dbe_key_t *)(tb->tb_keys->data == tb->tb_primary_key ? tb->tb_keys->next->data : tb->tb_keys->data);
  caddr_t seq_box = box_dv_short_string (value_seq_name);
  int64 res = 0 == strcmp ("RDF_URL_IID_NAMED", seq_box)
    ? rdf_new_iri_id (lt, &value_seq_name, lt->lt_trx_no, NULL) : sequence_next_inc (seq_box, OUTSIDE_MAP, 1);
  dbe_column_t * id_col = (dbe_column_t *)id_key->key_parts->data;
  caddr_t res_box;
  LOCAL_RD (rd);
  if (!res)
    res = sequence_next_inc (seq_box, OUTSIDE_MAP, 1);
  dk_free_box (seq_box);
  res_box = box_iri_int64 (res, id_col->col_sqt.sqt_dtp);
  itc->itc_insert_key = tb->tb_primary_key;
  itc->itc_owned_search_par_fill= 0; /* do not free the name yet */
  itc_from (itc, itc->itc_insert_key);
  ITC_SEARCH_PARAM(itc, name);
  ITC_OWNS_PARAM(itc, name);
  rd.rd_non_comp_len = itc->itc_insert_key->key_row_var_start[0] + box_length (name) - 1;
  rd.rd_n_values = 2;
  rd.rd_values[0] = name;
  rd.rd_values[1] = res_box;
  itc->itc_key_spec = itc->itc_insert_key->key_insert_spec;
  itc_insert_unq_ck (itc, &rd, NULL);
  rd.rd_values[0] = res_box;
  rd.rd_values[1] = name;
  itc->itc_insert_key = id_key;
  rd.rd_non_comp_len = itc->itc_insert_key->key_row_var_start[0] + box_length (name) - 1;
  itc->itc_owned_search_par_fill = 0; /* do not free the name yet */
  itc_from (itc, itc->itc_insert_key);
  ITC_SEARCH_PARAM(itc, res_box);
  ITC_SEARCH_PARAM(itc, name);
  ITC_OWNS_PARAM (itc, name);
  itc->itc_key_spec = itc->itc_insert_key->key_insert_spec;

  itc_insert_unq_ck (itc, &rd, NULL);
  log_array = list (5, box_string ("DB.DBA.ID_REPLAY (?, ?, ?, ?)"),
		    box_dv_short_string (tb->tb_name), box_dv_short_string (value_seq_name), box_copy (name), box_copy (res_box));
  mutex_enter (log_write_mtx);
  rc = log_text_array_sync (lt, log_array);
  mutex_leave (log_write_mtx);
  dk_free_tree (log_array);
  if (rc != LTE_OK)
    {
static caddr_t details = NULL;
      if (NULL == details)
        details = box_dv_short_string ("while writing new IRI_ID allocation to log file");
/*      if (lt->lt_client != bootstrap_cli) */
      sqlr_resignal (srv_make_trx_error (rc, details));
    }
  return res_box;
}


caddr_t
tb_cl_name_to_id (lock_trx_t * lt, it_cursor_t * itc)
{
  return (caddr_t) -1; /* is local, go ahead */
}

caddr_t
tb_name_to_id (lock_trx_t * lt, char * tb_name, caddr_t name, char * value_seq_name)
{
  /* the name param is freed */
  int res, rc;
  caddr_t iri = NULL;
  dbe_table_t * tb = sch_name_to_table (wi_inst.wi_schema, tb_name);
  dbe_key_t * key = tb ? tb->tb_primary_key : NULL;
  dbe_column_t * iri_col = key && key->key_parts && key->key_parts->next ? key->key_parts->next->data : NULL;
  it_cursor_t itc_auto;
  it_cursor_t * itc = &itc_auto;
  buffer_desc_t * buf;
  dbe_column_t *str_col, *id_col;
  if (!iri_col)
    return NULL;
  if (!tb_string_and_id_check (tb, &str_col, &id_col))
    return NULL;
  ITC_INIT (itc, key->key_fragments[0]->kf_it, NULL);
  itc->itc_ltrx = lt;
  itc_from (itc, key);
  ITC_SEARCH_PARAM (itc, name);
  ITC_OWNS_PARAM(itc, name);
  if(lt)
    itc->itc_isolation =ISO_COMMITTED;
  else
    itc->itc_isolation = ISO_UNCOMMITTED;
  itc->itc_search_mode = SM_INSERT;
  itc->itc_key_spec = key->key_insert_spec;
  if (itc->itc_insert_key->key_partition)
    {
      sqlr_new_error ("42000", "CL...", "tb_cl_name_to_id is not to be run. Problem of initializing partitioned db with local only mode.");
      iri = tb_cl_name_to_id (lt, itc);
      if ((caddr_t)-1 != iri)
	{
	  itc_free (itc);
	  return iri;
	}
    }
  ITC_FAIL (itc)
    {
re_search:
      buf = itc_reset (itc);
      res = itc_search (itc, &buf);
      if (DVC_MATCH == res)
	{
	  iri = itc_box_column (itc, buf, iri_col->col_id, NULL);
	  itc_page_leave (itc, buf);
	}
      else if (NULL == value_seq_name)
        {
           iri = 0;
           itc_page_leave (itc, buf);
        }
      else
	{
	  itc->itc_isolation = ISO_SERIALIZABLE;
          itc->itc_lock_mode = PL_EXCLUSIVE;
          itc->itc_search_mode = SM_READ;
	  if (ITC_AT_END == itc->itc_map_pos)
	    rc = NO_WAIT;
	  else
	    rc = itc_set_lock_on_row (itc, &buf);
	  if(NO_WAIT != rc)
	    {
	      itc_page_leave(itc, buf);
	      goto re_search; /* see above */
	    }
	  itc_page_leave (itc, buf);
          iri = tb_new_id_and_name (lt, itc, tb, name, value_seq_name);
	}
    }
  ITC_FAILED
      {
	du_thread_t * self = THREAD_CURRENT_THREAD;
	itc_free (itc);
	if (!lt || !self->thr_reset_ctx)
	  return NULL;
      }
  END_FAIL (itc);
  itc_free (itc);
  return iri;
}

int32 rdf_shorten_long_iri = 0;

static const char ctohex[] = "0123456789abcdef";

static char *
iri_shorten (char * iri, char * buf, size_t buf_len, int * ret_len)
{
  unsigned char digest[16];
  MD5_CTX ctx;
  int inx = 0;
  char *end = buf + buf_len - 1, *tail = buf + buf_len - 33, *str = iri + buf_len - 33;

  memcpy (buf, iri, buf_len - 33); /* hex md5 + zero byte */
  memset (&ctx, 0, sizeof (MD5_CTX));
  MD5Init (&ctx);
  MD5Update (&ctx, (unsigned char *) str, strlen (str));
  MD5Final (digest, &ctx);
  while (tail < end)
    {
      unsigned c = (unsigned) digest[inx++];
      *(tail++) = ctohex[0xf & (c >> 4)];
      *(tail++) = ctohex[0xf & c];
    }
  *tail = 0;
  *ret_len = strlen (buf);
  return buf;
}

int
iri_split (char * iri, caddr_t * pref, caddr_t * name)
{
  char * local_start;
  int len = strlen (iri);
  char tmp[MAX_RULING_PART_BYTES - 20];
  if (len > MAX_RULING_PART_BYTES - 20)
    {
      if (rdf_shorten_long_iri)
	iri = iri_shorten (iri, tmp, sizeof (tmp), &len);
      else
    return 0;
    }
  if (('_' == iri[0]) && (':' == iri[1]))
    { /* named blank node is a special case. Label can contain weird chars but it is treated as */
      local_start = iri + 2;
      goto local_start_found; /* see below */
    }
  local_start = strrchr (iri, '#');
  if (!local_start)
    local_start = strrchr (iri, '?');
  if (!local_start)
    {
      /* first / that is not // */
      char * ptr =iri;
      char * s;
      for (;;)
	{
	  s = strchr (ptr, '/');
	  if (!s)
	    break;
	  if ('/' != s[1])
	    break;
	  ptr = s + 2;
	}
      if (!s)
	local_start = iri;
      else
	local_start = s + 1;
    }
  else
    local_start++;

local_start_found:
  *pref = box_dv_short_nchars (iri, local_start - iri);
  *name = box_dv_short_nchars (local_start - 4, 4 + strlen (local_start));
  return 1;
}

void
iri_split_ttl_qname (const char * iri, caddr_t * pref_ret, caddr_t * name_ret, int abbreviate_nodeid)
{
  const char *tail;
  int iri_strlen;
  if (!iri)
    {
      *name_ret = box_dv_short_string ("null_iri");
      *pref_ret = box_dv_short_string ("null_prefix");
	return;
    }
  iri_strlen = strlen (iri);
  for (tail = iri + iri_strlen; tail > iri; tail--)
    {
      unsigned char c = (unsigned char) tail[-1];
      if (!isalnum(c) && ('_' != c) && ('-' != c) && !(c & 0x80))
        break;
    }
  if (isdigit (tail[0]) || ('-' == tail[0]) || ((tail > iri) && (NULL == strchr ("#/:?", tail[-1]))))
    tail = iri + iri_strlen;
/*                                                         0123456789 */
  if (abbreviate_nodeid && (tail-iri >= 9) && !memcmp (iri, "nodeID://", 9))
    {
      int boxlen = (tail - iri) - (9 - 1);
      caddr_t pref = pref_ret[0] = dk_alloc_box (boxlen, DV_STRING);
      pref[0] = '_';
      memcpy (pref + 1, iri + 9, tail - (iri + 9));
      pref[boxlen-1] = '\0';
    }
  else
    pref_ret[0] = box_dv_short_nchars (iri, tail - iri);
  name_ret[0] = box_dv_short_nchars (tail, iri + iri_strlen - tail);
}

name_id_cache_t * iri_name_cache;
name_id_cache_t * iri_prefix_cache;
name_id_cache_t * rdf_lang_cache;
name_id_cache_t * rdf_type_cache;

caddr_t
key_name_to_iri_id_1 (lock_trx_t * lt, caddr_t name, int make_new)
{
  boxint pref_id_no, iri_id_no;
  caddr_t local_copy;
  caddr_t prefix, local;
  caddr_t pref_id, iri_id;
  if (DV_IRI_ID == DV_TYPE_OF (name))
    return box_copy (name);
#ifdef DEBUG
/*                                             01234567 */
  if (uriqa_dynamic_local && !strncmp (name, "http://", 7))
    {
      const char *regname = "URIQADefaultHost";
      caddr_t *host_ptr;
      IN_TXN;
      ASSERT_IN_TXN;
      host_ptr = (caddr_t *) id_hash_get (registry, (caddr_t) & regname);
      LEAVE_TXN;
      if ((NULL != host_ptr) && IS_BOX_POINTER (host_ptr[0]))
        {
          caddr_t host = host_ptr[0];
          int host_strlen = strlen (host);
          if (!strncmp (name + 7, host, host_strlen) && ('/' == name[7+host_strlen]))
            {
              printf ("\nOops, %s in key_name_to_iri_id()", name);
            }
        }
    }
#endif
  if (CL_RUN_SINGLE_CLUSTER == cl_run_local_only)
    return NULL;
  if (!iri_split (name, &prefix, &local))
    return NULL;
  pref_id_no = lt_nic_name_id (lt, iri_prefix_cache, prefix);
  if (!pref_id_no)
    {
      caddr_t pref_copy;
      pref_copy = box_copy (prefix);
      pref_id = tb_name_to_id (lt, "DB.DBA.RDF_PREFIX", prefix, make_new ? "RDF_PREF_SEQ" : NULL);
      if (!pref_id)
	{
	  dk_free_box (pref_copy);
	  dk_free_box (local);
	  return NULL;
	}
      pref_id_no = unbox (pref_id);
      lt_nic_set (lt, iri_prefix_cache, pref_copy, pref_id_no);
      dk_free_box (pref_id);
      dk_free_box (pref_copy);
    }
  else
    dk_free_box (prefix);
  LONG_SET_NA (local, pref_id_no);
  iri_id_no = lt_nic_name_id (lt, iri_name_cache, local);
  if (iri_id_no)
    {
      dk_free_box (local);
      return box_iri_id (iri_id_no);
    }
  local_copy = box_copy (local);
  iri_id = tb_name_to_id (lt, "DB.DBA.RDF_IRI", local,
    (  make_new ?
      ((('_' == name[0]) && (':' == name[1])) ?
        "RDF_URL_IID_NAMED_BLANK" : "RDF_URL_IID_NAMED" ) :
      NULL ) );
  if(!iri_id)
    {
      dk_free_box (local_copy);
      return NULL;
    }
  lt_nic_set (lt, iri_name_cache, local_copy, unbox_iri_id (iri_id));
  dk_free_box (local_copy);
  return iri_id;
}


caddr_t
key_name_to_iri_id (lock_trx_t * lt, caddr_t name, int make_new)
{
  caddr_t r;
  int entered = 0;
  if (!lt)
    {
      int rc;
      client_connection_t * cli = sqlc_client ();
      lt = cli->cli_trx;
      if (0 == cli->cli_trx->lt_threads)
	{
	  entered = 1;
	  rc = lt_enter (cli->cli_trx);
	  if (LTE_OK != rc)
	    return NULL;
    }
    }
  r = key_name_to_iri_id_1 (lt, name, make_new);
  if (entered)
    {
      IN_TXN;
      lt_leave (lt);
      LEAVE_TXN;
    }
  return r;
}

dbe_key_t * sch_find_key (dbe_schema_t * sc, char * tb_name, char * key_name);


int64
key_find_rdf_obj_1 (rdf_box_t * rb, caddr_t name)
{
  int res;
  int64 id = 0;
  caddr_t id_box = NULL;
  dbe_key_t * key = sch_find_key (NULL, "DB.DBA.RDF_OBJ", "RO_VAL");
  dbe_column_t * val_col;
  dbe_column_t * dtlang_col;
  dbe_column_t * id_col;
  it_cursor_t itc_auto;
  it_cursor_t * itc = &itc_auto;
  caddr_t dtlang = box_num ((rb->rb_type << 16) | rb->rb_lang);
  buffer_desc_t * buf;
  search_spec_t sp, sp2;
  rb_dt_lang_check(rb);
  if (dk_set_length (key->key_parts) < 3)
    return 0;
  id_col = (dbe_column_t*)key->key_parts->next->next->data;
  val_col = (dbe_column_t*)key->key_parts->data;
  dtlang_col = (dbe_column_t*)key->key_parts->next->next->data;
  ITC_INIT (itc, key->key_fragments[0]->kf_it, NULL);
  itc_from (itc, key);
  ITC_SEARCH_PARAM (itc, name);
  ITC_SEARCH_PARAM (itc, dtlang);
  ITC_OWNS_PARAM (itc, dtlang);
  itc->itc_isolation = ISO_UNCOMMITTED;
  itc->itc_key_spec.ksp_spec_array = &sp;
  itc->itc_key_spec.ksp_key_cmp = NULL;
  memset (&sp, 0, sizeof (sp));
  sp.sp_min_op = CMP_EQ;
  sp.sp_cl = *key_find_cl (key, ((dbe_column_t *) key->key_parts->data)->col_id);
  memset (&sp2, 0, sizeof (sp2));
  sp2.sp_min_op = CMP_EQ;
  sp2.sp_min = 1;
  sp2.sp_cl = *key_find_cl (key, ((dbe_column_t *) key->key_parts->next->data)->col_id);
  sp.sp_next = &sp2;
  ITC_FAIL (itc)
    {
      buf = itc_reset (itc);
      res = itc_search (itc, &buf);
      if (DVC_MATCH == res)
	{
	  id_box = itc_box_column (itc, buf, id_col->col_id, NULL);
	  id = unbox (id_box);
	  dk_free_box (id_box);
	}
      itc_page_leave (itc, buf);
    }
	ITC_FAILED
      {
	return 0;
      }
  END_FAIL (itc);
  itc_free (itc);
  return id;
}


rdf_box_t *
key_find_rdf_obj (lock_trx_t * lt, rdf_box_t * rb)
{
  int len;
  caddr_t allocd_content = NULL;
  int entered = 0;

  if (!lt)
    {
      int rc;
      client_connection_t * cli = sqlc_client ();
      lt = cli->cli_trx;
      if (0 == cli->cli_trx->lt_threads)
	{
	  entered = 1;
	  rc = lt_enter (cli->cli_trx);
	  if (LTE_OK != rc)
	    return rb;
	}
    }
  {
    caddr_t content = rb->rb_box;
    dtp_t cdtp = DV_TYPE_OF (content);

    if (DV_XML_ENTITY == cdtp && rb->rb_chksum_tail)
      {
	QNCAST (rdf_bigbox_t, rbb, rb);
	dk_check_tree (rbb->rbb_chksum);
	rb->rb_ro_id = key_find_rdf_obj_1 (rb, rbb->rbb_chksum);
      }
    else
      {
	if (DV_GEO == cdtp)
	  {
	    caddr_t err = NULL;
	    content = box_to_any (content, &err);
	    allocd_content = content;
	  }
	len = box_length (content) - 1;
	if (len > RB_BOX_HASH_MIN_LEN)
	  {
	    caddr_t trid = mdigest5 (content);
	    rb->rb_ro_id = key_find_rdf_obj_1 (rb, trid);
	    dk_free_box (trid);
	  }
	else
	  rb->rb_ro_id = key_find_rdf_obj_1 (rb, rb->rb_box);
	dk_free_box (allocd_content);
      }
  }
  if (entered)
    {
      IN_TXN;
      lt_leave (lt);
      LEAVE_TXN;
    }
  return rb;
}


caddr_t
key_name_to_existing_cached_iri_id (lock_trx_t * lt, caddr_t name)
{
  boxint pref_id_no, iri_id_no;
  caddr_t prefix, local;
  if (DV_IRI_ID == DV_TYPE_OF (name))
    return box_copy (name);
  if (!iri_split (name, &prefix, &local))
    return NULL;
#ifndef NDEBUG
/*                                             01234567 */
  if (uriqa_dynamic_local && !strncmp (name, "http://", 7))
    {
      const char *regname = "URIQADefaultHost";
      caddr_t *host_ptr;
      IN_TXN;
      ASSERT_IN_TXN;
      host_ptr = (caddr_t *) id_hash_get (registry, (caddr_t) & regname);
      LEAVE_TXN;
      if ((NULL != host_ptr) && IS_BOX_POINTER (host_ptr[0]))
        {
          caddr_t host = host_ptr[0];
          int host_strlen = strlen (host);
          if (!strncmp (name + 7, host, host_strlen) && ('/' == name[7+host_strlen]))
            {
              printf ("\nOops, %s in key_name_to_existing_cached_iri_id()", name);
            }
        }
    }
#endif
  pref_id_no = lt_nic_name_id (lt, iri_prefix_cache, prefix);
  dk_free_box (prefix);
  if (!pref_id_no)
    {
      dk_free_box (local);
      return NULL;
    }
  LONG_SET_NA (local, pref_id_no);
  iri_id_no = lt_nic_name_id (lt, iri_name_cache, local);
  dk_free_box (local);
  if (!iri_id_no)
    return NULL;
  return box_iri_id (iri_id_no);
}




caddr_t
bif_iri_id_new (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
  query_instance_t * qi = (query_instance_t *) qst;
  boxint id;
  caddr_t value_seq;
  int nth = -1;
  if (BOX_ELEMENTS (args))
    nth = bif_long_arg (qst, args, 0, "iri_id_new");
  else
    nth = qi->qi_client->cli_trx->lt_trx_no;
  id = rdf_new_iri_id (qi->qi_trx, &value_seq, nth, (query_instance_t *)qst);
  log_sequence (qi->qi_trx, value_seq, id + 1);
  return box_iri_id (id);
}

int
iri_canonicalize (query_instance_t *qi, caddr_t name, int mode, caddr_t *res_ret, caddr_t *err_ret)
{
  dtp_t dtp = DV_TYPE_OF (name);
  dtp_t orig_dtp = dtp;
  caddr_t box_to_delete = NULL;
  res_ret[0] = NULL;
again:
  switch (dtp)
    {
    case DV_DB_NULL:
      return 0;
    case DV_IRI_ID:
      res_ret[0] = name;
      return 1;
    case DV_WIDE:
      box_to_delete = name = box_wide_as_utf8_char (name, (box_length (name) / sizeof (wchar_t)) - 1, DV_STRING);
      break;
    case DV_XML_ENTITY:
      {
        xml_entity_t *xe = (xml_entity_t *)name;
        box_to_delete = NULL;
        xe_string_value_1 (xe, &box_to_delete, DV_STRING);
        if (NULL == box_to_delete)
          {
            err_ret[0] = srv_make_new_error ("RDFXX", ".....",
              "XML entity with no string value is passed as an argument to iri_to_id (), type %d", (unsigned int)orig_dtp );
            goto return_error; /* see below */
          }
        name = box_to_delete;
        break;
      }
    case DV_STRING:
    case DV_UNAME:
      break;
    case DV_RDF:
      {
        rdf_box_t *rb = (rdf_box_t *)name;
        if (!rb->rb_is_complete)
          {
            if (IRI_TO_ID_IF_CACHED == mode)
              return 0;
            rb_complete (rb, qi->qi_trx, qi);
          }
        name = box_copy_tree (rb->rb_box);
        dtp = DV_TYPE_OF (name);
        goto again; /* see above */
      }
    default:
      err_ret[0] = srv_make_new_error ("RDFXX", ".....",
        "Bad argument to iri_to_id (), type %d", (unsigned int)orig_dtp );
      goto return_error; /* see below */
    }
  if (1 == box_length (name))
    {
      err_ret[0] = srv_make_new_error ("RDFXX", ".....",
        "Empty string is not a valid argument to iri_to_id (), type %d", (unsigned int)orig_dtp );
      goto return_error; /* see below */
    }
/*                     0123456789 */
  if (!strncmp (name, "nodeID://", 9))
    {
      unsigned char *tail = (unsigned char *)(name + 9);
      int64 acc = 0;
      int b_first = 0;
      if ('b' == tail[0]) { b_first = 1; tail++; }
      while (isdigit (tail[0]))
        acc = acc * 10 + ((tail++)[0] - '0');
      if ('\0' != tail[0])
        {
          err_ret[0] = srv_make_new_error ("RDFXX", ".....",
            "Bad argument to iri_to_id (), '%.100s' is not valid bnode IRI", name );
          goto return_error; /* see below */
        }
      if (b_first) acc += MIN_64BIT_BNODE_IRI_ID;
      if ((acc > (2 * min_bnode_iri_id())) || (acc < min_bnode_iri_id()))
        {
          if ((bnode_iri_ids_are_huge) || (acc < 0))
            err_ret[0] = srv_make_new_error ("RDFXX", ".....",
              "Bad argument to iri_to_id (), '%.100s' is not valid bnode IRI", name );
          else
            err_ret[0] = srv_make_new_error ("RDFXX", ".....",
              "Bad argument to iri_to_id (), '%.100s' is not valid bnode IRI for 32-bit RDF storage", name );
          goto return_error; /* see below */
        }
      if (NULL != box_to_delete)
        dk_free_box (box_to_delete);
      res_ret[0] = box_iri_int64 (acc, DV_IRI_ID);
      return 1;
    }
  if (uriqa_dynamic_local)
    {
      int ofs = uriqa_iri_is_local (qi, name);
      if (0 != ofs)
        {
          int name_box_len = box_length (name);
/*  0123456 */
/* "local:" */
          caddr_t localized_name = dk_alloc_box (6 + name_box_len - ofs, DV_STRING);
          memcpy (localized_name, "local:", 6);
          memcpy (localized_name + 6, name + ofs, name_box_len - ofs);
          if (box_to_delete == name)
            dk_free_box (box_to_delete);
          box_to_delete = name = localized_name;
        }
    }
  res_ret[0] = name;
  if ((NULL != box_to_delete) && (name != box_to_delete))
    dk_free_box (box_to_delete);
  return 2;
return_error:
  if (NULL != box_to_delete)
    dk_free_box (box_to_delete);
  return 0;
}

caddr_t
canon_iri_to_id (query_instance_t *qi, caddr_t canon_name, int mode, caddr_t *err_ret)
{
  switch (cl_run_local_only)
    {
    case CL_RUN_LOCAL:
      switch (mode)
        {
        case IRI_TO_ID_IF_KNOWN:
          return key_name_to_iri_id (qi->qi_trx, canon_name, 0);
        case IRI_TO_ID_WITH_CREATE:
          return key_name_to_iri_id (qi->qi_trx, canon_name, 1);
        case IRI_TO_ID_IF_CACHED:
          return key_name_to_existing_cached_iri_id (qi->qi_trx, canon_name);
        }
      break;
    default: err_ret[0] = srv_make_new_error ("RDFXX", ".....",
              "iri_to_id () refers to partitioned tables and can not be used before cluster is up");
    }
  return NULL;
}

caddr_t
iri_to_id (caddr_t *qst, caddr_t raw_name, int mode, caddr_t *err_ret)
    {
  query_instance_t *qi = (query_instance_t *) qst;
  caddr_t canon_name = NULL;
  caddr_t boxed_iid = NULL;
  int status;
  err_ret[0] = NULL;
  status = iri_canonicalize (qi, raw_name, mode, &canon_name, err_ret);
  if (NULL != err_ret[0])
    goto return_error;
  switch (status)
    {
    case 0:
      break;
    case 1:
#ifndef NDEBUG
      if (DV_IRI_ID != DV_TYPE_OF (canon_name))
	GPF_T1 ("iri_to_id: weird dv_type, should be IRI_ID");
#endif
      return ((canon_name == raw_name) ? box_copy_tree (canon_name) : canon_name);
    case 2:
      boxed_iid = canon_iri_to_id (qi, canon_name, mode, err_ret);
      break;
    }
  dk_check_tree (canon_name);
#ifndef NDEBUG
  if (DV_IRI_ID == DV_TYPE_OF (canon_name))
    GPF_T1 ("iri_to_id: weird dv_type, should not be IRI_ID");
  if ((canon_name == boxed_iid) && IS_BOX_POINTER (canon_name))
    GPF_T1 ("iri_to_id: weird cast");
#endif
  if ((NULL != canon_name) && (canon_name != raw_name))
    dk_free_box (canon_name);
  if (NULL == boxed_iid)
    return NEW_DB_NULL;
#ifndef NDEBUG
  if ((boxed_iid == raw_name) && IS_BOX_POINTER (raw_name))
    GPF_T1 ("iri_to_id: arg not copied and returned");
#endif
  return boxed_iid;
return_error:
  if (canon_name != raw_name)
    dk_free_box (canon_name);
  return NULL;
}

caddr_t
bif_iri_to_id (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
  caddr_t name = bif_arg (qst, args, 0, "iri_to_id");
  int make_new = (BOX_ELEMENTS (args) > 1 ? bif_long_arg (qst, args, 1, "iri_to_id") : 1);
  caddr_t err = NULL;
  caddr_t res = iri_to_id (qst, name, make_new ? IRI_TO_ID_WITH_CREATE : IRI_TO_ID_IF_KNOWN, &err);
  if (NULL != err)
    sqlr_resignal (err);
  if (NULL == res)
    {
      if (BOX_ELEMENTS (args) > 2)
        return box_copy_tree (bif_arg (qst, args, 2, "iri_to_id"));
      return NEW_DB_NULL;
    }
  return res;
}

caddr_t
bif_iri_to_id_repl (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
  caddr_t name = bif_arg (qst, args, 0, "iri_to_id_repl");
  caddr_t err = NULL;
  caddr_t res;
  switch (DV_TYPE_OF (name))
    {
    case DV_LONG_INT: case DV_IRI_ID:
      {
        /* caddr_t tmp_name = NULL; */
        iri_id_t iid = unbox_iri_int64 (name);
        if (iid < min_bnode_iri_id ())
          sqlr_new_error ("22023", "SR626", "The argument of iri_to_id_repl() is an IRI_ID of URI");
#if 0
        if (iid >= MIN_64BIT_BNODE_IRI_ID)
          tmp_name = box_sprintf (40, "_:rr_b" BOXINT_FMT, (boxint)(iid - MIN_64BIT_BNODE_IRI_ID));
        else
          tmp_name = box_sprintf (40, "_:rr" BOXINT_FMT, (boxint)iid);
        res = canon_iri_to_id ((query_instance_t *)qst, tmp_name, IRI_TO_ID_WITH_CREATE, &err);
        dk_free_box (tmp_name);
        if (NULL != err)
          sqlr_resignal (err);
        return res;
#else
        return box_copy (name);
#endif
      }
    case DV_STRING: case DV_UNAME:
      break;
    default:
      sqlr_new_error ("22023", "SR626", "The argument of iri_to_id_repl() is of wrong type");
    }
  res = canon_iri_to_id ((query_instance_t *)qst, name, IRI_TO_ID_WITH_CREATE, &err);
  if (NULL != err)
    sqlr_resignal (err);
  return res;
}

caddr_t
bif_iri_canonicalize (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
  caddr_t raw_name = bif_arg (qst, args, 0, "iri_canonicalize");
  caddr_t canon_name = NULL;
  caddr_t err = NULL;
  err_ret[0] = NULL;
  iri_canonicalize ((query_instance_t *)qst, raw_name, IRI_TO_ID_WITH_CREATE, &canon_name, &err);
  if (NULL != err)
    {
      if ((NULL != canon_name) && (canon_name != raw_name))
        dk_free_box (canon_name);
      sqlr_resignal (err);
    }
  if (NULL == canon_name)
    return NEW_DB_NULL;
  if (DV_IRI_ID == DV_TYPE_OF (canon_name))
    {
      iri_id_t iid = unbox_iri_int64 (canon_name);
      if (min_bnode_iri_id() > iid)
        {
          caddr_t iri = key_id_to_iri ((query_instance_t *)qst, iid);
          if (canon_name != raw_name)
            dk_free_tree (canon_name);
          if (!iri)
            sqlr_new_error ("22023", "SR626", "Can not canonicalize unknown IRI ID " BOXINT_FMT, (boxint)(iid));
          return iri;
        }
    }
  if (canon_name == raw_name)
    return box_copy_tree (canon_name);
  return canon_name;
}

caddr_t
bif_iri_to_id_nosignal (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
  caddr_t name = bif_arg (qst, args, 0, "iri_to_id_nosignal");
  int make_new = (BOX_ELEMENTS (args) > 1 ? bif_long_arg (qst, args, 1, "iri_to_id_nosignal") : 1);
  caddr_t err = NULL;
  caddr_t res = iri_to_id (qst, name, make_new ? IRI_TO_ID_WITH_CREATE : IRI_TO_ID_IF_KNOWN, &err);
  if (NULL != err)
    {
      if (!strcmp (ERR_STATE(err), "RDFXX"))
        return NEW_DB_NULL;
      sqlr_resignal (err);
    }
  if (NULL == res)
    {
      if (BOX_ELEMENTS (args) > 2)
        return box_copy_tree (bif_arg (qst, args, 2, "iri_to_id"));
      return NEW_DB_NULL;
    }
  return res;
}

caddr_t
bif_iri_to_id_if_cached (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
  caddr_t name = bif_arg (qst, args, 0, "iri_to_id_if_cached");
  caddr_t err = NULL;
  caddr_t res = iri_to_id (qst, name, IRI_TO_ID_IF_CACHED, &err);
  if (NULL != err)
    sqlr_resignal (err);
  if (NULL == res)
    {
      if (BOX_ELEMENTS (args) > 1)
        return box_copy_tree (bif_arg (qst, args, 1, "iri_to_id_if_cached"));
      return NEW_DB_NULL;
    }
  return res;
}


caddr_t
tb_id_to_name (lock_trx_t * lt, char * tb_name, caddr_t id)
{
  int res;
  caddr_t iri = NULL;
  dbe_table_t * tb = sch_name_to_table (wi_inst.wi_schema, tb_name);
  dbe_key_t * key = tb ? tb->tb_primary_key : NULL;
  dbe_column_t * iri_col = key && key->key_parts && key->key_parts->next ? key->key_parts->next->data : NULL;
  it_cursor_t itc_auto;
  it_cursor_t * itc = &itc_auto;
  buffer_desc_t * buf;
  dbe_column_t *str_col, *id_col;
  if (!iri_col)
    return NULL;
  if (!tb_string_and_id_check (tb, &str_col, &id_col))
    return NULL;
  key = (dbe_key_t *)(tb->tb_keys->data == tb->tb_primary_key ? tb->tb_keys->next->data : tb->tb_keys->data);
  ITC_INIT (itc, key->key_fragments[0]->kf_it, NULL);
  itc->itc_ltrx = lt;
  itc_from (itc, key);
  ITC_SEARCH_PARAM (itc, id);
  itc->itc_isolation =ISO_COMMITTED;
  itc->itc_key_spec = key->key_insert_spec;
  ITC_FAIL (itc)
    {
      buf = itc_reset (itc);
      res = itc_search (itc, &buf);
      if (DVC_MATCH == res)
	{
	  iri = itc_box_column (itc, buf, str_col->col_id, NULL);
	}
      else
	iri = NULL;
      itc_page_leave (itc, buf);
    }
  ITC_FAILED
      {
	itc_free (itc);
	if (!lt)
	  return NULL;
      }
  END_FAIL (itc);
  itc_free (itc);
  return iri;
}

caddr_t
uriqa_dynamic_local_replace_nocheck (caddr_t name, client_connection_t * cli)
    {
      caddr_t host;
      int is_https = 0;
#ifndef NDEBUG
  if (strncmp (name, "local:", 6))
    GPF_T1 ("uriqa_dynamic_local_replace_nocheck(): should check but misused");
#endif
      host = uriqa_get_host_for_dynamic_local (cli, &is_https);
      if (NULL != host)
        {
          int name_box_len = box_length (name);
          int host_strlen = strlen (host);
          caddr_t expanded_name = dk_alloc_box (name_box_len - 6 + (7 + is_https + host_strlen), DV_STRING);
      if (!is_https)         /* 01234567 */
	    memcpy (expanded_name, "http://", 7);
      else                   /* 012345678 */
	    memcpy (expanded_name, "https://", 8);
          memcpy (expanded_name + 7 + is_https, host, host_strlen);
          memcpy (expanded_name + 7 + is_https + host_strlen, name + 6, name_box_len - 6);
          dk_free_box (name);
          name = expanded_name;
	  dk_free_box (host);
        }
  return name;
}

caddr_t
key_id_to_canonicalized_iri (query_instance_t * qi, iri_id_t iri_id_no)
{
  boxint pref_id;
  lock_trx_t * lt = qi->qi_trx;
  caddr_t local, prefix, name;
  local = lt_nic_id_name (lt, iri_name_cache, iri_id_no);
  if (!local)
    {
      caddr_t id_box;
      id_box = box_iri_id (iri_id_no);
      if (!cl_run_local_only)
	{
	  caddr_t ret = cl_id_to_iri (qi, id_box);
	  dk_free_box (id_box);
	  return ret;
	}

      local = tb_id_to_name (qi->qi_trx, "DB.DBA.RDF_IRI", id_box);
      dk_free_box (id_box);
      if (!local)
	return NULL;
      if (lt->lt_lock.ht_count)
	lt_nic_set (lt, iri_name_cache, local, iri_id_no);
      else
	nic_set (iri_name_cache, local, iri_id_no);
    }
  pref_id = LONG_REF_NA (local);
  prefix = nic_id_name (iri_prefix_cache, pref_id);
  if (!prefix)
    {
      if (!cl_run_local_only)
	{
	  caddr_t id_box = box_iri_id (iri_id_no);
	  caddr_t ret = cl_id_to_iri (qi, id_box);
	  dk_free_box (id_box);
	  dk_free_box (local);
	  return ret;
	}
      else
	{
	  caddr_t pref_id_box = box_num (pref_id);
	  prefix = tb_id_to_name (qi->qi_trx, "DB.DBA.RDF_PREFIX", pref_id_box);
	  dk_free_box (pref_id_box);
	  if (!prefix)
	    {
	      dk_free_box (local);
	      return NULL;
	    }
	  if (lt->lt_lock.ht_count)
	    lt_nic_set (lt, iri_prefix_cache, prefix, pref_id);
	  else
	    nic_set (iri_prefix_cache, prefix, pref_id);
	}
    }
  name = dk_alloc_box (box_length (local) + box_length (prefix) - 5, DV_STRING);
  /* subtract 4 for the prefix id in the local and 1 for one of the terminating nulls */
  memcpy (name, prefix, box_length (prefix) - 1);
  memcpy (name + box_length (prefix) - 1, local + 4, box_length (local) - 4);
  dk_free_box (prefix);
  dk_free_box (local);
  return name;
}


caddr_t
key_id_to_iri (query_instance_t * qi, iri_id_t iri_id_no)
        {
  caddr_t name = key_id_to_canonicalized_iri (qi, iri_id_no);
  caddr_t name_with_local_extended;
  dk_check_tree (name);
  if (NULL == name)
  return name;
  name_with_local_extended = uriqa_dynamic_local_replace (name, qi->qi_client);
  dk_check_tree (name_with_local_extended);
  return name_with_local_extended;
}

int
key_id_to_namespace_and_local (query_instance_t *qi, iri_id_t iid, caddr_t *subj_ns_ret, caddr_t *subj_loc_ret)
{
  boxint pref_id;
  caddr_t local, prefix;
  lock_trx_t * lt = qi->qi_trx;
  local = lt_nic_id_name (lt, iri_name_cache, iid);
  if (!local)
    {
      caddr_t id_box = box_iri_id (iid);
      if (!cl_run_local_only)
	{
	  caddr_t ret = cl_id_to_iri (qi, id_box);
	  iri_split_ttl_qname (ret, subj_ns_ret, subj_loc_ret, 1);
	  dk_free_box (ret);
	  dk_free_box (id_box);
	  return (NULL != ret);
	}

      local = tb_id_to_name (qi->qi_trx, "DB.DBA.RDF_IRI", id_box);
      dk_free_box (id_box);
      if (!local)
	return 0;
      lt_nic_set (lt, iri_name_cache, local, iid);
    }
  pref_id = LONG_REF_NA (local);
  prefix = lt_nic_id_name (lt, iri_prefix_cache, pref_id);
  if (!prefix)
    {
      if (!cl_run_local_only)
	{
	  caddr_t id_box = box_iri_id (iid);
	  caddr_t ret = cl_id_to_iri (qi, id_box);
	  iri_split_ttl_qname (ret, subj_ns_ret, subj_loc_ret, 1);
	  dk_free_box (ret);
	  dk_free_box (id_box);
	  return (NULL != ret);
	}
      else
	{
	  caddr_t pref_id_box = box_num (pref_id);
	  prefix = tb_id_to_name (qi->qi_trx, "DB.DBA.RDF_PREFIX", pref_id_box);
	  dk_free_box (pref_id_box);
	  if (!prefix)
        {
          dk_free_box (local);
	  return 0;
        }
	  lt_nic_set (lt, iri_prefix_cache, prefix, pref_id);
	}
    }
  /* may be replace following with uriqa_dynamic_local_replace */
/*                       0123456 */
  if (!strncmp (prefix, "local:", 6))
    {
      caddr_t host;
      int is_https = 0;
      host = uriqa_get_host_for_dynamic_local (qi->qi_client, &is_https);
      if (NULL != host)
        {
/*                                         012345678    01234567 */
          const char *proto = (is_https ? "https://" : "http://");
          int proto_strlen = (is_https ? 8 : 7);
          int prefix_box_len = box_length (prefix);
          int host_strlen = strlen (host);
          caddr_t expanded_prefix = dk_alloc_box (prefix_box_len - 6 + (proto_strlen + host_strlen), DV_STRING);
          memcpy (expanded_prefix, proto, proto_strlen);
          memcpy (expanded_prefix + proto_strlen, host, host_strlen);
          memcpy (expanded_prefix + proto_strlen + host_strlen, prefix + 6, prefix_box_len - 6);
          dk_free_box (prefix);
          prefix = expanded_prefix;
	  dk_free_box (host);
        }
    }
  subj_ns_ret[0] = prefix;
  subj_loc_ret[0] = box_dv_short_nchars (local+4, box_length (local) - 5);
  dk_free_tree (local);
  return 1;
}

caddr_t
bif_id_to_iri (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
  query_instance_t * qi = (query_instance_t *) qst;
  iri_id_t iid = bif_iri_id_or_null_arg (qst, args, 0, "id_to_iri");
  caddr_t iri;
  if (0L == iid)
    return NEW_DB_NULL;
  if ((min_bnode_iri_id () <= iid) && (min_named_bnode_iri_id () > iid))
    iri = BNODE_IID_TO_LABEL(iid);
  else
    {
      iri = key_id_to_iri (qi, iid);
      if (!iri)
        return NEW_DB_NULL;
    }
  box_flags (iri) = BF_IRI;
  return iri;
}

caddr_t
bif_id_to_iri_nosignal (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
  query_instance_t * qi = (query_instance_t *) qst;
  caddr_t iid_box;
  iri_id_t iid;
  caddr_t iri;
  iid_box = bif_arg (qst, args, 0, "id_to_iri_nosignal");
  if (DV_IRI_ID != DV_TYPE_OF (iid_box))
    return NEW_DB_NULL;
  iid = unbox_iri_id (iid_box);
  if (min_bnode_iri_id () <= iid)
    iri = BNODE_IID_TO_LABEL(iid);
  else
    {
      iri = key_id_to_iri (qi, iid);
      if (!iri)
        return NEW_DB_NULL;
    }
  box_flags (iri) = BF_IRI;
  return iri;
}

caddr_t
bif_iri_id_cache_flush (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
  query_instance_t * qi = (query_instance_t *) qst;
  lock_trx_t *lt = qi->qi_trx;
  caddr_t log_array;
  int rc;
  if (!srv_have_global_lock (THREAD_CURRENT_THREAD))
    srv_make_new_error ("42000", "SR535", "iri_id_cache_flush() can be used only inside atomic section");
  nic_flush (iri_name_cache);
  nic_flush (iri_prefix_cache);
  log_array = list (1, box_string ("iri_id_cache_flush()"));
  mutex_enter (log_write_mtx);
  rc = log_text_array_sync (lt, log_array);
  mutex_leave (log_write_mtx);
  dk_free_tree (log_array);
  if (rc != LTE_OK)
    {
static caddr_t details = NULL;
      if (NULL == details)
        details = box_dv_short_string ("while writing new IRI_ID allocation to log file");
/*      if (lt->lt_client != bootstrap_cli) */
      sqlr_resignal (srv_make_trx_error (rc, details));
    }
  return NULL;
}

caddr_t
bif_iri_to_rdf_prefix_and_local (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
  caddr_t name = bif_string_or_uname_arg (qst, args, 0, "iri_to_rdf_prefix_and_local");
  caddr_t prefix, local;
  int res = iri_split (name, &prefix, &local);
  if (res)
    return list (2, prefix, local);
  return NEW_DB_NULL;
}


caddr_t
bif_rdf_cache_id (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
  QNCAST (query_instance_t, qi, qst);
  caddr_t mode = bif_string_arg (qst, args, 0, "rdf_cache_id");
  caddr_t pref = bif_string_or_uname_arg (qst, args, 1, "rdf_cache_id");
  name_id_cache_t * cache = mode[0] == 'p' ? iri_prefix_cache
    : mode[0] == 'l' ? rdf_lang_cache
    : mode[0] == 't' ? rdf_type_cache : NULL;
  if (!cache)
    sqlr_new_error ("42000", "RDF..", "bad mode for rdf_cache_id");
  if (BOX_ELEMENTS (args) > 2)
    {
      boxint new_id = bif_long_arg (qst, args, 2, "rdf_cache_id");
      if (cache == iri_name_cache || cache == iri_prefix_cache)
	lt_nic_set (qi->qi_trx, cache, pref, new_id);
      else
	nic_set (cache, pref, new_id);
      return 0;
    }
  if (cache == iri_name_cache || cache == iri_prefix_cache)
    return box_num (lt_nic_name_id (qi->qi_trx, cache, pref));
  else
    return box_num (nic_name_id (cache, pref));
}


caddr_t
bif_rdf_cache_id_to_name (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
  QNCAST (query_instance_t, qi, qst);
  caddr_t mode = bif_string_arg (qst, args, 0, "rdf_cache_id_to_name");
  boxint id = bif_long_arg (qst, args, 1, "rdf_cache_id_to_name");
  name_id_cache_t * cache = mode[0] == 'p' ? iri_prefix_cache
    : mode[0] == 'l' ? rdf_lang_cache
    : mode[0] == 't' ? rdf_type_cache : NULL;
  if (!cache)
    sqlr_new_error ("42000", "RDF..", "bad mode for rdf_cache_id_to_name");
  if (cache == iri_name_cache || cache == iri_prefix_cache)
    return lt_nic_id_name (qi->qi_trx, cache, id);
  else
    return nic_id_name (cache, id);
}


#undef tf_bnode_iid
caddr_t DBG_NAME (tf_bnode_iid) (DBG_PARAMS triple_feed_t *tf, caddr_t txt)
{
  char params_buf [BOX_AUTO_OVERHEAD + sizeof (caddr_t) * 3];
  void **params;
  caddr_t res, *hit, err;
  query_t *cbk_qr = tf->tf_cbk_qrs[TRIPLE_FEED_NEW_BLANK];
  if (NULL == cbk_qr)
    {
      dk_free_box (txt);
      return box_iri_id (min_bnode_iri_id());
    }
  if (NULL != txt)
    {
      hit = (caddr_t *)id_hash_get (tf->tf_blank_node_ids, (caddr_t)(&(txt)));
      if (NULL != hit)
        {
          dk_free_box (txt);
          return box_copy_tree (hit[0]);
        }
    }
  BOX_AUTO_TYPED (void **, params, params_buf, sizeof (caddr_t) * 3, DV_ARRAY_OF_POINTER);
  res = NULL;
  params[0] = &(tf->tf_current_graph_uri);
  params[1] = &(tf->tf_app_env);
  params[2] = &res;
  err = qr_exec (tf->tf_qi->qi_client, cbk_qr, tf->tf_qi, NULL, NULL, NULL, (caddr_t *)params, NULL, 0);
  BOX_DONE (params, params_buf);
  if (NULL != err)
    {
      dk_free_box (txt);
      sqlr_resignal (err);
    }
  if (NULL == txt)
    return res;
  id_hash_set (tf->tf_blank_node_ids, (caddr_t)(&txt), (caddr_t)(&res));
  return DBG_NAME (box_copy_tree) (DBG_ARGS res);
}

#undef tf_formula_bnode_iid
caddr_t DBG_NAME (tf_formula_bnode_iid) (DBG_PARAMS ttlp_t *ttlp_arg, caddr_t txt)
{
  caddr_t btext = box_sprintf (10+strlen (txt), "%ld%s", (long)(unbox_iri_id(ttlp_arg[0].ttlp_formula_iid)), txt);
  caddr_t res;
  dk_free_box (txt);
  /*dk_set_push (&(ttlp_arg[0].ttlp_saved_uris), btext);*/
  res = DBG_NAME (tf_bnode_iid) (DBG_ARGS ttlp_arg[0].ttlp_tf, btext);
  /*dk_set_pop (&(ttlp_arg[0].ttlp_saved_uris));*/
  return res;
}


char * range_replay =
"create procedure ID_RANGE_REPLAY (in iri_seq varchar, in iri_seq_max varchar, in id int, in max_id int)\n"
"{\n"
"  sequence_set (iri_seq, id, 1);\n"
"  sequence_set (iri_seq_max, max_id, 1);\n"
"	sequence_set ('RDF_URL_IID_NAMED', max_id, 1);\n"
"}";


char * iri_replay =
"create procedure  DB.DBA.ID_REPLAY (in tb varchar, in seq varchar, in name varchar, in id an)\n"
"{\n"
"  if (isiri_id (id))\n"
"    id := iri_id_num (id);\n"
"  sequence_set (seq, id + 1, 1);\n"
"  if (tb = 'DB.DBA.RDF_PREFIX')\n"
"    insert replacing DB.DBA.RDF_PREFIX (RP_ID, RP_NAME) values (id, name);\n"
"  else if (tb = 'DB.DBA.RDF_IRI')\n"
"    insert replacing DB.DBA.RDF_IRI (RI_ID, RI_NAME) values (iri_id_from_num (id), name);\n"
"  else \n"
"    signal ('RDFXX', 'Unknown table in ID_REEPLAY ');\n"
  "}\n";

char * rdf_prefix_text = "create table DB.DBA.RDF_PREFIX (RP_NAME varchar not null primary key, RP_ID bigint not null unique)"
"alter index RDF_PREFIX on RDF_PREFIX partition (RP_NAME varchar (-10, 0hexffff))"
"alter index DB_DBA_RDF_PREFIX_UNQC_RP_ID on DB.DBA.RDF_PREFIX partition (RP_ID int (0hexffff00))";


char * rdf_iri_text = "create table DB.DBA.RDF_IRI (RI_NAME varchar not null primary key, RI_ID IRI_ID_8 not null unique)"
"alter index RDF_IRI on RDF_IRI partition (RI_NAME varchar (-10, 0hexffff))"
"alter index DB_DBA_RDF_IRI_UNQC_RI_ID on DB.DBA.RDF_IRI partition (RI_ID int (0hexffff00))";



/* Free text on DB.DBA.RDF_QUAD */

dk_mutex_t *rdf_obj_ft_rules_mtx = NULL;
id_hash_t *rdf_obj_ft_rules_by_iids = NULL;
id_hash_t *rdf_obj_ft_rules_by_iris = NULL;
id_hash_t *rdf_obj_ft_graph_rule_counts = NULL;
ptrlong rdf_obj_ft_predonly_rule_count = 0;

typedef struct rdf_obj_ft_rule_iid_hkey_s
{
   iri_id_t hkey_g;
   iri_id_t hkey_iid_p;
} rdf_obj_ft_rule_iid_hkey_t;

typedef struct rdf_obj_ft_rule_iri_hkey_s
{
   iri_id_t hkey_g;
   caddr_t hkey_iri_p;
} rdf_obj_ft_rule_iri_hkey_t;

id_hashed_key_t
rdf_obj_ft_rule_iid_hkey_hash (caddr_t p_data)
{
  rdf_obj_ft_rule_iid_hkey_t *ht = (rdf_obj_ft_rule_iid_hkey_t *)p_data;
  return ((id_hashed_key_t)(ht->hkey_g * 65539 + ht->hkey_iid_p) & ID_HASHED_KEY_MASK);
}

int
rdf_obj_ft_rule_iid_hkey_cmp (caddr_t d1, caddr_t d2)
{
  rdf_obj_ft_rule_iid_hkey_t *ht1 = (rdf_obj_ft_rule_iid_hkey_t *)d1;
  rdf_obj_ft_rule_iid_hkey_t *ht2 = (rdf_obj_ft_rule_iid_hkey_t *)d2;
  return ((ht1->hkey_g == ht2->hkey_g) && (ht1->hkey_iid_p == ht2->hkey_iid_p));
}

id_hashed_key_t
rdf_obj_ft_rule_iri_hkey_hash (caddr_t p_data)
{
  rdf_obj_ft_rule_iri_hkey_t *ht = (rdf_obj_ft_rule_iri_hkey_t *)p_data;
  caddr_t p_box = ht->hkey_iri_p;
  id_hashed_key_t hash = (ht->hkey_g & ID_HASHED_KEY_MASK);
  if (p_box)
    {
      BYTE_BUFFER_HASH (hash, p_box, box_length_inline (p_box) - 1);
    }
  return hash & ID_HASHED_KEY_MASK;
}

int rdf_obj_ft_rule_iri_hkey_cmp (caddr_t d1, caddr_t d2)
{
  rdf_obj_ft_rule_iri_hkey_t *ht1 = (rdf_obj_ft_rule_iri_hkey_t *)d1;
  rdf_obj_ft_rule_iri_hkey_t *ht2 = (rdf_obj_ft_rule_iri_hkey_t *)d2;
  return ((ht1->hkey_g == ht2->hkey_g) && !strcmp (ht1->hkey_iri_p, ht2->hkey_iri_p));
}

static ptrlong *
rdf_obj_ft_get_rule_count_ptr (iri_id_t g_id)
{
  boxint g_id_int = g_id;
  ptrlong *res;
  if (0 == g_id)
    return &rdf_obj_ft_predonly_rule_count;
  res = (ptrlong *)id_hash_get (rdf_obj_ft_graph_rule_counts, (caddr_t)(&g_id_int));
  if (NULL == res)
    {
      ptrlong ctr = 0;
      res = (ptrlong *) id_hash_add_new (rdf_obj_ft_graph_rule_counts, (caddr_t)(&g_id_int), (caddr_t)(&ctr));
    }
  return res;
}

caddr_t
bif_rdf_obj_ft_rule_add (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
  iri_id_t g_id = bif_iri_id_or_null_arg (qst, args, 0, "__rdf_obj_ft_rule_add");
  iri_id_t p_id = bif_iri_id_or_null_arg (qst, args, 1, "__rdf_obj_ft_rule_add");
  caddr_t reason = bif_string_arg (qst, args, 2, "__rdf_obj_ft_rule_add");
  caddr_t p_iri = NULL;
  dk_set_t *known_reasons_ptr;
  rdf_obj_ft_rule_iid_hkey_t iid_hkey;
  rdf_obj_ft_rule_iri_hkey_t iri_hkey;
  iid_hkey.hkey_g = g_id;
  iid_hkey.hkey_iid_p = p_id;
  if (CL_RUN_LOCAL != cl_run_local_only)
    {
      iri_hkey.hkey_g = g_id;
      iri_hkey.hkey_iri_p = p_iri = ((0 == p_id) ? NULL
				     : cl_id_to_iri ((query_instance_t*)qst, bif_arg_nochecks (qst, args, 1)));
    }
  mutex_enter (rdf_obj_ft_rules_mtx);
  known_reasons_ptr = (dk_set_t *)id_hash_get (rdf_obj_ft_rules_by_iids, (caddr_t)(&iid_hkey));
  if (NULL == known_reasons_ptr)
    {
      ptrlong *rule_count_ptr = rdf_obj_ft_get_rule_count_ptr (g_id);
      dk_set_t new_reasons = NULL;
      dk_set_push (&new_reasons, box_copy (reason));
      id_hash_add_new (rdf_obj_ft_rules_by_iids, (caddr_t)(&iid_hkey), (caddr_t)(&new_reasons));
      if (CL_RUN_LOCAL != cl_run_local_only)
        id_hash_add_new (rdf_obj_ft_rules_by_iris, (caddr_t)(&iri_hkey), (caddr_t)(&new_reasons));
      rule_count_ptr[0]++;
      mutex_leave (rdf_obj_ft_rules_mtx);
      return box_num (1);
    }
  if (0 > dk_set_position_of_string (known_reasons_ptr[0], reason))
    {
      dk_set_push (known_reasons_ptr, box_copy (reason));
      if (CL_RUN_LOCAL != cl_run_local_only)
        {
          dk_set_t *second_ptr = (dk_set_t *)id_hash_get (rdf_obj_ft_rules_by_iris, (caddr_t)(&iri_hkey));
          second_ptr[0] = known_reasons_ptr[0];
          dk_free_box (p_iri);
        }
      mutex_leave (rdf_obj_ft_rules_mtx);
      return box_num (1);
    }
  mutex_leave (rdf_obj_ft_rules_mtx);
  if (CL_RUN_LOCAL != cl_run_local_only)
    dk_free_box (p_iri);
  return box_num (0);
}

caddr_t
bif_rdf_obj_ft_rule_del (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
  iri_id_t g_id = bif_iri_id_or_null_arg (qst, args, 0, "__rdf_obj_ft_rule_del");
  iri_id_t p_id = bif_iri_id_or_null_arg (qst, args, 1, "__rdf_obj_ft_rule_del");
  caddr_t reason = bif_string_arg (qst, args, 2, "__rdf_obj_ft_rule_del");
  dk_set_t *known_reasons_ptr;
  int reason_pos;
  caddr_t p_iri = NULL;
  rdf_obj_ft_rule_iid_hkey_t iid_hkey;
  rdf_obj_ft_rule_iri_hkey_t iri_hkey;
  iid_hkey.hkey_g = g_id;
  iid_hkey.hkey_iid_p = p_id;
  mutex_enter (rdf_obj_ft_rules_mtx);
  known_reasons_ptr = (dk_set_t *)id_hash_get (rdf_obj_ft_rules_by_iids, (caddr_t)(&iid_hkey));
  if (NULL == known_reasons_ptr)
    {
      mutex_leave (rdf_obj_ft_rules_mtx);
      return box_num (0);
    }
  reason_pos = dk_set_position_of_string (known_reasons_ptr[0], reason);
  if (0 > reason_pos)
    {
      mutex_leave (rdf_obj_ft_rules_mtx);
      return box_num (0);
    }
  dk_free_box (dk_set_delete_nth (known_reasons_ptr, reason_pos));
  if (CL_RUN_LOCAL != cl_run_local_only)
    {
      iri_hkey.hkey_g = g_id;
      iri_hkey.hkey_iri_p = p_iri = ((0 == p_id) ? NULL
				     : cl_id_to_iri ((query_instance_t *)qst, bif_arg_nochecks (qst, args, 1)));
    }
  if (NULL == known_reasons_ptr[0])
    {
      ptrlong *rule_count_ptr = rdf_obj_ft_get_rule_count_ptr (g_id);
      id_hash_remove (rdf_obj_ft_rules_by_iids, (caddr_t)(&iid_hkey));
      if (CL_RUN_LOCAL != cl_run_local_only)
        {
          rdf_obj_ft_rule_iri_hkey_t *old_iri_hkey = (rdf_obj_ft_rule_iri_hkey_t *)id_hash_get_key (rdf_obj_ft_rules_by_iris, (caddr_t)(&iri_hkey));
          caddr_t old_iri_p = old_iri_hkey->hkey_iri_p;
          id_hash_remove (rdf_obj_ft_rules_by_iris, (caddr_t)(&iri_hkey));
          dk_free_box (old_iri_p);
          dk_free_box (p_iri);
        }
      rule_count_ptr[0]--;
    }
  else
    {
      if (CL_RUN_LOCAL != cl_run_local_only)
        {
          dk_set_t *second_ptr = (dk_set_t *)id_hash_get (rdf_obj_ft_rules_by_iris, (caddr_t)(&iri_hkey));
          second_ptr[0] = known_reasons_ptr[0];
          dk_free_box (p_iri);
        }
    }
  mutex_leave (rdf_obj_ft_rules_mtx);
  return box_num (1);
}

caddr_t
bif_rdf_obj_ft_rule_zap_all (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
  int bucket_ctr = 0;
  mutex_enter (rdf_obj_ft_rules_mtx);
  for (bucket_ctr = rdf_obj_ft_rules_by_iids->ht_buckets; bucket_ctr--; /* no step */)
    {
      rdf_obj_ft_rule_iid_hkey_t iid_key;
      dk_set_t reasons;
      while (id_hash_remove_rnd (rdf_obj_ft_rules_by_iids, bucket_ctr, (caddr_t)(&iid_key), (caddr_t)(&reasons)))
        {
          while (NULL != reasons)
            {
              caddr_t reason = dk_set_pop (&reasons);
              dk_free_box (reason);
            }
        }
    }
  for (bucket_ctr = rdf_obj_ft_rules_by_iris->ht_buckets; bucket_ctr--; /* no step */)
    {
      rdf_obj_ft_rule_iri_hkey_t iri_key;
      dk_set_t reasons;
      while (id_hash_remove_rnd (rdf_obj_ft_rules_by_iris, bucket_ctr, (caddr_t)(&iri_key), (caddr_t)(&reasons)))
        {
          dk_free_box (iri_key.hkey_iri_p);
        }
    }
  mutex_leave (rdf_obj_ft_rules_mtx);
  return 0;
}

caddr_t
bif_rdf_obj_ft_rule_check (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
  iri_id_t g_id;
  caddr_t p;
  rdf_obj_ft_rule_iid_hkey_t iid_hkey;
  dtp_t p_dtp;
  if (CL_RUN_LOCAL != cl_run_local_only)
    {
      caddr_t cl_text_set = registry_get ("cl_rdf_text_index");
      char flag = cl_text_set ? cl_text_set[0] : '\0';
      dk_free_tree (cl_text_set);
      if ('1' == flag)
        return box_num (2);
    }
  g_id = bif_iri_id_arg (qst, args, 0, "__rdf_obj_ft_rule_check");
  p = bif_arg (qst, args, 1, "__rdf_obj_ft_rule_check");
  iid_hkey.hkey_g = 0;
  iid_hkey.hkey_iid_p = 0;
  p_dtp = DV_TYPE_OF (p);
  switch (p_dtp)
    {
      case DV_IRI_ID: case DV_STRING: case DV_UNAME:
	  break;
      case DV_DB_NULL:
	  p = 0;
	  break;
      default:
	  sqlr_new_error ("22023", "SR008",
		    "Function __rdf_obj_ft_rule_check needs a string or UNAME or IRI_ID as argument 2, "
		    "not an arg of type %s (%d)", dv_type_title (p_dtp), p_dtp);
    }

  mutex_enter (rdf_obj_ft_rules_mtx);
  if (NULL != id_hash_get (rdf_obj_ft_rules_by_iids, (caddr_t)(&iid_hkey)))
    goto hit; /* see_below */
  if (DV_IRI_ID == p_dtp || DV_DB_NULL == p_dtp)
    {
      iri_id_t p_id = ((DV_IRI_ID == p_dtp) ? unbox_iri_id (p) : (iri_id_t)0);
      iid_hkey.hkey_g = g_id;
      iid_hkey.hkey_iid_p = 0;
      if (NULL != id_hash_get (rdf_obj_ft_rules_by_iids, (caddr_t)(&iid_hkey)))
        goto hit; /* see_below */
      iid_hkey.hkey_g = 0;
      iid_hkey.hkey_iid_p = p_id;
      if (NULL != id_hash_get (rdf_obj_ft_rules_by_iids, (caddr_t)(&iid_hkey)))
        goto hit; /* see_below */
      iid_hkey.hkey_g = g_id;
      iid_hkey.hkey_iid_p = p_id;
      if (NULL != id_hash_get (rdf_obj_ft_rules_by_iids, (caddr_t)(&iid_hkey)))
        goto hit; /* see_below */
      mutex_leave (rdf_obj_ft_rules_mtx);
      return box_num (0);
   }
  else
    {
      rdf_obj_ft_rule_iri_hkey_t iri_hkey;
      iri_hkey.hkey_g = g_id;
      iri_hkey.hkey_iri_p = 0;
      if (NULL != id_hash_get (rdf_obj_ft_rules_by_iris, (caddr_t)(&iri_hkey)))
        goto hit; /* see_below */
      iri_hkey.hkey_g = 0;
      iri_hkey.hkey_iri_p = p;
      if (NULL != id_hash_get (rdf_obj_ft_rules_by_iris, (caddr_t)(&iri_hkey)))
        goto hit; /* see_below */
      iri_hkey.hkey_g = g_id;
      iri_hkey.hkey_iri_p = p;
      if (NULL != id_hash_get (rdf_obj_ft_rules_by_iris, (caddr_t)(&iri_hkey)))
        goto hit; /* see_below */
      mutex_leave (rdf_obj_ft_rules_mtx);
      return box_num (0);
   }
hit:
  mutex_leave (rdf_obj_ft_rules_mtx);
  return box_num (1);
}

caddr_t
bif_rdf_obj_ft_rule_count_in_graph (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
  ptrlong res = rdf_obj_ft_predonly_rule_count;
  ptrlong *graph_specific;
  boxint g_id_int;
  if (CL_RUN_LOCAL != cl_run_local_only)
    {
      caddr_t cl_text_set = registry_get ("cl_rdf_text_index");
      char flag = cl_text_set ? cl_text_set[0] : '\0';
      dk_free_tree (cl_text_set);
      if ('1' == flag)
        return box_num (-1);
    }
  g_id_int = bif_iri_id_arg (qst, args, 0, "__rdf_obj_ft_rule_count_in_graph");
  mutex_enter (rdf_obj_ft_rules_mtx);
  graph_specific = (ptrlong *)id_hash_get (rdf_obj_ft_graph_rule_counts, (caddr_t)(&g_id_int));
  if (NULL != graph_specific)
    res += graph_specific[0];
  mutex_leave (rdf_obj_ft_rules_mtx);
  return box_num (res);
}

void
rdf_graph_keyword (iri_id_t id, char *ret)
{
  /* number in hex.  Low 6 digits first, then high digits */
  int brk;
  char ch[2];
  char str[30];
  sprintf (str, "%llx", id);
  if ((brk = strlen (str)) <= 6)
    {
      sprintf (ret, "g%s", str);
      return;
    }
  brk -= 6;
  ch[0] = str[brk];
  ch[1] = 0;
  str[brk] = 0;
  sprintf (ret, "g%s%s%s", ch, &str[brk+1], str);
}

caddr_t
bif_rdf_graph_keyword (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
  iri_id_t id = bif_iri_id_arg (qst, args, 0, "rdf_graph_keyword");
  char ret[30];
  rdf_graph_keyword (id, ret);
  return box_dv_short_string (ret);
}

caddr_t
bif_iri_split (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
  int n_args = BOX_ELEMENTS (args);
  int is_int = n_args > 2 ? bif_long_arg (qst, args, 2, "iri_split") : 0;
  caddr_t iri = bif_string_arg (qst, args, 0, "iri_split");
  caddr_t local, pref;
  if (is_int)
    iri_split (iri, &pref, &local);
  else
    iri_split_ttl_qname (iri, &pref, &local, 0);
  if (n_args > 1 && ssl_is_settable (args[1]))
    qst_set (qst, args[1], local);
  else
    dk_free_box (local);
  return pref;
}


caddr_t
bif_iri_ensure (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
  caddr_t name = bif_string_arg (qst, args, 0, "iri_ensure");
  return iri_ensure (qst, name, 0, err_ret);
}

caddr_t
bif_uriqa_dynamic_local_replace (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
  caddr_t name = box_copy (bif_string_arg (qst, args, 0, "uriqa_dynamic_local_replace"));
  name = uriqa_dynamic_local_replace (name, ((query_instance_t *) qst)->qi_client);
  return name;
}


void rdf_inf_init ();

int iri_cache_size = 0;


dbe_key_t *
sch_find_key (dbe_schema_t * sc, char * tb_name, char * key_name)
{
  dbe_table_t *tb = sch_name_to_table (sc ? sc : wi_inst.wi_schema, tb_name);
  if (!tb)
    return NULL;
  DO_SET (dbe_key_t *, key, &tb->tb_keys)
    {
      if (0 == strcmp (key->key_name, key_name))
	return key;
    }
  END_DO_SET();
  return NULL;
}


dbe_col_loc_t *
key_cl_by_name (dbe_key_t * key, char * col_name)
{
  static dbe_col_loc_t dummy;
  dbe_col_loc_t * cl;
  dbe_column_t * col = tb_name_to_column (key->key_table, col_name);
  if (!col)
    return &dummy;
  cl =  key_find_cl (key, col->col_id);
  return cl ? cl : &dummy;
}


int sql_comp_iri_look_ready = 0;


int
sql_ensure_proc (char * name)
{
  query_t * qr = sch_proc_def (wi_inst.wi_schema, name);
  if (!qr)
    return 0;
  if (qr->qr_to_recompile)
    {
      caddr_t err = NULL;
      qr = qr_recompile (qr, &err);
      if (err)
	{
	  return 0;
	}
    }
  return 1;
}


void
rdf_key_comp_init ()
{
  /* set the key comp options for the rdf keys */
  dbe_key_t * gspo = sch_find_key (NULL, "DB.DBA.RDF_QUAD", "RDF_QUAD");
  dbe_key_t * ogps = sch_find_key (NULL, "DB.DBA.RDF_QUAD", "RDF_QUAD_OGPS");
  sql_comp_iri_look_ready = sql_ensure_proc ("DB.DBA.II_I_LOOK");
  sql_comp_iri_look_ready &= sql_ensure_proc ("DB.DBA.II_P_LOOK");
  if (!gspo || !ogps)
    return;
  key_cl_by_name (gspo, "P")->cl_comp_asc = 1;
  key_cl_by_name (gspo, "O")->cl_comp_asc = 1;

  key_cl_by_name (ogps, "P")->cl_comp_asc = 1;
  key_cl_by_name (ogps, "G")->cl_comp_asc = 1;
  key_cl_by_name (ogps, "S")->cl_comp_asc = 1;
}


void
rdf_core_init (void)
{
  jso_init ();
  rdf_mapping_jso_init ();
  iri_nic_rc = resource_allocate (10, NULL, (rc_destr_t)nic_free, (rc_destr_t)nic_clear, 0);
  prefix_nic_rc = resource_allocate (10, NULL, (rc_destr_t)nic_free, (rc_destr_t)nic_clear, 0);
  bif_define_typed ("rdf_load_rdfxml", bif_rdf_load_rdfxml, &bt_xml_entity);
  bif_set_uses_index (bif_rdf_load_rdfxml);
  bif_define_typed ("rdf_load_rdfa", bif_rdf_load_rdfa, &bt_xml_entity);
  bif_set_uses_index (bif_rdf_load_rdfa);
  bif_define_typed ("rdf_load_microdata", bif_rdf_load_microdata, &bt_xml_entity);
  bif_set_uses_index (bif_rdf_load_microdata);
  bif_define ("rdf_load_turtle", bif_rdf_load_turtle);
  bif_set_uses_index (bif_rdf_load_turtle);
  bif_define ("rdf_load_turtle_local_file", bif_rdf_load_turtle_local_file);
  bif_set_uses_index (bif_rdf_load_turtle_local_file);
  bif_define ("turtle_lex_analyze", bif_turtle_lex_analyze);
  bif_define ("iri_to_id", bif_iri_to_id);
  bif_define ("iri_ensure", bif_iri_ensure);
  bif_set_uses_index (bif_iri_to_id);
  bif_set_no_cluster ("iri_to_id");
  bif_define ("iri_to_id_repl", bif_iri_to_id_repl);
  bif_set_uses_index (bif_iri_to_id_repl);
  bif_define ("iri_canonicalize", bif_iri_canonicalize);
  bif_set_uses_index (bif_iri_canonicalize);
  bif_set_no_cluster ("iri_to_id_repl");
  bif_define ("iri_to_id_nosignal", bif_iri_to_id_nosignal);
  bif_set_uses_index (bif_iri_to_id_nosignal);
  bif_set_no_cluster ("iri_to_id_nosignal");
  bif_define ("iri_to_id_if_cached", bif_iri_to_id_if_cached);
  bif_define ("id_to_iri", bif_id_to_iri);
  bif_set_uses_index (bif_id_to_iri);
  bif_set_no_cluster ("id_to_iri");
  bif_define ("id_to_iri_nosignal", bif_id_to_iri_nosignal);
  bif_set_uses_index (bif_id_to_iri_nosignal);
  bif_set_no_cluster ("id_to_iri_nosignal");

  bif_define ("iri_to_rdf_prefix_and_local", bif_iri_to_rdf_prefix_and_local);
  bif_define ("rdf_cache_id", bif_rdf_cache_id);
  bif_define ("rdf_cache_id_to_name", bif_rdf_cache_id_to_name);
  bif_define ("iri_id_cache_flush", bif_iri_id_cache_flush);
  bif_define ("iri_id_new", bif_iri_id_new);
  bif_define ("__rdf_obj_ft_rule_add", bif_rdf_obj_ft_rule_add);
  bif_define ("__rdf_obj_ft_rule_del", bif_rdf_obj_ft_rule_del);
  bif_define ("__rdf_obj_ft_rule_zap_all", bif_rdf_obj_ft_rule_zap_all);
  bif_define ("__rdf_obj_ft_rule_check", bif_rdf_obj_ft_rule_check);
  bif_define ("__rdf_obj_ft_rule_count_in_graph", bif_rdf_obj_ft_rule_count_in_graph);
  /* Short aliases for use in generated SQL text: */
  bif_define ("__i2idn", bif_iri_to_id_nosignal);
  bif_define ("__i2id", bif_iri_to_id);
  bif_define ("__i2idc", bif_iri_to_id_if_cached);
  bif_define ("__id2i", bif_id_to_iri);
  bif_define ("__id2in", bif_id_to_iri_nosignal);
  bif_define ("rdf_graph_keyword", bif_rdf_graph_keyword);
  bif_define ("iri_split", bif_iri_split);
  bif_define ("uriqa_dynamic_local_replace", bif_uriqa_dynamic_local_replace);
#ifdef DEBUG
  bif_define ("turtle_lex_test", bif_turtle_lex_test);
#endif
  if (100 >= iri_cache_size)
    iri_cache_size = main_bufs / 2;
  iri_name_cache = nic_allocate (iri_cache_size, 1, 0);
  iri_prefix_cache = nic_allocate (iri_cache_size / 10, 0, 0);
  rdf_lang_cache = nic_allocate (255, 0, 0);
  rdf_type_cache = nic_allocate (1000, 0, 0);
  ddl_ensure_table ("DB.DBA.RDF_PREFIX", rdf_prefix_text);
  ddl_ensure_table ("DB.DBA.RDF_IRI", rdf_iri_text);
  rdf_obj_ft_rules_mtx = mutex_allocate ();
  rdf_obj_ft_rules_by_iids = id_hash_allocate (1000,
    sizeof (rdf_obj_ft_rule_iid_hkey_t), sizeof (dk_set_t),
    rdf_obj_ft_rule_iid_hkey_hash, rdf_obj_ft_rule_iid_hkey_cmp );
  rdf_obj_ft_rules_by_iris = id_hash_allocate (1000,
    sizeof (rdf_obj_ft_rule_iri_hkey_t), sizeof (dk_set_t),
    rdf_obj_ft_rule_iri_hkey_hash, rdf_obj_ft_rule_iri_hkey_cmp );
  rdf_obj_ft_graph_rule_counts = id_hash_allocate (1000,
    sizeof (boxint), sizeof (ptrlong),
    boxint_hash, boxint_hashcmp );
  ddl_std_proc (iri_replay, 0);
  ddl_std_proc (range_replay, 0);
  rdf_inf_init ();
  rdf_key_comp_init  ();
}