Add subscription support to perl lib
Also some minor refactoring inside Database.xs and subscription
functions now actually return values when they should.
sb = stringbucket_open(filename);
stringbucket_add(sb, from);
stringbucket_close(sb);
+
+ return 1;
}
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) {
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) {
#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
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);
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)
use strict;
use warnings;
-use Test::More tests => 25;
+use Test::More tests => 30;
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;
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");
$n++;
ok( $blerg->fetch($n) eq $data );
+END {
+ chdir;
+ remove_tree "/tmp/blerg_test_$$";
+}