| 1 | # -*- perl -*- | ||
| 2 | |||
| 3 | # Copyright (c) 2010 AdCopy | ||
| 4 | # Author: Jeff Weisberg | ||
| 5 | # Created: 2010-Jan-27 11:34 (EST) | ||
| 6 | # Function: buffer output + open/close files as needed | ||
| 7 | # | ||
| 8 | # $Id: OutFile.pm,v 1.2 2011/01/06 17:58:13 jaw Exp $ | ||
| 9 | |||
| 10 | package AC::MrGamoo::OutFile; | ||
| 11 | use AC::MrGamoo::Debug 'outfile'; | ||
| 12 | use IO::Compress::Gzip; | ||
| 13 | use strict; | ||
| 14 | |||
| 15 | my $BUFMAX = 200000; | ||
| 16 | my $max_open; | ||
| 17 | my $currently_open = 0; | ||
| 18 | my %all; | ||
| 19 | |||
| 20 | |||
| 21 | $max_open = `sh -c "ulimit -n"`; | ||
| 22 | $max_open = 255 if $^O eq 'solaris' && $max_open > 255; | ||
| 23 | $max_open -= 32; # room for other fds | ||
| 24 | |||
| 25 | |||
| 26 | sub new { | ||
| 27 | my $class = shift; | ||
| 28 | my $file = shift; | ||
| 29 | my $gz = shift; | ||
| 30 | |||
| 31 | my $me = bless { | ||
| 32 | file => $file, | ||
| 33 | gz => $gz, | ||
| 34 | }, $class; | ||
| 35 | |||
| 36 | $all{$file} = $me; | ||
| 37 | |||
| 38 | # open as many as we can up front | ||
| 39 | $me->_open() if $currently_open < $max_open; | ||
| 40 | return $me; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub close { | ||
| 44 | my $me = shift; | ||
| 45 | |||
| 46 | $me->_flush(); | ||
| 47 | $me->_touch() unless $me->{been_opened}; | ||
| 48 | $me->_close(); | ||
| 49 | } | ||
| 50 | |||
| 51 | sub output { | ||
| 52 | my $me = shift; | ||
| 53 | |||
| 54 | $me->{lastused} = $^T; # $^T as been updated with current time | ||
| 55 | |||
| 56 | if( my $fd = $me->{fd} ){ | ||
| 57 | print $fd @_; | ||
| 58 | }else{ | ||
| 59 | $me->{buffer} .= $_ for @_; | ||
| 60 | $me->_flush() if length($me->{buffer}) >= $BUFMAX; | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | ################################################################ | ||
| 65 | |||
| 66 | sub DESTROY { | ||
| 67 | my $me = shift; | ||
| 68 | $me->close(); | ||
| 69 | } | ||
| 70 | |||
| 71 | ################################################################ | ||
| 72 | |||
| 73 | sub _close { | ||
| 74 | my $me = shift; | ||
| 75 | |||
| 76 | return unless $me->{fd}; | ||
| 77 | close($me->{fd}); | ||
| 78 | $currently_open --; | ||
| 79 | delete $me->{fd}; | ||
| 80 | debug("closed file $me->{file}"); | ||
| 81 | } | ||
| 82 | |||
| 83 | sub _open { | ||
| 84 | my $me = shift; | ||
| 85 | |||
| 86 | if( $me->{gz} ){ | ||
| 87 | my $fd = IO::Compress::Gzip->new( $me->{file}, | ||
| 88 | Append => $me->{been_opened}, | ||
| 89 | Merge => $me->{been_opened}, | ||
| 90 | ); | ||
| 91 | $me->{fd} = $fd; | ||
| 92 | debug("opened file (compressed) $me->{file}"); | ||
| 93 | }else{ | ||
| 94 | my $mode = $me->{been_opened} ? '>>' : '>'; | ||
| 95 | |||
| 96 | open(my $fd, $mode, $me->{file}) || die "cannot open '$me->{file}': $!\n"; | ||
| 97 | $me->{fd} = $fd; | ||
| 98 | debug("opened file $me->{file}"); | ||
| 99 | } | ||
| 100 | |||
| 101 | $me->{been_opened} = 1; | ||
| 102 | $currently_open ++; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub _flush { | ||
| 106 | my $me = shift; | ||
| 107 | |||
| 108 | return unless $me->{buffer}; | ||
| 109 | _close_things() if $currently_open >= $max_open; | ||
| 110 | |||
| 111 | $me->_open(); | ||
| 112 | my $fd = $me->{fd}; | ||
| 113 | |||
| 114 | print $fd $me->{buffer}; | ||
| 115 | delete $me->{buffer}; | ||
| 116 | } | ||
| 117 | |||
| 118 | # to make sure file gets created | ||
| 119 | sub _touch { | ||
| 120 | my $me = shift; | ||
| 121 | |||
| 122 | _close_things() if $currently_open >= $max_open; | ||
| 123 | $me->_open(); | ||
| 124 | } | ||
| 125 | |||
| 126 | sub _close_things { | ||
| 127 | |||
| 128 | my $this_many = ($currently_open / 3) || 1; | ||
| 129 | |||
| 130 | # close least recently used | ||
| 131 | my @all = sort { | ||
| 132 | $a->{lastused} <=> $b->{lastused} | ||
| 133 | } values %all; | ||
| 134 | |||
| 135 | for my $io (@all){ | ||
| 136 | next unless $io->{fd}; | ||
| 137 | $io->close(); | ||
| 138 | last if --$this_many <= 0; | ||
| 139 | } | ||
| 140 | |||
| 141 | } | ||
| 142 | |||
| 143 | |||
| 144 | 1; | ||
| 145 |