| 1 | ## -*- Mode: CPerl -*- | ||
| 2 | ## | ||
| 3 | ## File: DiaColloDB::PDL::MM.pm | ||
| 4 | ## Author: Bryan Jurish <moocow@cpan.org> | ||
| 5 | ## Description: DiaColloDB utilities: (temporary) mmaped PDLs | ||
| 6 | |||
| 7 | package DiaColloDB::PDL::MM; | ||
| 8 | use DiaColloDB::Logger; | ||
| 9 | use PDL; | ||
| 10 | use PDL::IO::FastRaw; | ||
| 11 | use File::Temp; | ||
| 12 | |||
| 13 | ##============================================================================== | ||
| 14 | ## Globals & Constants | ||
| 15 | |||
| 16 | our @ISA = qw(PDL DiaColloDB::Logger); | ||
| 17 | our %MMTMP = qw(); ##-- all tempfiles created, for END block | ||
| 18 | our $LOG_DEFAULT = undef; ##-- default log-level (undef: off) | ||
| 19 | |||
| 20 | ##============================================================================== | ||
| 21 | ## Constructors etc. | ||
| 22 | |||
| 23 | ## $mmpdl = CLASS->new($file?, $type?, @dims, \%opts?) | ||
| 24 | ## $mmpdl = $pdl->mmzeroes($file?, $type?, \%opts?) | ||
| 25 | ## + %opts, %$mmpdl: | ||
| 26 | ## ( | ||
| 27 | ## file => $template, ##-- file basename or File::Temp template; default='pdlXXXX' | ||
| 28 | ## suffix => $suffix, ##-- File::Temp::tempfile() suffix (default='.pdl') | ||
| 29 | ## log => $level, ##-- logging verbosity (default=$LOG_DEFAULT) | ||
| 30 | ## temp => $bool, ##-- delete on END (default: $file =~ /X{4}/) | ||
| 31 | ## PDL => $pdl, ##-- guts: real underlying mmap()ed piddle (must be key 'PDL' for PDL inheritance to work) | ||
| 32 | ## ) | ||
| 33 | sub new { | ||
| 34 | my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__; | ||
| 35 | my $src = UNIVERSAL::isa($_[0],'PDL') ? shift : undef; | ||
| 36 | my $file = !ref($_[0]) && $_[0] !~ /^[0-9]+$/ ? shift : undef; | ||
| 37 | my $type = UNIVERSAL::isa($_[0],'PDL::Type') ? shift : (defined($src) ? $src->type : PDL::double()); | ||
| 38 | my $opts = UNIVERSAL::isa($_[$#_],'HASH') ? pop(@_) : {}; | ||
| 39 | my @dims = defined($src) ? $src->dims : @_; | ||
| 40 | my $suffix = $opts->{suffix} // '.pdl'; | ||
| 41 | my $temp = $opts->{temp}; | ||
| 42 | my $log = $opts->{log} // $LOG_DEFAULT; | ||
| 43 | $file //= $opts->{file} // 'pdlXXXXX'; | ||
| 44 | |||
| 45 | ##-- maybe use File::Temp to get filename | ||
| 46 | if ($file =~ /X{4}/) { | ||
| 47 | my ($tmpfh,$tmpfile) = File::Temp::tempfile($file, SUFFIX=>$suffix, TMPDIR=>1, UNLINK=>0); | ||
| 48 | $tmpfh->close(); | ||
| 49 | $file = $tmpfile; | ||
| 50 | $temp //= 1; | ||
| 51 | } | ||
| 52 | |||
| 53 | ##-- remove any stale data | ||
| 54 | $that->unlink($file); | ||
| 55 | |||
| 56 | ##-- track tempfiles for END block | ||
| 57 | if ($temp) { | ||
| 58 | $MMTMP{$file} = undef; | ||
| 59 | } else { | ||
| 60 | delete $MMTMP{$file}; | ||
| 61 | } | ||
| 62 | |||
| 63 | ##-- guts | ||
| 64 | $that->vlog($log, "CREATE $file -> $type \[".join(',',@dims)."] (".($temp ? "TEMP" : "KEEP").")"); | ||
| 65 | my $pdl = PDL::IO::FastRaw::mapfraw($file, {Dims=>\@dims, Datatype=>$type, Creat=>1}); | ||
| 66 | return bless({ | ||
| 67 | PDL =>$pdl, | ||
| 68 | file=>$file, | ||
| 69 | temp=>($temp||0), | ||
| 70 | log =>$log, | ||
| 71 | }, ref($that)||$that); | ||
| 72 | } | ||
| 73 | BEGIN { | ||
| 74 | *PDL::mmzeroes = \&new; | ||
| 75 | } | ||
| 76 | |||
| 77 | ## $mmpdl = CLASS->mmtemp($file?, $type?, @dims, \%opts?) | ||
| 78 | ## + like new(), but always sets $opts->{temp}=1 | ||
| 79 | sub mmtemp { | ||
| 80 | my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__; | ||
| 81 | my $opts = UNIVERSAL::isa($_[$#_],'HASH') ? pop(@_) : {}; | ||
| 82 | $opts->{temp} = 1; | ||
| 83 | return $that->new(@_,$opts); | ||
| 84 | } | ||
| 85 | BEGIN { | ||
| 86 | *PDL::mmtemp = \&mmtemp; | ||
| 87 | } | ||
| 88 | |||
| 89 | |||
| 90 | ## $bool = CLASS->unlink(@basenames) | ||
| 91 | ## $bool = $mmpdl->unlink() | ||
| 92 | ## + unlinks file(s) generated by mmzeroes($basename) | ||
| 93 | ## + $basename defaults to $obj->{file} | ||
| 94 | sub unlink { | ||
| 95 | my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__; | ||
| 96 | my $rc = 1; | ||
| 97 | foreach my $file (grep {defined($_)} ($that->file, @_)) { | ||
| 98 | $that->vlog($that->{log}, "UNLINK ${file}") if (UNIVERSAL::isa($that,'HASH')); | ||
| 99 | foreach (grep {-e "$file$_"} ('','.hdr')) { | ||
| 100 | $rc &&= CORE::unlink("$file$_") or do { $that->logwarn("unlink(): failed to unlink tempfile '$file$_': $!"); 0 }; | ||
| 101 | } | ||
| 102 | delete $MMTMP{$file} if ($rc); | ||
| 103 | } | ||
| 104 | return $rc; | ||
| 105 | } | ||
| 106 | |||
| 107 | ## undef = $obj->DESTROY() | ||
| 108 | ## + must also handle "pure" piddles created e.g. by $mmpdl->xvals() | ||
| 109 | sub DESTROY { | ||
| 110 | my $obj = shift; | ||
| 111 | return if (!UNIVERSAL::isa($obj,'HASH')); | ||
| 112 | delete $obj->{pdl}; | ||
| 113 | $obj->unlink() if ($obj->{temp}); | ||
| 114 | } | ||
| 115 | |||
| 116 | ##============================================================================== | ||
| 117 | ## Accessors | ||
| 118 | |||
| 119 | ## $file = $mmdpl->file() | ||
| 120 | sub file { | ||
| 121 | return undef if (!UNIVERSAL::isa($_[0],'HASH')); | ||
| 122 | return $_[0]{file}; | ||
| 123 | } | ||
| 124 | |||
| 125 | ## $istmp = $mmdpl->temp() | ||
| 126 | sub temp { | ||
| 127 | return undef if (!UNIVERSAL::isa($_[0],'HASH')); | ||
| 128 | return $_[0]{temp}; | ||
| 129 | } | ||
| 130 | |||
| 131 | ## $pdl = $mmpdl->_pdl() | ||
| 132 | sub _pdl { | ||
| 133 | return $_[0] if (!UNIVERSAL::isa($_[0],'HASH')); | ||
| 134 | return $_[0]{PDL}; | ||
| 135 | } | ||
| 136 | |||
| 137 | ##============================================================================== | ||
| 138 | ## final cleanup in END block | ||
| 139 | END { | ||
| 140 | #DiaColloDB::PDL::MM->trace("END"); | ||
| 141 | DiaColloDB::PDL::MM::unlink(keys %MMTMP); | ||
| 142 | } | ||
| 143 | |||
| 144 | ##============================================================================== | ||
| 145 | ## Footer | ||
| 146 | 1; ##-- be happy |