src/siri/db/points.c
/*
 * Return NULL in case an error has occurred. This function destroys the
 * original points.
 */
Return NULL in case an error has occurred. This function destroys the
original points.
Return NULL in case an error has occurred . This function destroys the original points .
unsigned char * siridb_points_zip_double(
        siridb_points_t * points,
        uint_fast32_t start,
        uint_fast32_t end,
        uint16_t * cinfo,
        size_t * size)
{
    if (end - start < ZIP_THRESHOLD)
    {
        return POINTS_zip_raw(points, start, end, cinfo, size);
    }
    uint64_t tdiff = 0;
    uint64_t val;
    size_t i = end - 2;
    siridb_point_t * point = points->data + i;
    uint64_t ts = (point + 1)->ts - point->ts;
    uint64_t mask = (point + 1)->val.uint64;
    uint64_t vdiff = mask ^ point->val.uint64;
    uint8_t tcount = 0;
    uint8_t vcount = 0;
    uint8_t vstore = 0;
    uint8_t shift = 0;
    int vshift[RAW_VALUES_THRESHOLD];
    unsigned char * bits, *pt;
    int * pshift;
    while (i-- > start)
    {
        point--;
        vdiff |= mask ^ point->val.uint64;
        tdiff |= ts ^ ((point + 1)->ts - point->ts);
    }
    for (i = 0, mask = 0xff; i < 8; mask <<= 8, i++)
    {
        if (vdiff & mask)
        {
            vstore |= 1 << i;
            vshift[vcount++] = i * 8;
        }
        if (tdiff & mask)
        {
            tcount = i + 1;
        }
        else if (ts & mask)
        {
            shift = i + 1;
        }
    }
    shift = (tcount > shift) ? tcount : shift;
    *cinfo = vstore;
    *cinfo <<= 8;
    *cinfo |= tcount | (shift << 4);
    *size = 16 + shift - tcount + (tcount + vcount) * (end - start - 1);
    bits = (unsigned char *) malloc(*size);
    if (bits == NULL)
    {
        return NULL;
    }
    pt = bits;
    memcpy(pt, &point->ts, sizeof(uint64_t));
    pt += sizeof(uint64_t);
    memcpy(pt, &point->val.uint64, sizeof(uint64_t));
    pt += sizeof(uint64_t);
    for (; shift-- > tcount; ++pt)
    {
        *pt = ts >> (shift * 8);
    }
    for (i = end; --i > start;)
    {
        point++;
        ts = point->ts - (point - 1)->ts;
        for (shift = tcount; shift--; ++pt)
        {
            *pt = ts >> (shift * 8);
        }
        val = point->val.uint64;
        if (vcount > RAW_VALUES_THRESHOLD)
        {
            memcpy(pt, &val, sizeof(uint64_t));
            pt += sizeof(uint64_t);
            continue;
        }
        for (pshift = vshift, shift = vcount; shift--; ++pshift, ++pt)
        {
            *pt = val >> *pshift;
        }
    }
    return bits;
}
unsigned char * siridb_points_zip_double ( siridb_points_t * points , uint_fast32_t start , uint_fast32_t end , uint16_t * cinfo , size_t * size ) { if ( end - start < ZIP_THRESHOLD ) { return POINTS_zip_raw ( points , start , end , cinfo , size ) ; } uint64_t tdiff = 0 ; uint64_t val ; size_t i = end - 2 ; siridb_point_t * point = points -> data + i ; uint64_t ts = ( point + 1 ) -> ts - point -> ts ; uint64_t mask = ( point + 1 ) -> val . uint64 ; uint64_t vdiff = mask ^ point -> val . uint64 ; uint8_t tcount = 0 ; uint8_t vcount = 0 ; uint8_t vstore = 0 ; uint8_t shift = 0 ; int vshift [ RAW_VALUES_THRESHOLD ] ; unsigned char * bits , * pt ; int * pshift ; while ( i -- > start ) { point -- ; vdiff |= mask ^ point -> val . uint64 ; tdiff |= ts ^ ( ( point + 1 ) -> ts - point -> ts ) ; } for ( i = 0 , mask = 0xff ; i < 8 ; mask <<= 8 , i ++ ) { if ( vdiff & mask ) { vstore |= 1 << i ; vshift [ vcount ++ ] = i * 8 ; } if ( tdiff & mask ) { tcount = i + 1 ; } else if ( ts & mask ) { shift = i + 1 ; } } shift = ( tcount > shift ) ? tcount : shift ; * cinfo = vstore ; * cinfo <<= 8 ; * cinfo |= tcount | ( shift << 4 ) ; * size = 16 + shift - tcount + ( tcount + vcount ) * ( end - start - 1 ) ; bits = ( unsigned char * ) malloc ( * size ) ; if ( bits == NULL ) { return NULL ; } pt = bits ; memcpy ( pt , & point -> ts , sizeof ( uint64_t ) ) ; pt += sizeof ( uint64_t ) ; memcpy ( pt , & point -> val . uint64 , sizeof ( uint64_t ) ) ; pt += sizeof ( uint64_t ) ; for ( ; shift -- > tcount ; ++ pt ) { * pt = ts >> ( shift * 8 ) ; } for ( i = end ; -- i > start ; ) { point ++ ; ts = point -> ts - ( point - 1 ) -> ts ; for ( shift = tcount ; shift -- ; ++ pt ) { * pt = ts >> ( shift * 8 ) ; } val = point -> val . uint64 ; if ( vcount > RAW_VALUES_THRESHOLD ) { memcpy ( pt , & val , sizeof ( uint64_t ) ) ; pt += sizeof ( uint64_t ) ; continue ; } for ( pshift = vshift , shift = vcount ; shift -- ; ++ pshift , ++ pt ) { * pt = val >> * pshift ; } } return bits ; }
Return NULL in case an error has occurred.
Return NULL in case an error has occurred .