Example #1
0
/*!
 * bigint start_vid,
 * bigint end_vid,
 * float agg_cost,
 */
void pgr_get_matrixRows(
        char *sql,
        Matrix_cell_t **rows,
        size_t *total_rows) {
    clock_t start_t = clock();

    const int tuple_limit = 1000000;

    size_t ntuples;
    size_t total_tuples = 0;

    Column_info_t info[3];

    int i;
    for (i = 0; i < 3; ++i) {
        info[i].colNumber = -1;
        info[i].type = 0;
        info[i].strict = true;
        info[i].eType = ANY_INTEGER;
    }
    info[0].name = strdup("start_vid");
    info[1].name = strdup("end_vid");
    info[2].name = strdup("agg_cost");

    info[2].eType = ANY_NUMERICAL;


    void *SPIplan;
    SPIplan = pgr_SPI_prepare(sql);

    Portal SPIportal;
    SPIportal = pgr_SPI_cursor_open(SPIplan);


    bool moredata = TRUE;
    (*total_rows) = total_tuples;

    while (moredata == TRUE) {
        SPI_cursor_fetch(SPIportal, TRUE, tuple_limit);
        if (total_tuples == 0)
            pgr_fetch_column_info(info, 3);

        ntuples = SPI_processed;
        total_tuples += ntuples;

        if (ntuples > 0) {
            if ((*rows) == NULL)
                (*rows) = (Matrix_cell_t *)palloc0(
                        total_tuples * sizeof(Matrix_cell_t));
            else
                (*rows) = (Matrix_cell_t *)repalloc(
                        (*rows), total_tuples * sizeof(Matrix_cell_t));

            if ((*rows) == NULL) {
                elog(ERROR, "Out of memory");
            }

            SPITupleTable *tuptable = SPI_tuptable;
            TupleDesc tupdesc = SPI_tuptable->tupdesc;
            PGR_DBG("processing %ld edge tupĺes", ntuples);

            size_t t;
            for (t = 0; t < ntuples; t++) {
                HeapTuple tuple = tuptable->vals[t];
                pgr_fetch_row(&tuple, &tupdesc, info,
                        &(*rows)[total_tuples - ntuples + t]);
            }
            SPI_freetuptable(tuptable);
        } else {
            moredata = FALSE;
        }
    }

    SPI_cursor_close(SPIportal);


    if (total_tuples == 0) {
        (*total_rows) = 0;
        PGR_DBG("NO rows");
        return;
    }

    (*total_rows) = total_tuples;
    time_msg(" reading Edges", start_t, clock());
}
Example #2
0
void
pgr_get_restriction_data(
        char *restrictions_sql,
        Restrict_t **restrictions,
        size_t *total_restrictions) {
    const int tuple_limit = 1000000;
    clock_t start_t = clock();

    PGR_DBG("pgr_get_restriction_data");
    PGR_DBG("%s", restrictions_sql);
    Column_info_t info[3];

    int i;
    for (i = 0; i < 3; ++i) {
        info[i].colNumber = -1;
        info[i].type = 0;
        info[i].strict = true;
        info[i].eType = ANY_INTEGER;
    }
    info[0].name = strdup("id");
    info[1].name = strdup("cost");
    info[2].name = strdup("restricted_edges");

    info[1].eType = ANY_NUMERICAL;
    info[2].eType = ANY_INTEGER_ARRAY;

#if 0
    // experiment starts

    size_t total_tuples = (*total_restrictions) ;

    (*restrictions) = (Restrict_t *)palloc0(sizeof(Restrict_t));

    (*restrictions)[0].id = 1;
    (*restrictions)[0].cost = -1;
    (*restrictions)[0].restricted_edges[0] = 4;
    (*restrictions)[0].restricted_edges[1] = 7;

    // experiment ends
#endif

#if 1
    size_t ntuples;
    size_t total_tuples;

    void *SPIplan;
    SPIplan = pgr_SPI_prepare(restrictions_sql);
    Portal SPIportal;
    SPIportal = pgr_SPI_cursor_open(SPIplan);

    bool moredata = TRUE;
    (*total_restrictions) = total_tuples = 0;


    while (moredata == TRUE) {
        SPI_cursor_fetch(SPIportal, TRUE, tuple_limit);
        if (total_tuples == 0) {
            pgr_fetch_column_info(info, 3);
        }
        ntuples = SPI_processed;
        total_tuples += ntuples;
        PGR_DBG("SPI_processed %ld", ntuples);
        if (ntuples > 0) {
            if ((*restrictions) == NULL)
                (*restrictions) = (Restrict_t *)palloc0(
                        total_tuples * sizeof(Restrict_t));
            else
                (*restrictions) = (Restrict_t *)repalloc(
                        (*restrictions),
                        total_tuples * sizeof(Restrict_t));

            if ((*restrictions) == NULL) {
                elog(ERROR, "Out of memory");
            }

            size_t t;
            SPITupleTable *tuptable = SPI_tuptable;
            TupleDesc tupdesc = SPI_tuptable->tupdesc;
            PGR_DBG("processing %ld", ntuples);
            for (t = 0; t < ntuples; t++) {
                HeapTuple tuple = tuptable->vals[t];
                fetch_restriction(&tuple, &tupdesc, info,
                        &(*restrictions)[total_tuples - ntuples + t]);
            }
            SPI_freetuptable(tuptable);
        } else {
            moredata = FALSE;
        }
    }

    SPI_cursor_close(SPIportal);

    if (total_tuples == 0) {
        (*total_restrictions) = 0;
        PGR_DBG("NO restrictions");
        return;
    }

    (*total_restrictions) = total_tuples;
#endif
    PGR_DBG("Finish reading %ld data, %ld",
            total_tuples,
            (*total_restrictions));
    clock_t end_t = clock();
    time_msg(" reading Restrictions", start_t, end_t);
}
Example #3
0
void
pgr_get_customers_data(
        char *customers_sql,
        Customer_t **customers,
        size_t *total_customers) {
    const int tuple_limit = 1000000;

    PGR_DBG("pgr_get_customers_data");
    PGR_DBG("%s", customers_sql);

    Column_info_t info[9];

    int i;
    for (i = 0; i < 9; ++i) {
        info[i].colNumber = -1;
        info[i].type = 0;
        info[i].strict = true;
        info[i].eType = ANY_NUMERICAL;
    }

    /*!
      int64_t id;
      double x;
      double y;
      double demand;
      double Etime;
      double Ltime;
      double Stime;
      int64_t Pindex;
      int64_t Dindex;
      double Ddist;
      */

    info[0].name = strdup("id");
    info[1].name = strdup("x");
    info[2].name = strdup("y");
    info[3].name = strdup("demand");
    info[4].name = strdup("opentime");
    info[5].name = strdup("closetime");
    info[6].name = strdup("servicetime");
    info[7].name = strdup("pindex");
    info[8].name = strdup("dindex");

    info[0].eType = ANY_INTEGER;
    info[7].eType = ANY_INTEGER;
    info[8].eType = ANY_INTEGER;


    size_t ntuples;
    size_t total_tuples;

    void *SPIplan;
    SPIplan = pgr_SPI_prepare(customers_sql);
    Portal SPIportal;
    SPIportal = pgr_SPI_cursor_open(SPIplan);

    bool moredata = TRUE;
    (*total_customers) = total_tuples = 0;

    /* on the first tuple get the column numbers */

    while (moredata == TRUE) {
        SPI_cursor_fetch(SPIportal, TRUE, tuple_limit);
        if (total_tuples == 0) {
            pgr_fetch_column_info(info, 9);
        }
        ntuples = SPI_processed;
        total_tuples += ntuples;
        PGR_DBG("SPI_processed %ld", ntuples);
        if (ntuples > 0) {
            if ((*customers) == NULL)
                (*customers) = (Customer_t *)palloc0(
                        total_tuples * sizeof(Customer_t));
            else
                (*customers) = (Customer_t *)repalloc(
                        (*customers), total_tuples * sizeof(Customer_t));

            if ((*customers) == NULL) {
                elog(ERROR, "Out of memory");
            }

            size_t t;
            SPITupleTable *tuptable = SPI_tuptable;
            TupleDesc tupdesc = SPI_tuptable->tupdesc;
            PGR_DBG("processing %ld", ntuples);
            for (t = 0; t < ntuples; t++) {
                HeapTuple tuple = tuptable->vals[t];
                fetch_customer(&tuple, &tupdesc, info,
                        &(*customers)[total_tuples - ntuples + t]);
            }
            SPI_freetuptable(tuptable);
        } else {
            moredata = FALSE;
        }
    }

    SPI_cursor_close(SPIportal);

    if (total_tuples == 0) {
        (*total_customers) = 0;
        PGR_DBG("NO customers");
        return;
    }

    (*total_customers) = total_tuples;
    PGR_DBG("Finish reading %ld data, %ld", total_tuples, (*total_customers));
}
Example #4
0
static
void
get_edges_basic(
    char *sql,
    pgr_basic_edge_t **edges,
    size_t *totalTuples,
    bool ignore_id) {
    clock_t start_t = clock();

    const int tuple_limit = 1000000;

    size_t ntuples;
    size_t total_tuples;
    size_t valid_edges;

    Column_info_t info[5];

    int i;
    for (i = 0; i < 5; ++i) {
        info[i].colNumber = -1;
        info[i].type = 0;
        info[i].strict = true;
        info[i].eType = ANY_INTEGER;
    }
    info[0].name = strdup("id");
    info[1].name = strdup("source");
    info[2].name = strdup("target");
    info[3].name = strdup("going");
    info[4].name = strdup("coming");

    info[0].strict = !ignore_id;
    info[4].strict = false;

    info[3].eType = ANY_NUMERICAL;
    info[4].eType = ANY_NUMERICAL;


    void *SPIplan;
    SPIplan = pgr_SPI_prepare(sql);

    Portal SPIportal;
    SPIportal = pgr_SPI_cursor_open(SPIplan);


    bool moredata = TRUE;
    (*totalTuples) = total_tuples = valid_edges = 0;


    int64_t default_id = 0;
    while (moredata == TRUE) {
        SPI_cursor_fetch(SPIportal, TRUE, tuple_limit);
        if (total_tuples == 0)
            pgr_fetch_column_info(info, 5);

        ntuples = SPI_processed;
        total_tuples += ntuples;

        if (ntuples > 0) {
            if ((*edges) == NULL)
                (*edges) = (pgr_basic_edge_t *)palloc0(
                        total_tuples * sizeof(pgr_basic_edge_t));
            else
                (*edges) = (pgr_basic_edge_t *)repalloc(
                        (*edges), total_tuples * sizeof(pgr_basic_edge_t));

            if ((*edges) == NULL) {
                elog(ERROR, "Out of memory");
            }

            size_t t;
            SPITupleTable *tuptable = SPI_tuptable;
            TupleDesc tupdesc = SPI_tuptable->tupdesc;

            for (t = 0; t < ntuples; t++) {
                HeapTuple tuple = tuptable->vals[t];
                fetch_basic_edge(&tuple, &tupdesc, info,
                           &default_id,
                           &(*edges)[total_tuples - ntuples + t],
                           &valid_edges);
            }
            SPI_freetuptable(tuptable);
        } else {
            moredata = FALSE;
        }
    }

    SPI_cursor_close(SPIportal);

    if (total_tuples == 0 || valid_edges == 0) {
        PGR_DBG("No edges found");
    }

    (*totalTuples) = total_tuples;
    PGR_DBG("Reading %ld edges", total_tuples);
    time_msg("reading edges", start_t, clock());
}