Treex/Core/CacheBlock.pm

filter: Statementstmt 44.0% 26 / 59 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 50.0% 6 / 12 Podpod 75.0% 3 / 4 totaltotal 43.2% 35 / 81 CC 4 SCAR 14.2
CC
4
Cov
40%
process_document
12.0
__ANON__
6.0
BUILD
2.0
CRAP 4.1
« Treex/Core/Zone.pm ↑ Summary Treex/Core/BundleZone.pm »
1 package Treex::Core::CacheBlock;
2 1 $Treex::Core::CacheBlock::VERSION = '2.20151210';
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Treex::Core::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Treex::Core::Resource;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 extends 'Treex::Core::Block';
10  
11 #TODO: params could be obtained directly from block
12  
13 1 has block => ( is => 'ro', isa => 'Treex::Core::Block' );
14  
15 1 has cache => (is => 'ro', isa => 'Cache::Memcached');
16 1 has _loaded => (is => 'rw', isa => 'Bool', default => 0);
17  
18 sub BUILD {
19 0 ▶ my $self = shift;
Subroutine BUILD: not called CC=1 SCAR=6.9
Pod undocumented
20 0 require Treex::Tool::Memcached::Memcached;
21  
22 0 return;
23 }
24  
25 override 'get_hash' => sub {
26 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
27 0 return $self->block->get_hash();
28 1 };
29  
30 sub process_document {
31 0 ▶ my $self = shift;
Subroutine process_document: not called CC=3 SCAR=24.8
Pod documented
32  
33 0 my ($document) = pos_validated_list(
34 \@_,
35 { isa => 'Treex::Core::Document' },
36 );
37  
38 0 $Storable::canonical = 1;
39  
40 # $document->set_hash(md5_hex($document->get_hash() . $self->block->get_hash()));
41  
42 # my $document_hash = md5_hex(Storable::freeze($document));
43 # log_info("CACHE: document_hash\t$document_hash");
44  
45 0 my $full_hash = $document->get_hash();
46 0 my $cached_document = $self->cache->get($full_hash);
47  
48 0 my $return_code = $Treex::Core::Block::DOCUMENT_PROCESSED;
49  
50 0 ▶ if ( ! $cached_document ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $cached_document) { } 0 0 0
51 0 ▶ if ( ! $self->_loaded() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_loaded) 0 0 0
52 0 $self->block->process_start();
53 0 $self->_set_loaded(1);
54 }
55 0 log_info("CACHE: calling process_document " . $self->block->get_block_name());
56 0 $self->block->process_document($document);
57 #my $str = Storable::nfreeze($document);
58 #log_info("CACHE: Storing - $full_hash - $document - " . length($str) . "b");
59 0 log_info("CACHE: Storing - $full_hash - $document");
60 0 $self->cache->set($full_hash, $document);
61 #
62 # my $tmp_doc = $self->cache->get($full_hash);
63 # if ( ! $tmp_doc ) {
64 # sleep(1);
65 # log_info("CACHE: Storing - $full_hash - again!!");
66 # log_info("SET: " . $self->cache->set($full_hash, $document));
67 # log_info("ADD: " . $self->cache->add($full_hash, $document));
68 # my $tmp_doc = $self->cache->get($full_hash);
69 # if ( ! defined($tmp_doc) ) {
70 # my $str = Storable::nfreeze($document);
71 # log_warn("Document size: " . length($str));
72 # }
73 # }
74 # log_fatal("AAAAAAAAAAAAA") if ! $tmp_doc;
75 } else {
76 0 log_info("CACHE: loading from cache " . $self->block->get_block_name());
77 0 $_[0] = $cached_document;
78 0 $return_code = $Treex::Core::Block::DOCUMENT_FROM_CACHE;
79 }
80  
81 0 $Storable::canonical = 0;
82  
83 0 return $return_code;
84 }
85  
86 sub get_required_share_files {
87 0 ▶ my ($self) = @_;
Subroutine get_required_share_files: not called CC=1 SCAR=6.9
Pod documented
88  
89 0 return $self->block->get_required_share_files();
90 }
91  
92 sub process_end {
93 0 ▶ my ($self) = @_;
Subroutine process_end: not called CC=1 SCAR=6.9
Pod documented
94 0 $self->block->process_end();
95 # default implementation is empty, but can be overriden
96 0 return;
97 }
98  
99 override 'get_block_name' => sub {
100 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
101 0 ▶ if ( ! defined($self->block) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->block) { } 0 0 0
102 0 return ref($self);
103 } else {
104 0 return $self->block->get_block_name();
105 }
106 1 };
107  
108 1 1;
109  
110 __END__
111  
112 =encoding utf-8
113  
114 =head1 NAME
115  
116 Treex::Core::CacheBlock - Treex::Core::Block with caching
117  
118 =head1 VERSION
119  
120 version 2.20151210
121  
122 =head1 SYNOPSIS
123  
124 package Treex::Block::My::Block;
125 use Moose;
126 use Treex::Core::Common;
127 extends 'Treex::Core::Block';
128  
129 sub process_bundle {
130 my ( $self, $bundle) = @_;
131  
132 # bundle processing
133  
134 }
135  
136 =head1 DESCRIPTION
137  
138 C<Treex::Core::CacheBlock> is a decorator for any C<Treex::Core::Block>
139 which provides transparen caching.
140  
141 =head1 SEE ALSO
142  
143 L<Treex::Core::Block>,
144  
145 =head1 AUTHOR
146  
147 Martin Majlis <majlis@ufal.mff.cuni.cz>
148  
149 =head1 COPYRIGHT AND LICENSE
150  
151 Copyright © 2012 by Institute of Formal and Applied Linguistics, Charles University in Prague
152  
153 This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
« Treex/Core/Zone.pm ↑ Summary Treex/Core/BundleZone.pm »