src/siri/db/points.c
/*
 * Returns NULL and raises a SIGNAL in case an error has occurred.
 * (err_msg is set when an error has occurred)
 * Use this function only when having at least two 'series' in the list.
 */
Returns NULL and raises a SIGNAL in case an error has occurred.
(err_msg is set when an error has occurred)
Use this function only when having at least two 'series' in the list.
Returns NULL and raises a SIGNAL in case an error has occurred . ( err_msg is set when an error has occurred ) Use this function only when having at least two ' series ' in the list .
siridb_points_t * siridb_points_merge(slist_t * plist, char * err_msg)
{
#ifdef DEBUG
    assert (plist->len >= 2);
#endif
    siridb_points_t * points;
    siridb_points_t * tpts = NULL;
    size_t n = 0;
    size_t i;
    uint8_t int2double = 0;
    for (i = 0; i < plist->len; )
    {
        points = (siridb_points_t *) plist->data[i];
        if (!points->len)
        {
            if (!--plist->len)
            {
                return plist->data[i];
            }
            siridb_points_free(plist->data[i]);
            plist->data[i] = plist->data[plist->len];
            continue;
        }
        n += points->len;
        if (tpts != NULL && tpts->tp != points->tp)
        {
            if (tpts->tp == TP_STRING || points->tp == TP_STRING)
            {
                sprintf(err_msg, "Cannot merge string and number series.");
                return NULL;
            }
            int2double = 1;
        }
        points->len--;
        tpts = points;
        i++;
    }
#ifdef DEBUG
    assert (plist->len >= 1);
#endif
    if (plist->len == 1)
    {
        points = (siridb_points_t *) slist_pop(plist);
        points->len++;
        return points;
    }
    points = siridb_points_new(n, (int2double) ? TP_DOUBLE : tpts->tp);
    if (points == NULL)
    {
        sprintf(err_msg, "Memory allocation error.");
    }
    else
    {
        usleep(1000);
        if (int2double)
        {
            size_t j;
            for (i = 0; i < plist->len; i++)
            {
                tpts = (siridb_points_t *) plist->data[i];
                if (tpts->tp == TP_INT)
                {
                    for (j = 0; j <= tpts->len; j++)
                    {
                        tpts->data[j].val.real =
                                (double) tpts->data[j].val.int64;
                    }
                    usleep(100);
                }
            }
        }
        points->len = n;
        if (plist->len < 4)
        {
            POINTS_sort_while_merge(plist, points);
        }
        else if (n == plist->len || n < POINTS_MAX_QSORT)
        {
            POINTS_merge_and_sort(plist, points);
        }
        else
        {
            POINTS_highest_and_merge(plist, points);
        }
    }
    return points;
}
siridb_points_t * siridb_points_merge ( slist_t * plist , char * err_msg ) { #ifdef DEBUG assert ( plist -> len >= 2 ) ; #endif siridb_points_t * points ; siridb_points_t * tpts = NULL ; size_t n = 0 ; size_t i ; uint8_t int2double = 0 ; for ( i = 0 ; i < plist -> len ; ) { points = ( siridb_points_t * ) plist -> data [ i ] ; if ( ! points -> len ) { if ( ! -- plist -> len ) { return plist -> data [ i ] ; } siridb_points_free ( plist -> data [ i ] ) ; plist -> data [ i ] = plist -> data [ plist -> len ] ; continue ; } n += points -> len ; if ( tpts != NULL && tpts -> tp != points -> tp ) { if ( tpts -> tp == TP_STRING || points -> tp == TP_STRING ) { sprintf ( err_msg , " " ) ; return NULL ; } int2double = 1 ; } points -> len -- ; tpts = points ; i ++ ; } #ifdef DEBUG assert ( plist -> len >= 1 ) ; #endif if ( plist -> len == 1 ) { points = ( siridb_points_t * ) slist_pop ( plist ) ; points -> len ++ ; return points ; } points = siridb_points_new ( n , ( int2double ) ? TP_DOUBLE : tpts -> tp ) ; if ( points == NULL ) { sprintf ( err_msg , " " ) ; } else { usleep ( 1000 ) ; if ( int2double ) { size_t j ; for ( i = 0 ; i < plist -> len ; i ++ ) { tpts = ( siridb_points_t * ) plist -> data [ i ] ; if ( tpts -> tp == TP_INT ) { for ( j = 0 ; j <= tpts -> len ; j ++ ) { tpts -> data [ j ] . val . real = ( double ) tpts -> data [ j ] . val . int64 ; } usleep ( 100 ) ; } } } points -> len = n ; if ( plist -> len < 4 ) { POINTS_sort_while_merge ( plist , points ) ; } else if ( n == plist -> len || n < POINTS_MAX_QSORT ) { POINTS_merge_and_sort ( plist , points ) ; } else { POINTS_highest_and_merge ( plist , points ) ; } } return points ; }
Returns NULL and raises a SIGNAL in case an error has occurred.
Returns NULL and raises a SIGNAL in case an error has occurred .
/* all series are empty, return the last one */ /* cleanup empty points */ /* shrink plist length and fill gap */ /*
         * Decrement the points->len so we can use this not truly as length
         * property but as position of the last element.
         */ /* we have at least one series left */ /*
         * Return the only left points since there is nothing to merge as set
         * list length to 0. We do have to restore the points length since
         * this is decremented by one.
         */ /*
         * When both series from type double and type integer are merged
         * we need to promote the integer series to double.
         */ /*
         * Select a merge method depending on the ratio series and points.
         */