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
|
package LXRng::Index::Pg;
use strict;
use DBI;
use base qw(LXRng::Index::DBI);
sub dbh {
my ($self) = @_;
return $$self{'dbh'} if $$self{'dbh'};
$$self{'dbh'} = DBI->connect('dbi:Pg:'.$$self{'db_spec'},
$$self{'db_user'}, $$self{'db_pass'},
{AutoCommit => 1,
RaiseError => 1,
pg_server_prepare => 1})
or die($DBI::errstr);
$$self{'dbh_pid'} = $$;
return $$self{'dbh'};
}
sub init_db {
my ($self) = @_;
my $dbh = $self->dbh;
my $pre = $self->prefix;
$dbh->{AutoCommit} = 0;
$dbh->do(qq{create sequence ${pre}treenum}) or die($dbh->errstr);
$dbh->do(qq{create sequence ${pre}relnum}) or die($dbh->errstr);
$dbh->do(qq{create sequence ${pre}filenum cache 50}) or die($dbh->errstr);
$dbh->do(qq{create sequence ${pre}revnum cache 50}) or die($dbh->errstr);
$dbh->do(qq{create sequence ${pre}symnum cache 50}) or die($dbh->errstr);
$dbh->do(qq{create sequence ${pre}identnum cache 50}) or die($dbh->errstr);
$dbh->do(qq{
create table ${pre}charsets
(
id serial,
name varchar,
primary key (id)
)
}) or die($dbh->errstr);
$dbh->do(qq{insert into ${pre}charsets(name) values ('ascii')})
or die($dbh->errstr);
$dbh->do(qq{insert into ${pre}charsets(name) values ('utf-8')})
or die($dbh->errstr);
$dbh->do(qq{insert into ${pre}charsets(name) values ('iso8859-1')})
or die($dbh->errstr);
$dbh->do(qq{
create table ${pre}trees
(
id int default nextval('${pre}treenum'),
name varchar,
primary key (id)
)
}) or die($dbh->errstr);
$dbh->do(qq{
create table ${pre}releases
(
id int default nextval('${pre}relnum'),
id_tree int references ${pre}trees(id),
release_tag varchar,
is_indexed bool default 'f',
primary key (id)
)
}) or die($dbh->errstr);
$dbh->do(qq{
create table ${pre}files
(
id int default nextval('${pre}filenum'),
path varchar,
primary key (id),
unique (path)
)
}) or die($dbh->errstr);
$dbh->do(qq{
create table ${pre}revisions
(
id int default nextval('${pre}revnum'),
id_file int references ${pre}files(id),
revision varchar,
last_modified_gmt timestamp without time zone, -- GMT
last_modified_tz varchar(5), -- Optional TZ
body_charset int references ${pre}charsets(id),
primary key (id),
unique (id_file, revision)
)
}) or die($dbh->errstr);
$dbh->do(qq{
create table ${pre}filestatus
(
id_rfile int references ${pre}revisions(id),
indexed bool default 'f',
referenced bool default 'f',
hashed bool default 'f',
primary key (id_rfile)
)
}) or die($dbh->errstr);
$dbh->do(qq{
create table ${pre}hashed_documents
(
id_rfile int references ${pre}revisions(id),
doc_id int not null,
primary key (id_rfile)
)
}) or die($dbh->errstr);
$dbh->do(qq{
create table ${pre}filereleases
(
id_rfile int,
id_release int,
primary key (id_rfile, id_release)
)
}) or die($dbh->errstr);
$dbh->do(qq{
create table ${pre}includes
(
id_rfile int references ${pre}revisions(id),
id_include_path int references ${pre}files(id)
)
}) or die($dbh->errstr);
$dbh->do(qq{
create table ${pre}symbols
(
id int default nextval('${pre}symnum'),
name varchar,
primary key (id),
unique (name)
)
}) or die($dbh->errstr);
$dbh->do(qq{
create table ${pre}identifiers
(
id int default nextval('${pre}identnum'),
id_symbol int references ${pre}symbols(id) deferrable,
id_rfile int references ${pre}revisions(id) deferrable,
line int,
type char(1),
context int references ${pre}identifiers(id) deferrable,
primary key (id)
)
}) or die($dbh->errstr);
$dbh->do(qq{
create table ${pre}usage
(
id_rfile int,
id_symbol int,
lines int[]
)
}) or die($dbh->errstr);
$dbh->do(qq{alter table ${pre}usage alter column id_symbol set statistics 250});
$dbh->do(qq{alter table ${pre}identifiers alter column id_symbol set statistics 250});
$dbh->do(qq{create index ${pre}symbol_idx1 on ${pre}symbols using btree (name)})
or die($dbh->errstr);
$dbh->do(qq{create index ${pre}ident_idx1 on ${pre}identifiers using btree (id_symbol)})
or die($dbh->errstr);
$dbh->do(qq{create index ${pre}ident_idx2 on ${pre}identifiers using btree (id_rfile)})
or die($dbh->errstr);
$dbh->do(qq{create index ${pre}ident_idx3 on ${pre}identifiers using btree (id_symbol, id_rfile)})
or die($dbh->errstr);
$dbh->do(qq{create index ${pre}usage_idx1 on ${pre}usage using btree (id_symbol)})
or die($dbh->errstr);
$dbh->do(qq{create index ${pre}usage_idx2 on ${pre}usage using btree (id_rfile)})
or die($dbh->errstr);
$dbh->do(qq{create index ${pre}include_idx1 on ${pre}includes using btree (id_rfile)})
or die($dbh->errstr);
$dbh->do(qq{create index ${pre}file_idx1 on ${pre}files using btree (path)})
or die($dbh->errstr);
$dbh->do(qq{create index ${pre}filerel_idx1 on ${pre}filereleases using btree (id_release)})
or die($dbh->errstr);
$dbh->do(qq{grant select on ${pre}charsets to public}) or die($dbh->errstr);
$dbh->do(qq{grant select on ${pre}trees to public}) or die($dbh->errstr);
$dbh->do(qq{grant select on ${pre}releases to public}) or die($dbh->errstr);
$dbh->do(qq{grant select on ${pre}files to public}) or die($dbh->errstr);
$dbh->do(qq{grant select on ${pre}releases to public}) or die($dbh->errstr);
$dbh->do(qq{grant select on ${pre}filereleases to public}) or die($dbh->errstr);
$dbh->do(qq{grant select on ${pre}filestatus to public}) or die($dbh->errstr);
$dbh->do(qq{grant select on ${pre}hashed_documents to public}) or die($dbh->errstr);
$dbh->do(qq{grant select on ${pre}includes to public}) or die($dbh->errstr);
$dbh->do(qq{grant select on ${pre}symbols to public}) or die($dbh->errstr);
$dbh->do(qq{grant select on ${pre}identifiers to public}) or die($dbh->errstr);
$dbh->do(qq{grant select on ${pre}usage to public}) or die($dbh->errstr);
$dbh->do(qq{grant select on ${pre}revisions to public}) or die($dbh->errstr);
$dbh->commit();
$dbh->{AutoCommit} = 0;
}
sub drop_db {
my ($self) = @_;
my $dbh = $self->dbh;
my $pre = $self->prefix;
local($dbh->{RaiseError}) = 0;
$dbh->do(qq{drop index ${pre}symbol_idx1});
$dbh->do(qq{drop index ${pre}ident_idx1});
$dbh->do(qq{drop index ${pre}ident_idx2});
$dbh->do(qq{drop index ${pre}usage_idx1});
$dbh->do(qq{drop index ${pre}usage_idx2});
$dbh->do(qq{drop index ${pre}include_idx1});
$dbh->do(qq{drop index ${pre}file_idx1});
$dbh->do(qq{drop index ${pre}filerel_idx1});
$dbh->do(qq{drop table ${pre}usage});
$dbh->do(qq{drop table ${pre}identifiers});
$dbh->do(qq{drop table ${pre}symbols});
$dbh->do(qq{drop table ${pre}includes});
$dbh->do(qq{drop table ${pre}filereleases});
$dbh->do(qq{drop table ${pre}hashed_documents});
$dbh->do(qq{drop table ${pre}filestatus});
$dbh->do(qq{drop table ${pre}revisions});
$dbh->do(qq{drop table ${pre}files});
$dbh->do(qq{drop table ${pre}releases});
$dbh->do(qq{drop table ${pre}trees});
$dbh->do(qq{drop table ${pre}charsets});
$dbh->do(qq{drop sequence ${pre}treenum});
$dbh->do(qq{drop sequence ${pre}relnum});
$dbh->do(qq{drop sequence ${pre}filenum});
$dbh->do(qq{drop sequence ${pre}revnum});
$dbh->do(qq{drop sequence ${pre}symnum});
$dbh->do(qq{drop sequence ${pre}identnum});
}
sub _add_tree {
my ($self, $tree) = @_;
my $dbh = $self->dbh;
my $pre = $self->prefix;
my $sth = $$self{'sth'}{'_add_tree_ins'} ||=
$dbh->prepare(qq{insert into ${pre}trees(name) values (?)});
$sth->execute($tree);
$sth = $$self{'sth'}{'_add_tree_insid'} ||=
$dbh->prepare(qq{select currval('${pre}treenum')});
$sth->execute();
my ($id) = $sth->fetchrow_array();
$sth->finish();
return $id;
}
sub _add_release {
my ($self, $tree_id, $release) = @_;
my $dbh = $self->dbh;
my $pre = $self->prefix;
my $sth = $$self{'sth'}{'_add_relase_ins'} ||=
$dbh->prepare(qq{insert into ${pre}releases(id_tree, release_tag)
values (?, ?)});
$sth->execute($tree_id, $release);
$sth = $$self{'sth'}{'_add_release_insid'} ||=
$dbh->prepare(qq{select currval('${pre}relnum')});
$sth->execute();
my ($id) = $sth->fetchrow_array();
$sth->finish();
return $id;
}
sub _add_file {
my ($self, $path) = @_;
my $dbh = $self->dbh;
my $pre = $self->prefix;
my $sth = $$self{'sth'}{'_add_file_ins'} ||=
$dbh->prepare(qq{insert into ${pre}files(path) values (?)});
$sth->execute($path);
$sth = $$self{'sth'}{'_add_file_insid'} ||=
$dbh->prepare(qq{select currval('${pre}filenum')});
$sth->execute();
my ($id) = $sth->fetchrow_array();
$sth->finish();
return $id;
}
sub _get_rfile {
my ($self, $file_id, $revision) = @_;
my $dbh = $self->dbh;
my $pre = $self->prefix;
my $sth = $$self{'sth'}{'_get_rfile'} ||=
$dbh->prepare(qq{select id, extract('epoch' from last_modified_gmt
at time zone 'UTC')
from ${pre}revisions
where id_file = ? and revision = ?});
my ($id, $gmt);
if ($sth->execute($file_id, $revision) > 0) {
($id, $gmt) = $sth->fetchrow_array();
}
$sth->finish();
return ($id, $gmt);
}
sub _add_rfile {
my ($self, $file_id, $revision, $time) = @_;
my ($epoch, $zone) = $time =~ /^(\d+)(?: ([-+]\d\d\d\d)|)$/;
my $dbh = $self->dbh;
my $pre = $self->prefix;
my $sth = $$self{'sth'}{'_add_rfile_ins'} ||=
$dbh->prepare(qq{
insert into ${pre}revisions(id_file, revision,
last_modified_gmt,
last_modified_tz)
values (?, ?,
timestamp 'epoch' + ? * interval '1 second', ?)});
$sth->execute($file_id, $revision, $epoch, $zone)
or die($dbh->errstr);
$sth = $$self{'sth'}{'_add_rfile_insid'} ||=
$dbh->prepare(qq{select currval('${pre}revnum')});
$sth->execute();
my ($id) = $sth->fetchrow_array();
$sth->finish();
return $id;
}
sub _update_rfile_timestamp {
my ($self, $rfile_id, $time) = @_;
my ($epoch, $zone) = $time =~ /^(\d+)(?: ([-+]\d\d\d\d)|)$/;
my $dbh = $self->dbh;
my $pre = $self->prefix;
my $sth = $$self{'sth'}{'_update_rfile_timestamp'} ||=
$dbh->prepare(qq{
update ${pre}revisions set
last_modified_gmt = timestamp 'epoch' + ? * interval '1 second',
last_modified_tz = ?
where id = ?});
$sth->execute($epoch, $zone, $rfile_id)
or die($dbh->errstr);
$sth->finish();
}
sub _add_filerelease {
my ($self, $rfile_id, $rel_id) = @_;
my $dbh = $self->dbh;
my $pre = $self->prefix;
my $sth = $$self{'sth'}{'_add_filerelease'} ||=
$dbh->prepare(qq{insert into ${pre}filereleases(id_rfile, id_release)
select ?, ? where not exists
(select 1 from ${pre}filereleases
where id_rfile = ? and id_release = ?)});
$sth->execute($rfile_id, $rel_id, $rfile_id, $rel_id);
}
sub _add_symbol {
my ($self, $symbol) = @_;
my $dbh = $self->dbh;
my $pre = $self->prefix;
my $sth = $$self{'sth'}{'_add_symbol_ins'} ||=
$dbh->prepare(qq{insert into ${pre}symbols(name) values (?)});
$sth->execute($symbol);
$sth = $$self{'sth'}{'_add_symbol_insid'} ||=
$dbh->prepare(qq{select currval('${pre}symnum')});
$sth->execute();
my ($id) = $sth->fetchrow_array();
$sth->finish();
return $id;
}
sub _add_ident {
my ($self, $rfile_id, $line, $sym_id, $type, $ctx_id) = @_;
my $dbh = $self->dbh;
my $pre = $self->prefix;
my $sth = $$self{'sth'}{'_add_ident_ins'} ||=
$dbh->prepare(qq{insert into ${pre}identifiers
(id_rfile, line, id_symbol, type, context)
values (?, ?, ?, ?, ?)});
$sth->execute($rfile_id, $line, $sym_id, $type, $ctx_id);
$sth = $$self{'sth'}{'_add_ident_insid'} ||=
$dbh->prepare(qq{select currval('${pre}identnum')});
$sth->execute();
my ($id) = $sth->fetchrow_array();
$sth->finish();
return $id;
}
sub _add_usage {
my ($self, $file_id, $symbol_id, $lines) = @_;
my $dbh = $self->dbh;
my $pre = $self->prefix;
my $sth = $$self{'sth'}{'_add_usage'} ||=
$dbh->prepare(qq{insert into ${pre}usage(id_rfile, id_symbol, lines)
values (?, ?, ?)});
$sth->execute($file_id, $symbol_id, '{'.join(',', @$lines).'}');
return 1;
}
sub DESTROY {
my ($self) = @_;
if ($$self{'dbh'}) {
if ($$self{'dbh_pid'} != $$) {
$$self{'dbh'}->{InactiveDestroy} = 1;
undef $$self{'dbh'};
}
else {
$$self{'dbh'}->rollback();
$$self{'dbh'}->disconnect();
delete($$self{'dbh'});
}
}
}
1;
|