Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use 5.16.1;
- use strict;
- use warnings;
- use constant {
- DFLT_SER_TAG => 'CB',
- N_PERC_RED => 15,
- };
- use subs qw(store_data);
- use File::Slurp qw(write_file);
- use File::Map qw(map_file);
- use AnyEvent;
- use Try::Tiny;
- use Tag::DeCoder;
- my %hsh=map {'t'.$_ => {'triggerid'=>$_, 'status'=>(int(rand 25) & 1), 'lostfunk'=>rand(),'svc-path'=>[map [map int(rand 10000), 2..(int(rand 5)+2)], 0..int(rand 5)]}} 1..10000;
- my $pthMapFile='/tmp/test.mmap.'.$$;
- # Initialy write data to the file that will be mmap'ed later
- store_data($pthMapFile, \%hsh);
- # Save mmaped file name
- write_file '/tmp/mmaped_file.pth', $pthMapFile;
- say "Mapping to: $pthMapFile";
- # Actually create mapping from file to $strMap string variable
- map_file my $strMap, $pthMapFile, '+<';
- my $cv=AnyEvent->condvar;
- my $aeh=AnyEvent->timer('after'=>0, 'interval'=>1, 'cb' => sub {
- # Doi it immediately and repeat every 1 sec...
- try {
- say scalar(localtime);
- @{$hsh{'t'.(int(rand 10000)+1)}}{qw/lostfunk status/} = (rand(), int(rand 25) & 1) for 7..int(rand 28)+7;
- store_data(\$strMap, \%hsh);
- } catch {
- say "ERROR: $_";
- }
- });
- $cv->recv;
- sub store_data ($$$) {
- my $where2store = $_[0];
- my $sData = encodeByTag($_[2] // DFLT_SER_TAG() => $_[1]);
- use bytes;
- unless (ref $where2store) {
- open my $fh, '>', $where2store;
- print $fh pack('V', length $sData), $sData, "\x00" x int(length($sData)*N_PERC_RED*0.01);
- close $fh;
- } else {
- substr(${$where2store}, 0, length($sData)+4) = pack('V', length $sData) . $sData
- }
- no bytes;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement