Change-Id: Ie8d5730cc4eabbcc2c907ef49640414fdd4ede4e
(cherry picked from commit 746742259a)
changes/04/25604/1
parent
02bc75b81f
commit
d01abbc99c
@ -0,0 +1,71 @@
|
||||
package NGCP::BulkProcessor::Projects::Export::Ama::Format::Block;
|
||||
use strict;
|
||||
|
||||
## no critic
|
||||
|
||||
require Exporter;
|
||||
our @ISA = qw(Exporter);
|
||||
our @EXPORT_OK = qw(
|
||||
|
||||
);
|
||||
|
||||
my $block_descriptor_word_length = 8;
|
||||
my $max_block_length = 2048; #bytes
|
||||
|
||||
sub new {
|
||||
|
||||
my $class = shift;
|
||||
my $self = bless {}, $class;
|
||||
$self->{records} = [];
|
||||
$self->{padded} = 0;
|
||||
$self->{length} = $block_descriptor_word_length;
|
||||
|
||||
return $self;
|
||||
|
||||
}
|
||||
|
||||
sub add_record {
|
||||
my $self = shift;
|
||||
my $record = shift;
|
||||
my $length;
|
||||
if (not $self->{padded} and ($length = $self->get_length() + $record->get_length()) <= 2 * $max_block_length) {
|
||||
push(@{$self->{records}},$record);
|
||||
$self->{length} = $length;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
sub get_hex {
|
||||
|
||||
my $self = shift;
|
||||
my $result = $self->_get_block_descriptor_word();
|
||||
foreach my $record (@{$self->{records}}) {
|
||||
$result .= $record->get_hex();
|
||||
}
|
||||
if ($self->{padded}) {
|
||||
$result .= 'aa' x (2 * $max_block_length - length($result));
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
sub get_length {
|
||||
my $self = shift;
|
||||
return $self->{length};
|
||||
}
|
||||
|
||||
|
||||
sub _get_block_descriptor_word {
|
||||
my $self = shift;
|
||||
return sprintf('%04x',$self->get_length() / 2) . '0000';
|
||||
#return total length in bytes (up to 256*256 bytes)
|
||||
}
|
||||
|
||||
sub set_padded {
|
||||
my $self = shift;
|
||||
$self->{padded} = shift;
|
||||
}
|
||||
|
||||
1;
|
||||
@ -0,0 +1,176 @@
|
||||
package NGCP::BulkProcessor::Projects::Export::Ama::Format::File;
|
||||
use strict;
|
||||
|
||||
## no critic
|
||||
|
||||
use NGCP::BulkProcessor::Projects::Export::Ama::Format::Block qw();
|
||||
|
||||
use NGCP::BulkProcessor::Logging qw(
|
||||
getlogger
|
||||
);
|
||||
|
||||
use NGCP::BulkProcessor::LogError qw(
|
||||
fileerror
|
||||
);
|
||||
|
||||
require Exporter;
|
||||
our @ISA = qw(Exporter);
|
||||
our @EXPORT_OK = qw(
|
||||
|
||||
);
|
||||
|
||||
my $max_blocks = 99;
|
||||
|
||||
sub new {
|
||||
|
||||
my $class = shift;
|
||||
my $self = bless {}, $class;
|
||||
$self->reset();
|
||||
|
||||
return $self;
|
||||
|
||||
}
|
||||
|
||||
sub reset {
|
||||
my $self = shift;
|
||||
$self->{current_block} = NGCP::BulkProcessor::Projects::Export::Ama::Format::Block->new();
|
||||
$self->{blocks} = [ $self->{current_block} ];
|
||||
$self->{record_count} = 0;
|
||||
|
||||
}
|
||||
|
||||
sub add_record {
|
||||
my $self = shift;
|
||||
my ($record,$pad) = shift;
|
||||
my $result;
|
||||
if (not $self->{current_block}->add_record($record)) {
|
||||
if ((scalar @{$self->{blocks}}) >= $max_blocks) {
|
||||
$result = 0;
|
||||
} else {
|
||||
$self->{current_block}->set_padded(1);
|
||||
$self->{current_block} = NGCP::BulkProcessor::Projects::Export::Ama::Format::Block->new();
|
||||
push(@{$self->{blocks}},$self->{current_block});
|
||||
$result = $self->{current_block}->add_record($record);
|
||||
$self->{record_count} += 1;
|
||||
}
|
||||
} else {
|
||||
$self->{record_count} += 1;
|
||||
$self->{current_block}->set_padded(1) if $pad;
|
||||
$result = 1;
|
||||
}
|
||||
return $result;
|
||||
|
||||
}
|
||||
|
||||
sub get_filename {
|
||||
my $self = shift;
|
||||
return 'test.ama';
|
||||
}
|
||||
|
||||
sub flush {
|
||||
my $self = shift;
|
||||
my %params = @_;
|
||||
my (
|
||||
$commit_cb,
|
||||
) = @params{qw/
|
||||
commit_cb
|
||||
/};
|
||||
#unlink 'test.ama';
|
||||
if ((scalar @{$self->{blocks}}) > 0 and (my $filename = $self->get_filename())) {
|
||||
if (open(my $fh,">:raw",$filename)) {
|
||||
foreach my $block (@{$self->{blocks}}) {
|
||||
print $fh pack('H*',$block->get_hex());
|
||||
}
|
||||
close $fh;
|
||||
&$commit_cb(@_) if defined $commit_cb;
|
||||
#restdebug($self,"$self->{crt_path} saved",getlogger(__PACKAGE__));
|
||||
return 1;
|
||||
} else {
|
||||
fileerror('failed to open ' . $filename . ": $!",getlogger(__PACKAGE__));
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
sub close {
|
||||
|
||||
my $self = shift;
|
||||
my %params = @_;
|
||||
my (
|
||||
$get_transfer_out,
|
||||
$commit_cb,
|
||||
) = @params{qw/
|
||||
get_transfer_out
|
||||
commit_cb
|
||||
/};
|
||||
my $result = 0;
|
||||
$self->add_record(
|
||||
&$get_transfer_out(
|
||||
|
||||
#file_sequence_number => 1,
|
||||
|
||||
#=> (scalar @records),
|
||||
@_
|
||||
),
|
||||
1,
|
||||
);
|
||||
$result |= $self->flush(
|
||||
commit_cb => $commit_cb,
|
||||
@_
|
||||
);
|
||||
$self->reset();
|
||||
return $result;
|
||||
|
||||
}
|
||||
|
||||
sub write_record {
|
||||
|
||||
my $self = shift;
|
||||
my %params = @_;
|
||||
my (
|
||||
$get_transfer_in,
|
||||
$get_record,
|
||||
$get_transfer_out,
|
||||
$commit_cb,
|
||||
) = @params{qw/
|
||||
get_transfer_in
|
||||
get_record
|
||||
get_transfer_out
|
||||
commit_cb
|
||||
/};
|
||||
|
||||
$self->add_record(
|
||||
&$get_transfer_in(
|
||||
#file_sequence_number => 1,
|
||||
@_,
|
||||
),
|
||||
1
|
||||
) unless $self->{record_count} > 0;
|
||||
|
||||
my $result = 0;
|
||||
my $record = &$get_record(@_);
|
||||
if (not $self->add_record($record)) {
|
||||
$result |= $self->close(
|
||||
get_transfer_out => $get_transfer_out,
|
||||
commit_cb => $commit_cb,
|
||||
@_
|
||||
);
|
||||
$self->add_record(
|
||||
&$get_transfer_in(
|
||||
|
||||
#file_sequence_number => 1,
|
||||
@_
|
||||
),
|
||||
1
|
||||
);
|
||||
|
||||
$self->add_record($record);
|
||||
}
|
||||
|
||||
return $result;
|
||||
|
||||
}
|
||||
|
||||
1;
|
||||
Loading…
Reference in new issue