]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/V2Writable.pm
4e7d6de1d132717712cdf5916a3b6d3268367554
[public-inbox.git] / lib / PublicInbox / V2Writable.pm
1 # Copyright (C) 2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # This interface wraps and mimics PublicInbox::Import
5 package PublicInbox::V2Writable;
6 use strict;
7 use warnings;
8 use base qw(PublicInbox::Lock);
9 use PublicInbox::SearchIdxPart;
10 use PublicInbox::SearchIdxSkeleton;
11 use PublicInbox::MIME;
12 use PublicInbox::Git;
13 use PublicInbox::Import;
14 use PublicInbox::MID qw(mids);
15 use PublicInbox::ContentId qw(content_id content_digest);
16 use PublicInbox::Inbox;
17
18 # an estimate of the post-packed size to the raw uncompressed size
19 my $PACKING_FACTOR = 0.4;
20
21 # assume 2 cores if GNU nproc(1) is not available
22 sub nproc () {
23         int($ENV{NPROC} || `nproc 2>/dev/null` || 2);
24 }
25
26 sub new {
27         my ($class, $v2ibx, $creat) = @_;
28         my $dir = $v2ibx->{mainrepo} or die "no mainrepo in inbox\n";
29         unless (-d $dir) {
30                 if ($creat) {
31                         require File::Path;
32                         File::Path::mkpath($dir);
33                 } else {
34                         die "$dir does not exist\n";
35                 }
36         }
37
38         my $nparts = 0;
39         my $xpfx = "$dir/xap" . PublicInbox::Search::SCHEMA_VERSION;
40
41         # always load existing partitions in case core count changes:
42         if (-d $xpfx) {
43                 foreach my $part (<$xpfx/*>) {
44                         -d $part && $part =~ m!/\d+\z! or next;
45                         eval {
46                                 Search::Xapian::Database->new($part)->close;
47                                 $nparts++;
48                         };
49                 }
50         }
51         $nparts = nproc() if ($nparts == 0);
52
53         my $self = {
54                 -inbox => $v2ibx,
55                 im => undef, #  PublicInbox::Import
56                 partitions => $nparts,
57                 parallel => 1,
58                 transact_bytes => 0,
59                 lock_path => "$dir/inbox.lock",
60                 # limit each repo to 1GB or so
61                 rotate_bytes => int((1024 * 1024 * 1024) / $PACKING_FACTOR),
62         };
63         bless $self, $class;
64 }
65
66 sub init_inbox {
67         my ($self, $parallel) = @_;
68         $self->{parallel} = $parallel;
69         $self->idx_init;
70         $self->git_init(0);
71         $self->done;
72 }
73
74 # returns undef on duplicate or spam
75 # mimics Import::add and wraps it for v2
76 sub add {
77         my ($self, $mime, $check_cb) = @_;
78
79         # spam check:
80         if ($check_cb) {
81                 $mime = $check_cb->($mime) or return;
82         }
83
84         # All pipes (> $^F) known to Perl 5.6+ have FD_CLOEXEC set,
85         # as does SQLite 3.4.1+ (released in 2007-07-20), and
86         # Xapian 1.3.2+ (released 2015-03-15).
87         # For the most part, we can spawn git-fast-import without
88         # leaking FDs to it...
89         $self->idx_init;
90
91         my $mid0;
92         my $num = num_for($self, $mime, \$mid0);
93         defined $num or return; # duplicate
94         defined $mid0 or die "BUG: $mid0 undefined\n";
95         my $im = $self->importer;
96         my $cmt = $im->add($mime);
97         $cmt = $im->get_mark($cmt);
98         my ($oid, $len, $msgref) = @{$im->{last_object}};
99
100         my $nparts = $self->{partitions};
101         my $part = $num % $nparts;
102         my $idx = $self->idx_part($part);
103         $idx->index_raw($len, $msgref, $num, $oid, $mid0, $mime);
104         my $n = $self->{transact_bytes} += $len;
105         if ($n > (PublicInbox::SearchIdx::BATCH_BYTES * $nparts)) {
106                 $self->checkpoint;
107         }
108
109         $mime;
110 }
111
112 sub num_for {
113         my ($self, $mime, $mid0) = @_;
114         my $mids = mids($mime->header_obj);
115         if (@$mids) {
116                 my $mid = $mids->[0];
117                 my $num = $self->{skel}->{mm}->mid_insert($mid);
118                 if (defined $num) { # common case
119                         $$mid0 = $mid;
120                         return $num;
121                 };
122
123                 # crap, Message-ID is already known, hope somebody just resent:
124                 $self->barrier;
125                 foreach my $m (@$mids) {
126                         # read-only lookup now safe to do after above barrier
127                         my $existing = $self->lookup_content($mime, $m);
128                         # easy, don't store duplicates
129                         # note: do not add more diagnostic info here since
130                         # it gets noisy on public-inbox-watch restarts
131                         return if $existing;
132                 }
133
134                 # very unlikely:
135                 warn "<$mid> reused for mismatched content\n";
136
137                 # try the rest of the mids
138                 foreach my $i (1..$#$mids) {
139                         my $m = $mids->[$i];
140                         $num = $self->{skel}->{mm}->mid_insert($m);
141                         if (defined $num) {
142                                 warn "alternative <$m> for <$mid> found\n";
143                                 $$mid0 = $m;
144                                 return $num;
145                         }
146                 }
147         }
148         # none of the existing Message-IDs are good, generate a new one:
149         num_for_harder($self, $mime, $mid0);
150 }
151
152 sub num_for_harder {
153         my ($self, $mime, $mid0) = @_;
154
155         my $hdr = $mime->header_obj;
156         my $dig = content_digest($mime);
157         $$mid0 = PublicInbox::Import::digest2mid($dig);
158         my $num = $self->{skel}->{mm}->mid_insert($$mid0);
159         unless (defined $num) {
160                 # it's hard to spoof the last Received: header
161                 my @recvd = $hdr->header_raw('Received');
162                 $dig->add("Received: $_") foreach (@recvd);
163                 $$mid0 = PublicInbox::Import::digest2mid($dig);
164                 $num = $self->{skel}->{mm}->mid_insert($$mid0);
165
166                 # fall back to a random Message-ID and give up determinism:
167                 until (defined($num)) {
168                         $dig->add(rand);
169                         $$mid0 = PublicInbox::Import::digest2mid($dig);
170                         warn "using random Message-ID <$$mid0> as fallback\n";
171                         $num = $self->{skel}->{mm}->mid_insert($$mid0);
172                 }
173         }
174         PublicInbox::Import::append_mid($hdr, $$mid0);
175         $num;
176 }
177
178 sub idx_part {
179         my ($self, $part) = @_;
180         $self->{idx_parts}->[$part];
181 }
182
183 # idempotent
184 sub idx_init {
185         my ($self) = @_;
186         return if $self->{idx_parts};
187         my $ibx = $self->{-inbox};
188
189         # do not leak read-only FDs to child processes, we only have these
190         # FDs for duplicate detection so they should not be
191         # frequently activated.
192         delete $ibx->{$_} foreach (qw(git mm search));
193
194         $self->lock_acquire;
195
196         # first time initialization, first we create the skeleton pipe:
197         my $skel = $self->{skel} = PublicInbox::SearchIdxSkeleton->new($self);
198
199         # need to create all parts before initializing msgmap FD
200         my $max = $self->{partitions} - 1;
201         my $idx = $self->{idx_parts} = [];
202         for my $i (0..$max) {
203                 push @$idx, PublicInbox::SearchIdxPart->new($self, $i, $skel);
204         }
205
206         # Now that all subprocesses are up, we can open the FD for SQLite:
207         $skel->_msgmap_init->{dbh}->begin_work;
208 }
209
210 sub purge_oids {
211         my ($self, $purge) = @_; # $purge = { $object_id => 1, ... }
212         $self->done;
213         my $pfx = "$self->{-inbox}->{mainrepo}/git";
214         foreach my $i (0..$self->{max_git}) {
215                 my $git = PublicInbox::Git->new("$pfx/$i.git");
216                 my $im = $self->import_init($git, 0);
217                 $im->purge_oids($purge);
218         }
219 }
220
221 sub remove_internal {
222         my ($self, $mime, $cmt_msg, $purge) = @_;
223         $self->barrier;
224         $self->idx_init;
225         my $im = $self->importer unless $purge;
226         my $ibx = $self->{-inbox};
227         my $srch = $ibx->search;
228         my $cid = content_id($mime);
229         my $skel = $self->{skel};
230         my $parts = $self->{idx_parts};
231         my $mm = $skel->{mm};
232         my $removed;
233         my $mids = mids($mime->header_obj);
234
235         # We avoid introducing new blobs into git since the raw content
236         # can be slightly different, so we do not need the user-supplied
237         # message now that we have the mids and content_id
238         $mime = undef;
239
240         foreach my $mid (@$mids) {
241                 $srch->reopen->each_smsg_by_mid($mid, sub {
242                         my ($smsg) = @_;
243                         $smsg->load_expand;
244                         my $msg = $ibx->msg_by_smsg($smsg);
245                         if (!defined($msg)) {
246                                 warn "broken smsg for $mid\n";
247                                 return 1; # continue
248                         }
249                         my $orig = $$msg;
250                         my $cur = PublicInbox::MIME->new($msg);
251                         if (content_id($cur) eq $cid) {
252                                 $mm->num_delete($smsg->num);
253                                 # $removed should only be set once assuming
254                                 # no bugs in our deduplication code:
255                                 $removed = $smsg;
256                                 $removed->{mime} = $cur;
257                                 my $oid = $smsg->{blob};
258                                 if ($purge) {
259                                         $purge->{$oid} = 1;
260                                 } else {
261                                         $im->remove(\$orig, $cmt_msg);
262                                 }
263                                 $orig = undef;
264                                 $removed->num; # memoize this for callers
265
266                                 foreach my $idx (@$parts, $skel) {
267                                         $idx->remote_remove($oid, $mid);
268                                 }
269                         }
270                         1; # continue
271                 });
272                 $self->barrier;
273         }
274         if ($purge && scalar keys %$purge) {
275                 purge_oids($self, $purge);
276         }
277         $removed;
278 }
279
280 sub remove {
281         my ($self, $mime, $cmt_msg) = @_;
282         remove_internal($self, $mime, $cmt_msg);
283 }
284
285 sub purge {
286         my ($self, $mime) = @_;
287         remove_internal($self, $mime, undef, {});
288 }
289
290
291 sub done {
292         my ($self) = @_;
293         my $locked = defined $self->{idx_parts};
294         my $im = delete $self->{im};
295         $im->done if $im; # PublicInbox::Import::done
296         $self->searchidx_checkpoint(0);
297         $self->lock_release if $locked;
298 }
299
300 sub checkpoint {
301         my ($self) = @_;
302         my $im = $self->{im};
303         $im->checkpoint if $im; # PublicInbox::Import::checkpoint
304         $self->searchidx_checkpoint(1);
305 }
306
307 # issue a write barrier to ensure all data is visible to other processes
308 # and read-only ops.  Order of data importance is: git > SQLite > Xapian
309 sub barrier {
310         my ($self) = @_;
311
312         if (my $im = $self->{im}) {
313                 $im->barrier;
314         }
315         my $skel = $self->{skel};
316         my $parts = $self->{idx_parts};
317         if ($parts && $skel) {
318                 my $dbh = $skel->{mm}->{dbh};
319                 $dbh->commit; # SQLite data is second in importance
320
321                 # Now deal with Xapian
322                 $skel->barrier_init(scalar(@$parts));
323                 # each partition needs to issue a barrier command to skel:
324                 $_->remote_barrier foreach @$parts;
325
326                 $skel->barrier_wait; # wait for each Xapian partition
327
328                 $dbh->begin_work;
329         }
330         $self->{transact_bytes} = 0;
331 }
332
333 sub searchidx_checkpoint {
334         my ($self, $more) = @_;
335
336         # order matters, we can only close {skel} after all partitions
337         # are done because the partitions also write to {skel}
338         if (my $parts = $self->{idx_parts}) {
339                 foreach my $idx (@$parts) {
340                         $idx->remote_commit; # propagates commit to skel
341                         $idx->remote_close unless $more;
342                 }
343                 delete $self->{idx_parts} unless $more;
344         }
345
346         if (my $skel = $self->{skel}) {
347                 my $dbh = $skel->{mm}->{dbh};
348                 $dbh->commit;
349                 if ($more) {
350                         $dbh->begin_work;
351                 } else {
352                         $skel->remote_close;
353                         delete $self->{skel};
354                 }
355         }
356         $self->{transact_bytes} = 0;
357 }
358
359 sub git_init {
360         my ($self, $new) = @_;
361         my $pfx = "$self->{-inbox}->{mainrepo}/git";
362         my $git_dir = "$pfx/$new.git";
363         my @cmd = (qw(git init --bare -q), $git_dir);
364         PublicInbox::Import::run_die(\@cmd);
365
366         my $all = "$self->{-inbox}->{mainrepo}/all.git";
367         unless (-d $all) {
368                 @cmd = (qw(git init --bare -q), $all);
369                 PublicInbox::Import::run_die(\@cmd);
370                 @cmd = (qw/git config/, "--file=$all/config",
371                                 'repack.writeBitmaps', 'true');
372                 PublicInbox::Import::run_die(\@cmd);
373         }
374
375         @cmd = (qw/git config/, "--file=$git_dir/config",
376                         'include.path', '../../all.git/config');
377         PublicInbox::Import::run_die(\@cmd);
378
379         my $alt = "$all/objects/info/alternates";
380         my $new_obj_dir = "../../git/$new.git/objects";
381         my %alts;
382         if (-e $alt) {
383                 open(my $fh, '<', $alt) or die "open < $alt: $!\n";
384                 %alts = map { chomp; $_ => 1 } (<$fh>);
385         }
386         return $git_dir if $alts{$new_obj_dir};
387         open my $fh, '>>', $alt or die "open >> $alt: $!\n";
388         print $fh "$new_obj_dir\n" or die "print >> $alt: $!\n";
389         close $fh or die "close $alt: $!\n";
390         $git_dir
391 }
392
393 sub git_dir_latest {
394         my ($self, $max) = @_;
395         $$max = -1;
396         my $pfx = "$self->{-inbox}->{mainrepo}/git";
397         return unless -d $pfx;
398         my $latest;
399         opendir my $dh, $pfx or die "opendir $pfx: $!\n";
400         while (defined(my $git_dir = readdir($dh))) {
401                 $git_dir =~ m!\A(\d+)\.git\z! or next;
402                 if ($1 > $$max) {
403                         $$max = $1;
404                         $latest = "$pfx/$git_dir";
405                 }
406         }
407         $latest;
408 }
409
410 sub importer {
411         my ($self) = @_;
412         my $im = $self->{im};
413         if ($im) {
414                 if ($im->{bytes_added} < $self->{rotate_bytes}) {
415                         return $im;
416                 } else {
417                         $self->{im} = undef;
418                         $im->done;
419                         $self->searchidx_checkpoint(1);
420                         $im = undef;
421                         my $git_dir = $self->git_init(++$self->{max_git});
422                         my $git = PublicInbox::Git->new($git_dir);
423                         return $self->import_init($git, 0);
424                 }
425         }
426         my $new = 0;
427         my $max;
428         my $latest = git_dir_latest($self, \$max);
429         if (defined $latest) {
430                 my $git = PublicInbox::Git->new($latest);
431                 my $packed_bytes = $git->packed_bytes;
432                 if ($packed_bytes >= $self->{rotate_bytes}) {
433                         $new = $max + 1;
434                 } else {
435                         $self->{max_git} = $max;
436                         return $self->import_init($git, $packed_bytes);
437                 }
438         }
439         $self->{max_git} = $new;
440         $latest = $self->git_init($new);
441         $self->import_init(PublicInbox::Git->new($latest), 0);
442 }
443
444 sub import_init {
445         my ($self, $git, $packed_bytes) = @_;
446         my $im = PublicInbox::Import->new($git, undef, undef, $self->{-inbox});
447         $im->{bytes_added} = int($packed_bytes / $PACKING_FACTOR);
448         $im->{want_object_info} = 1;
449         $im->{lock_path} = undef;
450         $im->{path_type} = 'v2';
451         $self->{im} = $im;
452 }
453
454 # XXX experimental
455 sub diff ($$$) {
456         my ($mid, $cur, $new) = @_;
457         use File::Temp qw(tempfile);
458         use PublicInbox::Spawn qw(spawn);
459
460         my ($ah, $an) = tempfile('email-cur-XXXXXXXX', TMPDIR => 1);
461         print $ah $cur->as_string or die "print: $!";
462         close $ah or die "close: $!";
463         my ($bh, $bn) = tempfile('email-new-XXXXXXXX', TMPDIR => 1);
464         PublicInbox::Import::drop_unwanted_headers($new);
465         print $bh $new->as_string or die "print: $!";
466         close $bh or die "close: $!";
467         my $cmd = [ qw(diff -u), $an, $bn ];
468         print STDERR "# MID conflict <$mid>\n";
469         my $pid = spawn($cmd, undef, { 1 => 2 });
470         defined $pid or die "diff failed to spawn $!";
471         waitpid($pid, 0) == $pid or die "diff did not finish";
472         unlink($an, $bn);
473 }
474
475 sub lookup_content {
476         my ($self, $mime, $mid) = @_;
477         my $ibx = $self->{-inbox};
478
479         my $srch = $ibx->search->reopen;
480         my $cid = content_id($mime);
481         my $found;
482         $srch->each_smsg_by_mid($mid, sub {
483                 my ($smsg) = @_;
484                 $smsg->load_expand;
485                 my $msg = $ibx->msg_by_smsg($smsg);
486                 if (!defined($msg)) {
487                         warn "broken smsg for $mid\n";
488                         return 1; # continue
489                 }
490                 my $cur = PublicInbox::MIME->new($msg);
491                 if (content_id($cur) eq $cid) {
492                         $smsg->{mime} = $cur;
493                         $found = $smsg;
494                         return 0; # break out of loop
495                 }
496
497                 # XXX DEBUG_DIFF is experimental and may be removed
498                 diff($mid, $cur, $mime) if $ENV{DEBUG_DIFF};
499
500                 1; # continue
501         });
502         $found;
503 }
504
505 sub atfork_child {
506         my ($self) = @_;
507         my $fh = delete $self->{reindex_pipe};
508         close $fh if $fh;
509         if (my $parts = $self->{idx_parts}) {
510                 $_->atfork_child foreach @$parts;
511         }
512         if (my $im = $self->{im}) {
513                 $im->atfork_child;
514         }
515 }
516
517 sub mark_deleted {
518         my ($self, $D, $git, $oid) = @_;
519         my $msgref = $git->cat_file($oid);
520         my $mime = PublicInbox::MIME->new($$msgref);
521         my $mids = mids($mime->header_obj);
522         my $cid = content_id($mime);
523         foreach my $mid (@$mids) {
524                 $D->{"$mid\0$cid"} = 1;
525         }
526 }
527
528 sub reindex_oid {
529         my ($self, $mm_tmp, $D, $git, $oid, $regen) = @_;
530         my $len;
531         my $msgref = $git->cat_file($oid, \$len);
532         my $mime = PublicInbox::MIME->new($$msgref);
533         my $mids = mids($mime->header_obj);
534         my $cid = content_id($mime);
535
536         # get the NNTP article number we used before, highest number wins
537         # and gets deleted from mm_tmp;
538         my $mid0;
539         my $num = -1;
540         my $del = 0;
541         foreach my $mid (@$mids) {
542                 $del += (delete $D->{"$mid\0$cid"} || 0);
543                 my $n = $mm_tmp->num_for($mid);
544                 if (defined $n && $n > $num) {
545                         $mid0 = $mid;
546                         $num = $n;
547                 }
548         }
549         if (!defined($mid0) && $regen && !$del) {
550                 $num = $$regen--;
551                 die "BUG: ran out of article numbers\n" if $num <= 0;
552                 my $mm = $self->{skel}->{mm};
553                 foreach my $mid (@$mids) {
554                         if ($mm->mid_set($num, $mid) == 1) {
555                                 $mid0 = $mid;
556                                 last;
557                         }
558                 }
559                 if (!defined($mid0)) {
560                         my $id = '<' . join('> <', @$mids) . '>';
561                         warn "Message-Id $id unusable for $num\n";
562                 }
563         }
564
565         if (!defined($mid0) || $del) {
566                 if (!defined($mid0) && $del) { # expected for deletes
567                         $$regen--;
568                         return
569                 }
570
571                 my $id = '<' . join('> <', @$mids) . '>';
572                 defined($mid0) or
573                         warn "Skipping $id, no article number found\n";
574                 if ($del && defined($mid0)) {
575                         warn "$id was deleted $del " .
576                                 "time(s) but mapped to article #$num\n";
577                 }
578                 return;
579
580         }
581         $mm_tmp->mid_delete($mid0) or
582                 die "failed to delete <$mid0> for article #$num\n";
583
584         my $nparts = $self->{partitions};
585         my $part = $num % $nparts;
586         my $idx = $self->idx_part($part);
587         $idx->index_raw($len, $msgref, $num, $oid, $mid0, $mime);
588         my $n = $self->{transact_bytes} += $len;
589         if ($n > (PublicInbox::SearchIdx::BATCH_BYTES * $nparts)) {
590                 $git->cleanup;
591                 $mm_tmp->atfork_prepare;
592                 $self->done; # release lock
593                 # allow -watch or -mda to write...
594                 $self->idx_init; # reacquire lock
595                 $mm_tmp->atfork_parent;
596         }
597 }
598
599 sub reindex {
600         my ($self, $regen) = @_;
601         my $ibx = $self->{-inbox};
602         my $pfx = "$ibx->{mainrepo}/git";
603         my $max_git;
604         my $latest = git_dir_latest($self, \$max_git);
605         return unless defined $latest;
606         my $head = $ibx->{ref_head} || 'refs/heads/master';
607         $self->idx_init; # acquire lock
608         my $x40 = qr/[a-f0-9]{40}/;
609         my $mm_tmp = $self->{skel}->{mm}->tmp_clone;
610         if (!$regen) {
611                 my (undef, $max) = $mm_tmp->minmax;
612                 unless (defined $max) {
613                         $regen = 1;
614                         warn
615 "empty msgmap.sqlite3, regenerating article numbers\n";
616                 }
617         }
618         my $tip; # latest commit out of all git repos
619         if ($regen) {
620                 my $regen_max = 0;
621                 for (my $cur = $max_git; $cur >= 0; $cur--) {
622                         die "already reindexing!\n" if $self->{reindex_pipe};
623                         my $git = PublicInbox::Git->new("$pfx/$cur.git");
624                         chomp($tip = $git->qx('rev-parse', $head)) unless $tip;
625                         my $h = $cur == $max_git ? $tip : $head;
626                         my @count = ('rev-list', '--count', $h, '--', 'm');
627                         $regen_max += $git->qx(@count);
628                 }
629                 die "No messages found in $pfx/*.git, bug?\n" unless $regen_max;
630                 $regen = \$regen_max;
631         }
632         my $D = {};
633         my @cmd = qw(log --raw -r --pretty=tformat:%h
634                         --no-notes --no-color --no-abbrev);
635
636         # if we are regenerating, we must not use a newer tip commit than what
637         # the regeneration counter used:
638         $tip ||= $head;
639
640         # work backwards through history
641         for (my $cur = $max_git; $cur >= 0; $cur--) {
642                 die "already reindexing!\n" if delete $self->{reindex_pipe};
643                 my $cmt;
644                 my $git_dir = "$pfx/$cur.git";
645                 my $git = PublicInbox::Git->new($git_dir);
646                 my $h = $cur == $max_git ? $tip : $head;
647                 my $fh = $self->{reindex_pipe} = $git->popen(@cmd, $h);
648                 while (<$fh>) {
649                         if (/\A$x40$/o) {
650                                 chomp($cmt = $_);
651                         } elsif (/\A:\d{6} 100644 $x40 ($x40) [AM]\tm$/o) {
652                                 $self->reindex_oid($mm_tmp, $D, $git, $1,
653                                                 $regen);
654                         } elsif (m!\A:\d{6} 100644 $x40 ($x40) [AM]\t_/D$!o) {
655                                 $self->mark_deleted($D, $git, $1);
656                         }
657                 }
658                 delete $self->{reindex_pipe};
659         }
660         my ($min, $max) = $mm_tmp->minmax;
661         defined $max and die "leftover article numbers at $min..$max\n";
662         my @d = sort keys %$D;
663         if (@d) {
664                 warn "BUG: ", scalar(@d)," unseen deleted messages marked\n";
665                 foreach (@d) {
666                         my ($mid, undef) = split(/\0/, $_, 2);
667                         warn "<$mid>\n";
668                 }
669         }
670 }
671
672 1;