| 1 | package Antispam::Toolkit::Role::BerkeleyDB; | ||
| 2 | BEGIN { | ||
| 3 | $Antispam::Toolkit::Role::BerkeleyDB::VERSION = '0.08'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use autodie; | ||
| 9 | use namespace::autoclean; | ||
| 10 | |||
| 11 | use Antispam::Toolkit::Types qw( Bool File NonEmptyStr DataFile ); | ||
| 12 | use BerkeleyDB; | ||
| 13 | use DateTime; | ||
| 14 | |||
| 15 | BEGIN { | ||
| 16 | die 'The ' . __PACKAGE__ . ' role requires a BerkeleyDB linked against Berkeley DB 4.4+' | ||
| 17 | unless $BerkeleyDB::db_version >= 4.4; | ||
| 18 | } | ||
| 19 | |||
| 20 | use Moose::Role; | ||
| 21 | use MooseX::Params::Validate qw( validated_list ); | ||
| 22 | |||
| 23 | with 'Antispam::Toolkit::Role::Database'; | ||
| 24 | |||
| 25 | has database => ( | ||
| 26 | is => 'ro', | ||
| 27 | isa => File, | ||
| 28 | coerce => 1, | ||
| 29 | required => 1, | ||
| 30 | ); | ||
| 31 | |||
| 32 | has name => ( | ||
| 33 | is => 'ro', | ||
| 34 | isa => NonEmptyStr, | ||
| 35 | lazy => 1, | ||
| 36 | builder => '_build_name', | ||
| 37 | ); | ||
| 38 | |||
| 39 | has _db => ( | ||
| 40 | is => 'ro', | ||
| 41 | isa => 'BerkeleyDB::Hash', | ||
| 42 | init_arg => undef, | ||
| 43 | lazy => 1, | ||
| 44 | builder => '_build_db', | ||
| 45 | ); | ||
| 46 | |||
| 47 | sub _build_db { | ||
| 48 | my $self = shift; | ||
| 49 | |||
| 50 | die "The database file must already exist" | ||
| 51 | unless -f $self->database(); | ||
| 52 | |||
| 53 | my $env = BerkeleyDB::Env->new( | ||
| 54 | -Home => $self->database()->dir(), | ||
| 55 | -Flags => DB_INIT_CDB | DB_INIT_MPOOL, | ||
| 56 | ); | ||
| 57 | |||
| 58 | return BerkeleyDB::Hash->new( | ||
| 59 | -Filename => $self->database(), | ||
| 60 | -Env => $env, | ||
| 61 | ); | ||
| 62 | } | ||
| 63 | |||
| 64 | sub _build_name { | ||
| 65 | my $self = shift; | ||
| 66 | |||
| 67 | my $db_file = $self->database(); | ||
| 68 | |||
| 69 | return | ||
| 70 | $db_file->basename() . ' - ' | ||
| 71 | . DateTime->from_epoch( epoch => $db_file->stat()->mtime() ) | ||
| 72 | ->iso8601(); | ||
| 73 | } | ||
| 74 | |||
| 75 | sub build { | ||
| 76 | my $class = shift; | ||
| 77 | my ( $file, $database, $update ) = validated_list( | ||
| 78 | \@_, | ||
| 79 | file => { | ||
| 80 | isa => DataFile, | ||
| 81 | coerce => 1, | ||
| 82 | }, | ||
| 83 | database => { | ||
| 84 | isa => File, | ||
| 85 | coerce => 1, | ||
| 86 | }, | ||
| 87 | update => { | ||
| 88 | isa => Bool, | ||
| 89 | default => 0, | ||
| 90 | }, | ||
| 91 | ); | ||
| 92 | |||
| 93 | my $env = BerkeleyDB::Env->new( | ||
| 94 | -Home => $database->dir(), | ||
| 95 | -Flags => DB_CREATE | DB_INIT_CDB | DB_INIT_MPOOL, | ||
| 96 | ); | ||
| 97 | |||
| 98 | my $db = BerkeleyDB::Hash->new( | ||
| 99 | -Filename => $database, | ||
| 100 | -Flags => DB_CREATE, | ||
| 101 | -Env => $env, | ||
| 102 | ) or die "Could not open $database: $! $BerkeleyDB::Error\n"; | ||
| 103 | |||
| 104 | if ($update) { | ||
| 105 | $class->_extract_data_from_file( $file, $db ); | ||
| 106 | } | ||
| 107 | else { | ||
| 108 | my $lock = $db->cds_lock(); | ||
| 109 | |||
| 110 | $db->truncate( my $count ) | ||
| 111 | and die | ||
| 112 | "Fatal error trying to write to the BerkeleyDB file at $database"; | ||
| 113 | |||
| 114 | $class->_extract_data_from_file( $file, $db ); | ||
| 115 | |||
| 116 | # This seems to return a true value even if there's not a real error | ||
| 117 | # (maybe in the case where it doesn't actually comptact?) | ||
| 118 | $db->compact(); | ||
| 119 | |||
| 120 | $lock->cds_unlock(); | ||
| 121 | } | ||
| 122 | |||
| 123 | return; | ||
| 124 | } | ||
| 125 | |||
| 126 | sub _extract_data_from_file { | ||
| 127 | my $class = shift; | ||
| 128 | my $file = shift; | ||
| 129 | my $db = shift; | ||
| 130 | |||
| 131 | open my $fh, '<', $file; | ||
| 132 | |||
| 133 | while (<$fh>) { | ||
| 134 | chomp; | ||
| 135 | $class->_store_value( $db, $_ ); | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | sub _store_value { | ||
| 140 | my $self = shift; | ||
| 141 | my $db = shift; | ||
| 142 | my $value = shift; | ||
| 143 | |||
| 144 | $db->db_put( $value => 1 ) | ||
| 145 | and die "Fatal error trying to write to the BerkeleyDB file at " | ||
| 146 | . $self->database(); | ||
| 147 | |||
| 148 | return; | ||
| 149 | } | ||
| 150 | |||
| 151 | sub match_value { | ||
| 152 | my $self = shift; | ||
| 153 | my $key = shift; | ||
| 154 | |||
| 155 | my $value; | ||
| 156 | # The return value here indicates whether or not the key exists. | ||
| 157 | $self->_db()->db_get( $key, $value ); | ||
| 158 | |||
| 159 | return $value; | ||
| 160 | } | ||
| 161 | |||
| 162 | 1; | ||
| 163 | |||
| 164 | # ABSTRACT: A role for classes which store spam check data in a BerkeleyDB file | ||
| 165 | |||
| 166 | |||
| 167 | |||
| 168 | =pod | ||
| 169 | |||
| 170 | =head1 NAME | ||
| 171 | |||
| 172 | Antispam::Toolkit::Role::BerkeleyDB - A role for classes which store spam check data in a BerkeleyDB file | ||
| 173 | |||
| 174 | =head1 VERSION | ||
| 175 | |||
| 176 | version 0.08 | ||
| 177 | |||
| 178 | =head1 SYNOPSIS | ||
| 179 | |||
| 180 | package MyDBD; | ||
| 181 | |||
| 182 | use Moose; | ||
| 183 | |||
| 184 | with 'Antispam::Toolkit::BerkeleyDB'; | ||
| 185 | |||
| 186 | sub _store_value { | ||
| 187 | my $class = shift; | ||
| 188 | my $db = shift; | ||
| 189 | my $value = shift; | ||
| 190 | |||
| 191 | ... | ||
| 192 | } | ||
| 193 | |||
| 194 | =head1 DESCRIPTION | ||
| 195 | |||
| 196 | This role provides most of what's needed in order to store spam-checking data | ||
| 197 | in a BerkeleyDB file. The only method you must implement in your class is the | ||
| 198 | C<< $class->_store_value() >> method. | ||
| 199 | |||
| 200 | Typically, this will be a database containing things like bad ip addresses or | ||
| 201 | usernames. | ||
| 202 | |||
| 203 | =head1 ATTRIBUTES | ||
| 204 | |||
| 205 | This role provides the following attributes: | ||
| 206 | |||
| 207 | =head2 $db->database() | ||
| 208 | |||
| 209 | This is a L<Path::Class::File> object representing the path on disk for the | ||
| 210 | BerkeleyDB file. This attribute also accepts a string, which is coerced to a | ||
| 211 | file object. | ||
| 212 | |||
| 213 | =head2 $db->name() | ||
| 214 | |||
| 215 | The name of the database. This can be any non-empty string. It is intended for | ||
| 216 | use in things like logs, so that you know exactly which database matched a | ||
| 217 | particular value. | ||
| 218 | |||
| 219 | By default, the name will contain the database file's basename and it's mtime | ||
| 220 | as an ISO8601 datetime, something like "bad-ip.db - 2010-11-16T10:31:03". | ||
| 221 | |||
| 222 | =head2 $db->_db() | ||
| 223 | |||
| 224 | This attribute contains the L<BerkeleyDB> object for the database. It cannot | ||
| 225 | be set in the constructor, and is always lazily built. | ||
| 226 | |||
| 227 | =head1 REQUIRED METHODS | ||
| 228 | |||
| 229 | This role requires one method: | ||
| 230 | |||
| 231 | =head1 METHODS | ||
| 232 | |||
| 233 | This role provides the following methods: | ||
| 234 | |||
| 235 | =head2 $db->_build_db() | ||
| 236 | |||
| 237 | This will build the L<BerkeleyDB> object for the database. | ||
| 238 | |||
| 239 | =head2 $db->_build_name() | ||
| 240 | |||
| 241 | This creates a default name for the object. | ||
| 242 | |||
| 243 | =head2 $class->build( ... ) | ||
| 244 | |||
| 245 | This is a I<class> method that can be used to construct a new L<BerkeleyDB> | ||
| 246 | file from a data source. | ||
| 247 | |||
| 248 | It accepts the following arguments: | ||
| 249 | |||
| 250 | =over 4 | ||
| 251 | |||
| 252 | =item * file | ||
| 253 | |||
| 254 | This should be a file containing data to be imported into the database. By | ||
| 255 | default, this should be a file which lists one value per line. You can provide | ||
| 256 | your own C<< $class->_extract_data_from_file() >> to handle different data | ||
| 257 | formats. | ||
| 258 | |||
| 259 | =item * database | ||
| 260 | |||
| 261 | The path to the BerkeleyDB file that will be created or updated. | ||
| 262 | |||
| 263 | If you're using multiple BerkeleyDB files for different types of data, you | ||
| 264 | probably should put each one in a separate directory, because the BerkeleyDB | ||
| 265 | library creates identically named log files for each database file. | ||
| 266 | |||
| 267 | =item * update | ||
| 268 | |||
| 269 | By default, if the database parameter points to an existing BerkeleyDB file, | ||
| 270 | it will be emptied completely and rebuilt from scratch. If this parameter is | ||
| 271 | true, it will simply add new data and leave the old data in place. | ||
| 272 | |||
| 273 | =back | ||
| 274 | |||
| 275 | =head2 $class->_extract_data_from_file( $file, $db ) | ||
| 276 | |||
| 277 | This method takes a data file and adds that data to the BerkeleyDB file. By | ||
| 278 | default, this expects that the file contains one value per line, so it chomps | ||
| 279 | each line and stores that value. | ||
| 280 | |||
| 281 | Internally, it calls C<< $class->_store_value() >> to actually store the value. | ||
| 282 | |||
| 283 | =head2 $class->_store_value( $db, $value ) | ||
| 284 | |||
| 285 | This method will be called as a class method. The method is passed a | ||
| 286 | L<BerkeleyDB> object and value to store in the database. | ||
| 287 | |||
| 288 | By default, it just stores the literal value. You can replace this method if | ||
| 289 | you want to do something different, like handle wildcard values. | ||
| 290 | |||
| 291 | =head2 $db->match_value($value) | ||
| 292 | |||
| 293 | This method looks up a value to see if it is stored in the database. By | ||
| 294 | default, it expects the value to match a key stored in the database. | ||
| 295 | |||
| 296 | =head1 ROLES | ||
| 297 | |||
| 298 | This role does the L<Antispam::Toolkit::Role::Database> role. It provides an | ||
| 299 | implementation of C<< $db->match_value() >> method, but you can write your own | ||
| 300 | if necessary. | ||
| 301 | |||
| 302 | =head1 BUGS | ||
| 303 | |||
| 304 | See L<Antispam::Toolkit> for bug reporting details. | ||
| 305 | |||
| 306 | =head1 AUTHOR | ||
| 307 | |||
| 308 | Dave Rolsky <autarch@urth.org> | ||
| 309 | |||
| 310 | =head1 COPYRIGHT AND LICENSE | ||
| 311 | |||
| 312 | This software is Copyright (c) 2011 by Dave Rolsky. | ||
| 313 | |||
| 314 | This is free software, licensed under: | ||
| 315 | |||
| 316 | The Artistic License 2.0 (GPL Compatible) | ||
| 317 | |||
| 318 | =cut | ||
| 319 | |||
| 320 | |||
| 321 | __END__ |