Add new udafs and RMR support to gsprintconsole_ves
[com/gs-lite.git] / src / lib / gscphftaaux / hfta_udaf.cc
index 7eef46d..a5ff5cb 100644 (file)
@@ -27,6 +27,7 @@ Copyright 2014 AT&T Intellectual Property
 #include <iostream>
 
 #include "hfta_runtime_library.h"
+#include"stringhash.h"
 
 
 #define max(a,b) ((a) > (b) ? (a) : (b))
@@ -666,7 +667,7 @@ void count_diff_hfta_HFTA_AGGR_DESTROY_(gs_sp_t scratch){ }
 //             running_array_aggr aggregate
 
 void running_array_aggr_hfta_HFTA_AGGR_INIT_(vstring* scratch) {
-  scratch->offset = NULL;  
+  scratch->offset = (gs_p_t)NULL;  
   scratch->length = 0;
 }
 
@@ -742,12 +743,12 @@ void CAT_aggr_HFTA_AGGR_REINIT_(gs_sp_t s){
        v->val="";
 }
 void CAT_aggr_HFTA_AGGR_UPDATE_(gs_sp_t s, vstring *sep, vstring *str){
-char buf1[MAXTUPLESZ-20], buf2[MAXTUPLESZ-20];
-int i;
-for(i=0;i<sep->length;++i) buf1[i] = *(((char *)sep->offset)+i);
-buf1[i]='\0';
-for(i=0;i<str->length;++i) buf2[i] = *(((char *)str->offset)+i);
-buf2[i]='\0';
+//char buf1[MAXTUPLESZ-20], buf2[MAXTUPLESZ-20];
+//int i;
+//for(i=0;i<sep->length;++i) buf1[i] = *(((char *)sep->offset)+i);
+//buf1[i]='\0';
+//for(i=0;i<str->length;++i) buf2[i] = *(((char *)str->offset)+i);
+//buf2[i]='\0';
        CAT_aggr_scratch_ptr *p = (CAT_aggr_scratch_ptr *)s;
        CAT_aggr_scratch *v = p->ptr;
        if(v->val.size()>0)
@@ -925,3 +926,148 @@ gs_int64_t extr_running_sum_max(vstring *v){
 }
 
 
+// ---------------------------------------------
+//             aggr_diff : from a sequence of strings, collect
+//               the ones which are different than the previous.
+//               this includes the prior time period.
+//               the idea is to see the sequence of handovers
+
+struct CAT_aggr_diff_scratch{
+       std::string val;
+       std::string prev_s;
+//     gs_int64_t prev_ts;     // for now, just catenate strings
+};
+
+struct CAT_aggr_diff_scratch_ptr{
+       CAT_aggr_diff_scratch *ptr;
+};
+
+
+
+void CAT_aggr_diff_HFTA_AGGR_INIT_(gs_sp_t s){
+       CAT_aggr_diff_scratch_ptr *p = (CAT_aggr_diff_scratch_ptr *)s;
+       CAT_aggr_diff_scratch *v = new CAT_aggr_diff_scratch();
+       v->prev_s = "";
+       v->val = "";
+
+       p->ptr = v;
+}
+void CAT_aggr_diff_HFTA_AGGR_REINIT_(gs_sp_t s){
+       CAT_aggr_diff_scratch_ptr *p = (CAT_aggr_diff_scratch_ptr *)s;
+       CAT_aggr_diff_scratch *v = p->ptr;
+       v->val=v->prev_s;
+}
+void CAT_aggr_diff_HFTA_AGGR_UPDATE_(gs_sp_t s,  vstring *str){
+       char str_buf[MAXTUPLESZ-20];
+       int i;
+       for(i=0;i<str->length;++i) str_buf[i] = *(((char *)str->offset)+i);
+       str_buf[i]='\0';
+
+       CAT_aggr_diff_scratch_ptr *p = (CAT_aggr_diff_scratch_ptr *)s;
+       CAT_aggr_diff_scratch *v = p->ptr;
+       if(str_buf != v->prev_s){
+               if(v->val.size()>0)
+                       v->val += ':';
+               v->val += str_buf;
+               v->prev_s = str_buf;
+       }
+}
+
+void CAT_aggr_diff_HFTA_AGGR_OUTPUT_(vstring *res, gs_sp_t s){
+       CAT_aggr_diff_scratch_ptr *p = (CAT_aggr_diff_scratch_ptr *)s;
+       CAT_aggr_diff_scratch *v = p->ptr;
+//printf("output val=%s\n",v->val.c_str());
+       res->offset = (gs_p_t)malloc(v->val.size());
+       res->length = v->val.size();
+       if(res->length>MAXTUPLESZ-20)
+               res->length=MAXTUPLESZ-20;
+//     v->val.copy((char *)(res->offset), 0, res->length);
+       const char *dat = v->val.c_str();
+       memcpy((char *)(res->offset), dat, res->length);
+//     for(int i=0;i<res->length;++i)
+//             *(((char *)res->offset)+i) = dat[i];
+       res->reserved = INTERNAL;
+}
+void CAT_aggr_diff_HFTA_AGGR_DESTROY_(gs_sp_t s){
+       CAT_aggr_diff_scratch_ptr *p = (CAT_aggr_diff_scratch_ptr *)s;
+       CAT_aggr_diff_scratch *v = p->ptr;
+       delete v;
+}
+
+
+
+// ---------------------------------------------
+//             Approximate count distinct.
+//             Rely on the minhashing approach.
+//             Currently HFTA-only
+//             Uses a 32-bit hash, tested up to 100,000,000 elements
+//             and it gave good results (within 7%)
+
+
+#define COUNT_DISTINCT_NREPS 250
+#define COUNT_DISTINCT_MAX_STRING_LEN 200      // number of 4-byte words
+
+static Hash32bit2univID hids[COUNT_DISTINCT_NREPS];
+static int approx_count_distinct_udaf_initialized = 0;
+struct approx_count_distinct_udaf_str{
+       unsigned int mn[COUNT_DISTINCT_NREPS];
+};
+
+
+void approx_count_distinct_udaf_HFTA_AGGR_INIT_(gs_sp_t buf){
+       approx_count_distinct_udaf_str *cd = (approx_count_distinct_udaf_str *)buf;
+       for(int i=0;i<COUNT_DISTINCT_NREPS;++i)
+               cd->mn[i]=4294967295;
+       if(approx_count_distinct_udaf_initialized==0){
+               for(int i=0;i<COUNT_DISTINCT_NREPS;++i)
+                       hids[i] = InitStringHash32bit2univ(COUNT_DISTINCT_MAX_STRING_LEN);
+               approx_count_distinct_udaf_initialized=1;
+       }
+}
+void running_approx_count_distinct_udaf_HFTA_AGGR_INIT_(gs_sp_t buf){
+       approx_count_distinct_udaf_HFTA_AGGR_INIT_(buf);
+}
+
+void approx_count_distinct_udaf_HFTA_AGGR_REINIT_(gs_sp_t buf){ }
+void running_approx_count_distinct_udaf_HFTA_AGGR_REINIT_(gs_sp_t buf){}
+
+void approx_count_distinct_udaf_HFTA_AGGR_DESTROY_(gs_sp_t buf){ }
+void running_approx_count_distinct_udaf_HFTA_AGGR_DESTROY_(gs_sp_t buf){ }
+
+void approx_count_distinct_udaf_HFTA_AGGR_UPDATE_(gs_sp_t buf, vstring *val){
+       approx_count_distinct_udaf_str *cd = (approx_count_distinct_udaf_str *)buf;
+       unsigned int buffer[sizeof(unsigned int)*COUNT_DISTINCT_MAX_STRING_LEN];
+       buffer[val->length/4] = 0;
+       memcpy((char *)buffer, (char *)val->offset, min(val->length, 800));
+       unsigned int len4 = val->length/4 + ((val->length&0x03)>0);
+
+       for(int i=0; i<COUNT_DISTINCT_NREPS; ++i){
+               unsigned int h = StringHash32bit2univ(buffer, len4, hids[i]);
+               if(h < cd->mn[i]) cd->mn[i] = h;
+       }
+}
+void running_approx_count_distinct_udaf_HFTA_AGGR_UPDATE_(gs_sp_t buf, vstring *val){
+       approx_count_distinct_udaf_HFTA_AGGR_UPDATE_(buf, val);
+}
+
+void approx_count_distinct_udaf_HFTA_AGGR_OUTPUT_(vstring *res, gs_sp_t buf){
+       res->offset = (gs_p_t)buf;
+       res->length = sizeof(approx_count_distinct_udaf_str);
+       res->reserved = SHALLOW_COPY;
+}
+void running_approx_count_distinct_udaf_HFTA_AGGR_OUTPUT_(vstring *res, gs_sp_t buf){
+       approx_count_distinct_udaf_HFTA_AGGR_OUTPUT_(res, buf);
+}
+
+gs_float_t extr_approx_count_distinct(vstring *v){
+       approx_count_distinct_udaf_str *cd = (approx_count_distinct_udaf_str *)(v->offset);
+       gs_float_t avg = 0.0;
+       for(int i=0;i<COUNT_DISTINCT_NREPS;++i){
+               avg += cd->mn[i];
+       }
+       avg /= COUNT_DISTINCT_NREPS;
+       gs_float_t est = (4294967295.0 / avg) - 1;
+       return est;
+}
+
+