Example #1
0
void add_to_search_space( State S, int op, int father )

{

    /* see if state is already a part of this search space
     */
    if ( state_hashed( S ) ) {
        return;
    }

    source_to_dest( &(lsearch_space[lspace_end].S), S );
    lsearch_space[lspace_end].op = op;
    lsearch_space[lspace_end].father = father;
    if ( father == -1 ) {
        lsearch_space[lspace_end].depth = 1;
    } else {
        lsearch_space[lspace_end].depth = lsearch_space[father].depth + 1;
    }

    lspace_end++;
    if ( lspace_end == MAX_SPACE ) {
        printf("\nsearch space size too small! currently %d\n\n", MAX_SPACE);
        exit( 1 );
    }

    lspace_size++;

    hash_state( S );

}
Example #2
0
Bool search_A_for_better_state( State S, int h, State *S_, int *h_ )

{

    int i, h__;
    State S__;

    lspace_start = 0;
    lspace_end = 0;
    lspace_size = 0;

    hash_state( S );

    for ( i = 0; i < gnum_A; i++ ) {
        result_to_dest( &S__, S, gA[i] );
        add_to_search_space( S__, gA[i], -1 );
    }

    while ( TRUE ) {
        if ( lspace_start == lspace_end ) {
            return FALSE;
        }
        h__ = expand_A_first_node( h );
        if ( LESS( h__, h ) ) {
            break;
        }
    }

    reset_hash_entrys();

    extract_plan_fragment();

    source_to_dest( S_, lsearch_space[lspace_start].S );
    *h_ = h__;

    if ( lsearch_space[lspace_start].depth > gmax_search_depth ) {
        gmax_search_depth = lsearch_space[lspace_start].depth;
    }

    if ( lspace_size > gmax_search_size ) {
        gmax_search_size = lspace_size;
    }

    return TRUE;

}
Example #3
0
File: values.c Project: schani/chpp
void
encodeValue (value *theValue, outputWriter *ow)
{
    assert(theValue != 0);

    switch (theValue->type)
    {
	case VALUE_UNDEFINED :
	    return;

	case VALUE_INTERNAL :
	    OUT_STRING(ow, "<internal>", 10);
	    break;

	case VALUE_BUILT_IN :
	    OUT_STRING(ow, "<builtIn>", 9);
	    break;

	case VALUE_SCALAR :
	    encodeDynstring(&theValue->v.scalar.scalar, ow);
	    break;

	case VALUE_LIST :
	    {
		int i;

		OUT_CHAR(ow, metaChar);
		OUT_STRING(ow, "list(", 5);
		if (valueListLength(theValue) > 0)
		{
		    encodeValue(valueListGetElement(theValue, 0), ow);
		    for (i = 1; i < valueListLength(theValue); ++i)
		    {
			OUT_CHAR(ow, ',');
			encodeValue(valueListGetElement(theValue, i), ow);
		    }
		}
		OUT_CHAR(ow, ')');
	    }
	    break;

	case VALUE_HASH :
	    {
		value *aValue,
		    *keyValue;
		char *aKey;
		hstate state;

		OUT_CHAR(ow, metaChar);
		OUT_STRING(ow, "hash(", 5);
		state = hash_state(theValue->v.hash.hash);
		if ((aValue = (value*)hash_next(&state, &aKey)) != 0)
		{
		    keyValue = valueNewScalarFromCString(aKey);
		    encodeValue(keyValue, ow);
		    OUT_CHAR(ow, ',');
		    encodeValue(aValue, ow);
		    while ((aValue = (value*)hash_next(&state, &aKey)) != 0)
		    {
			OUT_CHAR(ow, ',');
			keyValue = valueNewScalarFromCString(aKey);
			encodeValue(keyValue, ow);
			OUT_CHAR(ow, ',');
			encodeValue(aValue, ow);
		    }
		}
		OUT_CHAR(ow, ')');
	    }
	    break;

	case VALUE_LAMBDA :
	    {
		int i;

		OUT_CHAR(ow, metaChar);
		OUT_STRING(ow, "lambda(", 7);
		for (i = 0; i < theValue->v.lambda.numParams; ++i)
		{
		    encodeDynstring(&theValue->v.lambda.paramNames[i], ow);
		    OUT_CHAR(ow, ',');
		}
		encodeBytecode(theValue->v.lambda.code, ow);
		OUT_CHAR(ow, ')');
	    }
	    break;

	case VALUE_ENVIRONMENT :
	    OUT_STRING(ow, "<environment>", 13);
	    break;

	case VALUE_BYTECODE :
	    OUT_STRING(ow, "<bytecode>", 10);
	    break;

	case VALUE_WHATSIT :
	    OUT_STRING(ow, "<whatsit>", 9);
	    break;

	default :
	    assert(0);
    }
}
int main(int argc, char **argv)
{
    // time the program
    struct timeval sysTimeStart, sysTimeEnd;
    gettimeofday(&sysTimeStart, NULL);
    
    mkdir("bplus", 0777);
    
    int i;
    // open file count information
    file_count_t *fc = read_file_count();
    
    // keep track of roots
    bplus_roots_t bpr;
    
    // users
    {
        printf("\n");
        printf("Making user B+ tree with %d users\n", fc->users);
        
        // time this section
        struct timeval startSysTimeSub, endSysTimeSub;
        gettimeofday(&startSysTimeSub, NULL);
        
        // create root
        int_node_t root;
        root.next = -1;
        root.previous = -1;
        root.tableType = TABLE_TYPE_USER;
        root.nodeType = NODE_TYPE_LEAF;
        root.count = 0;
        root.firstFile = -1;
        write_node(0, &root);
        
        int rootFileNum = 0;
        
        for (i = 0; i < fc->users; i++)
        {
            user_t *user = read_user(i);
            rootFileNum = insert_node(rootFileNum, TABLE_TYPE_USER, user->stateId, i);
            free_user(user);
        }
        
        bpr.user = rootFileNum;
        
        // end timing this section
        gettimeofday(&endSysTimeSub, NULL);
        float totalTime = (endSysTimeSub.tv_sec - startSysTimeSub.tv_sec)
        + (endSysTimeSub.tv_usec - startSysTimeSub.tv_usec) / 1000000.0f;
        printf("B+ tree creation time:  %f seconds\n", totalTime);
    }
    
    // cities
    {
        printf("\n");
        printf("Making city B+ tree with %d cities\n", fc->cities);
        
        // time this section
        struct timeval startSysTimeSub, endSysTimeSub;
        gettimeofday(&startSysTimeSub, NULL);
        
        // create root
        int_node_t root;
        root.next = -1;
        root.previous = -1;
        root.tableType = TABLE_TYPE_CITY;
        root.nodeType = NODE_TYPE_LEAF;
        root.count = 0;
        root.firstFile = -1;
        write_node(0, &root);
        
        int rootFileNum = 0;
        
        for (i = 0; i < fc->cities; i++)
        {
            city_t *city = read_city(i);
            rootFileNum = insert_node(rootFileNum, TABLE_TYPE_CITY, city->stateId, i);
            free_city(city);
        }
        
        bpr.city = rootFileNum;
        
        // end timing this section
        gettimeofday(&endSysTimeSub, NULL);
        float totalTime = (endSysTimeSub.tv_sec - startSysTimeSub.tv_sec)
        + (endSysTimeSub.tv_usec - startSysTimeSub.tv_usec) / 1000000.0f;
        printf("B+ tree creation time:  %f seconds\n", totalTime);
    }
    
    //states
    {
        printf("\n");
        printf("Making state B+ tree with %d states\n", fc->states);
        
        // time this section
        struct timeval startSysTimeSub, endSysTimeSub;
        gettimeofday(&startSysTimeSub, NULL);
        
        // create root
        int_node_t root;
        root.next = -1;
        root.previous = -1;
        root.tableType = TABLE_TYPE_STATE;
        root.nodeType = NODE_TYPE_LEAF;
        root.count = 0;
        root.firstFile = -1;
        write_node(0, &root);
        
        int rootFileNum = 0;
        
        for (i = 0; i < fc->states; i++)
        {
            state_t *state = read_state(i);
            rootFileNum = insert_node(rootFileNum, TABLE_TYPE_STATE, (int) hash_state(state), i);
            free_state(state);
        }
        
        bpr.state = rootFileNum;
        
        // end timing this section
        gettimeofday(&endSysTimeSub, NULL);
        float totalTime = (endSysTimeSub.tv_sec - startSysTimeSub.tv_sec)
        + (endSysTimeSub.tv_usec - startSysTimeSub.tv_usec) / 1000000.0f;
        printf("B+ tree creation time:  %f seconds\n", totalTime);
    }
    
    // messages
    {
        printf("\n");
        printf("Making message B+ tree with %d messages\n", fc->messages);
        
        // time this section
        struct timeval startSysTimeSub, endSysTimeSub;
        gettimeofday(&startSysTimeSub, NULL);
        
        // create root
        int_node_t root;
        root.next = -1;
        root.previous = -1;
        root.tableType = TABLE_TYPE_MESSAGE;
        root.nodeType = NODE_TYPE_LEAF;
        root.count = 0;
        root.firstFile = -1;
        write_node(0, &root);
        
        int rootFileNum = 0;
        
        for (i = 0; i < fc->messages; i++)
        {
            message_t *message = read_message(i);
            rootFileNum = insert_node(rootFileNum, TABLE_TYPE_MESSAGE, message->timestampId, i);
            free_message(message);
        }
        
        bpr.message = rootFileNum;
        
        // end timing this section
        gettimeofday(&endSysTimeSub, NULL);
        float totalTime = (endSysTimeSub.tv_sec - startSysTimeSub.tv_sec)
        + (endSysTimeSub.tv_usec - startSysTimeSub.tv_usec) / 1000000.0f;
        printf("B+ tree creation time:  %f seconds\n", totalTime);
    }
    
    // timestamps
    {
        printf("\n");
        printf("Making timestamp B+ tree with %d timestamps\n", fc->timestamps);
        
        // time this section
        struct timeval startSysTimeSub, endSysTimeSub;
        gettimeofday(&startSysTimeSub, NULL);
        
        // create root
        int_node_t root;
        root.next = -1;
        root.previous = -1;
        root.tableType = TABLE_TYPE_TIMESTAMP;
        root.nodeType = NODE_TYPE_LEAF;
        root.count = 0;
        root.firstFile = -1;
        write_node(0, &root);
        
        int rootFileNum = 0;
        
        for (i = 0; i < fc->timestamps; i++)
        {
            timestamp_t *timestamp = read_timestamp(i);
            rootFileNum = insert_node(rootFileNum, TABLE_TYPE_TIMESTAMP, (int) hash_timestamp(timestamp), i);
            free_timestamp(timestamp);
        }
        
        bpr.timestamp = rootFileNum;
        
        // end timing this section
        gettimeofday(&endSysTimeSub, NULL);
        float totalTime = (endSysTimeSub.tv_sec - startSysTimeSub.tv_sec)
        + (endSysTimeSub.tv_usec - startSysTimeSub.tv_usec) / 1000000.0f;
        printf("B+ tree creation time:  %f seconds\n", totalTime);
    }
    
    // datestamps
    {
        printf("\n");
        printf("Making datestamp B+ tree with %d datestamps\n", fc->datestamps);
        
        // time this section
        struct timeval startSysTimeSub, endSysTimeSub;
        gettimeofday(&startSysTimeSub, NULL);
        
        
        // create root
        int_node_t root;
        root.next = -1;
        root.previous = -1;
        root.tableType = TABLE_TYPE_DATESTAMP;
        root.nodeType = NODE_TYPE_LEAF;
        root.count = 0;
        root.firstFile = -1;
        write_node(0, &root);
        
        int rootFileNum = 0;
        
        for (i = 0; i < fc->datestamps; i++)
        {
            datestamp_t *datestamp = read_datestamp(i);
            rootFileNum = insert_node(rootFileNum, TABLE_TYPE_DATESTAMP, (int) hash_datestamp(datestamp), i);
            free_datestamp(datestamp);
        }
        
        bpr.datestamp = rootFileNum;
        
        // end timing this section
        gettimeofday(&endSysTimeSub, NULL);
        float totalTime = (endSysTimeSub.tv_sec - startSysTimeSub.tv_sec)
        + (endSysTimeSub.tv_usec - startSysTimeSub.tv_usec) / 1000000.0f;
        printf("B+ tree creation time:  %f seconds\n", totalTime);
    }
    
    free_file_count(fc);
    
    printf("\n");
    
    print_bplus_roots(&bpr);
    write_bplus_roots(&bpr);
    
    // end timing the program
    gettimeofday(&sysTimeEnd, NULL);
    float totalTime = (sysTimeEnd.tv_sec - sysTimeStart.tv_sec)
    + (sysTimeEnd.tv_usec - sysTimeStart.tv_usec) / 1000000.0f;
    printf("Process time %f seconds\n", totalTime);
    
    return 0;
}
int main(int argc, char **argv)
{
    //print usage if needed
    if (argc != 2) {
        fprintf(stderr, "Usage: %s totalRecordNumber\n", argv[0]);
        exit(0);
    }
    //get total record number from argument
    int totalRecordNumber = atoi(argv[1]);

    // time the program
    struct timeval sysTimeStart, sysTimeEnd;
    gettimeofday(&sysTimeStart, NULL);

    // set up directories;
    mkdir("cities", 0777);
    mkdir("datestamps", 0777);
    mkdir("messages", 0777);
    mkdir("states", 0777);
    mkdir("timestamps", 0777);
    mkdir("users", 0777);
    mkdir("bplus", 0777);

    // set up some counters, etc.
    unsigned int recordCount = 0,
    userCount = 0,
    cityCount = 0,
    stateCount = 0,
    timestampCount = 0,
    datestampCount = 0,
    messageCount = 0,
    i,j;

    // SET UP HASH TABLES FOR CITIES, STATES, TIMESTAMPS, DATESTAMPS
    // cities
    city_node *cityHT[HASH_SIZE];
    for (i = 0; i < HASH_SIZE; i++)
    {
        cityHT[i] = malloc(sizeof(city_node));
        cityHT[i] = NULL;
    }

    // states
    state_node *stateHT[HASH_SIZE];
    for (i = 0; i < HASH_SIZE; i++)
    {
        stateHT[i] = malloc(sizeof(state_node));
        stateHT[i] = NULL;
    }

    // timestamps
    timestamp_node *timestampHT[HASH_SIZE];
    for (i = 0; i < HASH_SIZE; i++)
    {
        timestampHT[i] = malloc(sizeof(timestamp_node));
        timestampHT[i] = NULL;
    }

    // datestamps
    datestamp_node *datestampHT[HASH_SIZE];
    for (i = 0; i < HASH_SIZE; i++)
    {
        datestampHT[i] = malloc(sizeof(datestamp_node));
        datestampHT[i] = NULL;
    }

    // LOOP OVER RECORD FILES
    char filename[1024];
    FILE *fp = NULL;
    for (i = 0; i < totalRecordNumber; i++) {
        //open the corresponding file
        sprintf(filename, "record_%06d.dat", i);
        fp = fopen(filename,"rb");
        if (!fp) {
            fprintf(stderr, "Cannot open %s\n", filename);
            continue;
        }
        record_t *record = read_record(fp);

        // split location into city and state, as best we can
        char cityStr[TEXT_SHORT];
        char stateStr[TEXT_SHORT];

        // there's one record where the location is \0, which strtok breaks on
        if (record->location[0] == '\0')
        {
            strncpy(cityStr, "", TEXT_SHORT);
            strncpy(stateStr, "", TEXT_SHORT);
        }
        else
        {
            char loc[TEXT_SHORT];
            strncpy(loc, record->location, TEXT_SHORT);
            strncpy(cityStr, strtok(loc, ","), TEXT_SHORT);
            strncpy(stateStr, strtok(NULL, ","), TEXT_SHORT);
        }

        // create state
        state_t state;
        strncpy(state.name, stateStr, TEXT_SHORT);

        // get stateId from hash if we have it already
        unsigned int stateHash = hash_state(&state) % HASH_SIZE;
        state_node *s;
        int stateId = -1;
        for(s = stateHT[stateHash]; (s != NULL) && (stateId == -1); s = s->next)
        {
            if (compare_states(&state, &(s->state)) == 0)
            {
                stateId = s->state.stateId;
            }
        }

        // assign stateId, add to hash table, and write file if we don't have it
        if (stateId == -1)
        {
            state.stateId = stateCount;
            stateId = stateCount;
            write_state(stateCount, &state);
            stateCount++;

            s = malloc(sizeof(state_node));
            s->state = state;
            s->next = stateHT[stateHash];
            stateHT[stateHash] = s;
        }

        // create city
        city_t city;
        city.stateId = stateId;
        strncpy(city.name, cityStr, TEXT_SHORT);

        // get cityId from hash if we have it already
        unsigned int cityHash = hash_city(&city) % HASH_SIZE;
        city_node *c;
        int cityId = -1;
        for(c = cityHT[cityHash]; (c != NULL) && (cityId == -1); c = c->next)
        {
            if (compare_cities(&city, &(c->city)) == 0)
            {
                cityId = c->city.cityId;
            }
        }

        // assign cityId, add to hash table, and write file if we don't have it
        if (cityId == -1)
        {
            city.cityId = cityCount;
            cityId = cityCount;
            write_city(cityCount, &city);
            cityCount++;

            c = malloc(sizeof(city_node));
            c->city = city;
            c->next = cityHT[cityHash];
            cityHT[cityHash] = c;
        }

        // create and write user
        user_t user;
        user.userId = record->id;
        user.cityId = cityId;
        user.stateId = stateId;
        strncpy(user.name, record->name, TEXT_SHORT);
        write_user(userCount, &user);
        userCount++;

        // loop over messages
        for(j = 0; j < record->message_num; j++) {
            // create timestamp
            timestamp_t timestamp;
            timestamp.hour = record->messages[j].hour;
            timestamp.minute = record->messages[j].minute;

            // get timestampId from hash if we have it already
            unsigned int timestampHash = hash_timestamp(&timestamp) % HASH_SIZE;
            timestamp_node *t;
            int tsId = -1;
            for(t = timestampHT[timestampHash]; (t != NULL) && (tsId == -1); t = t->next)
            {
                if (compare_timestamps(&timestamp, &(t->timestamp)) == 0)
                {
                    tsId = t->timestamp.timestampId;
                }
            }

            // assign timestampId, add to hash table, and write file if we don't have it
            if (tsId == -1)
            {
                timestamp.timestampId = timestampCount;
                tsId = timestampCount;
                write_timestamp(timestampCount, &timestamp);
                timestampCount++;

                t = malloc(sizeof(timestamp_node));
                t->timestamp = timestamp;
                t->next = timestampHT[timestampHash];
                timestampHT[timestampHash] = t;
            }

            // create datestamp
            datestamp_t datestamp;
            datestamp.year = record->messages[j].year;
            datestamp.month = record->messages[j].month;
            datestamp.day = record->messages[j].day;

            // get datestampId from hash if we have it already
            unsigned int datestampHash = hash_datestamp(&datestamp) % HASH_SIZE;
            datestamp_node *d;
            int dsId = -1;
            for(d = datestampHT[datestampHash]; (d != NULL) && (dsId == -1); d = d->next)
            {
                if (compare_datestamps(&datestamp, &(d->datestamp)) == 0)
                {
                    dsId = d->datestamp.datestampId;
                }
            }

            // assign datestampId, add to hash table, and write file if we don't have it
            if (dsId == -1)
            {
                datestamp.datestampId = datestampCount;
                dsId = datestampCount;
                write_datestamp(datestampCount, &datestamp);
                datestampCount++;

                d = malloc(sizeof(datestamp_node));
                d->datestamp = datestamp;
                d->next = datestampHT[datestampHash];
                datestampHT[datestampHash] = d;
            }

            // create and write message
            message_t message;
            strncpy(message.text, record->messages[j].text, TEXT_LONG);
            message.userId = user.userId;
            message.timestampId = tsId;
            message.datestampId = dsId;
            message.messageId = messageCount;

            write_message(messageCount, &message);
            messageCount++;
        }

        // free and close record
        free_record(record);
        fclose(fp);
    }

    // free city nodes
    city_node *cNode;
    for (i = 0; i < HASH_SIZE; i++)
    {
    	cNode = cityHT[i];
    	while (cNode != NULL)
    	{
    		city_node* tmp = cNode;
    		cNode = cNode->next;
    		free (tmp);
    	}
    }

    // free state nodes
    state_node *sNode;
    for (i = 0; i < HASH_SIZE; i++)
    {
    	sNode = stateHT[i];
    	while (sNode != NULL)
    	{
    		state_node* tmp = sNode;
    		sNode = sNode->next;
    		free (tmp);
    	}
    }

    // free timestamp nodes
    timestamp_node *tNode;
    for (i = 0; i < HASH_SIZE; i++)
    {
    	tNode = timestampHT[i];
    	while (tNode != NULL)
    	{
    		timestamp_node* tmp = tNode;
    		tNode = tNode->next;
    		free (tmp);
    	}
    }

    // free datestamp nodes
    datestamp_node *dNode;
    for (i = 0; i < HASH_SIZE; i++)
    {
    	dNode = datestampHT[i];
    	while (dNode != NULL)
    	{
    		datestamp_node* tmp = dNode;
    		dNode = dNode->next;
    		free (tmp);
    	}
    }

    // create, write, print file count information file
    file_count_t fc;
    fc.users = userCount;
    fc.cities = cityCount;
    fc.states = stateCount;
    fc.messages = messageCount;
    fc.timestamps = timestampCount;
    fc.datestamps = datestampCount;

    write_file_count(&fc);
    print_file_count(&fc);

    // end timing the program
    gettimeofday(&sysTimeEnd, NULL);
    float totaltime2 = (sysTimeEnd.tv_sec - sysTimeStart.tv_sec)
    + (sysTimeEnd.tv_usec - sysTimeStart.tv_usec) / 1000000.0f;
    printf("Process time %f seconds\n", totaltime2);

    return 0;
}