lib/LaTeXML/Util/ObjectDB.pm untested

filter: Statementstmt 0.0% 0 / 132 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 37 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 264 CC 21 SCAR 27.3
CC
21
Cov
0%
compare
42.0
finish
30.0
lookup
20.0
CRAP 15.3
1 # /=====================================================================\ #
2 # | LaTeXML::Util::ObjectDB | #
3 # | Database of Objects for crossreferencing, etc | #
4 # |=====================================================================| #
5 # | Part of LaTeXML: | #
6 # | Public domain software, produced as part of work done by the | #
7 # | United States Government & not subject to copyright in the US. | #
8 # |---------------------------------------------------------------------| #
9 # | Bruce Miller <bruce.miller@nist.gov> #_# | #
10 # | http://dlmf.nist.gov/LaTeXML/ (o o) | #
11 # \=========================================================ooo==U==ooo=/ #
12 package LaTeXML::Util::ObjectDB;
13 use strict;
14 use warnings;
15 use LaTeXML::Util::Pathname;
16 use DB_File;
17 use Storable qw(nfreeze thaw);
18 use strict;
19 use Encode;
20 use Carp;
21 use base qw(Storable);
22 use LaTeXML::Util::ObjectDB::Entry;
23  
24 #======================================================================
25 # NOTES:
26 # (1) If we can do make-like processing, when an entry is marked as
27 # modified, any referrers to it also need processing.
28 # (and we could defer a save if nothing was dirty)
29 #======================================================================
30 # Some Definitions:
31 # * Object: places a link will take you to. Several types
32 # * chunk: any significant document object with a reference
33 # number: sectional chunks, equations, ...
34 # * index : the target is the entry in the index itself.
35 # a back reference can take you to where the \index was invoked.
36 # * bib : the target is the entry in the bibliography.
37 # a back reference can take you to the \cite.
38 #
39 #======================================================================
40  
41 #======================================================================
42 # Creating an ObjectDB object, hooking up initial database.
43 sub new {
44 my ($class, %options) = @_;
45 my $dbfile = $options{dbfile};
46 if ($dbfile && $options{clean}) {
47 warn "\nWARN: Removing Object database file $dbfile!!!\n";
48 unlink($dbfile); }
49  
50 my $self = bless { dbfile => $dbfile,
51 objects => {}, externaldb => {},
52 verbosity => $options{verbosity} || 0,
53 readonly => $options{readonly},
54 }, $class;
55 if ($dbfile) {
56 ## my $flags = ($options{read_write} ? O_RDWR|O_CREAT : O_RDONLY);
57 my $flags = O_RDWR | O_CREAT;
58 tie %{ $$self{externaldb} }, 'DB_File', $dbfile, $flags
59 or die "Couldn't attach DB $dbfile for object table";
60 }
61 return $self; }
62  
63 sub DESTROY {
64 my ($self) = @_;
65 $self->finish;
66 return; }
67  
68 sub status {
69 my ($self) = @_;
70 my $status = scalar(keys %{ $$self{objects} }) . "/" . scalar(keys %{ $$self{externaldb} }) . " objects";
71 # if($$self{dbfile}){ ...
72 return $status; }
73  
74 #======================================================================
75 # This saves the db
76  
77 sub finish {
78 my ($self) = @_;
79 if ($$self{externaldb} && $$self{dbfile} && !$$self{readonly}) {
80 my $n = 0;
81 my %types = ();
82 foreach my $key (keys %{ $$self{objects} }) {
83 my $row = $$self{objects}{$key};
84 # Skip saving, unless there's some difference between stored value
85 if (my $stored = $$self{externaldb}{ Encode::encode('utf8', $key) }) { # Get the external object
86 next if compare_hash($row, thaw($stored)); }
87 $n++;
88 my %item = %$row;
89 $$self{externaldb}{ Encode::encode('utf8', $key) } = nfreeze({%item}); }
90  
91 print STDERR "ObjectDB Stored $n objects (" . scalar(keys %{ $$self{externaldb} }) . " total)\n"
92 if $$self{verbosity} > 0;
93 untie %{ $$self{externaldb} }; }
94  
95 $$self{externaldb} = undef;
96 $$self{objects} = undef;
97 return }
98  
99 sub compare {
100 my ($a, $b) = @_;
101 my $ra = ref $a;
102 if (!$ra) {
103 if (ref $b) {
104 return 0; }
105 else {
106 return compare_scalar($a, $b); } }
107 elsif ($ra ne ref $b) {
108 return 0; }
109 elsif ($ra eq 'HASH') {
110 return compare_hash($a, $b); }
111 elsif ($ra eq 'ARRAY') {
112 return compare_array($a, $b); }
113 else {
114 return compare_scalar($a, $b); } }
115  
116 sub compare_scalar {
117 my ($a, $b) = @_;
118 return ((!defined $a) && (!defined $b)) ||
119 (defined $a && defined $b && $a eq $b); }
120  
121 sub compare_hash {
122 my ($a, $b) = @_;
123 my %attr = ();
124 map { $attr{$_} = 1 } keys %$a;
125 map { $attr{$_} = 1 } keys %$b;
126 return (grep { !((exists $$a{$_}) && (exists $$b{$_}) && compare($$a{$_}, $$b{$_})) }
127 keys %attr) ? 0 : 1; }
128  
129 sub compare_array {
130 my ($a, $b) = @_;
131 my @a = @$a;
132 my @b = @$b;
133 while (@a && @b) {
134 return 0 unless compare(shift(@a), shift(@b)); }
135 return (@a || @b ? 0 : 1); }
136  
137 #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138 sub getKeys {
139 my ($self) = @_;
140 # Get union of all keys in externaldb & local objects.
141 my %keys = ();
142 map { $keys{$_} = 1 } keys %{ $$self{objects} };
143 map { $keys{ Encode::decode('utf8', $_) } = 1 } keys %{ $$self{externaldb} };
144 return (sort keys %keys); }
145  
146 #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
147 # Lookup of various kinds of things in the DB.
148 #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149  
150 # Lookup the Object associated with label
151 # If it is not already fetched from the external db (if any), fetch it now.
152  
153 sub lookup {
154 my ($self, $key) = @_;
155 return unless defined $key;
156 my $entry = $$self{objects}{$key}; # Get the local copy.
157 return $entry if $entry;
158 $entry = $$self{externaldb}{ Encode::encode('utf8', $key) }; # Get the external object
159 if ($entry) {
160 $entry = thaw($entry);
161 $$entry{key} = $key;
162 bless $entry, 'LaTeXML::Util::ObjectDB::Entry';
163 $$self{objects}{$key} = $entry; }
164 return $entry; }
165  
166 #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
167 # Register various interesting document nodes.
168 #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
169 # Register the labeled object $node, creating, or filling in, and
170 # returning a Chunk entry.
171 sub register {
172 my ($self, $key, %props) = @_;
173 carp("Missing key for object!") unless $key;
174 my $entry = $self->lookup($key);
175 if (!$entry) {
176 $entry = { key => $key };
177 bless $entry, 'LaTeXML::Util::ObjectDB::Entry';
178 $$self{objects}{$key} = $entry; }
179 $entry->setValues(%props);
180  
181 return $entry; }
182  
183 sub unregister {
184 my ($self, $key) = @_;
185 delete $$self{objects}{$key};
186 # Must remove external entry (if any) as well, else it'll get pulled back in!
187 delete $$self{externaldb}{ Encode::encode('utf8', $key) } if $$self{externaldb};
188 return; }
189  
190 #======================================================================
191 1;