summaryrefslogtreecommitdiff
path: root/fml/lib/FML/Article/Sequence.pm
blob: 4d3c1cbb12e26f79da55da966038024ec3f8f04a (plain)
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
#-*- perl -*-
#
#  Copyright (C) 2003,2004,2005,2006 Ken'ichi Fukamachi
#   All rights reserved. This program is free software; you can
#   redistribute it and/or modify it under the same terms as Perl itself.
#
# $FML: Sequence.pm,v 1.18 2006/10/11 14:54:49 fukachan Exp $
#

package FML::Article::Sequence;
use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK $AUTOLOAD);
use Carp;

# XXX_LOCK_CHANNEL: article_sequence
my $lock_channel = 'article_sequence';


=head1 NAME

FML::Article::Sequence - article sequence manipulation.

=head1 SYNOPSIS

=head1 DESCRIPTION

=head1 METHODS

=head2 increment_id()

increment the sequence number of this article C<$self> and
save it to C<$sequence_file>.

=cut


# Descriptions: determine the next article id (sequence number).
#    Arguments: OBJ($self)
# Side Effects: save and update the current article sequence number.
# Return Value: NUM(sequence identifier) or 0(failed)
sub increment_id
{
    my ($self)   = @_;
    my $curproc  = $self->{ _curproc };
    my $config   = $curproc->config();
    my $pcb      = $curproc->pcb();
    my $seq_file = $config->{ article_sequence_file };
    my $error    = 0;

    $curproc->lock($lock_channel);

    # XXX-TODO: we should enhance sequence_file to all IO::Adapter classes.
    use IO::Adapter;
    my $io = new IO::Adapter "file:$seq_file";
    my $id = $io->sequence_increment();
    if ($io->error()) {
	my $err = $io->error();
	$curproc->logerror( "article_id: $err" );
	$error = 1;
    }

    $curproc->unlock($lock_channel);

    unless ($error) {
	# XXX-TODO: use $curproc->article_set_id().
	# save $id in pcb (process control block) and return $id
	$pcb->set('article', 'id', $id);

	return $id;
    }
    else {
	return 0;
    }
}


=head2 id()

return the current article sequence number.

=cut

# Descriptions: return the article id (sequence number).
#    Arguments: OBJ($self)
# Side Effects: none
# Return Value: NUM(sequence number)
sub id
{
    my ($self)  = @_;
    my $curproc = $self->{ _curproc };
    my $config  = $curproc->config();
    my $pcb     = $curproc->pcb();

    # XXX-TODO: use $curproc->article_get_id().
    my $n = $pcb->get('article', 'id');

    # within Process::Distribute
    if ($n) {
	return $n;
    }
    # processes not Process::Distribute
    else {
	my $seq_file = $config->{ article_sequence_file };
	my $map      = sprintf("file:%s", $seq_file);

	return( $self->get_number_from_map($map) || 0 );
    }
}


# Descriptions: get number from map.
#    Arguments: OBJ($self) STR($map)
# Side Effects: none
# Return Value: NUM
sub get_number_from_map
{
    my ($self, $map) = @_;
    my $n = 0;

    use IO::Adapter;
    my $io = new IO::Adapter $map;
    if (defined $io) {
	$io->open();
	$n  = $io->getline() || 0;
	$n =~ s/^\s*//;
	$n =~ s/\s*$//;

	if ($n =~ /^\d+$/o) {
	    return $n;
	}
	else {
	    return 0;
	}
    }
    else {
	warn("cannot open map=$map");
    }

    return 0;
}


#
# XXX-TODO: speculate_max_id([$spool_dir]) NOT USED ?
#


=head2 speculate_max_id([$spool_dir])

scan the spool_dir and get the max number among files in it. It must
be the max (latest) article number in its folder.

=cut


# Descriptions: scan the spool_dir and get max number among files in it.
#               It must be the max (latest) article number in its folder.
#    Arguments: OBJ($curproc) STR($spool_dir)
# Side Effects: none
# Return Value: NUM(sequence number) or undef
sub speculate_max_id
{
    my ($curproc, $spool_dir) = @_;
    my $config     = $curproc->config();
    my $use_subdir = $config->{ spool_type } eq 'subdir' ? 1 : 0;

    unless (defined $spool_dir) {
	$spool_dir = $config->{ spool_dir };
    }

    $curproc->logdebug("max_id: scan $spool_dir subdir=$use_subdir");

    if ($use_subdir) {
	use DirHandle;
	my $dh = new DirHandle $spool_dir;

	if (defined $dh) {
	    my $fn         = ''; # file name
	    my $subdir     = '';
	    my $max_subdir = 0;

	  ENTRY:
	    while (defined($fn = $dh->read)) {
		next ENTRY unless $fn =~ /^\d+$/o;

		use File::Spec;
		$subdir = File::Spec->catfile($spool_dir, $fn);

		if (-d $subdir) {
		    my $max_subdir = $max_subdir > $fn ? $max_subdir : $fn;
		}
	    }

	    $dh->close();

	    # XXX-TODO wrong? to speculate max_id in subdir spool?
	    $subdir = File::Spec->catfile($spool_dir, $max_subdir);
	    $curproc->logdebug("max_id: scan $subdir");
	    $curproc->speculate_max_id($subdir);
	}
    }

    use DirHandle;
    my $dh = new DirHandle $spool_dir;
    if (defined $dh) {
	my $max = 0;
	my $fn  = ''; # file name

      ENTRY:
	while (defined($fn = $dh->read)) {
	    next ENTRY unless $fn =~ /^\d+$/o;
	    $max = $max < $fn ? $fn : $max;
	}

	$dh->close();

	return( $max > 0 ? $max : undef );
    }

    return undef;
}


=head1 CODING STYLE

See C<http://www.fml.org/software/FNF/> on fml coding style guide.

=head1 AUTHOR

Ken'ichi Fukamachi

=head1 COPYRIGHT

Copyright (C) 2003,2004,2005,2006 Ken'ichi Fukamachi

All rights reserved. This program is free software; you can
redistribute it and/or modify it under the same terms as Perl itself.

=head1 HISTORY

FML::Article::Sequence appeared in fml8 mailing list driver package.
See C<http://www.fml.org/> for more details.

=cut


1;