Data-TopK-Shared
view release on metacpan or search on metacpan
SV *
new_decayed_memfd(class, name = &PL_sv_undef, capacity = 0, key_size = 256, half_life = 0)
const char *class
SV *name
UV capacity
UV key_size
NV half_life
PREINIT:
char errbuf[TK_ERR_BUFLEN];
CODE:
const char *nm = (SvGETMAGIC(name), SvOK(name)) ? SvPV_nolen(name) : NULL;
if (capacity < 1) croak("Data::TopK::Shared->new_decayed_memfd: capacity must be >= 1");
if (!((double)half_life > 0.0) || !isfinite((double)half_life))
croak("Data::TopK::Shared->new_decayed_memfd: half_life must be a finite number > 0");
double alpha = log(2.0) / (double)half_life;
TkHandle *h = tk_create_memfd(nm, (uint64_t)capacity, (uint64_t)key_size, TK_MODE_DECAYED, alpha, errbuf);
if (!h) croak("Data::TopK::Shared->new_decayed_memfd: %s", errbuf);
MAKE_OBJ(class, h);
OUTPUT:
RETVAL
SV *
new_from_fd(class, fd)
const char *class
int fd
PREINIT:
char errbuf[TK_ERR_BUFLEN];
CODE:
TkHandle *h = tk_open_fd(fd, errbuf);
if (!h) croak("Data::TopK::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory");
MAKE_OBJ(class, h);
OUTPUT:
RETVAL
void
DESTROY(self)
SV *self
CODE:
if (sv_isobject(self) && sv_derived_from(self, "Data::TopK::Shared")) {
TkHandle *h = INT2PTR(TkHandle*, SvIV(SvRV(self)));
if (h) { sv_setiv(SvRV(self), 0); tk_destroy(h); } /* null first: activates EXTRACT's use-after-destroy croak + makes a double DESTROY a no-op */
}
SV *
add(self, item, timestamp = &PL_sv_undef)
SV *self
SV *item
SV *timestamp
PREINIT:
EXTRACT(self);
STRLEN n;
const char *s;
int has_ts; double ts = 0.0, g = 1.0; uint64_t raw;
CODE:
/* Resolve the optional timestamp's magic FIRST: a tied/overloaded timestamp
* runs arbitrary Perl in FETCH/numify that could realloc or free item's SV
* buffer. Capture item's bytes LAST so nothing runs between the capture and
* the locked use (add_many guards the same hazard by copying to a mortal). */
has_ts = (SvGETMAGIC(timestamp), SvOK(timestamp)); /* optional timestamp for decayed mode */
if (has_ts) { ts = (double)SvNV(timestamp); if (!isfinite(ts)) has_ts = 0; } /* ignore Inf/NaN -> per-add tick */
s = SvPVbyte(item, n); /* may croak (wide char) -- BEFORE the lock */
REEXTRACT(self);
tk_rwlock_wrlock(h);
raw = tk_observe_locked(h, s, n, has_ts, ts);
if (h->mode == TK_MODE_DECAYED) g = tk_g(h);
h->hdr->stat_ops++;
tk_rwlock_wrunlock(h);
RETVAL = (h->mode == TK_MODE_DECAYED) ? newSVnv(tk_w_get(raw) / g) : newSVuv((UV)raw);
OUTPUT:
RETVAL
UV
add_many(self, items)
SV *self
SV *items
PREINIT:
EXTRACT(self);
AV *av;
IV top;
UV processed = 0;
CODE:
SvGETMAGIC(items);
if (!SvROK(items) || SvTYPE(SvRV(items)) != SVt_PVAV)
croak("Data::TopK::Shared->add_many: expected an array reference");
av = (AV *)SvRV(items);
sv_2mortal(SvREFCNT_inc((SV *)av)); /* pin the arrayref: element magic below cannot free it mid-loop */
top = av_len(av); /* last index, -1 if empty */
{
STRLEN cnt = (top >= 0) ? (STRLEN)(top + 1) : 0, i;
const char **ps = NULL; STRLEN *ls = NULL;
if (cnt) { /* resolve all bytes BEFORE locking */
Newx(ps, cnt, const char *); SAVEFREEPV(ps); /* freed on return OR unwind */
Newx(ls, cnt, STRLEN); SAVEFREEPV(ls);
for (i = 0; i < cnt; i++) { /* a croak here holds NO lock; SAVEFREEPV cleans up */
SV **el = av_fetch(av, (SSize_t)i, 0);
if (el && *el) {
STRLEN len;
const char *src = SvPVbyte(*el, len); /* may run overload/tie/get-magic = arbitrary Perl */
/* Copy bytes into a private mortal SV NOW: a LATER element SvPVbyte can
* grow/free THIS element PV, dangling src before the locked loop uses it. */
SV *copy = sv_2mortal(newSVpvn(src, len));
ps[i] = SvPVX_const(copy);
ls[i] = len;
} else { ps[i] = ""; ls[i] = 0; }
}
}
REEXTRACT(self);
tk_rwlock_wrlock(h); /* locked region: NO croak-capable calls */
for (i = 0; i < cnt; i++) { tk_observe_locked(h, ps[i], ls[i], 0, 0.0); processed++; } /* decayed: per-add tick */
h->hdr->stat_ops++; /* a call always counts, even an empty batch */
tk_rwlock_wrunlock(h);
}
RETVAL = processed;
OUTPUT:
RETVAL
SV *
estimate(self, item)
SV *self
SV *item
( run in 1.710 second using v1.01-cache-2.11-cpan-60884a8df76 )