Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 14.35 KB | None | 0 0
  1. /* Simple C program that connects to MySQL Database server*/
  2. #include <mysql.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <glib.h>
  6.  
  7. #include <sys/time.h>
  8. #include <string.h>
  9.  
  10. #include <netdb.h>
  11. #include <netinet/in.h>
  12.  
  13.  
  14. #include <unistd.h>
  15.  
  16. const char *byte_to_binary(int x)
  17. {
  18.     static char b[9];
  19.     b[0] = '\0';
  20.  
  21.     int z;
  22.     for (z = 128; z > 0; z >>= 1)
  23.     {
  24.         strcat(b, ((x & z) == z) ? "1" : "0");
  25.     }
  26.  
  27.     return b;
  28. }
  29.  
  30.  
  31. main() {
  32.     MYSQL *conn;
  33.     MYSQL_RES *res;
  34.     MYSQL_ROW row;
  35.     char *server = "localhost";
  36.     char *user = "xxxxx";
  37.     char *password = "xxxxx"; /* set me first */
  38.     char *database = "xxxxx";
  39.  
  40.  
  41.  
  42.     conn = mysql_init(NULL);
  43.     if (!mysql_real_connect(conn, server,
  44.                             user, password, database, 0, NULL, 0)) {
  45.         fprintf(stderr, "%s\n", mysql_error(conn));
  46.         exit(1);
  47.     }
  48.     if (mysql_query(conn, ""
  49.             "SELECT "
  50.             "  COUNT(distinct ofv.offer_id) "
  51.             "FROM "
  52.             "  offers_filters_values ofv "
  53.             "WHERE "
  54.             "  ofv.category_id <> 0")) {
  55.         fprintf(stderr, "%s\n", mysql_error(conn));
  56.         exit(1);
  57.     }
  58.     res = mysql_use_result(conn);
  59.  
  60.     row = mysql_fetch_row(res);
  61.  
  62.     gint bitmap_size = (atoi(row[0]) + 8 - 1) / 8;
  63.  
  64.     printf("\nCount offers: %d\n", atoi(row[0]));
  65.  
  66.     mysql_free_result(res);
  67.  
  68.     if (mysql_query(conn, ""
  69.             "SELECT "
  70.             "  ofv.offer_id, "
  71.             "  ofv.filter_value_id "
  72.             "FROM "
  73.             "  offers_filters_values ofv "
  74.             "WHERE "
  75.             "  ofv.category_id <> 0 "
  76.             "ORDER BY "
  77.             "  ofv.order_priority")) {
  78.         fprintf(stderr, "%s\n", mysql_error(conn));
  79.         exit(1);
  80.     }
  81.     res = mysql_use_result(conn);
  82.  
  83.     GArray *offer_ids_garray = g_array_new (FALSE, FALSE, sizeof (gint));
  84.     GHashTable *bitmaps_hash_table = g_hash_table_new_full(g_int_hash, g_int_equal, NULL, NULL);
  85.  
  86.     gint position = 0;
  87.     gint prev_offer_id = 0;
  88.     gint bitmaps = 0;
  89.     while ((row = mysql_fetch_row(res)) != NULL) {
  90.         if (prev_offer_id == 0 || atoi(row[0]) != prev_offer_id)
  91.         {
  92.             if (prev_offer_id != 0)
  93.             {
  94.                 position++;
  95.             }
  96.             prev_offer_id = atoi(row[0]);
  97.             g_array_append_val(offer_ids_garray, prev_offer_id);
  98.         }
  99.  
  100.         gint *filter_value_id = (gint *)malloc(sizeof(gint));
  101.         *filter_value_id = atoi(row[1]);
  102.         unsigned char *bitmap;
  103.         if (!g_hash_table_contains(bitmaps_hash_table, filter_value_id)) {
  104.             printf("\ncalloc bitmap: %d\n", ++bitmaps);
  105.             bitmap = calloc(bitmap_size*sizeof(unsigned char), sizeof(unsigned char));
  106.             g_hash_table_insert(bitmaps_hash_table, filter_value_id, bitmap);
  107.         } else {
  108.             bitmap = (unsigned char *)g_hash_table_lookup(bitmaps_hash_table, filter_value_id);
  109.         }
  110.  
  111.         *(bitmap + (position / 8)) |= 1 << (position % 8);
  112.     }
  113.  
  114.     printf("done creating bitmaps");
  115.  
  116.     int num_offers = 0;
  117.     int num_filter_values = 0;
  118.     int i, j, k;
  119.  
  120.     /*
  121.     GHashTable *offer_ids_hash_table = g_hash_table_new_full(g_int_hash, g_int_equal, NULL, NULL);
  122.     GHashTable *filter_values_hash_table = g_hash_table_new_full(g_int_hash, g_int_equal, NULL, NULL);
  123.  
  124.     GArray *offer_ids_garray = g_array_new (FALSE, FALSE, sizeof (gint));
  125.     GArray *filter_values_garray = g_array_new (FALSE, FALSE, sizeof (gint));
  126.  
  127.  
  128.  
  129.     while ((row = mysql_fetch_row(res)) != NULL) {
  130.         gint *offer_id        = (gint *)malloc(sizeof(gint));
  131.         gint *filter_value_id = (gint *)malloc(sizeof(gint));
  132.  
  133.         *offer_id        = atoi(row[0]);
  134.         *filter_value_id = atoi(row[1]);
  135.  
  136.         //printf("\noffer_id: %d filter_value_id: %d\n", *offer_id, *filter_value_id);
  137.  
  138.         if (!g_hash_table_contains(offer_ids_hash_table, offer_id)) {
  139.             GArray *offer_filter_values_garray = g_array_new(FALSE, FALSE, sizeof(gint));
  140.             g_array_append_val(offer_filter_values_garray, *filter_value_id);
  141.  
  142.             g_hash_table_insert(offer_ids_hash_table, offer_id, offer_filter_values_garray);
  143.  
  144.             g_array_append_val(offer_ids_garray, *offer_id);
  145.             num_offers++;
  146.             //printf("+");
  147.         } else {
  148.             GArray *offer_filter_values_garray = (GArray *) g_hash_table_lookup(offer_ids_hash_table,offer_id);
  149.  
  150.             g_array_append_val(offer_filter_values_garray, *filter_value_id);
  151.         }
  152.  
  153.         if (!g_hash_table_contains(filter_values_hash_table, filter_value_id)) {
  154.             g_hash_table_insert(filter_values_hash_table, filter_value_id, filter_value_id);
  155.             g_array_append_val(filter_values_garray, *filter_value_id);
  156.             num_filter_values++;
  157.             //printf("f");
  158.         } else {
  159.             //printf("o");
  160.         }
  161.     }
  162.  
  163.  
  164.     GHashTable *bitmaps_hash_table = g_hash_table_new_full(g_int_hash, g_int_equal, NULL, NULL);
  165.  
  166.     int size = (num_offers + 8 - 1) / 8;
  167.  
  168.     printf("\nSize: %d num offers: %d\n", size, num_offers);
  169.  
  170.     int i, j, k;
  171.     for (i = 0; i < filter_values_garray->len; i++)
  172.     {
  173.  
  174.         gint filter_value_id = g_array_index(filter_values_garray, gint, i);
  175.         gint * filter_value_id_ptr = malloc(sizeof(gint));
  176.         *filter_value_id_ptr = filter_value_id;
  177.  
  178.         printf("malloc for filter_value_id: %d...", filter_value_id);
  179.         unsigned char *bitmap = calloc(size, sizeof(unsigned char));
  180.  
  181.         g_hash_table_insert(bitmaps_hash_table, filter_value_id_ptr, bitmap);
  182.         printf("done\n");
  183.     }
  184.  
  185.     for (i = 0; i < offer_ids_garray->len; i++)
  186.     {
  187.         printf("K");
  188.         gint offer_id = g_array_index(offer_ids_garray, gint, i);
  189.         GArray *offer_filter_values_garray = (GArray *) g_hash_table_lookup(offer_ids_hash_table,&offer_id);
  190.  
  191.         int position = i / 8;
  192.         int offset   = i % 8;
  193.  
  194.         for (j = 0; j < filter_values_garray->len; j++)
  195.         {
  196.             gint filter_value_id = g_array_index(filter_values_garray, gint, j);
  197.  
  198.             int bit = 0;
  199.             for(k = 0; k < offer_filter_values_garray->len; k++)
  200.             {
  201.                 gint offer_filter_value_id = g_array_index(offer_filter_values_garray, gint, k);
  202.  
  203.                 if (offer_filter_value_id == filter_value_id)
  204.                 {
  205.                     bit = 1;
  206.                     break;
  207.                 }
  208.             }
  209.  
  210.             unsigned char *bitmap = (unsigned char *)g_hash_table_lookup(bitmaps_hash_table, &filter_value_id);
  211.  
  212.             if (bit) {
  213.                 *(bitmap + position) |= 1 << offset;
  214.             } else {
  215.                 *(bitmap + position) &= ~(1 << offset);
  216.             }
  217.         }
  218.     }
  219.  
  220.     */
  221.     mysql_free_result(res);
  222.     mysql_close(conn);
  223.  
  224.  
  225.  
  226.     int sockfd, newsockfd, portno, clilen;
  227.     char buffer[256];
  228.     struct sockaddr_in serv_addr, cli_addr;
  229.     int n, pid;
  230.  
  231.     /* First call to socket() function */
  232.     sockfd = socket(AF_INET, SOCK_STREAM, 0);
  233.  
  234.     if (sockfd < 0) {
  235.         perror("ERROR opening socket");
  236.         exit(1);
  237.     }
  238.  
  239.     /* Initialize socket structure */
  240.     bzero((char *) &serv_addr, sizeof(serv_addr));
  241.     portno = 54545;
  242.  
  243.     serv_addr.sin_family = AF_INET;
  244.     serv_addr.sin_addr.s_addr = INADDR_ANY;
  245.     serv_addr.sin_port = htons(portno);
  246.  
  247.     /* Now bind the host address using bind() call.*/
  248.     if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
  249.         perror("ERROR on binding");
  250.         exit(1);
  251.     }
  252.  
  253.     /* Now start listening for the clients, here
  254.        * process will go in sleep mode and will wait
  255.        * for the incoming connection
  256.     */
  257.  
  258.     listen(sockfd,5);
  259.     clilen = sizeof(cli_addr);
  260.  
  261.     while (1) {
  262.         newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
  263.  
  264.         if (newsockfd < 0) {
  265.             perror("ERROR on accept");
  266.             exit(1);
  267.         }
  268.  
  269.         bzero(buffer,256);
  270.         n = read(newsockfd,buffer,255);
  271.  
  272.         if (n < 0) {
  273.             perror("ERROR reading from socket");
  274.             exit(1);
  275.         }
  276.  
  277.         printf("Here is the message: %s\n",buffer);
  278.  
  279.  
  280.         printf("\n starting compare\n");
  281.  
  282.         struct timeval time;
  283.         gettimeofday(&time, NULL);
  284.         unsigned long start = ((unsigned long long)time.tv_sec * 1000000) + time.tv_usec;
  285.  
  286.  
  287.         //char *query = "2,5202,10000033,4258";
  288.         gchar **sought_filter_value_ids = g_strsplit(buffer, ",", -1);
  289.         gchar **ptr;
  290.  
  291.         GArray *bitmaps = g_array_new (FALSE, FALSE, sizeof (unsigned char *));
  292.  
  293.         for (ptr = sought_filter_value_ids; *ptr; ptr++) {
  294.             gint filter_value_id = atoi(*ptr);
  295.             printf("query: %d\n", filter_value_id);
  296.             unsigned char *bitmap = g_hash_table_lookup(bitmaps_hash_table, &filter_value_id);
  297.             g_array_append_val(bitmaps, bitmap);
  298.         }
  299.  
  300.         GArray *found_offer_ids_array = g_array_new (FALSE, FALSE, sizeof (gint));
  301.  
  302.         static unsigned char mask[] = {
  303.                 128,        // 8
  304.                 64,         // 7
  305.                 32,         // 6
  306.                 16,         // 5
  307.                 8,          // 4
  308.                 4,          // 3
  309.                 2,          // 2
  310.                 1           // 1
  311.         };
  312.  
  313.         for (i = 0; i < bitmap_size/8; i++)
  314.         {
  315.             uint64_t prev = 0ULL;
  316.             int first = 1;
  317.             for (j = 0; j < bitmaps->len - 1; j++)
  318.             {
  319.                 uint64_t *bitmap1 = g_array_index(bitmaps, uint64_t *, j);
  320.                 uint64_t *bitmap2 = g_array_index(bitmaps, uint64_t *, j+1);
  321.  
  322.                 uint64_t byte1 = *(bitmap1 + i);
  323.                 if (first == 1)
  324.                 {
  325.                     prev = byte1;
  326.                     first = 0;
  327.                 }
  328.  
  329.                 uint64_t byte2 = *(bitmap2 + i);
  330.  
  331.                 uint64_t byte3 = prev & byte2;
  332.  
  333.  
  334.  
  335.                 prev = byte3;
  336.  
  337.                 if (!prev)
  338.                 {
  339.                     break;
  340.                 }
  341.                 //printf("binary1: %s\n", byte_to_binary((int)byte1));
  342.                 //printf("binary2: %s\n", byte_to_binary((int)byte2));
  343.  
  344.                 //printf("binary3: %s\n\n", byte_to_binary((int)byte3));
  345.             }
  346.  
  347.             unsigned char *bytes = (unsigned char *)&prev;
  348.             for (j = 0; j < 8; j++) {
  349.                 unsigned char the_byte = *(bytes + j);
  350.                 for (k = 0; k < 8; k++) {
  351.                     if ((the_byte & mask[k]) != 0) {
  352.                         //printf("found match pos: %d idx: %d\n", i, k);
  353.                         gint offer_id = g_array_index(offer_ids_garray, gint, (i * 64) + (j * 8) +  7 - k);
  354.                         g_array_append_val(found_offer_ids_array, offer_id);
  355.                     }
  356.                 }
  357.             }
  358.  
  359. //        printf("binary: %s", byte_to_binary((int)byte1));
  360. //        printf("binary: %s", byte_to_binary((int)byte2));
  361.  
  362.             //printf("binary: %s", byte_to_binary((int)byte3));
  363.  
  364.  
  365.         }
  366.  
  367.         /*
  368.         for (i = 0; i < bitmap_size; i++)
  369.         {
  370.             unsigned char prev = 0;
  371.             int first = 1;
  372.             for (j = 0; j < bitmaps->len - 1; j++)
  373.             {
  374.                 unsigned char *bitmap1 = g_array_index(bitmaps, unsigned char *, j);
  375.                 unsigned char *bitmap2 = g_array_index(bitmaps, unsigned char *, j+1);
  376.  
  377.  
  378.                 unsigned char byte1 = *(bitmap1 + i);
  379.                 if (first == 1)
  380.                 {
  381.                     prev = byte1;
  382.                     first = 0;
  383.                 }
  384.  
  385.                 unsigned char byte2 = *(bitmap2 + i);
  386.  
  387.                 unsigned char byte3 = prev & byte2;
  388.  
  389.  
  390.  
  391.                 prev = byte3;
  392.  
  393.                 //printf("binary1: %s\n", byte_to_binary((int)byte1));
  394.                 //printf("binary2: %s\n", byte_to_binary((int)byte2));
  395.  
  396.                 //printf("binary3: %s\n\n", byte_to_binary((int)byte3));
  397.             }
  398.  
  399.             for (k = 0; k < 8; k++)
  400.             {
  401.                 if ((prev & mask[k]) != 0)
  402.                 {
  403.                     //printf("found match pos: %d idx: %d\n", i, k);
  404.                     gint offer_id = g_array_index(offer_ids_garray, gint, (i*8)+7-k);
  405.                     g_array_append_val(found_offer_ids_array, offer_id);
  406.                 }
  407.             }
  408.  
  409.  
  410. //        printf("binary: %s", byte_to_binary((int)byte1));
  411. //        printf("binary: %s", byte_to_binary((int)byte2));
  412.  
  413.             //printf("binary: %s", byte_to_binary((int)byte3));
  414.  
  415.  
  416.         }*/
  417.  
  418.         //sleep(1);
  419.         gettimeofday(&time, NULL);
  420.  
  421.         unsigned long end = ((unsigned long long)time.tv_sec * 1000000) + time.tv_usec;
  422.  
  423.         printf("\n time to compare: %lu", end - start);
  424.  
  425.         printf("\nNum offers: %d\n", num_offers);
  426.         printf("\nNum offers found: %d\n", found_offer_ids_array->len);
  427.         /*for (i = 0; i < offer_ids_garray->len; i++)
  428.         {
  429.             printf("%d. %d\n", i, g_array_index(offer_ids_garray, gint, i));
  430.         }*/
  431.  
  432.         printf("init result array\n");
  433.  
  434.         int result_string_size = 0;
  435.         for (i = 0; i < found_offer_ids_array->len; i++)
  436.         {
  437.             gint offer_id = g_array_index(found_offer_ids_array, gint, i);
  438.             printf("f: %d\n", offer_id);
  439.  
  440.             gchar *offer_id_string = g_strdup_printf("%d", offer_id);
  441.             result_string_size += strlen(offer_id_string);
  442.         }
  443.  
  444.         result_string_size += found_offer_ids_array->len;
  445.  
  446.         printf("result string size: %d\n", result_string_size);
  447.         gchar *offer_ids = malloc((sizeof(gchar) * result_string_size) + 10);
  448.         offer_ids[0] = '\0';
  449.         for (i = 0; i < found_offer_ids_array->len; i++)
  450.         {
  451.             gint offer_id = g_array_index(found_offer_ids_array, gint, i);
  452.             gchar *offer_id_string = g_strdup_printf("%d", offer_id);
  453.  
  454.  
  455.             printf("offer_ids: %s\n", offer_ids);
  456.             printf("offer_id_string: %s\n", offer_id_string);
  457.             offer_ids = g_strconcat(offer_ids, offer_id_string, ",", NULL);
  458.         }
  459.  
  460.         printf("\nNum filter values: %d\n", num_filter_values);
  461.  
  462.         n = write(newsockfd,offer_ids,result_string_size);
  463.  
  464.  
  465.         if (n < 0) {
  466.             perror("ERROR writing to socket");
  467.             exit(1);
  468.         }
  469.  
  470.  
  471.         close(newsockfd);
  472.     } /* end of while */
  473.  
  474.  
  475.  
  476. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement