Search Postgresql Archives

Re: Persistence problem

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



OK, here is the part of the code.

typedef struct {
    int4 length;
    int noOfUnits;
    void *units;
} mapping_t;

typedef struct {
    timeint_t interval;
    double x1, x0, y1, y0;
    // fx(t) = x1*t+x0, fy(t) = y1*t+y0
} upoint_t;

typedef struct {
    time_T start, end;
    short int LC, RC; // left or right closed
} timeint_t;

typedef struct {
    double time;
    short int infinity;
} time_T;

typedef upoint_t    uPoint;
typedef mapping_t    mPoint;

mPoint * create_mPoint(char *str) {
    /*
     *
     * str: ((start end x1 x0 y1 y0),(start end x1 x0 y1 y0),...)
     *
     */

    char *unit = NULL;
    mPoint * result = NULL;
    uPoint * units = NULL;
    uPoint * singleUnit;
    int stringLength;
    char *strTemp;
    int i;

    if (str == NULL) {
        return NULL;
    }

    stringLength = strlen(str);

    if (stringLength < 2 || str[0] != '(' || str[stringLength-1] != ')') {
        return NULL;
    } else {
        str = &str[1];
        strTemp = (char *) palloc ((stringLength-1)*sizeof(char));
        strncpy(strTemp, str, stringLength-2);
        strTemp[stringLength-2] = '\0';
        str = strTemp;
    };

    // allocating memory
    result = (mPoint *) palloc(sizeof(mPoint));
   
    result->noOfUnits = 0;
    unit = strtok(str, ",");   
   
    while (unit != NULL) {
        Log ("create_mPoint: str units:");
        Log2 ("\tunit:", unit);

        result->noOfUnits++;

        singleUnit = create_uPoint(unit);
       
        if (singleUnit == NULL) {
            return NULL;
        }

        units = (uPoint *) realloc(units, result->noOfUnits * sizeof(uPoint)); // EXPLAINED AT THE END OF THE POST
        units[result->noOfUnits - 1] = * singleUnit;

        unit = strtok(NULL, ",");
    };

    result->units = units;                     // EXPLAINED AT THE END OF THE POST
   
    if (SortUnits(result, MPOINT) == NULL) {
        return NULL;
    };

    result->length = sizeof(int4) + sizeof(int) + result->noOfUnits * sizeof(uPoint);
   
    //pfree(singleUnit);
    //pfree(strTemp);
    //pfree(unit);

    Log ("create_mPoint: moving type created");

    return result;
};


uPoint * create_uPoint(char *str) {
    double startTime, endTime;
    double x0, x1, y0, y1;
    timeint_t * interval;

    uPoint *result;

    if (sscanf(str, " ( %lf %lf %lf %lf %lf %lf )", &startTime, &endTime, &x1, &x0, &y1, &y0) != 6) {
        return NULL;
    }

    // allocate memory
    result = (uPoint *) palloc(sizeof(uPoint));
   
    result->x0 = x0;
    result->x1 = x1;
    result->y0 = y0;
    result->y1 = y1;

    // insert interval
    interval = (timeint_t *) createTimeInterval(startTime, endTime);
    if (interval == NULL) {
        return NULL;
    };
   
    result->interval = *interval;

    Log ("create_uPoint: uPoint (unit type) created");

    return result;
};


timeint_t * createTimeInterval(double startTime, double endTime) {
    timeint_t * interval = (timeint_t *) palloc(sizeof(timeint_t));

    Log ("createTimeInterval: entering function");
   
    if (startTime > endTime) {
        return NULL;
    };
   
    interval->start.time = startTime;
    interval->end.time = endTime;
   
    if (startTime < 0) {
        interval->LC = 0;
        interval->start.infinity = 1;
    }
    else {
        interval->LC = 1;
        interval->start.infinity = 0;
    }
    if (endTime < 0) {
        interval->RC = 0;
        interval->end.infinity = 1;
    }
    else {
        interval->RC = 1;
        interval->end.infinity = 1;
    }

    return interval;
};

The part where I've used realloc and set result->units=units, I've tried to replace it with palloc in the way to create the array units and then do this:

result->units = (uPoint *) palloc(result->noOfUnits * sizeof(uPoint));
memcpy(result->units, units, result->noOfUnits * sizeof(uPoint));

I've also tried to reserve the whole memory with palloc:

result = (mPoint *)palloc(sizeof(int4) + sizeof(int) + noOfUnits*sizeof(uPoint));
result->noOfUnits = noOfUnits;
result->length = sizeof(int4) + sizeof(int) + noOfUnits*sizeof(uPoint);
memcpy(result->units, units, result->noOfUnits * sizeof(uPoint));

I've then tried to palloc both result and result->units... I just don't know how I should do it. :-/
I hope you understand the problem better now.

Thanks,
Ivan

> To: i.bre@xxxxxxxx
> CC: kleptog@xxxxxxxxx; dalroi@xxxxxxxxxxxxxxxxxxxxxxxxxxxx; pgsql-general@xxxxxxxxxxxxxx
> Subject: Re: Persistence problem
> Date: Fri, 14 May 2010 17:32:49 -0400
> From: tgl@xxxxxxxxxxxxx
>
> "I. B." <i.bre@xxxxxxxx> writes:
> > How to fix this?
>
> As long as you keep on showing us wrappers, and not the code that
> actually does the work, we're going to remain in the dark. What
> you have shown us just copies data from point A to point B, and
> it looks like it would be fine if the source data conforms to
> PG's expectations. But the problem is presumably that the source
> data *doesn't* conform to those expectations.
>
> regards, tom lane


Hotmail: Free, trusted and rich email service. Get it now.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux