1 Reply Latest reply: Apr 14, 2013 1:00 PM by 1002858 RSS

    when i insert data into db, run-time error occurs.

    1002858
      Hi, I'm a student who studying database.
      I'd like to insert two tables(customer, orders) of TPC-H into berkeley db and primary/foreign key join them.
      So i made two structures for tables and secondary B-Tree index on orders.custkey.
      When i insert data into orders table, run-time error occurs.
      If add annotation before 'orders->associate(orders, txn, sorders, GetCustkey, 0);', there is no error.

      Here is my c source code.
      Could you give me a favor??

      -----
      #include <sys/types.h>
      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      #include "db.h"

      #define PATH "tpch"
      #define CUSTOMER "customer.db"
      #define ORDERS "orders.db"
      #define SORDERS "sorders.db"
      #define ACCESSMETHOD DB_BTREE
      #define ENV_FLAGS DB_CREATE|DB_INIT_TXN|DB_INIT_LOCK|DB_INIT_MPOOL
      #define DB_FLAGS DB_CREATE|DB_AUTO_COMMIT

      typedef int customer_key;
      typedef struct customerdata {
           int          custkey;
           char     name[26];
           char     address[41];
           int          nationkey;
           char     phone[16];
           double     acctbal;
           char     mktsegment[11];
           char     comment[118];
      } customer_data;

      typedef int orders_key;
      typedef struct ordersdata {
           int          orderkey;
           int          custkey;
           char     orderstatus[2];
           double     totalprice;
           char     date[16];
           char     orderpriority[16];
           char     clerk[16];
           int          shippriority;
           char     comment[80];
      } orders_data;

      int GetCustkey(DB secondary, const DBT pkey, const DBT pdata, DBT skey);

      int main()
      {
           /********************
           *     Set Variable     *
           ********************/
           DB_ENV *env=NULL;                                                       // DATABASE
           DB_TXN *txn=NULL;                                                       // TRANSACTION
           DB customer=NULL, orders=NULL, *sorders=NULL;                    // TABLE
           //DBC customer_curs=NULL, join_curs=NULL;//, *carray[2];          // CURSOR
           DBT key, data;                                                            // DBT
           int ret=0, i=0;
           FILE *fp;
           char s[300]={0};
           char *ptr;
           customer_key     ckey;
           customer_data     cdata;
           orders_key          okey;
           orders_data          odata;
                
           /********************
           *     Open Database     *
           ********************/
      if((ret=db_env_create(&env, 0))!=0) goto err;
           if((ret=env->set_cachesize(env,0,2097152,1))!=0) goto err;
           if((ret=env->open(env, PATH, ENV_FLAGS, 0))!=0) goto err;
           if((ret=db_create(&customer, env, 0))!=0) goto err;
           if((ret=db_create(&orders, env, 0))!=0) goto err;
           if((ret=db_create(&sorders, env, 0))!=0) goto err;
           if((ret=customer->set_pagesize(customer,4096))!=0) goto err;
           if((ret=orders->set_pagesize(orders,4096))!=0) goto err;
           if((ret=sorders->set_pagesize(sorders,4096))!=0) goto err;
           if((ret=sorders->set_flags(sorders, DB_DUP | DB_DUPSORT))!=0) goto err;
           if((ret=customer->open(customer, txn, CUSTOMER, NULL, ACCESSMETHOD, DB_FLAGS, 0))!=0) goto err;
           if((ret=orders->open(orders, txn, ORDERS, NULL, ACCESSMETHOD, DB_FLAGS, 0))!=0) goto err;
           if((ret=sorders->open(sorders, txn, SORDERS, NULL, ACCESSMETHOD, DB_FLAGS, 0))!=0) goto err;
           
           /****************************
           *     Make Secondary Index     *
           ****************************/
           orders->associate(orders, txn, sorders, GetCustkey, 0);

           /********************
           *     Insert Data     *
           ********************/
           fp = fopen("orders.tbl", "r");
           while(!feof(fp)) {
                memset(&s,0,sizeof(s));
                if(fgets(s, 300, fp)){
                     memset(&key, 0, sizeof(key));
                     memset(&data, 0, sizeof(data));
                     memset(&okey, 0, sizeof(okey));
                     memset(&odata, 0, sizeof(odata));
                     ptr = strtok(s, "|");
                     okey = atoi(ptr);
                     odata.orderkey = okey;
                     ptr = strtok(NULL, "|");
                     odata.custkey = atoi(ptr);
                     ptr = strtok(NULL, "|");
                     memcpy(odata.orderstatus, ptr, sizeof(odata.orderstatus));
                     ptr = strtok(NULL, "|");
                     odata.totalprice = atof(ptr);
                     ptr = strtok(NULL, "|");
                     memcpy(odata.date, ptr, sizeof(odata.date));
                     ptr = strtok(NULL, "|");
                     memcpy(odata.orderpriority, ptr, sizeof(odata.orderpriority));
                     ptr = strtok(NULL, "|");
                     memcpy(odata.clerk, ptr, sizeof(odata.clerk));
                     ptr = strtok(NULL, "|");
                     odata.shippriority = atoi(ptr);
                     ptr = strtok(NULL, "|");
                     memcpy(odata.comment, ptr, sizeof(odata.comment));
                     key.data = &okey;
                     key.size = sizeof(okey);
                     data.data = &odata;
                     data.size = sizeof(odata);
                     if((ret=orders->put(orders, txn, &key, &data, DB_NOOVERWRITE|DB_AUTO_COMMIT))!=0) goto err;
                     i++;
                }
           }
           fclose(fp);
           printf("Orders : %d data inserted.\n",i);
           i=0;
           fp = fopen("customer.tbl", "r");
           while(!feof(fp)) {
                memset(&s,0,sizeof(s));
                if(fgets(s, 300, fp)){
                     memset(&key, 0, sizeof(key));
                     memset(&data, 0, sizeof(data));
                     memset(&ckey, 0, sizeof(ckey));
                     memset(&cdata, 0, sizeof(cdata));
                     ptr = strtok(s, "|");
                     ckey = atoi(ptr);
                     cdata.custkey = ckey;
                     ptr = strtok(NULL, "|");
                     memcpy(cdata.name, ptr, sizeof(cdata.name));
                     ptr = strtok(NULL, "|");
                     memcpy(cdata.address, ptr, sizeof(cdata.address));
                     ptr = strtok(s, "|");
                     cdata.nationkey = atoi(ptr);
                     ptr = strtok(NULL, "|");
                     memcpy(cdata.phone, ptr, sizeof(cdata.phone));
                     ptr = strtok(NULL, "|");
                     cdata.acctbal = atof(ptr);
                     ptr = strtok(NULL, "|");
                     memcpy(cdata.mktsegment, ptr, sizeof(cdata.mktsegment));
                     ptr = strtok(NULL, "|");
                     memcpy(cdata.comment, ptr, sizeof(cdata.comment));
                     key.data = &ckey;
                     key.size = sizeof(ckey);
                     data.data = &cdata;
                     data.size = sizeof(cdata);
                     if((ret=customer->put(customer, txn, &key, &data, DB_NOOVERWRITE|DB_AUTO_COMMIT))!=0) goto err;
                     i++;
                }
           }
           fclose(fp);
           printf("Customers : %d data inserted.\n",i);
           
      err:
           /********************
           *     Close Database     *
           ********************/
           if(sorders) sorders->close(sorders,0);
           if(customer) customer->close(customer,0);
           if(orders) orders->close(orders,0);
           if(env) env->close(env, 0);

      return (ret);
      }

      int GetCustkey(DB secondary, const DBT pkey, const DBT pdata, DBT skey) {
           memset(skey, 0, sizeof(DBT));
           skey->data = ((orders_data *)pdata->data)->custkey;
           skey->size = sizeof(((orders_data *)pdata->data)->custkey);
           return 0;
      }