commit:c5ce1eda864392d8ca5d34fd16761da9eec2c86b
author:Chip Black
committer:Chip Black
date:Mon Jun 3 01:42:54 2013 -0500
parents:71d846fac84f2266b1a4a0324f2932a0435aab1d
Add subscription support to perl lib

Also some minor refactoring inside Database.xs and subscription
functions now actually return values when they should.
diff --git a/database/subscription.c b/database/subscription.c
line changes: +6/-0
index 5d305d1..e55e8f1
--- a/database/subscription.c
+++ b/database/subscription.c
@@ -25,6 +25,8 @@ int subscription_add(const char *from, const char *to) {
 	sb = stringbucket_open(filename);
 	stringbucket_add(sb, from);
 	stringbucket_close(sb);
+
+	return 1;
 }
 
 int subscription_remove(const char *from, const char *to) {
@@ -40,6 +42,8 @@ int subscription_remove(const char *from, const char *to) {
 	sb = stringbucket_open(filename);
 	stringbucket_delete(sb, from);
 	stringbucket_close(sb);
+
+	return 1;
 }
 
 void subscription_notify_add_item(char *to, void *stuff) {
@@ -62,6 +66,8 @@ int subscription_notify(const char *author, uint64_t record) {
 	struct stringbucket * sb = stringbucket_open(filename);
 	stringbucket_iterate(sb, subscription_notify_add_item, &r);
 	stringbucket_close(sb);
+
+	return 1;
 }
 
 struct blergref * subscription_list(const char *author, uint64_t offset, int *count, int direction) {

diff --git a/lib/perl/Blerg-Database/Database.xs b/lib/perl/Blerg-Database/Database.xs
line changes: +52/-6
index a26e736..9fc728f
--- a/lib/perl/Blerg-Database/Database.xs
+++ b/lib/perl/Blerg-Database/Database.xs
@@ -12,6 +12,19 @@
 #include "database/subscription.h"
 #include "database/util.h"
 
+HV * blergref_to_perl_hash(struct blergref *list) {
+        HV *tmp;
+        char buf[21];
+        int n;
+
+        tmp = newHV();
+        hv_store(tmp, "author", 6, newSVpv(list->author, 0), 0);
+        n = snprintf(buf, 21, "%llu", list->record);
+        hv_store(tmp, "record", 6, newSVpv(buf, n), 0);
+
+        return tmp;
+}
+
 MODULE = Blerg::Database		PACKAGE = Blerg::Database		
 
 INCLUDE: const-xs.inc
@@ -104,8 +117,7 @@ void _tag_list(const char *tag, const char *str_offset, int direction)
         HV * tmp;
         struct blergref *list;
         uint64_t offset;
-        char buf[21];
-        int count, i, n;
+        int count, i;
     PPCODE:
         offset = strtoull(str_offset, NULL, 0);
         list = tag_list(tag, offset, &count, direction);
@@ -115,11 +127,45 @@ void _tag_list(const char *tag, const char *str_offset, int direction)
 
         i = count - 1;
         while (i >= 0) {
-            tmp = newHV();
-            hv_store(tmp, "author", 6, newSVpv(list[i].author, 0), 0);
-            n = snprintf(buf, 21, "%llu", list[i].record);
-            hv_store(tmp, "record", 6, newSVpv(buf, n), 0);
+            tmp = blergref_to_perl_hash(&list[i]);
+            XPUSHs(sv_2mortal(newRV_noinc((SV*)tmp)));
+            i--;
+        }
+        free(list);
+
+int subscription_add(const char *from, const char *to)
+    CODE:
+        RETVAL = subscription_add(from, to);
+    OUTPUT:
+        RETVAL
+
+int subscription_remove(const char *from, const char *to)
+    CODE:
+        RETVAL = subscription_remove(from, to);
+    OUTPUT:
+        RETVAL
+
+void subscription_list(const char *author, const char *str_offset, int direction)
+    INIT:
+        HV *tmp;
+        struct blergref *list;
+        uint64_t offset;
+        int count, i;
+    PPCODE:
+        offset = strtoull(str_offset, NULL, 0);
+        list = subscription_list(author, offset, &count, direction);
+        if (list == NULL) {
+            XSRETURN_UNDEF;
+        }
+
+        i = count - 1;
+        while (i >= 0) {
+            tmp = blergref_to_perl_hash(&list[i]);
             XPUSHs(sv_2mortal(newRV_noinc((SV*)tmp)));
             i--;
         }
         free(list);
+
+int valid_tag_name(const char *name)
+
+int valid_name(const char *name)

diff --git a/lib/perl/Blerg-Database/t/Blerg-Database.t b/lib/perl/Blerg-Database/t/Blerg-Database.t
line changes: +22/-6
index eebfbae..b64e40e
--- a/lib/perl/Blerg-Database/t/Blerg-Database.t
+++ b/lib/perl/Blerg-Database/t/Blerg-Database.t
@@ -9,7 +9,7 @@ use File::Path qw/remove_tree/;
 use strict;
 use warnings;
 
-use Test::More tests => 25;
+use Test::More tests => 30;
 BEGIN { use_ok('Blerg::Database') };
 
 #########################
@@ -19,9 +19,12 @@ BEGIN { use_ok('Blerg::Database') };
 
 # Setup
 my $test_user = 'barfy';
-chdir '../../..';
-remove_tree "data/$test_user";
-remove_tree "hash_tags/data";
+my $test_dir = "/tmp/blerg_test_$$";
+mkdir $test_dir;
+chdir $test_dir;
+mkdir 'data';
+mkdir 'hash_tags';
+mkdir 'ref_tags';
 my ($ptr, @list, $n);
 $n = 0;
 
@@ -49,8 +52,17 @@ ok( not defined Blerg::Database::_fetch($ptr, $n + 1) );
 my $t = Blerg::Database::_get_timestamp($ptr, $n);
 ok( $t0 - $t <= 1.0 );
 
-# Will have to test subscription bits after the Subscription module is
-# made
+# Subscription
+my ($ptra, $ptrb);
+$ptra = Blerg::Database::_open("a");
+$ptrb = Blerg::Database::_open("b");
+ok( Blerg::Database::subscription_add("a", "b") == 1 );
+Blerg::Database::_store($ptrb, "Hello, A!");
+@list = Blerg::Database::subscription_list("a", 0, 1);
+ok( @list == 1 );
+ok( $list[0]->{author} eq 'b' );
+ok( $list[0]->{record} == 0 );
+ok( Blerg::Database::_fetch($ptrb, $list[0]->{record}) eq 'Hello, A!' );
 
 # Test tags
 Blerg::Database::_store($ptr, "This is data A #data");
@@ -98,3 +110,7 @@ ok( $blerg->store($data) == $n + 1 );
 $n++;
 ok( $blerg->fetch($n) eq $data );
 
+END {
+	chdir;
+	remove_tree "/tmp/blerg_test_$$";
+}