hashfile/src/hash_file.c
HT_ErrorCode HT_PrintAllEntries(int indexDesc, int *id) {
    if ( id == NULL ){
        // Print all entries in the hashfile.
        char *data;
        BF_Block *block;
        BF_Block_Init(&block);
        int blocks_num;
        CALL_BF(BF_GetBlockCounter(indexDesc, &blocks_num));
        for (int i = 1; i <= blocks_num-1; i++){
                CALL_BF(BF_GetBlock(indexDesc, i, block));
                data = BF_Block_GetData(block);
                int records_num;
                memcpy(&records_num, data, sizeof(int));
                if(records_num <= 0)
                    break;
		        printf("==================== BLOCK: %d - RECORDS: %d ====================\n", i, records_num);

                for (int j = 1; j <= records_num; j++) {
                        Record *rec;
                        rec = (Record *)malloc(sizeof(Record));
                        memcpy(rec, data+sizeof(int)*2+(j-1)*sizeof(Record), sizeof(Record));
                                printf("(%d, %s, %s, %s)\n", rec->id, rec->name, rec->surname, rec->city);
                        free(rec);
                }
                CALL_BF(BF_UnpinBlock(block));
        }
	BF_Block_Destroy(&block);
    } else {
        char *data;
        BF_Block *block;
        BF_Block_Init(&block);
        int no_of_buckets;

	    no_of_buckets = table[indexDesc];
        int bucket_position = hash(*id, no_of_buckets);
	
        CALL_BF(BF_GetBlock(indexDesc, bucket_position, block));
        data = BF_Block_GetData(block);
        int flag = 0;
        int records;
        int next_block;
        Record *record;
        record = (Record*)malloc(sizeof(Record));
        memcpy(&records, data, sizeof(int));
        memcpy(&next_block, data+sizeof(int), sizeof(int));
        while( flag == 0 ){
            for( int i = 0; i < records; i++ ){
                memcpy(record, data+sizeof(int)*2+sizeof(Record)*i, sizeof(Record));
                if( record->id == *id ){
                    printf("(%d, %s, %s, %s)\n", record->id, record->name, record->surname, record->city);
                        flag = 1;
                }
            }
            if(flag == 1){
			    break;
		    }
		    if (next_block != 0 ) {
                CALL_BF(BF_UnpinBlock(block));
                CALL_BF(BF_GetBlock(indexDesc, next_block, block));
                data = BF_Block_GetData(block);
                memcpy(&records, data, sizeof(int));
                memcpy(&next_block, data+sizeof(int), sizeof(int));
            } else {
                printf("Couldn't find the key.\n");
                free(record);
                CALL_BF(BF_UnpinBlock(block));
			    BF_Block_Destroy(&block);
                return HT_OK;
            }
        }
        free(record);
        CALL_BF(BF_UnpinBlock(block));
	    BF_Block_Destroy(&block);
    }
    return HT_OK;
}
/**
 * To print all entries given an id, all we have to do is iterate over the chain of blocks in the corrensponding bucket, given through the hash function.
 * If the id is NULL then we have to print all entries, meaning iterate over all blocks and print all their records.
 */
To print all entries given an id, all we have to do is iterate over the chain of blocks in the corrensponding bucket, given through the hash function.
If the id is NULL then we have to print all entries, meaning iterate over all blocks and print all their records.
To print all entries given an id all we have to do is iterate over the chain of blocks in the corrensponding bucket given through the hash function . If the id is NULL then we have to print all entries meaning iterate over all blocks and print all their records .
HT_ErrorCode HT_PrintAllEntries(int indexDesc, int *id) {
    if ( id == NULL ){
        char *data;
        BF_Block *block;
        BF_Block_Init(&block);
        int blocks_num;
        CALL_BF(BF_GetBlockCounter(indexDesc, &blocks_num));
        for (int i = 1; i <= blocks_num-1; i++){
                CALL_BF(BF_GetBlock(indexDesc, i, block));
                data = BF_Block_GetData(block);
                int records_num;
                memcpy(&records_num, data, sizeof(int));
                if(records_num <= 0)
                    break;
		        printf("==================== BLOCK: %d - RECORDS: %d ====================\n", i, records_num);
                for (int j = 1; j <= records_num; j++) {
                        Record *rec;
                        rec = (Record *)malloc(sizeof(Record));
                        memcpy(rec, data+sizeof(int)*2+(j-1)*sizeof(Record), sizeof(Record));
                                printf("(%d, %s, %s, %s)\n", rec->id, rec->name, rec->surname, rec->city);
                        free(rec);
                }
                CALL_BF(BF_UnpinBlock(block));
        }
	BF_Block_Destroy(&block);
    } else {
        char *data;
        BF_Block *block;
        BF_Block_Init(&block);
        int no_of_buckets;
	    no_of_buckets = table[indexDesc];
        int bucket_position = hash(*id, no_of_buckets);
        CALL_BF(BF_GetBlock(indexDesc, bucket_position, block));
        data = BF_Block_GetData(block);
        int flag = 0;
        int records;
        int next_block;
        Record *record;
        record = (Record*)malloc(sizeof(Record));
        memcpy(&records, data, sizeof(int));
        memcpy(&next_block, data+sizeof(int), sizeof(int));
        while( flag == 0 ){
            for( int i = 0; i < records; i++ ){
                memcpy(record, data+sizeof(int)*2+sizeof(Record)*i, sizeof(Record));
                if( record->id == *id ){
                    printf("(%d, %s, %s, %s)\n", record->id, record->name, record->surname, record->city);
                        flag = 1;
                }
            }
            if(flag == 1){
			    break;
		    }
		    if (next_block != 0 ) {
                CALL_BF(BF_UnpinBlock(block));
                CALL_BF(BF_GetBlock(indexDesc, next_block, block));
                data = BF_Block_GetData(block);
                memcpy(&records, data, sizeof(int));
                memcpy(&next_block, data+sizeof(int), sizeof(int));
            } else {
                printf("Couldn't find the key.\n");
                free(record);
                CALL_BF(BF_UnpinBlock(block));
			    BF_Block_Destroy(&block);
                return HT_OK;
            }
        }
        free(record);
        CALL_BF(BF_UnpinBlock(block));
	    BF_Block_Destroy(&block);
    }
    return HT_OK;
}
HT_ErrorCode HT_PrintAllEntries ( int indexDesc , int * id ) { if ( id == NULL ) { char * data ; BF_Block * block ; BF_Block_Init ( & block ) ; int blocks_num ; CALL_BF ( BF_GetBlockCounter ( indexDesc , & blocks_num ) ) ; for ( int i = 1 ; i <= blocks_num - 1 ; i ++ ) { CALL_BF ( BF_GetBlock ( indexDesc , i , block ) ) ; data = BF_Block_GetData ( block ) ; int records_num ; memcpy ( & records_num , data , sizeof ( int ) ) ; if ( records_num <= 0 ) break ; printf ( " \n " , i , records_num ) ; for ( int j = 1 ; j <= records_num ; j ++ ) { Record * rec ; rec = ( Record * ) malloc ( sizeof ( Record ) ) ; memcpy ( rec , data + sizeof ( int ) * 2 + ( j - 1 ) * sizeof ( Record ) , sizeof ( Record ) ) ; printf ( " \n " , rec -> id , rec -> name , rec -> surname , rec -> city ) ; free ( rec ) ; } CALL_BF ( BF_UnpinBlock ( block ) ) ; } BF_Block_Destroy ( & block ) ; } else { char * data ; BF_Block * block ; BF_Block_Init ( & block ) ; int no_of_buckets ; no_of_buckets = table [ indexDesc ] ; int bucket_position = hash ( * id , no_of_buckets ) ; CALL_BF ( BF_GetBlock ( indexDesc , bucket_position , block ) ) ; data = BF_Block_GetData ( block ) ; int flag = 0 ; int records ; int next_block ; Record * record ; record = ( Record * ) malloc ( sizeof ( Record ) ) ; memcpy ( & records , data , sizeof ( int ) ) ; memcpy ( & next_block , data + sizeof ( int ) , sizeof ( int ) ) ; while ( flag == 0 ) { for ( int i = 0 ; i < records ; i ++ ) { memcpy ( record , data + sizeof ( int ) * 2 + sizeof ( Record ) * i , sizeof ( Record ) ) ; if ( record -> id == * id ) { printf ( " \n " , record -> id , record -> name , record -> surname , record -> city ) ; flag = 1 ; } } if ( flag == 1 ) { break ; } if ( next_block != 0 ) { CALL_BF ( BF_UnpinBlock ( block ) ) ; CALL_BF ( BF_GetBlock ( indexDesc , next_block , block ) ) ; data = BF_Block_GetData ( block ) ; memcpy ( & records , data , sizeof ( int ) ) ; memcpy ( & next_block , data + sizeof ( int ) , sizeof ( int ) ) ; } else { printf ( " \n " ) ; free ( record ) ; CALL_BF ( BF_UnpinBlock ( block ) ) ; BF_Block_Destroy ( & block ) ; return HT_OK ; } } free ( record ) ; CALL_BF ( BF_UnpinBlock ( block ) ) ; BF_Block_Destroy ( & block ) ; } return HT_OK ; }
To print all entries given an id, all we have to do is iterate over the chain of blocks in the corrensponding bucket, given through the hash function.
To print all entries given an id all we have to do is iterate over the chain of blocks in the corrensponding bucket given through the hash function .
// Print all entries in the hashfile.