Quiq/Hash/Db.pm

filter: Statementstmt 91.3% 21 / 23 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 93.5% 29 / 31 CC 1 SCAR 0.4
CC
1
Cov
91%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Quiq/Web/Navigation.pm ↑ Summary Quiq/ContentProcessor.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Hash::Db - Persistenter Hash
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Hash>
12  
13 =head1 DESCRIPTION
14  
15 Diese Klasse ist eine objektorientierte Überdeckung des Moduls DB_File,
16 das eine Schnittstelle zu Berkeley DB 1.x darstellt.
17  
18 =head1 EXAMPLE
19  
20 Alle Hash-Keys ausgeben ($file ist der Name der Hash-Datei):
21  
22 $ perl -MQuiq::Hash::Db -E '$h = Quiq::Hash::Db->new($file,"r"); for (keys %$h) { say $_}'
23  
24 =cut
25  
26 # -----------------------------------------------------------------------------
27  
28 package Quiq::Hash::Db;
29 4 ▶use base qw/Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 4 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 our $VERSION = '1.240';
37  
38 4 ▶use Quiq::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 4 ▶use Fcntl ();
Subroutine BEGIN: called CC=1 SCAR=0.0
40 0 ▶use DB_File ();
Subroutine BEGIN: called CC=1 SCAR=2.6
41  
42 # -----------------------------------------------------------------------------
43  
44 =head1 METHODS
45  
46 =head2 Konstruktor
47  
48 =head3 new() - Konstruktor
49  
50 =head4 Synopsis
51  
52 my $h = $class->new($file,$mode);
53  
54 =head4 Arguments
55  
56 =over 4
57  
58 =item $file
59  
60 Datei, in dem der Hash gespeichert wird.
61  
62 =item $mode
63  
64 Modus, in dem die Datei geöffnet wird:
65  
66 Mode Bedeutung
67 ---- --------------------------------------------------------------
68 r nur lesen, Datei muss existieren
69 w nur schreiben, Datei wird angelegt, falls nicht existent
70 rw lesen und schreiben, Datei wird angelegt, falls nicht existent
71  
72 =back
73  
74 =head4 Returns
75  
76 Referenz auf das Hash-Objekt.
77  
78 =head4 Description
79  
80 Öffne einen Hash mit Datei $file als persistentem Speicher
81 im Modus $mode und liefere eine Referenz auf das Objekt zurück.
82  
83 =cut
84  
85 # -----------------------------------------------------------------------------
86  
87 sub new {
88 my ($class,$file,$mode) = @_;
89  
90 $file = Quiq::Path->expandTilde($file);
91  
92 my $openMode = 0;
93 if ($mode eq 'rw') {
94 $openMode = Fcntl::O_RDWR|Fcntl::O_CREAT;
95 }
96 elsif ($mode eq 'r') {
97 $openMode = Fcntl::O_RDONLY;
98 }
99 elsif ($mode eq 'w') {
100 $openMode = Fcntl::O_WRONLY|Fcntl::O_CREAT;
101 }
102 else {
103 $class->throw(
104 'BDB-00001: Unbekannter Mode',
105 Mode => $mode,
106 );
107 }
108  
109 my %hash;
110 my $ref = tie %hash,'DB_File',$file,$openMode,0644,
111 $DB_File::DB_HASH;
112 unless ($ref) {
113 $class->throw(
114 'BDB-00001: Kann Persistenten Hash nicht öffnen',
115 File => $file,
116 Mode => $mode,
117 Errstr => $!,
118 );
119 }
120  
121 return bless \%hash,$class;
122 }
123  
124 # -----------------------------------------------------------------------------
125  
126 =head2 Objektmethoden
127  
128 =head3 sync() - Schreibe Cache-Daten auf Platte
129  
130 =head4 Synopsis
131  
132 $h->sync;
133  
134 =cut
135  
136 # -----------------------------------------------------------------------------
137  
138 sub sync {
139 my $self = shift;
140  
141 my $ref = tied %$self || $self->throw(
142 'BDB-00002: Kann Tie-Objekt nicht ermitteln',
143 );
144 my $r = $ref->sync;
145 if ($r < 0) {
146 $self->throw(
147 'BDB-00003: Sync ist fehlgeschlagen',
148 TiedObject => "$ref",
149 Errcode => $r,
150 Errstr => $!,
151 );
152 }
153  
154 return;
155 }
156  
157 # -----------------------------------------------------------------------------
158  
159 =head3 close() - Schließe Persistenten Hash
160  
161 =head4 Synopsis
162  
163 $h->close;
164  
165 =head4 Description
166  
167 Schreibe den Persistenten Hash auf Platte und zerstöre das Objekt.
168 Das gleiche geschieht, wenn die letzte Referenz auf das Objekt aus
169 dem Scope geht.
170  
171 =cut
172  
173 # -----------------------------------------------------------------------------
174  
175 sub close {
176 untie %{$_[0]};
177 $_[0] = undef;
178 return;
179 }
180  
181 # -----------------------------------------------------------------------------
182  
183 =head1 VERSION
184  
185 1.240
186  
187 =head1 AUTHOR
188  
189 Frank Seitz, L<http://fseitz.de/>
190  
191 =head1 COPYRIGHT
192  
193 Copyright (C) 2026 Frank Seitz
194  
195 =head1 LICENSE
196  
197 This code is free software; you can redistribute it and/or modify
198 it under the same terms as Perl itself.
199  
200 =cut
201  
202 # -----------------------------------------------------------------------------
203  
204 1;
205  
206 # eof
« Quiq/Web/Navigation.pm ↑ Summary Quiq/ContentProcessor.pm »