| 1 | # Iterator version of Brackup::Dict::SQLite - slower, but lighter on memory | ||
| 2 | |||
| 3 | package Brackup::Dict::SQLite2; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use DBI; | ||
| 7 | use DBD::SQLite; | ||
| 8 | |||
| 9 | sub new { | ||
| 10 | my ($class, %opts) = @_; | ||
| 11 | my $self = bless { | ||
| 12 | table => $opts{table}, | ||
| 13 | file => $opts{file}, | ||
| 14 | }, $class; | ||
| 15 | |||
| 16 | my $dbh = $self->{dbh} = DBI->connect("dbi:SQLite:dbname=$opts{file}","","", { RaiseError => 1, PrintError => 0 }) or | ||
| 17 | die "Failed to connect to SQLite filesystem digest cache database at $opts{file} " . DBI->errstr; | ||
| 18 | |||
| 19 | eval { | ||
| 20 | $dbh->do("CREATE TABLE $opts{table} (key TEXT PRIMARY KEY, value TEXT)"); | ||
| 21 | }; | ||
| 22 | die "Error: $@" if $@ && $@ !~ /table \w+ already exists/; | ||
| 23 | |||
| 24 | $self->{get_sth} = $self->{dbh}->prepare("SELECT value FROM $self->{table} WHERE key = ?"); | ||
| 25 | |||
| 26 | return $self; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub get { | ||
| 30 | my ($self, $key) = @_; | ||
| 31 | $self->{get_sth}->bind_param(1, $key); | ||
| 32 | $self->{get_sth}->execute; | ||
| 33 | my ($val) = $self->{get_sth}->fetchrow_array; | ||
| 34 | return $val; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub set { | ||
| 38 | my ($self, $key, $val) = @_; | ||
| 39 | $self->{dbh}->do("REPLACE INTO $self->{table} VALUES (?,?)", undef, $key, $val); | ||
| 40 | return 1; | ||
| 41 | } | ||
| 42 | |||
| 43 | # Iterator interface, returning ($key, $value), and () on eod | ||
| 44 | sub each { | ||
| 45 | my $self = shift; | ||
| 46 | if (! $self->{each_sth}) { | ||
| 47 | $self->{each_sth} = $self->{dbh}->prepare("SELECT key, value from $self->{table}"); | ||
| 48 | $self->{each_sth}->execute; | ||
| 49 | } | ||
| 50 | my ($k, $v) = $self->{each_sth}->fetchrow_array; | ||
| 51 | return wantarray ? ($k, $v) : $k if defined $k; | ||
| 52 | return wantarray ? () : undef; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub delete { | ||
| 56 | my ($self, $key) = @_; | ||
| 57 | $self->{dbh}->do("DELETE FROM $self->{table} WHERE key = ?", undef, $key); | ||
| 58 | return 1; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub count { | ||
| 62 | my $self = shift; | ||
| 63 | my ($count) = $self->{dbh}->selectrow_array("SELECT COUNT(key) FROM $self->{table}"); | ||
| 64 | return $count; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub backing_file { | ||
| 68 | my $self = shift; | ||
| 69 | return $self->{file}; | ||
| 70 | } | ||
| 71 | |||
| 72 | 1; | ||
| 73 | |||
| 74 | =head1 NAME | ||
| 75 | |||
| 76 | Brackup::Dict::SQLite2 - key-value dictionary implementation, using a | ||
| 77 | SQLite database for storage (lighter/slower version of | ||
| 78 | Brackup::Dict::SQLite) | ||
| 79 | |||
| 80 | =head1 DESCRIPTION | ||
| 81 | |||
| 82 | Brackup::Dict::SQLite2 implements a simple key-value dictionary using | ||
| 83 | a SQLite database for storage. Brackup::Dict::SQLite2 is identical to | ||
| 84 | L<Brackup::Dict::SQLite> (so see that for more details), but it uses | ||
| 85 | conventional database cursors/iterators for all operations, instead of | ||
| 86 | pre-loading the entire database into memory. As such, it is slightly | ||
| 87 | slower than Brackup::Dict::SQLite, but uses much less memory. | ||
| 88 | |||
| 89 | See L<Brackup::DigestCache> and L<Brackup::InventoryDatabase> for | ||
| 90 | how to manually specify the dictionary class to use. | ||
| 91 | |||
| 92 | =head1 SEE ALSO | ||
| 93 | |||
| 94 | L<brackup> | ||
| 95 | |||
| 96 | L<Brackup> | ||
| 97 | |||
| 98 | L<Brackup::Dict::SQLite> | ||
| 99 | |||
| 100 | =cut |