From ac2c65c84fed370fb4dc21e40dce5b407e230821 Mon Sep 17 00:00:00 2001 From: Chip Black Date: Sun, 16 Jun 2013 03:32:36 -0500 Subject: [PATCH] Flesh out Blerg::Database OO layer and convenience functions --- lib/perl/Blerg-Database/Database.xs | 7 ++- lib/perl/Blerg-Database/Makefile.PL | 7 ++- lib/perl/Blerg-Database/lib/Blerg/Database.pm | 60 ++++++++++++++++++- lib/perl/Blerg-Database/t/Blerg-Database.t | 6 +- 4 files changed, 71 insertions(+), 9 deletions(-) diff --git a/lib/perl/Blerg-Database/Database.xs b/lib/perl/Blerg-Database/Database.xs index 9fc728f..1ad9fa6 100644 --- a/lib/perl/Blerg-Database/Database.xs +++ b/lib/perl/Blerg-Database/Database.xs @@ -4,14 +4,15 @@ #include "ppport.h" -#include "const-c.inc" - +#include "config.h" #include "database/database.h" #include "database/blergref.h" #include "database/tags.h" #include "database/subscription.h" #include "database/util.h" +#include "const-c.inc" + HV * blergref_to_perl_hash(struct blergref *list) { HV *tmp; char buf[21]; @@ -112,7 +113,7 @@ int _get_mute(struct blerg *ptr) OUTPUT: RETVAL -void _tag_list(const char *tag, const char *str_offset, int direction) +void tag_list(const char *tag, const char *str_offset, int direction) INIT: HV * tmp; struct blergref *list; diff --git a/lib/perl/Blerg-Database/Makefile.PL b/lib/perl/Blerg-Database/Makefile.PL index 2514c10..c8810f0 100644 --- a/lib/perl/Blerg-Database/Makefile.PL +++ b/lib/perl/Blerg-Database/Makefile.PL @@ -21,7 +21,12 @@ if (eval {require ExtUtils::Constant; 1}) { # you will need to use the generated const-c.inc and const-xs.inc # files to replace their "fallback" counterparts before distributing your # changes. - my @names = (qw()); + my @names = (qw(RECORDS_PER_SEGMENT MAX_RECORD_SIZE MAX_TAG_LENGTH MAX_TAGS_PER_RECORD), + {name => 'BASEURL', type => 'PV'}, + {name => 'DATA_PATH', type => 'PV'}, + {name => 'HASH_TAGS_PATH', type => 'PV'}, + {name => 'REF_TAGS_PATH', type => 'PV'}, + ); ExtUtils::Constant::WriteConstants( NAME => 'Blerg::Database', NAMES => \@names, diff --git a/lib/perl/Blerg-Database/lib/Blerg/Database.pm b/lib/perl/Blerg-Database/lib/Blerg/Database.pm index 3da5ad3..823dc6a 100644 --- a/lib/perl/Blerg-Database/lib/Blerg/Database.pm +++ b/lib/perl/Blerg-Database/lib/Blerg/Database.pm @@ -61,7 +61,8 @@ sub open { my ($class, $name) = @_; my $ptr = Blerg::Database::_open($name); my $obj = { - ptr => $ptr + ptr => $ptr, + name => $name, }; return bless $obj, $class; } @@ -70,7 +71,7 @@ sub open_existing { my ($class, $name) = @_; if (Blerg::Database::exists($name)) { - return Blerg::Database::open($name); + return Blerg::Database->open($name); } return undef; } @@ -94,6 +95,44 @@ DESTROY { $obj->close; } +sub record_count { + my ($obj) = @_; + $obj->_ensure_pointer; + return Blerg::Database::_get_record_count($obj->{ptr}); +} + +sub set_subscription_mark { + my ($obj) = @_; + $obj->_ensure_pointer; + return Blerg::Database::_set_subscription_mark($obj->{ptr}); +} + +sub get_subscription_mark { + my ($obj) = @_; + $obj->_ensure_pointer; + return Blerg::Database::_get_subscription_mark($obj->{ptr}); +} + +sub subscription_list { + my ($obj) = @_; + return Blerg::Database::subscription_list($obj->{name}); +} + +sub mute { + my ($obj, $v) = @_; + $obj->_ensure_pointer; + if (defined $v) { + return Blerg::Database::_set_mute($obj->{ptr}, $v); + } else { + return Blerg::Database::_get_mute($obj->{ptr}); + } +} + +sub refs { + my ($obj) = @_; + return Blerg::Database::tag_list('@' . $obj->{name}, 50, -1); +} + sub store { my ($obj, $data) = @_; $obj->_ensure_pointer; @@ -106,6 +145,23 @@ sub fetch { return Blerg::Database::_fetch($obj->{ptr}, $record); } +sub timestamp { + my ($obj, $record) = @_; + $obj->_ensure_pointer; + return Blerg::Database::_get_timestamp($obj->{ptr}, $record); +} + +# Convenience shortcuts +sub hash_tag_list { + my ($name, $str_offset, $direction) = @_; + return Blerg::Database::tag_list("#$name", $str_offset, $direction); +} + +sub ref_tag_list { + my ($name, $str_offset, $direction) = @_; + return Blerg::Database::tag_list("@$name", $str_offset, $direction); +} + # Autoload methods go after =cut, and are processed by the autosplit program. 1; diff --git a/lib/perl/Blerg-Database/t/Blerg-Database.t b/lib/perl/Blerg-Database/t/Blerg-Database.t index b64e40e..74b5e16 100644 --- a/lib/perl/Blerg-Database/t/Blerg-Database.t +++ b/lib/perl/Blerg-Database/t/Blerg-Database.t @@ -70,7 +70,7 @@ Blerg::Database::_store($ptr, "This is data B #data"); Blerg::Database::_store($ptr, "This is data C #data"); $n += 3; -@list = Blerg::Database::_tag_list('#data', 0, 1); +@list = Blerg::Database::tag_list('#data', 0, 1); ok( @list == 3 ); ok( $list[0]->{author} eq $test_user ); ok( $list[0]->{record} == $n ); @@ -79,12 +79,12 @@ ok( $list[0]->{record} == $n ); ok( Blerg::Database::_set_mute($ptr, 1) ); ok( Blerg::Database::_get_mute($ptr) == 1 ); Blerg::Database::_store($ptr, "Testing more #data"); -@list = Blerg::Database::_tag_list('#data', 0, 1); +@list = Blerg::Database::tag_list('#data', 0, 1); ok( @list == 3 ); ok( Blerg::Database::_set_mute($ptr, 0) ); ok( Blerg::Database::_get_mute($ptr) == 0 ); Blerg::Database::_store($ptr, "Testing more #data"); -@list = Blerg::Database::_tag_list('#data', 0, 1); +@list = Blerg::Database::tag_list('#data', 0, 1); ok( @list == 4 ); $n += 2; -- 2.34.1