Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Simple C program that connects to MySQL Database server*/
- #include <mysql.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <glib.h>
- #include <sys/time.h>
- #include <string.h>
- #include <netdb.h>
- #include <netinet/in.h>
- #include <unistd.h>
- const char *byte_to_binary(int x)
- {
- static char b[9];
- b[0] = '\0';
- int z;
- for (z = 128; z > 0; z >>= 1)
- {
- strcat(b, ((x & z) == z) ? "1" : "0");
- }
- return b;
- }
- main() {
- MYSQL *conn;
- MYSQL_RES *res;
- MYSQL_ROW row;
- char *server = "localhost";
- char *user = "xxxxx";
- char *password = "xxxxx"; /* set me first */
- char *database = "xxxxx";
- conn = mysql_init(NULL);
- if (!mysql_real_connect(conn, server,
- user, password, database, 0, NULL, 0)) {
- fprintf(stderr, "%s\n", mysql_error(conn));
- exit(1);
- }
- if (mysql_query(conn, ""
- "SELECT "
- " COUNT(distinct ofv.offer_id) "
- "FROM "
- " offers_filters_values ofv "
- "WHERE "
- " ofv.category_id <> 0")) {
- fprintf(stderr, "%s\n", mysql_error(conn));
- exit(1);
- }
- res = mysql_use_result(conn);
- row = mysql_fetch_row(res);
- gint bitmap_size = (atoi(row[0]) + 8 - 1) / 8;
- printf("\nCount offers: %d\n", atoi(row[0]));
- mysql_free_result(res);
- if (mysql_query(conn, ""
- "SELECT "
- " ofv.offer_id, "
- " ofv.filter_value_id "
- "FROM "
- " offers_filters_values ofv "
- "WHERE "
- " ofv.category_id <> 0 "
- "ORDER BY "
- " ofv.order_priority")) {
- fprintf(stderr, "%s\n", mysql_error(conn));
- exit(1);
- }
- res = mysql_use_result(conn);
- GArray *offer_ids_garray = g_array_new (FALSE, FALSE, sizeof (gint));
- GHashTable *bitmaps_hash_table = g_hash_table_new_full(g_int_hash, g_int_equal, NULL, NULL);
- gint position = 0;
- gint prev_offer_id = 0;
- gint bitmaps = 0;
- while ((row = mysql_fetch_row(res)) != NULL) {
- if (prev_offer_id == 0 || atoi(row[0]) != prev_offer_id)
- {
- if (prev_offer_id != 0)
- {
- position++;
- }
- prev_offer_id = atoi(row[0]);
- g_array_append_val(offer_ids_garray, prev_offer_id);
- }
- gint *filter_value_id = (gint *)malloc(sizeof(gint));
- *filter_value_id = atoi(row[1]);
- unsigned char *bitmap;
- if (!g_hash_table_contains(bitmaps_hash_table, filter_value_id)) {
- printf("\ncalloc bitmap: %d\n", ++bitmaps);
- bitmap = calloc(bitmap_size*sizeof(unsigned char), sizeof(unsigned char));
- g_hash_table_insert(bitmaps_hash_table, filter_value_id, bitmap);
- } else {
- bitmap = (unsigned char *)g_hash_table_lookup(bitmaps_hash_table, filter_value_id);
- }
- *(bitmap + (position / 8)) |= 1 << (position % 8);
- }
- printf("done creating bitmaps");
- int num_offers = 0;
- int num_filter_values = 0;
- int i, j, k;
- /*
- GHashTable *offer_ids_hash_table = g_hash_table_new_full(g_int_hash, g_int_equal, NULL, NULL);
- GHashTable *filter_values_hash_table = g_hash_table_new_full(g_int_hash, g_int_equal, NULL, NULL);
- GArray *offer_ids_garray = g_array_new (FALSE, FALSE, sizeof (gint));
- GArray *filter_values_garray = g_array_new (FALSE, FALSE, sizeof (gint));
- while ((row = mysql_fetch_row(res)) != NULL) {
- gint *offer_id = (gint *)malloc(sizeof(gint));
- gint *filter_value_id = (gint *)malloc(sizeof(gint));
- *offer_id = atoi(row[0]);
- *filter_value_id = atoi(row[1]);
- //printf("\noffer_id: %d filter_value_id: %d\n", *offer_id, *filter_value_id);
- if (!g_hash_table_contains(offer_ids_hash_table, offer_id)) {
- GArray *offer_filter_values_garray = g_array_new(FALSE, FALSE, sizeof(gint));
- g_array_append_val(offer_filter_values_garray, *filter_value_id);
- g_hash_table_insert(offer_ids_hash_table, offer_id, offer_filter_values_garray);
- g_array_append_val(offer_ids_garray, *offer_id);
- num_offers++;
- //printf("+");
- } else {
- GArray *offer_filter_values_garray = (GArray *) g_hash_table_lookup(offer_ids_hash_table,offer_id);
- g_array_append_val(offer_filter_values_garray, *filter_value_id);
- }
- if (!g_hash_table_contains(filter_values_hash_table, filter_value_id)) {
- g_hash_table_insert(filter_values_hash_table, filter_value_id, filter_value_id);
- g_array_append_val(filter_values_garray, *filter_value_id);
- num_filter_values++;
- //printf("f");
- } else {
- //printf("o");
- }
- }
- GHashTable *bitmaps_hash_table = g_hash_table_new_full(g_int_hash, g_int_equal, NULL, NULL);
- int size = (num_offers + 8 - 1) / 8;
- printf("\nSize: %d num offers: %d\n", size, num_offers);
- int i, j, k;
- for (i = 0; i < filter_values_garray->len; i++)
- {
- gint filter_value_id = g_array_index(filter_values_garray, gint, i);
- gint * filter_value_id_ptr = malloc(sizeof(gint));
- *filter_value_id_ptr = filter_value_id;
- printf("malloc for filter_value_id: %d...", filter_value_id);
- unsigned char *bitmap = calloc(size, sizeof(unsigned char));
- g_hash_table_insert(bitmaps_hash_table, filter_value_id_ptr, bitmap);
- printf("done\n");
- }
- for (i = 0; i < offer_ids_garray->len; i++)
- {
- printf("K");
- gint offer_id = g_array_index(offer_ids_garray, gint, i);
- GArray *offer_filter_values_garray = (GArray *) g_hash_table_lookup(offer_ids_hash_table,&offer_id);
- int position = i / 8;
- int offset = i % 8;
- for (j = 0; j < filter_values_garray->len; j++)
- {
- gint filter_value_id = g_array_index(filter_values_garray, gint, j);
- int bit = 0;
- for(k = 0; k < offer_filter_values_garray->len; k++)
- {
- gint offer_filter_value_id = g_array_index(offer_filter_values_garray, gint, k);
- if (offer_filter_value_id == filter_value_id)
- {
- bit = 1;
- break;
- }
- }
- unsigned char *bitmap = (unsigned char *)g_hash_table_lookup(bitmaps_hash_table, &filter_value_id);
- if (bit) {
- *(bitmap + position) |= 1 << offset;
- } else {
- *(bitmap + position) &= ~(1 << offset);
- }
- }
- }
- */
- mysql_free_result(res);
- mysql_close(conn);
- int sockfd, newsockfd, portno, clilen;
- char buffer[256];
- struct sockaddr_in serv_addr, cli_addr;
- int n, pid;
- /* First call to socket() function */
- sockfd = socket(AF_INET, SOCK_STREAM, 0);
- if (sockfd < 0) {
- perror("ERROR opening socket");
- exit(1);
- }
- /* Initialize socket structure */
- bzero((char *) &serv_addr, sizeof(serv_addr));
- portno = 54545;
- serv_addr.sin_family = AF_INET;
- serv_addr.sin_addr.s_addr = INADDR_ANY;
- serv_addr.sin_port = htons(portno);
- /* Now bind the host address using bind() call.*/
- if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
- perror("ERROR on binding");
- exit(1);
- }
- /* Now start listening for the clients, here
- * process will go in sleep mode and will wait
- * for the incoming connection
- */
- listen(sockfd,5);
- clilen = sizeof(cli_addr);
- while (1) {
- newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
- if (newsockfd < 0) {
- perror("ERROR on accept");
- exit(1);
- }
- bzero(buffer,256);
- n = read(newsockfd,buffer,255);
- if (n < 0) {
- perror("ERROR reading from socket");
- exit(1);
- }
- printf("Here is the message: %s\n",buffer);
- printf("\n starting compare\n");
- struct timeval time;
- gettimeofday(&time, NULL);
- unsigned long start = ((unsigned long long)time.tv_sec * 1000000) + time.tv_usec;
- //char *query = "2,5202,10000033,4258";
- gchar **sought_filter_value_ids = g_strsplit(buffer, ",", -1);
- gchar **ptr;
- GArray *bitmaps = g_array_new (FALSE, FALSE, sizeof (unsigned char *));
- for (ptr = sought_filter_value_ids; *ptr; ptr++) {
- gint filter_value_id = atoi(*ptr);
- printf("query: %d\n", filter_value_id);
- unsigned char *bitmap = g_hash_table_lookup(bitmaps_hash_table, &filter_value_id);
- g_array_append_val(bitmaps, bitmap);
- }
- GArray *found_offer_ids_array = g_array_new (FALSE, FALSE, sizeof (gint));
- static unsigned char mask[] = {
- 128, // 8
- 64, // 7
- 32, // 6
- 16, // 5
- 8, // 4
- 4, // 3
- 2, // 2
- 1 // 1
- };
- for (i = 0; i < bitmap_size/8; i++)
- {
- uint64_t prev = 0ULL;
- int first = 1;
- for (j = 0; j < bitmaps->len - 1; j++)
- {
- uint64_t *bitmap1 = g_array_index(bitmaps, uint64_t *, j);
- uint64_t *bitmap2 = g_array_index(bitmaps, uint64_t *, j+1);
- uint64_t byte1 = *(bitmap1 + i);
- if (first == 1)
- {
- prev = byte1;
- first = 0;
- }
- uint64_t byte2 = *(bitmap2 + i);
- uint64_t byte3 = prev & byte2;
- prev = byte3;
- if (!prev)
- {
- break;
- }
- //printf("binary1: %s\n", byte_to_binary((int)byte1));
- //printf("binary2: %s\n", byte_to_binary((int)byte2));
- //printf("binary3: %s\n\n", byte_to_binary((int)byte3));
- }
- unsigned char *bytes = (unsigned char *)&prev;
- for (j = 0; j < 8; j++) {
- unsigned char the_byte = *(bytes + j);
- for (k = 0; k < 8; k++) {
- if ((the_byte & mask[k]) != 0) {
- //printf("found match pos: %d idx: %d\n", i, k);
- gint offer_id = g_array_index(offer_ids_garray, gint, (i * 64) + (j * 8) + 7 - k);
- g_array_append_val(found_offer_ids_array, offer_id);
- }
- }
- }
- // printf("binary: %s", byte_to_binary((int)byte1));
- // printf("binary: %s", byte_to_binary((int)byte2));
- //printf("binary: %s", byte_to_binary((int)byte3));
- }
- /*
- for (i = 0; i < bitmap_size; i++)
- {
- unsigned char prev = 0;
- int first = 1;
- for (j = 0; j < bitmaps->len - 1; j++)
- {
- unsigned char *bitmap1 = g_array_index(bitmaps, unsigned char *, j);
- unsigned char *bitmap2 = g_array_index(bitmaps, unsigned char *, j+1);
- unsigned char byte1 = *(bitmap1 + i);
- if (first == 1)
- {
- prev = byte1;
- first = 0;
- }
- unsigned char byte2 = *(bitmap2 + i);
- unsigned char byte3 = prev & byte2;
- prev = byte3;
- //printf("binary1: %s\n", byte_to_binary((int)byte1));
- //printf("binary2: %s\n", byte_to_binary((int)byte2));
- //printf("binary3: %s\n\n", byte_to_binary((int)byte3));
- }
- for (k = 0; k < 8; k++)
- {
- if ((prev & mask[k]) != 0)
- {
- //printf("found match pos: %d idx: %d\n", i, k);
- gint offer_id = g_array_index(offer_ids_garray, gint, (i*8)+7-k);
- g_array_append_val(found_offer_ids_array, offer_id);
- }
- }
- // printf("binary: %s", byte_to_binary((int)byte1));
- // printf("binary: %s", byte_to_binary((int)byte2));
- //printf("binary: %s", byte_to_binary((int)byte3));
- }*/
- //sleep(1);
- gettimeofday(&time, NULL);
- unsigned long end = ((unsigned long long)time.tv_sec * 1000000) + time.tv_usec;
- printf("\n time to compare: %lu", end - start);
- printf("\nNum offers: %d\n", num_offers);
- printf("\nNum offers found: %d\n", found_offer_ids_array->len);
- /*for (i = 0; i < offer_ids_garray->len; i++)
- {
- printf("%d. %d\n", i, g_array_index(offer_ids_garray, gint, i));
- }*/
- printf("init result array\n");
- int result_string_size = 0;
- for (i = 0; i < found_offer_ids_array->len; i++)
- {
- gint offer_id = g_array_index(found_offer_ids_array, gint, i);
- printf("f: %d\n", offer_id);
- gchar *offer_id_string = g_strdup_printf("%d", offer_id);
- result_string_size += strlen(offer_id_string);
- }
- result_string_size += found_offer_ids_array->len;
- printf("result string size: %d\n", result_string_size);
- gchar *offer_ids = malloc((sizeof(gchar) * result_string_size) + 10);
- offer_ids[0] = '\0';
- for (i = 0; i < found_offer_ids_array->len; i++)
- {
- gint offer_id = g_array_index(found_offer_ids_array, gint, i);
- gchar *offer_id_string = g_strdup_printf("%d", offer_id);
- printf("offer_ids: %s\n", offer_ids);
- printf("offer_id_string: %s\n", offer_id_string);
- offer_ids = g_strconcat(offer_ids, offer_id_string, ",", NULL);
- }
- printf("\nNum filter values: %d\n", num_filter_values);
- n = write(newsockfd,offer_ids,result_string_size);
- if (n < 0) {
- perror("ERROR writing to socket");
- exit(1);
- }
- close(newsockfd);
- } /* end of while */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement