| 1 | package Hailo::Storage::SQLite; | ||||||||||||||||||||||||||||||||||
| 2 | 28 | our $AUTHORITY = 'cpan:AVAR'; | |||||||||||||||||||||||||||||||||
| 3 | 28 | $Hailo::Storage::SQLite::VERSION = '0.73'; | |||||||||||||||||||||||||||||||||
| 4 | 28 | ▶ | use v5.28.0; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | 28 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | 28 | ▶ | use MooseX::StrictConstructor; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | 28 | ▶ | use namespace::clean -except => 'meta'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||
| 9 | 28 | extends 'Hailo::Storage'; | |||||||||||||||||||||||||||||||||
| 10 | 28 | with qw(Hailo::Role::Arguments Hailo::Role::Storage); | |||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||
| 12 | 249 | ▶ | sub _build_dbd { return 'SQLite' }; | ||||||||||||||||||||||||||||||||
|
Subroutine
_build_dbd: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||
| 14 | override _build_dbd_options => sub { | ||||||||||||||||||||||||||||||||||
| 15 | return { | ||||||||||||||||||||||||||||||||||
| 16 | 250 | ▶ | %{ super() }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 17 | sqlite_unicode => 1, | ||||||||||||||||||||||||||||||||||
| 18 | }; | ||||||||||||||||||||||||||||||||||
| 19 | 28 | }; | |||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||
| 21 | around _build_dbi_options => sub { | ||||||||||||||||||||||||||||||||||
| 22 | 249 | ▶ | my $orig = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 23 | 249 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||
| 25 | 249 | my $return; | |||||||||||||||||||||||||||||||||
| 26 | 249 | ▶ | if ($self->_backup_memory_to_disk) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 27 | 2 | my $file = $self->brain; | |||||||||||||||||||||||||||||||||
| 28 | 2 | $self->brain(':memory:'); | |||||||||||||||||||||||||||||||||
| 29 | 2 | $return = $self->$orig(@_); | |||||||||||||||||||||||||||||||||
| 30 | 2 | $self->brain($file); | |||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||
| 32 | else { | ||||||||||||||||||||||||||||||||||
| 33 | 247 | $return = $self->$orig(@_); | |||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||
| 36 | 249 | return $return; | |||||||||||||||||||||||||||||||||
| 37 | 28 | }; | |||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||
| 39 | # Are we running in a mixed mode where we run in memory but | ||||||||||||||||||||||||||||||||||
| 40 | # restore/backup to disk? | ||||||||||||||||||||||||||||||||||
| 41 | sub _backup_memory_to_disk { | ||||||||||||||||||||||||||||||||||
| 42 | 552 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
_backup_memory_to_disk: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||
| 44 | return (defined $self->brain | ||||||||||||||||||||||||||||||||||
| 45 | and $self->brain ne ':memory:' | ||||||||||||||||||||||||||||||||||
| 46 | 552 | ▶ | and $self->arguments->{in_memory}); | ||||||||||||||||||||||||||||||||
|
Condition100%6/6
defined $self->brain && $self->brain ne ':memory:' && $self->arguments->{'in_memory'}
defined $self->brain and $self->brain ne ":memory:"
MC/DC100%3/3
defined $self->brain && $self->brain ne ':memory:' && $self->arguments->{'in_memory'}
defined $self->brain
$self->brain ne ":memory:"
$self->arguments->{'in_memory'}
Truth table100%4/4
A and B && C
A= defined $self->brainB= $self->brain ne ":memory:"C= $self->arguments->{'in_memory'}
| |||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||
| 49 | before _engage => sub { | ||||||||||||||||||||||||||||||||||
| 50 | 251 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||
| 52 | # Set any user-defined pragmas | ||||||||||||||||||||||||||||||||||
| 53 | 251 | $self->_set_pragmas; | |||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||
| 55 | 251 | ▶ | if ($self->_backup_memory_to_disk) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 56 | 2 | $self->dbh->sqlite_backup_from_file($self->brain); | |||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||
| 59 | 251 | return; | |||||||||||||||||||||||||||||||||
| 60 | 28 | }; | |||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||
| 62 | before start_training => sub { | ||||||||||||||||||||||||||||||||||
| 63 | 23 | ▶ | my $dbh = shift->dbh; | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 64 | 23 | $dbh->do('PRAGMA synchronous=OFF;'); | |||||||||||||||||||||||||||||||||
| 65 | 23 | $dbh->do('PRAGMA journal_mode=OFF;'); | |||||||||||||||||||||||||||||||||
| 66 | 23 | return; | |||||||||||||||||||||||||||||||||
| 67 | 28 | }; | |||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||
| 69 | after stop_training => sub { | ||||||||||||||||||||||||||||||||||
| 70 | 19 | ▶ | my $dbh = shift->dbh; | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 71 | 19 | $dbh->do('PRAGMA journal_mode=DELETE;'); | |||||||||||||||||||||||||||||||||
| 72 | 19 | $dbh->do('PRAGMA synchronous=ON;'); | |||||||||||||||||||||||||||||||||
| 73 | 19 | return; | |||||||||||||||||||||||||||||||||
| 74 | 28 | }; | |||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||
| 76 | override initialized => sub { | ||||||||||||||||||||||||||||||||||
| 77 | 251 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||
| 79 | 251 | my $brain = $self->brain; | |||||||||||||||||||||||||||||||||
| 80 | 251 | ▶ | return unless defined $brain; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 81 | 240 | ▶ | return if $brain eq ':memory:'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 82 | 214 | ▶ | return -e $brain && super(); | ||||||||||||||||||||||||||||||||
|
Condition66%2/3
-e $brain && super()
MC/DC50%1/2
-e $brain && super()
-e $brain
super()
Truth table66%2/3
A && B
A= -e $brainB= super()
| |||||||||||||||||||||||||||||||||||
| 83 | 28 | }; | |||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||
| 85 | sub ready { | ||||||||||||||||||||||||||||||||||
| 86 | 22 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
ready: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 87 | 22 | my $brain = $self->brain; | |||||||||||||||||||||||||||||||||
| 88 | 22 | ▶ | return unless defined $self->brain; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 89 | 18 | ▶ | return 1 if $self->brain eq ':memory:'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 90 | 2 | return 1; | |||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||
| 93 | sub _set_pragmas { | ||||||||||||||||||||||||||||||||||
| 94 | 251 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
_set_pragmas: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||
| 96 | 251 | my %pragmas; | |||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||
| 98 | # speedy defaults when DB is not kept in memory | ||||||||||||||||||||||||||||||||||
| 99 | 251 | ▶ | if (!$self->{in_memory}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 100 | 251 | $pragmas{synchronous} = 'OFF'; | |||||||||||||||||||||||||||||||||
| 101 | 251 | $pragmas{journal_mode} = 'OFF'; | |||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||
| 104 | 251 | ▶ | while (my ($k, $v) = each %{ $self->arguments }) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 105 | 19 | ▶ | if (my ($pragma) = $k =~ /^pragma_(.*)/) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 106 | 5 | $pragmas{$pragma} = $v; | |||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||
| 110 | 251 | ▶ | while (my ($k, $v) = each %pragmas) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 111 | 505 | $self->dbh->do(qq[PRAGMA $k="$v";]) | |||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||
| 114 | 251 | return; | |||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||
| 117 | sub save { | ||||||||||||||||||||||||||||||||||
| 118 | 254 | ▶ | my ($self, $filename) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
save: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 119 | 254 | ▶ | my $file = $filename // $self->brain; | ||||||||||||||||||||||||||||||||
|
Condition66%2/3
$filename // $self->brain
MC/DC50%1/2
$filename // $self->brain
$filename
$self->brain
Truth table66%2/3
A // B
A= $filenameB= $self->brain
| |||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||
| 121 | 254 | ▶ | return unless $self->_engaged; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 122 | 49 | ▶ | if ($self->_backup_memory_to_disk) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 123 | 1 | $self->dbh->sqlite_backup_to_file($file); | |||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||
| 125 | 49 | return; | |||||||||||||||||||||||||||||||||
| 126 | }; | ||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||
| 128 | 28 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||
| 130 | =encoding utf8 | ||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||
| 132 | =head1 NAME | ||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||
| 134 | Hailo::Storage::SQLite - A storage backend for L<Hailo|Hailo> using L<DBD::SQLite> | ||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||
| 136 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||
| 138 | As a module: | ||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||
| 140 | my $hailo = Hailo->new( | ||||||||||||||||||||||||||||||||||
| 141 | storage_class => 'SQLite', | ||||||||||||||||||||||||||||||||||
| 142 | ); | ||||||||||||||||||||||||||||||||||
| 143 | $hailo->train("hailo.trn"); | ||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||
| 145 | From the command line: | ||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||
| 147 | hailo --train hailo.trn --storage SQLite | ||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||
| 149 | See L<Hailo's documentation|Hailo> for other non-MySQL specific options. | ||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||
| 151 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||
| 153 | This backend maintains information in an SQLite database. It is the default | ||||||||||||||||||||||||||||||||||
| 154 | storage backend. | ||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||
| 156 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||
| 158 | =head2 C<storage_args> | ||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||
| 160 | This is a hash reference which can have the following keys: | ||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||
| 162 | =head3 C<pragma_*> | ||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||
| 164 | Any option starting with B<'pragma_'> will be considered to be an L<SQLite | ||||||||||||||||||||||||||||||||||
| 165 | pragma|http://www.sqlite.org/pragma.html> which will be set after we connect | ||||||||||||||||||||||||||||||||||
| 166 | to the database. An example of this would be | ||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||
| 168 | storage_args => { | ||||||||||||||||||||||||||||||||||
| 169 | pragma_cache_size => 10000, | ||||||||||||||||||||||||||||||||||
| 170 | pragma_synchronous => 'OFF', | ||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||
| 173 | Setting B<'pragma_cache_size'> in particular can be beneficial. It's the | ||||||||||||||||||||||||||||||||||
| 174 | size of the page cache used by SQLite. See L<SQLite's | ||||||||||||||||||||||||||||||||||
| 175 | documentation|http://www.sqlite.org/pragma.html#pragma_cache_size> for | ||||||||||||||||||||||||||||||||||
| 176 | more information. | ||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||
| 178 | Increasing it might speed up Hailo, especially when disk IO is slow on | ||||||||||||||||||||||||||||||||||
| 179 | your machine. Obviously, you shouldn't bother with this option if | ||||||||||||||||||||||||||||||||||
| 180 | L<B<'in_memory'>|/in_memory> is enabled. | ||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||
| 182 | Setting B<'pragma_synchronous'> to B<'OFF'> or B<'pragma_journal_mode'> | ||||||||||||||||||||||||||||||||||
| 183 | to B<'OFF'> will speed up operations at the expense of safety. Since Hailo | ||||||||||||||||||||||||||||||||||
| 184 | is most likely not running as a mission-critical component this trade-off | ||||||||||||||||||||||||||||||||||
| 185 | should be acceptable in most cases. If the database becomes corrupt | ||||||||||||||||||||||||||||||||||
| 186 | it's easy to rebuild it by retraining from the input it was trained on | ||||||||||||||||||||||||||||||||||
| 187 | to begin with. For performance reasons, these two are set to B<'OFF'> | ||||||||||||||||||||||||||||||||||
| 188 | by default unless L<B<'in_memory'>|/in_memory> is enabled. | ||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||
| 190 | =head3 C<in_memory> | ||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||
| 192 | When set to a true value, Hailo behaves much like MegaHAL. The entire | ||||||||||||||||||||||||||||||||||
| 193 | database will be kept in memory, and only written out to disk when the | ||||||||||||||||||||||||||||||||||
| 194 | L<C<save>|Hailo/save> method is called and/or when the Hailo object gets | ||||||||||||||||||||||||||||||||||
| 195 | destroyed (unless you disabled | ||||||||||||||||||||||||||||||||||
| 196 | L<C<save_on_exit>|Hailo/save_on_exit>). This is disabled by default. | ||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||
| 198 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||
| 200 | E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason <avar@cpan.org> | ||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||
| 202 | Hinrik E<Ouml>rn SigurE<eth>sson, hinrik.sig@gmail.com | ||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||
| 204 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||
| 206 | Copyright 2010 E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason and | ||||||||||||||||||||||||||||||||||
| 207 | Hinrik E<Ouml>rn SigurE<eth>sson | ||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||
| 209 | This program is free software, you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||||
| 210 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||
| 212 | =cut | ||||||||||||||||||||||||||||||||||