liquid_feedback_core

changeset 406:1821e7aee87f

Removed algorithm to calculate "order_in_open_states"
author jbe
date Mon Oct 14 00:19:38 2013 +0200 (2013-10-14)
parents 9905a70a475f
children 92cdb3af0b54
files lf_update_issue_order.c
line diff
     1.1 --- a/lf_update_issue_order.c	Mon Oct 14 00:09:01 2013 +0200
     1.2 +++ b/lf_update_issue_order.c	Mon Oct 14 00:19:38 2013 +0200
     1.3 @@ -88,13 +88,6 @@
     1.4    struct candidate **candidates;  // all candidates equally preferred
     1.5  };
     1.6  
     1.7 -// open issue to be assigned an "order_in_open_states":
     1.8 -struct open_issue {
     1.9 -  char *issue_id;
    1.10 -  int minimum_position;
    1.11 -  int position;
    1.12 -};
    1.13 -
    1.14  // determine candidate, which is assigned the next seat (starting with the worst rank):
    1.15  static struct candidate *loser(int round_number, struct ballot *ballots, int ballot_count) {
    1.16    int i, j;       // index variables for loops
    1.17 @@ -180,33 +173,12 @@
    1.18    abort();
    1.19  }
    1.20  
    1.21 -// calculate "order_in_open_states":
    1.22 -static void calculate_order_in_open_states(struct open_issue *open_issues, int open_issue_count) {
    1.23 -  int i, j, fallback_j = 0, fallback_minimum_position;
    1.24 -  for (i=1; i<=open_issue_count; i++) {
    1.25 -    fallback_minimum_position = -1;
    1.26 -    for (j=0; j<open_issue_count; j++) {
    1.27 -      if (open_issues[j].position) continue;
    1.28 -      if (open_issues[j].minimum_position <= i) break;
    1.29 -      if (
    1.30 -        fallback_minimum_position < 0 ||
    1.31 -        open_issues[j].minimum_position < fallback_minimum_position
    1.32 -      ) {
    1.33 -        fallback_j = j;
    1.34 -        fallback_minimum_position = open_issues[j].minimum_position;
    1.35 -      }
    1.36 -    }
    1.37 -    if (j==open_issue_count) j = fallback_j;
    1.38 -    open_issues[j].position = i;
    1.39 -  }
    1.40 -}
    1.41 -
    1.42 -// write results to database (calls calculate_open_issue_order):
    1.43 +// write results to database:
    1.44  static int write_ranks(PGconn *db, char *escaped_area_id) {
    1.45    PGresult *res;
    1.46    char *cmd;
    1.47    int i;
    1.48 -  if (asprintf(&cmd, "BEGIN; DELETE FROM \"issue_order\" USING \"issue\" WHERE \"issue_order\".\"id\" = \"issue\".\"id\" AND \"issue\".\"area_id\" = %s; INSERT INTO \"issue_order\" (\"id\") SELECT \"id\" FROM \"issue\" WHERE \"area_id\" = %s", escaped_area_id, escaped_area_id) < 0) {
    1.49 +  if (asprintf(&cmd, "BEGIN; DELETE FROM \"issue_order\" USING \"issue\" WHERE \"issue_order\".\"id\" = \"issue\".\"id\" AND \"issue\".\"area_id\" = %s", escaped_area_id) < 0) {
    1.50      fprintf(stderr, "Could not prepare query string in memory.\n");
    1.51      abort();
    1.52    }
    1.53 @@ -232,7 +204,7 @@
    1.54        fprintf(stderr, "Could not escape literal in memory.\n");
    1.55        abort();
    1.56      }
    1.57 -    if (asprintf(&cmd, "UPDATE \"issue_order\" SET \"order_in_admission_state\" = %i WHERE \"id\" = %s", candidates[i].seat, escaped_issue_id) < 0) {
    1.58 +    if (asprintf(&cmd, "INSERT INTO \"issue_order\" (\"id\", \"order_in_admission_state\") VALUES (%s, %i)", escaped_issue_id, candidates[i].seat) < 0) {
    1.59        fprintf(stderr, "Could not prepare query string in memory.\n");
    1.60        abort();
    1.61      }
    1.62 @@ -240,12 +212,12 @@
    1.63      res = PQexec(db, cmd);
    1.64      free(cmd);
    1.65      if (!res) {
    1.66 -      fprintf(stderr, "Error in pqlib while sending SQL command to update issue order in admission state.\n");
    1.67 +      fprintf(stderr, "Error in pqlib while sending SQL command to insert issue order.\n");
    1.68      } else if (
    1.69        PQresultStatus(res) != PGRES_COMMAND_OK &&
    1.70        PQresultStatus(res) != PGRES_TUPLES_OK
    1.71      ) {
    1.72 -      fprintf(stderr, "Error while executing SQL command to update issue order in admission state:\n%s", PQresultErrorMessage(res));
    1.73 +      fprintf(stderr, "Error while executing SQL command to insert issue order:\n%s", PQresultErrorMessage(res));
    1.74        PQclear(res);
    1.75      } else {
    1.76        PQclear(res);
    1.77 @@ -255,85 +227,6 @@
    1.78      if (res) PQclear(res);
    1.79      return 1;
    1.80    }
    1.81 -  if (asprintf(&cmd, "SELECT \"issue_id\", \"minimum_position\" FROM \"open_issues_ordered_with_minimum_position\" WHERE \"area_id\" = %s", escaped_area_id) < 0) {
    1.82 -    fprintf(stderr, "Could not prepare query string in memory.\n");
    1.83 -    abort();
    1.84 -  }
    1.85 -  res = PQexec(db, cmd);
    1.86 -  free(cmd);
    1.87 -  if (!res) {
    1.88 -    fprintf(stderr, "Error in pqlib while sending SQL command selecting ordered issues with minimum position.\n");
    1.89 -    res = PQexec(db, "ROLLBACK");
    1.90 -    if (res) PQclear(res);
    1.91 -    return 1;
    1.92 -  } else if (PQresultStatus(res) != PGRES_TUPLES_OK) {
    1.93 -    fprintf(stderr, "Error while executing SQL command selecting ordered issues with minimum position:\n%s", PQresultErrorMessage(res));
    1.94 -    PQclear(res);
    1.95 -    res = PQexec(db, "ROLLBACK");
    1.96 -    if (res) PQclear(res);
    1.97 -    return 1;
    1.98 -  } else if (PQnfields(res) < 2) {
    1.99 -    fprintf(stderr, "Too few columns returned by SQL command selecting ordered issues with minimum position.\n");
   1.100 -    PQclear(res);
   1.101 -    res = PQexec(db, "ROLLBACK");
   1.102 -    if (res) PQclear(res);
   1.103 -    return 1;
   1.104 -  } else {
   1.105 -    int open_issue_count;
   1.106 -    struct open_issue *open_issues;
   1.107 -    open_issue_count = PQntuples(res);
   1.108 -    open_issues = calloc(open_issue_count, sizeof(struct open_issue));
   1.109 -    for (i=0; i<open_issue_count; i++) {
   1.110 -      open_issues[i].issue_id = PQgetvalue(res, i, 0);
   1.111 -      if (PQgetisnull(res, i, 1)) {
   1.112 -        open_issues[i].minimum_position = 0;
   1.113 -      } else {
   1.114 -        open_issues[i].minimum_position = (int)strtol(PQgetvalue(res, i, 1), (char **)NULL, 10);
   1.115 -        if (open_issues[i].minimum_position < 1) {
   1.116 -          fprintf(stderr, "Unexpected minimum position value.\n");
   1.117 -          PQclear(res);
   1.118 -          free(open_issues);
   1.119 -          res = PQexec(db, "ROLLBACK");
   1.120 -          if (res) PQclear(res);
   1.121 -          return 1;
   1.122 -        }
   1.123 -      }
   1.124 -    }
   1.125 -    PQclear(res);
   1.126 -    calculate_order_in_open_states(open_issues, open_issue_count);
   1.127 -    for (i=0; i<open_issue_count; i++) {
   1.128 -      char *escaped_issue_id;
   1.129 -      escaped_issue_id = escapeLiteral(db, open_issues[i].issue_id, strlen(open_issues[i].issue_id));  // TODO: BUG: the string is already freed
   1.130 -      if (!escaped_issue_id) {
   1.131 -        fprintf(stderr, "Could not escape literal in memory.\n");
   1.132 -        abort();
   1.133 -      }
   1.134 -      if (asprintf(&cmd, "UPDATE \"issue_order\" SET \"order_in_open_states\" = %i WHERE \"id\" = %s", open_issues[i].position, escaped_issue_id) < 0) {
   1.135 -        fprintf(stderr, "Could not prepare query string in memory.\n");
   1.136 -        abort();
   1.137 -      }
   1.138 -      freemem(escaped_issue_id);
   1.139 -      res = PQexec(db, cmd);
   1.140 -      free(cmd);
   1.141 -      if (!res) {
   1.142 -        fprintf(stderr, "Error in pqlib while sending SQL command to update issue order in open states.\n");
   1.143 -      } else if (
   1.144 -        PQresultStatus(res) != PGRES_COMMAND_OK &&
   1.145 -        PQresultStatus(res) != PGRES_TUPLES_OK
   1.146 -      ) {
   1.147 -        fprintf(stderr, "Error while executing SQL command to update issue order in open states:\n%s", PQresultErrorMessage(res));
   1.148 -        PQclear(res);
   1.149 -      } else {
   1.150 -        PQclear(res);
   1.151 -        continue;
   1.152 -      }
   1.153 -      free(open_issues);
   1.154 -      res = PQexec(db, "ROLLBACK");
   1.155 -      if (res) PQclear(res);
   1.156 -      return 1;
   1.157 -    }
   1.158 -    free(open_issues);
   1.159 -  }
   1.160    res = PQexec(db, "COMMIT");
   1.161    if (!res) {
   1.162      fprintf(stderr, "Error in pqlib while sending SQL command to commit transaction.\n");

Impressum / About Us