Changeset 15606 for lang/perl/Data-Valve

Show
Ignore:
Timestamp:
07/10/08 14:28:25 (6 years ago)
Author:
daisuke
Message:

still broken, but serialization works more or less

Location:
lang/perl/Data-Valve/trunk
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/Data-Valve/trunk/Valve.xs

    r15605 r15606  
    88 
    99dv_bucket * 
    10 dv_bucket_create(float interval, unsigned long max) 
     10dv_bucket_create(double interval, unsigned long max) 
    1111 
    1212void 
     
    2424dv_bucket_first(dv_bucket *bucket) 
    2525 
     26long 
     27dv_bucket_max_items(dv_bucket *bucket) 
     28 
     29double  
     30dv_bucket_interval(dv_bucket *bucket) 
     31 
     32SV * 
     33dv_bucket_serialize(dv_bucket *bucket) 
     34 
     35dv_bucket * 
     36dv_bucket__deserialize(SV *buf, float interval, long max) 
     37    PREINIT: 
     38        STRLEN len; 
     39        char *c_buf = (char *)SvPV(ST(0), len); 
     40    CODE: 
     41        RETVAL = dv_bucket_deserialize(buf, len, interval, max); 
     42    OUTPUT: 
     43        RETVAL 
     44 
     45 
    2646MODULE = Data::Valve      PACKAGE = Data::Valve::BucketItem PREFIX = dv_bucket_item_ 
    2747 
  • lang/perl/Data-Valve/trunk/dv_bucket.c

    r15605 r15606  
    33 
    44#include "dv_bucket.h" 
     5#include "EXTERN.h" 
     6#include "perl.h" 
    57#include <stdio.h> 
    68 
     
    1618 
    1719dv_bucket_item * 
    18 dv_bucket_item_create(struct timeval *tp) 
     20dv_bucket_item_create(double time) 
    1921{ 
    2022    dv_bucket_item *item; 
     
    2224    item = (dv_bucket_item *) malloc( sizeof(dv_bucket_item) ); 
    2325    item->next = NULL; 
    24     item->time = dv_bucket_timeval2double(tp); 
     26    item->time = time; 
    2527    return item; 
    2628} 
     
    3335 
    3436dv_bucket* 
    35 dv_bucket_create(float interval, unsigned long max) 
     37dv_bucket_create(double interval, unsigned long max) 
    3638{ 
    3739    dv_bucket *bucket; 
     
    3941    bucket = (dv_bucket *) malloc( sizeof(dv_bucket)); 
    4042    bucket->max = max; 
    41     bucket->interval = (long) interval * DV_1E6; 
     43    bucket->interval = interval * DV_1E6; 
    4244    bucket->count = 0; 
    4345    bucket->head = NULL; 
    4446    bucket->tail = NULL; 
    4547    return bucket; 
     48} 
     49 
     50long 
     51dv_bucket_max_items(dv_bucket *bucket) 
     52{ 
     53    return bucket->max; 
     54} 
     55 
     56double 
     57dv_bucket_interval(dv_bucket *bucket) 
     58{ 
     59    return bucket->interval / DV_1E6; 
    4660} 
    4761 
     
    131145 
    132146void 
    133 dv_bucket_push(dv_bucket *bucket, struct timeval *tp) 
    134 { 
    135     dv_bucket_item *item = dv_bucket_item_create(tp); 
     147dv_bucket_push(dv_bucket *bucket, double time) 
     148{ 
     149    dv_bucket_item *item = dv_bucket_item_create(time); 
    136150    if (bucket->count == 0) { 
    137151        bucket->head = item; 
     
    155169 
    156170    if ( dv_bucket_count( bucket ) == 0 ) { 
    157         dv_bucket_push( bucket, &t ); 
     171        dv_bucket_push( bucket, dv_bucket_timeval2double(&t) ); 
    158172        return 1; 
    159173    } 
     
    163177    } 
    164178 
    165     dv_bucket_push( bucket, &t ); 
     179    dv_bucket_push( bucket, dv_bucket_timeval2double(&t) ); 
    166180    return 1; 
    167181} 
     182 
     183SV * 
     184dv_bucket_serialize(dv_bucket *bucket) 
     185{ 
     186    SV *sv = newSVpv("[", 1); 
     187    dv_bucket_item *item = bucket->head; 
     188 
     189    while (item) { 
     190        sv_catpvf(sv, "%f%s", item->time, item->next ? "," : ""); 
     191        item = item->next; 
     192    } 
     193 
     194    sv_catpv(sv, "]"); 
     195    return sv; 
     196} 
     197 
     198dv_bucket * 
     199dv_bucket_deserialize(char *buf, size_t len, double interval, unsigned long max) 
     200{ 
     201    dv_bucket *bucket = dv_bucket_create(interval, max); 
     202    char *start; 
     203    char *end = buf + len; 
     204 
     205    if (buf != end && *buf == '[') { 
     206        buf++; 
     207        while (buf != end && !isdigit(*buf)) { 
     208            buf++; 
     209        } 
     210    } 
     211 
     212    while (buf != end) { 
     213        start = buf; 
     214        while ((isdigit(*buf) || *buf == '.' ) ) { 
     215            buf++; 
     216        } 
     217        dv_bucket_push(bucket, strtod(buf, NULL)); 
     218 
     219        while ( buf != end && ! isdigit(*buf)) { 
     220            buf++; 
     221        } 
     222    } 
     223    return bucket; 
     224} 
     225 
    168226 
    169227dv_bucket_item * 
  • lang/perl/Data-Valve/trunk/dv_bucket.h

    r15605 r15606  
    1414typedef struct dv_bucket { 
    1515    unsigned long max; 
    16     unsigned long interval; 
     16    double interval; 
    1717    unsigned long count; 
    1818    dv_bucket_item *head; 
     
    2121 
    2222/* Creates a new bucket */ 
    23 dv_bucket *dv_bucket_create(float interval, unsigned long max); 
     23dv_bucket *dv_bucket_create(double interval, unsigned long max); 
    2424 
    2525#endif /* __DV_BUCKET_H__ */ 
  • lang/perl/Data-Valve/trunk/lib/Data/Valve/Bucket.pm

    r15597 r15606  
    99     
    1010    return $self; 
     11} 
     12 
     13sub deserialize { 
     14    my ($class, @args) = @_; 
     15    return bless _deserialize(@args), $class; 
    1116} 
    1217 
  • lang/perl/Data-Valve/trunk/t/02_bucket.t

    r15605 r15606  
    11use strict; 
    2 use Test::More (tests => 20); 
     2use Test::More (tests => 24); 
    33 
    44BEGIN 
     
    1515    ok($bucket); 
    1616    isa_ok($bucket, "Data::Valve::Bucket"); 
     17    ok($bucket->interval == 10, "interval is 10"); 
    1718} 
    1819 
     
    4041    ok(! $item->next ); 
    4142 
     43    my $serialized = $bucket->serialize(); 
     44    like($serialized, qr/^\[(?:(?:[\d\.]+,?))+\]$/, "serialization format ok"); 
     45 
     46    my $bucket2 = Data::Valve::Bucket->deserialize($serialized, 10, 5); 
     47 
     48    is($bucket2->interval, 10, "interval is 10"); 
     49    is($bucket2->max_items, 5, "max_items is 5"); 
    4250}