Data-DDSketch-Shared

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

    for cross-user sharing; it defaults to 0600 (owner-only). "new_readonly"
    opens a frozen file read-only for lock-free querying (see "FROZEN
    (READ-ONLY) MODE").

  Feeding values
        my $n = $dd->add($value);        # add one value; returns the new total count
        $dd->insert($value);             # alias for add
        $dd->add_many(\@values);         # add a batch under a single write lock
        $dd->clear;                      # empty the sketch

    "add" adds one finite number (croaks on "NaN" or infinity) and returns the
    running total count. "add_many" adds an array reference of numbers under
    one write lock, validating them all first. Negative values and zero are
    supported.

  Querying
        my $v   = $dd->quantile($q);     # value at quantile $q in 0 .. 1 (undef if empty)
        my $med = $dd->median;           # quantile(0.5)
        $dd->min; $dd->max;              # exact smallest / largest value (undef if empty)
        $dd->mean;                       # running mean (undef if empty)
        $dd->sum; $dd->count;            # running sum and exact number of values

Shared.xs  view on Meta::CPAN

    }

UV
add(self, value)
    SV *self
    double value
  PREINIT:
    EXTRACT(self);
  CODE:
    if (h->readonly) croak("Data::DDSketch::Shared->add: sketch is frozen (read-only)");
    if (!isfinite(value))                  /* reject NaN/Inf BEFORE the lock */
        croak("Data::DDSketch::Shared->add: value must be finite");
    dd_rwlock_wrlock(h);
    if (h->hdr->sealed) { dd_rwlock_wrunlock(h); croak("Data::DDSketch::Shared->add: sketch is frozen (read-only)"); }
    dd_insert_locked(h, value, 1);
    h->hdr->stat_ops++;
    RETVAL = (UV)h->hdr->total_count;
    dd_rwlock_wrunlock(h);
  OUTPUT:
    RETVAL

ddsketch.h  view on Meta::CPAN

    h->occ          = (uint64_t *)((uint8_t *)base + dd_layout_for(0).occ);  /* trusted layout offset (occ offset is num_buckets-independent) */
    /* single validated read of each geometry field; cached so the bound and the
       pointers/arithmetic it feeds stay consistent even under later peer corruption */
    h->neg_off      = hdr->neg_off;
    h->pos_off      = hdr->pos_off;
    h->num_buckets  = hdr->num_buckets;
    h->bias         = hdr->bias;
    /* Derive gamma / inv_ln_gamma from the validated alpha rather than trusting
       the stored copies: validate_header bounds alpha to [DD_MIN_ALPHA,
       DD_MAX_ALPHA], so these are always finite and in range, whereas a corrupt
       stored inv_ln_gamma (NaN/wild) would make the key computation's double->
       int64 cast undefined behaviour. */
    h->gamma        = (1.0 + hdr->alpha) / (1.0 - hdr->alpha);
    h->inv_ln_gamma = 1.0 / log(h->gamma);
    h->mmap_size    = map_size;
    /* Layer B: if the mapping cannot hold `num_buckets` in each store, clamp the
       cached count to what actually fits (pos is the last, tightest region). */
    {
        uint64_t nfit = dd_store_max(h, h->neg_off);
        uint64_t pfit = dd_store_max(h, h->pos_off);
        uint64_t fit  = nfit < pfit ? nfit : pfit;

ddsketch.h  view on Meta::CPAN

    h->my_slot_idx  = UINT32_MAX;
    return h;
}

/* Validate a mapped header (shared by dd_create reopen and dd_open_fd). */
static inline int dd_validate_header(const DdHeader *hdr, uint64_t file_size) {
    if (hdr->magic != DD_MAGIC) return 0;
    if (hdr->version != DD_VERSION) return 0;
    if (hdr->num_buckets < DD_MIN_BUCKETS || hdr->num_buckets > DD_MAX_BUCKETS) return 0;
    if (hdr->bias != hdr->num_buckets / 2) return 0;
    if (!(hdr->alpha >= DD_MIN_ALPHA && hdr->alpha <= DD_MAX_ALPHA)) return 0;  /* same bounds as create; rejects NaN */
    if (!(hdr->gamma > 1.0)) return 0;
    if (hdr->total_size != file_size) return 0;
    if (hdr->total_size != dd_total_size(hdr->num_buckets)) return 0;
    DdLayout L = dd_layout_for(hdr->num_buckets);
    if (hdr->reader_slots_off != L.reader_slots) return 0;
    if (hdr->neg_off != L.neg) return 0;
    if (hdr->pos_off != L.pos) return 0;
    return 1;
}

ddsketch.h  view on Meta::CPAN


/* map a bucket key to a store slot in the fixed centred window, collapsing keys
 * past either end into the extreme bucket. */
static inline uint64_t dd_slot_of_key(DdHandle *h, int64_t key) {
    int64_t s = key + (int64_t)h->bias;
    if (s < 0) return 0;
    if ((uint64_t)s >= (uint64_t)h->num_buckets) return h->num_buckets ? h->num_buckets - 1 : 0;
    return (uint64_t)s;
}

/* insert one finite value (caller holds the write lock; caller has rejected NaN/Inf) */
static void dd_insert_locked(DdHandle *h, double v, uint64_t count) {
    DdHeader *hdr = h->hdr;
    if (v == 0.0) {
        hdr->zero_count += count;
    } else if (h->num_buckets != 0) {                        /* Layer B: unusable mapping */
        double mag = v > 0 ? v : -v;
        int64_t key = (int64_t)ceil(log(mag) * h->inv_ln_gamma);
        uint64_t slot = dd_slot_of_key(h, key);
        uint64_t *store = (v > 0) ? dd_pos(h) : dd_neg(h);
        store[slot] += count;

lib/Data/DDSketch/Shared.pm  view on Meta::CPAN

a B<frozen> file read-only for lock-free querying (see L</"FROZEN (READ-ONLY)
MODE">).

=head2 Feeding values

    my $n = $dd->add($value);        # add one value; returns the new total count
    $dd->insert($value);             # alias for add
    $dd->add_many(\@values);         # add a batch under a single write lock
    $dd->clear;                      # empty the sketch

C<add> adds one finite number (croaks on C<NaN> or infinity) and returns the
running total count. C<add_many> adds an array reference of numbers under one
write lock, validating them all first. Negative values and zero are supported.

=head2 Querying

    my $v   = $dd->quantile($q);     # value at quantile $q in 0 .. 1 (undef if empty)
    my $med = $dd->median;           # quantile(0.5)
    $dd->min; $dd->max;              # exact smallest / largest value (undef if empty)
    $dd->mean;                       # running mean (undef if empty)
    $dd->sum; $dd->count;            # running sum and exact number of values

t/01-basic.t  view on Meta::CPAN


# error paths
ok !eval { Data::DDSketch::Shared->new(undef, 0.6); 1 }, 'alpha too large rejected';
like $@, qr/alpha/, 'alpha croak';
ok !eval { Data::DDSketch::Shared->new(undef, 0); 1 }, 'alpha 0 rejected';
ok !eval { Data::DDSketch::Shared->new(undef, 0.01, 4); 1 }, 'num_buckets too small rejected';
{
    my $dd = Data::DDSketch::Shared->new(undef, 0.01);
    ok !eval { $dd->add(9**9**9); 1 }, 'add croaks on infinity';
    like $@, qr/finite/, 'infinity croak';
    ok !eval { $dd->add("NaN" + 0); 1 }, 'add croaks on NaN';
    ok !eval { $dd->quantile(1.5); 1 }, 'quantile croaks on q > 1';
    like $@, qr/between 0 and 1/, 'quantile range croak';
    ok !eval { $dd->quantile(-0.1); 1 }, 'quantile croaks on q < 0';
    ok !eval { $dd->add_many("notaref"); 1 }, 'add_many non-arrayref croaks';
    ok !eval { $dd->add_many([ 1, 2, 9**9**9 ]); 1 }, 'add_many croaks on a non-finite element';
}

# file-backed reopen: geometry wins, distribution persists
my $path = "/tmp/dd-basic-$$.bin";
unlink $path;



( run in 2.861 seconds using v1.01-cache-2.11-cpan-60884a8df76 )