BackPAN/Index/Database.pm

filter: Statementstmt 100% 56 / 56 Branchbran 50.0% 3 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 17 / 17 Podpod 0.0% 0 / 7 totaltotal 88.3% 76 / 86 CC 6 SCAR 4.5
CC
6
Cov
95%
should_update_db
3.1
cache_is_old
2.0
create_tables
2.0
CRAP 1.6
« BackPAN/Index/Role/AsHash.pm ↑ Summary BackPAN/Index/Role/HasCache.pm »
1 package BackPAN::Index::Database;
2  
3 10 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 with 'BackPAN::Index::Role::HasCache';
5  
6 10 ▶use BackPAN::Index::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 has db_file =>
10 is => 'ro',
11 isa => 'Path::Class::File',
12 lazy => 1,
13 coerce => 1,
14 10 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
15 10 my $self = shift;
16 10 return Path::Class::File->new($self->cache->directory, "backpan.sqlite").'';
17 10 };
18  
19 has dsn =>
20 is => 'ro',
21 isa => 'Str',
22 lazy => 1,
23 10 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
24 10 my $self = shift;
25 10 return "dbi:SQLite:dbname=@{[$self->db_file]}";
26 10 };
27  
28 has dbh =>
29 is => 'ro',
30 isa => 'DBI::db',
31 lazy => 1,
32 10 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
33 10 my $self = shift;
34 10 require DBI;
35 10 return DBI->connect($self->dsn, undef, undef, {RaiseError => 1});
36 10 };
37  
38 has schema =>
39 is => 'ro',
40 isa => 'DBIx::Class::Schema',
41 lazy => 1,
42 10 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
43 10 my $self = shift;
44  
45 10 require BackPAN::Index::Schema;
46 10 ▶ return BackPAN::Index::Schema->connect(sub { $self->dbh });
Subroutine __ANON__: called CC=1 SCAR=0.0
47 10 };
48  
49 # If you change the schema, be sure to run ./Build result_classes
50 # to update the result classes.
51 #
52 # This is denormalized for performance, its read-only anyway
53 has create_tables_sql =>
54 is => 'ro',
55 isa => 'HashRef[Str]',
56 10 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
57 return {
58 10 files => <<'SQL',
59 CREATE TABLE IF NOT EXISTS files (
60 path TEXT NOT NULL PRIMARY KEY,
61 date INTEGER NOT NULL,
62 size INTEGER NOT NULL CHECK ( size >= 0 )
63 )
64 SQL
65 releases => <<'SQL',
66 CREATE TABLE IF NOT EXISTS releases (
67 path TEXT NOT NULL PRIMARY KEY REFERENCES files,
68 dist TEXT NOT NULL REFERENCES dists,
69 date INTEGER NOT NULL,
70 size TEXT NOT NULL,
71 version TEXT NOT NULL,
72 maturity TEXT NOT NULL,
73 distvname TEXT NOT NULL,
74 cpanid TEXT NOT NULL
75 )
76 SQL
77  
78 dists => <<'SQL',
79 CREATE TABLE IF NOT EXISTS dists (
80 name TEXT NOT NULL PRIMARY KEY,
81 first_release TEXT NOT NULL REFERENCES releases,
82 latest_release TEXT NOT NULL REFERENCES releases,
83 first_date INTEGER NOT NULL,
84 latest_date INTEGER NOT NULL,
85 first_author TEXT NOT NULL,
86 latest_author TEXT NOT NULL,
87 num_releases INTEGER NOT NULL
88 )
89 SQL
90 }
91 10 };
92  
93 has create_indexes_sql =>
94 is => 'ro',
95 isa => 'ArrayRef[Str]',
96 10 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
97 return [
98 # Speed up dists_by several orders of magnitude
99 10 "CREATE INDEX IF NOT EXISTS dists_by ON releases (cpanid, dist)",
100  
101 # Speed up files_by a lot
102 "CREATE INDEX IF NOT EXISTS files_by ON releases (cpanid, path)",
103  
104 # Let us order releases by date quickly
105 "CREATE INDEX IF NOT EXISTS releases_by_date ON releases (date, dist)",
106 ]
107 10 };
108  
109 1 ▶sub create_tables {
Subroutine create_tables: called CC=2 SCAR=6.9
Pod undocumented
110 1 my $self = shift;
111  
112 1 my $dbh = $self->dbh;
113  
114 1 for my $sql (values %{$self->create_tables_sql}) {
115 3 $dbh->do($sql);
116 }
117  
118 1 return;
119 }
120  
121 1 ▶sub create_indexes {
Subroutine create_indexes: called CC=2 SCAR=6.9
Pod undocumented
122 1 my $self = shift;
123  
124 1 my $dbh = $self->dbh;
125 1 for my $sql (@{$self->create_indexes_sql}) {
126 3 $dbh->do($sql);
127 }
128  
129 1 return;
130 }
131  
132 9 ▶sub db_file_exists {
Subroutine db_file_exists: called CC=1 SCAR=0.0
Pod undocumented
133 9 my $self = shift;
134 9 return -e $self->db_file;
135 }
136  
137 9 ▶sub should_update_db {
Subroutine should_update_db: called CC=3 SCAR=11.4
Pod undocumented
138 9 my $self = shift;
139  
140 9 ▶ return 1 if !$self->db_file_exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->db_file_exists 0 9 9
141 9 ▶ return 1 if $self->cache_is_old;
Branch: 1 branch
BranchTrueFalseTotal
if $self->cache_is_old 0 9 9
142 9 return 0;
143 }
144  
145 9 ▶sub cache_is_old {
Subroutine cache_is_old: called CC=2 SCAR=7.1
Pod undocumented
146 9 my $self = shift;
147  
148 9 ▶ return 1 if $self->db_age > $self->cache->ttl;
Branch: 1 branch
BranchTrueFalseTotal
if $self->db_age > $self->cache->ttl 0 9 9
149 9 return 0;
150 }
151  
152 18 ▶sub db_mtime {
Subroutine db_mtime: called CC=1 SCAR=0.0
Pod undocumented
153 18 my $self = shift;
154  
155 # XXX Should probably just put a timestamp in the DB
156 18 return $self->db_file->stat->mtime;
157 }
158  
159 9 ▶sub db_age {
Subroutine db_age: called CC=1 SCAR=0.0
Pod undocumented
160 9 my $self = shift;
161  
162 9 return time - $self->db_mtime;
163 }
164  
165 10 1;
« BackPAN/Index/Role/AsHash.pm ↑ Summary BackPAN/Index/Role/HasCache.pm »