Flux-File

 view release on metacpan or  search on metacpan

lib/Flux/File.pm  view on Meta::CPAN

        }
    }
    delete $self->{data};
}

sub _flush {
    my ($self) = @_;
    return unless defined $self->{data};

    my $lock;
    if (!$self->{fh} || $self->reopen) {
        $lock = $self->_open;
    } else {
        $lock = $self->_lockfile;
        sysseek($self->{fh}, 0, SEEK_END) if $self->safe;
    }

    $self->_write();
}

sub write {
    my ($self, $line) = @_;

    $self->write_chunk([$line]);
}

sub write_chunk {
    my ($self, $chunk) = @_;
    croak "write_chunk method expects arrayref" unless ref($chunk) eq 'ARRAY'; # can chunks be blessed into something?
    return unless @$chunk;
    for my $line (@$chunk) {
        die "invalid line $line" if ($line !~ /\n\z/);
        if (defined $self->{data}) {
            $self->{data} .= $line;
        }
        else {
            $self->{data} = $line;
        }
    }
    if (length($self->{data}) > 1_000) {
        $self->_flush;
    }
    return; # TODO - what useful data can we return?
}

sub commit {
    my ($self) = @_;
    $self->_flush;
}

sub in {
    my $self = shift;
    my ($posfile) = validate_pos(@_, SCALAR);

    return Flux::File::In->new(cursor => Flux::File::Cursor->new(posfile => $posfile), file => $self->file);
}

sub owner {
    my ($self) = @_;
    if (-e $self->file) {
        return scalar getpwuid( (stat($self->file))[4] );
    }
    else {
        return scalar getpwuid($>);
    }
}


1;

__END__

=pod

=head1 NAME

Flux::File - file storage

=head1 VERSION

version 1.01

=head1 SYNOPSIS

    $storage = Flux::File->new($filename);
    $in = $storage->in(
        Flux::File::Cursor->new($posfile)
    );

=head1 DESCRIPTION

This is a simplest implementation of C<Flux::Storage>.

It stores lines by appending them to the file. It supports clients identifiable by L<Flux::File::Cursor> objects.

It also have several options for fine control over performance vs data consistency trade-off. (See the constructor documentation below.)

=head1 METHODS

=over

=item B<new($file, [$options])>

Create new object. C<$file> should be a name of any writable file into which lines will be appended.

If C<$file> does not yet exist, it will be created.

Options can contains the following keys:

=over

=item I<lock> (default = 1)

Get lock on each write (useful when many processes writes in one file).

=item I<reopen> (default = 0)

Reopen file on each write (useful for files, which can be rotated).

=item I<safe> (default = 0)

Truncate file to the last endline (useful when your unit for writings is a single lines
and you don't want to have a hanging lines in your log in case of failure).
If C<reopen> is true, then file checks on each flush, otherwise it will be checked only at



( run in 1.691 second using v1.01-cache-2.11-cpan-39bf76dae61 )