-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsrank.sql
More file actions
395 lines (358 loc) · 10.3 KB
/
Copy pathsrank.sql
File metadata and controls
395 lines (358 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
--
-- $Id$
--
-- This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
-- project.
--
-- Copyright (C) 1998-2021 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
--
-- Rank RDF subjects.
--
EXEC_STMT ('create table RDF_IRI_RANK (rnk_iri iri_id_8 primary key, rnk_string varchar no compress)',0);
EXEC_STMT ('alter index RDF_IRI_RANK on RDF_IRI_RANK partition (rnk_iri int (0hexffff00))',0);
EXEC_STMT ('create table RDF_IRI_STAT (rst_iri iri_id_8 primary key, rst_string varchar no compress)',0);
EXEC_STMT ('alter index RDF_IRI_STAT on RDF_IRI_STAT partition (rst_iri int (0hexffff00))',0);
create procedure f_s (in f double precision)
{
declare i double precision;
i := log (f) * 1000 + 0hex7fff;
if (i > 0hexffff)
return 0hexffff;
return cast (i as int);
}
;
create procedure s_f (in i int)
{
return exp ((i - 0hex7fff) / 1e3);
}
;
create procedure rnk_scale (in i int)
{
declare ret, tmp any;
ret := exp ((i - 0hex7fff) / 1e3);
if (ret < 1)
{
return (2 * atan (ret*5));
}
if (ret > 1 and ret < 10)
{
return 3 + ((atan (ret-1) * 4) / 3.14e0);
}
else
{
return 7 + (atan ((ret-10)/50) * 2);
}
}
;
create procedure DB.DBA.IR_SRV (in iri iri_id_8)
{
declare str varchar;
declare n, nth, ni int;
if (not isiri_id (iri))
return vector (0, 1);
ni := iri_id_num (iri);
n := bit_and (0hexffffffffffffff00, ni);
nth := 2 * bit_and (ni, 0hexff);
str := (select rnk_string from rdf_iri_rank table option (no cluster) where rnk_iri = iri_id_from_num (n));
if (nth >= length (str))
return vector (0, 1);
return vector (str[nth] * 256 + str[nth + 1], 1);
}
;
dpipe_define ('IRI_RANK', 'DB.DBA.RDF_IRI_RANK', 'RDF_IRI_RANK', 'DB.DBA.IR_SRV', 128);
dpipe_define ('DB.DBA.IRI_RANK', 'DB.DBA.RDF_IRI_RANK', 'RDF_IRI_RANK', 'DB.DBA.IR_SRV', 128);
create procedure DB.DBA.IRI_RANK (in iri iri_id_8)
{
declare str varchar;
declare n, nth, ni int;
if (__tag (iri) <> 243 and __tag (iri) <> 244)
return 0;
ni := iri_id_num (iri);
n := bit_and (0hexffffffffffffff00, ni);
nth := 2 * bit_and (ni, 0hexff);
str := (select rnk_string from rdf_iri_rank where rnk_iri = iri_id_from_num (n));
if (nth >= length (str))
return 0;
return str[nth] * 256 + str[nth + 1];
}
;
create procedure rnk_store_w (inout first int, inout str varchar, inout fill int)
{
if (fill < 1000)
str := subseq (str, 0, fill);
insert replacing rdf_iri_stat option (no cluster) values (iri_id_from_num (first), str);
commit work;
}
;
create procedure rnk_count_refs_srv ()
{
declare cr cursor for select s, p from rdf_quad table option (no cluster, index rdf_quad) where isiri_id (o);
declare s_first, s_prev, nth, sn, cnt, fill int;
declare s, p iri_id;
declare str varchar;
whenever not found goto last;
s_first := null;
s_prev := null;
open cr;
for (;;)
{
fetch cr into s, p;
sn := iri_id_num (s);
if (s_first is null)
{
s_first := bit_and (sn, 0hexffffffffffffff00);
s_prev := sn;
cnt := 0;
}
if (sn = s_prev)
{
cnt := cnt + 1;
}
else
{
if (not isstring (str))
str := make_string (1536);
nth := 6 * (s_prev - s_first);
str[nth] := bit_shift (cnt, -8);
str[nth + 1] := cnt;
fill := nth + 6;
cnt := 1;
s_prev := sn;
if (sn - s_first > 255)
{
rnk_store_w (s_first, str, fill);
str := make_string (1536);
s_first := bit_and (sn, 0hexffffffffffffff00);
fill := 0;
}
}
}
last:
if (not isstring (str))
str := make_string (1536);
nth := 6 * (s_prev - s_first);
str[nth] := bit_shift (cnt, -8);
str[nth + 1] := cnt;
fill := nth + 6;
rnk_store_w (s_first, str, fill);
}
;
create procedure DB.DBA.Ist_SRV (in iri iri_id_8)
{
declare str varchar;
declare n, nth, ni int;
ni := iri_id_num (iri);
n := bit_and (0hexffffffffffffff00, ni);
nth := 6 * bit_and (ni, 0hexff);
str := (select rst_string from rdf_iri_stat table option (no cluster) where rst_iri = iri_id_from_num (n));
if (str is null)
return vector (0, 1);
if (nth > length (str) - 6)
return vector (0, 1);
return vector (bit_shift (str[nth], 40) + bit_shift (str[nth + 1], 32) + bit_shift(str[nth + 2], 24)
+ bit_shift (str[nth + 3], 16) + bit_shift (str[nth + 4], 8) + str[nth + 5], 1);
}
;
create procedure decl2_dpipe_define ()
{
if (sys_stat ('cl_run_local_only'))
return;
dpipe_define ('IRI_RANK', 'DB.DBA.RDF_IRI_RANK', 'RDF_IRI_RANK', 'DB.DBA.IR_SRV', 128);
dpipe_define ('DB.DBA.IRI_RANK', 'DB.DBA.RDF_IRI_RANK', 'RDF_IRI_RANK', 'DB.DBA.IR_SRV', 128);
dpipe_define ('IRI_STAT', 'DB.DBA.RDF_IRI_STAT', 'RDF_IRI_STAT', 'DB.DBA.IST_SRV', 128);
}
;
decl2_dpipe_define ();
create procedure DB.DBA.IRI_STAT (in iri iri_id_8)
{
declare str varchar;
declare n, nth, ni int;
ni := iri_id_num (iri);
n := bit_and (0hexffffffffffffff00, ni);
nth := 6 * bit_and (ni, 0hexff);
str := (select rst_string from rdf_iri_stat where rst_iri = iri_id_from_num (n));
if (str is null)
return 0;
if (nth > length (str) - 6)
return 0;
return bit_shift (str[nth], 40) + bit_shift (str[nth + 1], 32) + bit_shift(str[nth + 2], 24)
+ bit_shift (str[nth + 3], 16) + bit_shift (str[nth + 4], 8) + str[nth + 5];
}
;
create procedure rst_old_sc (in rst int)
{
return bit_and (0hexffff, bit_shift (rst, -16));
}
;
create procedure rnk_inc (in rnk int, in nth_iter int)
{
/* the score increment is 1 / n_outgoing * (score_now - score_before) */
declare n_out, sc, prev_sc, inc double precision;
n_out := bit_shift (rnk, -32);
if (n_out < 1)
n_out := 1;
if (1 = nth_iter)
return 1e0 / n_out;
sc := s_f (bit_and (bit_shift (rnk, -16), 0hexffff));
prev_sc := s_f (bit_and (rnk, 0hexffff));
inc := log (1 + sc - prev_sc) / log (2);
return (1e0 / n_out) * (inc / nth_iter);
}
;
create procedure rnk_store_sc (inout first int, inout str varchar, inout fill int)
{
if (fill < 300)
str := subseq (str, 0, fill);
insert replacing rdf_iri_rank option (no cluster) values (iri_id_from_num (first), str);
commit work;
}
;
create procedure rnk_get_ranks (in s_first iri_id)
{
declare str varchar;
str := (select rnk_string from rdf_iri_rank where rnk_iri = iri_id_from_num (s_first));
if (str is null)
return make_string (512);
if (length (str) < 512)
return str || make_string (512 - length (str));
return str;
}
;
create procedure rnk_score (in nth_iter int)
{
declare cr cursor for select o, p, iri_stat (s) from rdf_quad table option (no cluster, index rdf_quad_opgs) where o >#i0 and o < iri_id_from_num (0hexffffffffffffff00);
declare s_first, s_prev, nth, sn, rnk, ssc, fill, n_iters int;
declare sc double precision;
declare s, p iri_id;
declare str varchar;
set isolation = 'committed';
log_enable (2);
whenever not found goto last;
s_first := null;
s_prev := null;
open cr;
for (;;)
{
fetch cr into s, p, rnk;
sn := iri_id_num (s);
if (s_first is null)
{
s_first := bit_and (sn, 0hexffffffffffffff00);
if (nth_iter > 1)
str := rnk_get_ranks (s_first);
else
str := make_string (512);
s_prev := sn;
sc := 0;
}
if (sn = s_prev)
{
sc := sc + rnk_inc (rnk, nth_iter);
-- dbg_obj_princ (' sc of ', s, ' ', sc);
}
else
{
if (not isstring (str))
str := make_string (512);
nth := 2 * (s_prev - s_first);
ssc := f_s (sc + s_f (str[nth] * 256 + str[nth + 1]));
str[nth] := bit_shift (ssc, -8);
str[nth + 1] := ssc;
fill := nth + 2;
sc := rnk_inc (rnk, nth_iter);
s_prev := sn;
if (sn - s_first > 255)
{
rnk_store_sc (s_first, str, fill);
s_first := bit_and (sn, 0hexffffffffffffff00);
if (nth_iter > 1)
str := rnk_get_ranks (s_first);
else
str := make_string (512);
fill := 0;
}
}
}
last:
if (not isstring (str))
str := make_string (512);
nth := 2 * (s_prev - s_first);
ssc := f_s (sc);
str[nth] := bit_shift (ssc, -8);
str[nth + 1] := ssc;
fill := nth + 2;
rnk_store_sc (s_first, str, fill);
}
;
create procedure RNK_SCORE_SRV (in nth int)
{
declare aq any;
aq := async_queue (1);
aq_request (aq, 'DB.DBA.RNK_SCORE', vector (nth));
aq_wait_all (aq);
}
;
create procedure rnk_next_cycle ()
{
/* copy rank to stat and set previous rank in stat to last rank */
declare stat, rank varchar;
declare iri iri_id;
declare n_done int;
declare cr cursor for select rst_iri, rst_string from rdf_iri_stat table option (no cluster);
-- log_enable (2);
whenever not found goto done;
open cr;
for (;;)
{
fetch cr into iri, stat;
rank := (select rnk_string from rdf_iri_rank where rnk_iri = iri);
if (isstring (rank) and isstring (stat))
{
declare nr, ns, inx, rnth, snth int;
nr := length (rank) /2;
ns := length (stat) /6;
if (nr < ns)
ns := nr;
for (inx := 0; inx < ns; inx := inx + 1)
{
n_done := n_done + 1;
rnth := inx * 2;
snth := inx * 6;
stat[snth +4] := stat[snth + 2];
stat[snth +5] := stat[snth + 3];
stat[snth + 2] := rank [rnth];
stat[snth + 3] := rank[rnth + 1];
}
update rdf_iri_stat set rst_string = stat where current of cr option (no cluster);
commit work;
}
}
done:
return n_done;
}
;
create procedure s_rank ()
{
if (not exists (select 1 from SYS_KEYS, SYS_KEY_PARTS, SYS_COLS where KEY_TABLE = 'DB.DBA.RDF_QUAD' and KEY_ID = KP_KEY_ID and KP_COL = COL_ID and KEY_MIGRATE_TO is null and KP_NTH = 0 and KEY_TABLE = \TABLE and \COLUMN = 'S'))
signal ('42000', 'The RDF_QUAD table do not have index with leading "S", can not perform the operation');
cl_exec ('rnk_count_refs_srv ()');
cl_exec ('rnk_score_srv (1)');
cl_exec ('rnk_next_cycle ()');
cl_exec ('rnk_score_srv (2)');
cl_exec ('rnk_next_cycle ()');
cl_exec ('rnk_score_srv (3)');
}
;