liquid_feedback_core

view lf_update_suggestion_order.c @ 354:c23bb5ebcdec

Free memory for candidate and ballot structure in "lf_update_suggestion_order"
author jbe
date Sat Mar 16 18:23:06 2013 +0100 (2013-03-16)
parents 31ce1877320b
children accffcefeff7
line source
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <libpq-fe.h>
5 #include <search.h>
7 static char *escapeLiteral(PGconn *conn, const char *str, size_t len) {
8 // provides compatibility for PostgreSQL versions prior 9.0
9 // in future: return PQescapeLiteral(conn, str, len);
10 char *res;
11 size_t res_len;
12 res = malloc(2*len+3);
13 res[0] = '\'';
14 res_len = PQescapeStringConn(conn, res+1, str, len, NULL);
15 res[res_len+1] = '\'';
16 res[res_len+2] = 0;
17 return res;
18 }
20 static void freemem(void *ptr) {
21 // to be used for "escapeLiteral" function
22 // provides compatibility for PostgreSQL versions prior 9.0
23 // in future: PQfreemem(ptr);
24 free(ptr);
25 }
27 #define COL_MEMBER_ID 0
28 #define COL_WEIGHT 1
29 #define COL_PREFERENCE 2
30 #define COL_SUGGESTION_ID 3
32 struct candidate {
33 char *key;
34 double score;
35 int seat;
36 };
38 static int compare_candidate(struct candidate *c1, struct candidate *c2) {
39 return strcmp(c1->key, c2->key);
40 }
42 static int candidate_count;
43 static struct candidate *candidates;
45 static void register_candidate(char **candidate_key, VISIT visit, int level) {
46 if (visit == postorder || visit == leaf) {
47 struct candidate *candidate;
48 candidate = candidates + (candidate_count++);
49 candidate->key = *candidate_key;
50 candidate->score = 0.0;
51 candidate->seat = 0;
52 }
53 }
55 static struct candidate *candidate_by_key(char *candidate_key) {
56 struct candidate *candidate;
57 struct candidate compare;
58 compare.key = candidate_key;
59 candidate = bsearch(&compare, candidates, candidate_count, sizeof(struct candidate), (void *)compare_candidate);
60 if (!candidate) {
61 fprintf(stderr, "Candidate not found (should not happen)\n");
62 abort();
63 }
64 return candidate;
65 }
67 struct ballot_section {
68 int count;
69 struct candidate **candidates;
70 };
72 struct ballot {
73 int weight;
74 struct ballot_section sections[4];
75 };
77 static void process_initiative(PGresult *res) {
78 void *candidate_tree = NULL;
79 int ballot_count = 0;
80 int tuple_count, i;
81 char *old_member_id = NULL;
82 struct ballot *ballots, *ballot;
83 int candidates_in_sections[4] = {0, };
84 candidate_count = 0;
85 tuple_count = PQntuples(res);
86 for (i=0; i<=tuple_count; i++) {
87 char *member_id, *suggestion_id;
88 if (i<tuple_count) {
89 member_id = PQgetvalue(res, i, COL_MEMBER_ID);
90 suggestion_id = PQgetvalue(res, i, COL_SUGGESTION_ID);
91 if (!candidate_tree || !tfind(suggestion_id, &candidate_tree, (void *)strcmp)) {
92 candidate_count++;
93 if (!tsearch(suggestion_id, &candidate_tree, (void *)strcmp)) {
94 fprintf(stderr, "Insufficient memory\n");
95 abort();
96 }
97 }
98 }
99 if (i==tuple_count || (old_member_id && strcmp(old_member_id, member_id))) {
100 ballot_count++;
101 }
102 old_member_id = member_id;
103 }
104 printf("Candidate count: %i\n", candidate_count);
105 candidates = malloc(candidate_count * sizeof(struct candidate));
106 if (!candidates) {
107 fprintf(stderr, "Insufficient memory\n");
108 abort();
109 }
110 candidate_count = 0;
111 twalk(candidate_tree, (void *)register_candidate);
112 while (candidate_tree) tdelete(*(void **)candidate_tree, &candidate_tree, (void *)strcmp);
113 printf("Ballot count: %i\n", ballot_count);
114 ballots = calloc(ballot_count, sizeof(struct ballot));
115 if (!ballots) {
116 fprintf(stderr, "Insufficient memory\n");
117 abort();
118 }
119 ballot = ballots;
120 for (i=0; i<tuple_count; i++) {
121 char *member_id, *suggestion_id;
122 int weight, preference;
123 member_id = PQgetvalue(res, i, COL_MEMBER_ID);
124 suggestion_id = PQgetvalue(res, i, COL_SUGGESTION_ID);
125 weight = (int)strtol(PQgetvalue(res, i, COL_WEIGHT), (char **)NULL, 10);
126 if (weight <= 0) {
127 fprintf(stderr, "Unexpected weight value\n");
128 abort();
129 }
130 preference = (int)strtol(PQgetvalue(res, i, COL_PREFERENCE), (char **)NULL, 10);
131 if (preference < 1 || preference > 4) {
132 fprintf(stderr, "Unexpected preference value\n");
133 abort();
134 }
135 preference--;
136 ballot->weight = weight;
137 ballot->sections[preference].count++;
138 if (old_member_id && strcmp(old_member_id, member_id)) ballot++;
139 old_member_id = member_id;
140 }
141 for (i=0; i<ballot_count; i++) {
142 int j;
143 for (j=0; j<4; j++) {
144 if (ballots[i].sections[j].count) {
145 ballots[i].sections[j].candidates = malloc(ballots[i].sections[j].count * sizeof(struct candidate *));
146 if (!ballots[i].sections[j].candidates) {
147 fprintf(stderr, "Insufficient memory\n");
148 abort();
149 }
150 }
151 }
152 }
153 ballot = ballots;
154 for (i=0; i<=tuple_count; i++) {
155 char *member_id, *suggestion_id;
156 int preference;
157 if (i<tuple_count) {
158 member_id = PQgetvalue(res, i, COL_MEMBER_ID);
159 suggestion_id = PQgetvalue(res, i, COL_SUGGESTION_ID);
160 preference = (int)strtol(PQgetvalue(res, i, COL_PREFERENCE), (char **)NULL, 10);
161 if (preference < 1 || preference > 4) {
162 fprintf(stderr, "Unexpected preference value\n");
163 abort();
164 }
165 preference--;
166 ballot->sections[preference].candidates[candidates_in_sections[preference]++] = candidate_by_key(suggestion_id);
167 }
168 if (i==tuple_count || (old_member_id && strcmp(old_member_id, member_id))) {
169 ballot++;
170 candidates_in_sections[0] = 0;
171 candidates_in_sections[1] = 0;
172 candidates_in_sections[2] = 0;
173 candidates_in_sections[3] = 0;
174 }
175 old_member_id = member_id;
176 }
177 free(candidates);
178 for (i=0; i<ballot_count; i++) {
179 int j;
180 for (j=0; j<4; j++) {
181 if (ballots[i].sections[j].count) {
182 free(ballots[i].sections[j].candidates);
183 }
184 }
185 }
186 free(ballots);
187 }
189 int main(int argc, char **argv) {
191 // variable declarations:
192 int err = 0;
193 int i, count;
194 char *conninfo;
195 PGconn *db;
196 PGresult *res;
198 // parse command line:
199 if (argc == 0) return 1;
200 if (argc == 1 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
201 FILE *out;
202 out = argc == 1 ? stderr : stdout;
203 fprintf(stdout, "\n");
204 fprintf(stdout, "Usage: %s <conninfo>\n", argv[0]);
205 fprintf(stdout, "\n");
206 fprintf(stdout, "<conninfo> is specified by PostgreSQL's libpq,\n");
207 fprintf(stdout, "see http://www.postgresql.org/docs/9.1/static/libpq-connect.html\n");
208 fprintf(stdout, "\n");
209 fprintf(stdout, "Example: %s dbname=liquid_feedback\n", argv[0]);
210 fprintf(stdout, "\n");
211 return argc == 1 ? 1 : 0;
212 }
213 {
214 size_t len = 0;
215 for (i=1; i<argc; i++) len += strlen(argv[i]) + 1;
216 conninfo = malloc(len * sizeof(char));
217 if (!conninfo) {
218 fprintf(stderr, "Error: Could not allocate memory for conninfo string\n");
219 return 1;
220 }
221 conninfo[0] = 0;
222 for (i=1; i<argc; i++) {
223 if (i>1) strcat(conninfo, " ");
224 strcat(conninfo, argv[i]);
225 }
226 }
228 // connect to database:
229 db = PQconnectdb(conninfo);
230 if (!db) {
231 fprintf(stderr, "Error: Could not create database handle\n");
232 return 1;
233 }
234 if (PQstatus(db) != CONNECTION_OK) {
235 fprintf(stderr, "Could not open connection:\n%s", PQerrorMessage(db));
236 return 1;
237 }
239 // check initiatives:
240 res = PQexec(db, "SELECT \"initiative_id\", \"final\" FROM \"initiative_suggestion_order_calculation\"");
241 if (!res) {
242 fprintf(stderr, "Error in pqlib while sending SQL command selecting open issues\n");
243 err = 1;
244 } else if (PQresultStatus(res) != PGRES_TUPLES_OK) {
245 fprintf(stderr, "Error while executing SQL command selecting open issues:\n%s", PQresultErrorMessage(res));
246 err = 1;
247 PQclear(res);
248 } else {
249 count = PQntuples(res);
250 printf("Number of initiatives to process: %i\n", count);
251 for (i=0; i<count; i++) {
252 char *initiative_id, *escaped_initiative_id;
253 char *cmd;
254 PGresult *res2;
255 initiative_id = PQgetvalue(res, i, 0);
256 printf("Processing initiative_id: %s\n", initiative_id);
257 escaped_initiative_id = escapeLiteral(db, initiative_id, strlen(initiative_id));
258 if (asprintf(&cmd, "SELECT \"member_id\", \"weight\", \"preference\", \"suggestion_id\" FROM \"individual_suggestion_ranking\" WHERE \"initiative_id\" = %s ORDER BY \"member_id\", \"preference\"", escaped_initiative_id) < 0) {
259 fprintf(stderr, "Could not prepare query string in memory.\n");
260 err = 1;
261 freemem(escaped_initiative_id);
262 break;
263 }
264 res2 = PQexec(db, cmd);
265 free(cmd);
266 if (!res2) {
267 fprintf(stderr, "Error in pqlib while sending SQL command selecting open issues\n");
268 err = 1;
269 } else if (PQresultStatus(res2) != PGRES_TUPLES_OK) {
270 fprintf(stderr, "Error while executing SQL command selecting open issues:\n%s", PQresultErrorMessage(res));
271 err = 1;
272 PQclear(res2);
273 } else {
274 if (PQntuples(res2) == 0) {
275 printf("Nothing to do.\n");
276 } else {
277 process_initiative(res2);
278 }
279 PQclear(res2);
280 }
281 freemem(escaped_initiative_id);
282 }
283 PQclear(res);
284 }
286 // cleanup and exit
287 PQfinish(db);
288 return err;
290 }

Impressum / About Us