Replace /* fallthrouth*/ by __attribute__ ((fallthrough));
[paraslash.git] / score.c
1 /* Copyright (C) 2007 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file score.c Scoring functions to determine the audio file streaming order. */
4 #include <regex.h>
5 #include <osl.h>
6 #include <lopsub.h>
7
8 #include "para.h"
9 #include "error.h"
10 #include "string.h"
11 #include "afh.h"
12 #include "afs.h"
13 #include "list.h"
14
15 static struct osl_table *score_table;
16
17 static int ptr_compare(const struct osl_object *obj1, const struct osl_object *obj2)
18 {
19 void *d1 = *(void **)obj1->data;
20 void *d2 = *(void **)obj2->data;
21 return NUM_COMPARE(d1, d2);
22 }
23
24 /**
25 * Compare the score of two audio files
26 *
27 * \param obj1 Pointer to the first score object.
28 * \param obj2 Pointer to the second score object.
29 *
30 * This function first compares the score values as usual integers. If they compare as
31 * equal, the address of \a obj1 and \a obj2 are compared. So this compare function
32 * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
33 */
34 static int score_compare(const struct osl_object *obj1, const struct osl_object *obj2)
35 {
36 long d1 = *(long *)obj1->data;
37 long d2 = *(long *)obj2->data;
38 int ret = NUM_COMPARE(d2, d1);
39
40 if (ret)
41 return ret;
42 return NUM_COMPARE(obj2->data, obj1->data);
43 }
44
45 /**
46 * The score table consists of two columns: The \a aft_row column contains
47 * pointers to the rows of the audio file table, and the score column contains
48 * the current score of the audio file associated with that row.
49 */
50 enum score_table_columns {
51 /** The row of the audio file. */
52 SCORECOL_AFT_ROW,
53 /** The score */
54 SCORECOL_SCORE,
55 /** This table has two columns */
56 NUM_SCORE_COLUMNS
57 };
58
59 static struct osl_column_description score_cols[] = {
60 [SCORECOL_AFT_ROW] = {
61 .storage_type = OSL_NO_STORAGE,
62 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE | OSL_DONT_FREE,
63 .name = "aft_row",
64 .compare_function = ptr_compare,
65 .data_size = sizeof(void *)
66 },
67 [SCORECOL_SCORE] = {
68 .storage_type = OSL_NO_STORAGE,
69 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
70 .name = "score",
71 .compare_function = score_compare,
72 .data_size = sizeof(long)
73 }
74 };
75
76 static struct osl_table_description score_table_desc = {
77 .name = "score",
78 .num_columns = NUM_SCORE_COLUMNS,
79 .flags = 0,
80 .column_descriptions = score_cols
81 };
82
83 /**
84 * Compute the number of files in score table.
85 *
86 * \param num Result is returned here.
87 *
88 * \return Positive on success, negative on errors.
89 */
90 int get_num_admissible_files(unsigned *num)
91 {
92 return osl(osl_get_num_rows(score_table, num));
93 }
94
95 /**
96 * Get the score of the audio file associated with given row of the score table.
97 *
98 * \param score_row Pointer to the row in the score table.
99 * \param score Result is returned here on success.
100 *
101 * On errors (negative return value) the content of \a score is undefined.
102 *
103 * \return The return value of the underlying call to osl_get_object().
104 */
105 static int get_score_of_row(void *score_row, long *score)
106 {
107 struct osl_object obj;
108 int ret = osl(osl_get_object(score_table, score_row, SCORECOL_SCORE, &obj));
109
110 if (ret >= 0)
111 *score = *(long *)obj.data;
112 return ret;
113 }
114
115 /**
116 * Add an entry to the table of admissible files.
117 *
118 * \param aft_row The audio file to be added.
119 * \param score The score for this file.
120 *
121 * \return The return value of the underlying call to osl_add_row().
122 */
123 int score_add(const struct osl_row *aft_row, long score)
124 {
125 int ret;
126 struct osl_object score_objs[NUM_SCORE_COLUMNS];
127 size_t size;
128
129 assert(aft_row);
130 size = score_table_desc.column_descriptions[SCORECOL_AFT_ROW].data_size;
131 score_objs[SCORECOL_AFT_ROW].data = (struct osl_row *)aft_row;
132 score_objs[SCORECOL_AFT_ROW].size = size;
133
134 size = score_table_desc.column_descriptions[SCORECOL_SCORE].data_size;
135 score_objs[SCORECOL_SCORE].data = para_malloc(size);
136 score_objs[SCORECOL_SCORE].size = size;
137 *(long *)(score_objs[SCORECOL_SCORE].data) = score;
138
139 // PARA_DEBUG_LOG("adding %p\n", *(void **) (score_objs[SCORECOL_AFT_ROW].data));
140 ret = osl(osl_add_row(score_table, score_objs));
141 if (ret < 0) {
142 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
143 free(score_objs[SCORECOL_SCORE].data);
144 }
145 return ret;
146 }
147
148 static int get_nth_score(unsigned n, long *score)
149 {
150 struct osl_row *row;
151 int ret = osl(osl_get_nth_row(score_table, SCORECOL_SCORE, n, &row));
152
153 if (ret < 0)
154 return ret;
155 return get_score_of_row(row, score);
156 }
157
158 /**
159 * Replace a row of the score table.
160 *
161 * \param aft_row Determines the audio file to change.
162 * \param percent The position to re-insert the audio file.
163 *
164 * The percent parameter must be between 0 and 100. A value of zero means to
165 * re-insert the audio file into the score table with a score lower than any
166 * other admissible file.
167 *
168 * \return Positive on success, negative on errors.
169 */
170 int score_update(const struct osl_row *aft_row, long percent)
171 {
172 struct osl_row *row;
173 long new_score;
174 unsigned n, new_pos;
175 struct osl_object obj = {.data = (struct osl_row *)aft_row,
176 .size = sizeof(aft_row)};
177 int ret = osl(osl_get_row(score_table, SCORECOL_AFT_ROW, &obj, &row));
178
179 if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND)) /* not an error */
180 return 1;
181 if (ret < 0)
182 return ret;
183 ret = get_num_admissible_files(&n);
184 if (ret < 0)
185 return ret;
186 new_pos = 1 + (n - 1) * percent / 100;
187 ret = get_nth_score(new_pos, &new_score);
188 if (ret < 0)
189 return ret;
190 new_score--;
191 obj.size = sizeof(long);
192 obj.data = para_malloc(obj.size);
193 *(long *)obj.data = new_score;
194 PARA_DEBUG_LOG("new score: %ld, rank %u/%u\n", new_score, new_pos, n);
195 return osl(osl_update_object(score_table, row, SCORECOL_SCORE, &obj));
196 }
197
198 /**
199 * Given an admissible file, get the corresponding row in the aft and the score.
200 *
201 * \param score_row Determines the admissible file.
202 * \param score Result pointer.
203 * \param aft_row Result pointer.
204 *
205 * \return Negative on errors, positive on success. Possible errors: Errors
206 * returned by osl_get_object().
207 */
208 int get_score_and_aft_row(struct osl_row *score_row, long *score,
209 struct osl_row **aft_row)
210 {
211 struct osl_object obj;
212 int ret = get_score_of_row(score_row, score);
213
214 if (ret < 0)
215 return ret;
216 ret = osl(osl_get_object(score_table, score_row, SCORECOL_AFT_ROW, &obj));
217 if (ret < 0)
218 return ret;
219 *aft_row = obj.data;
220 return 1;
221 }
222
223 static int get_score_row_from_aft_row(const struct osl_row *aft_row,
224 struct osl_row **score_row)
225 {
226 struct osl_object obj = {.data = (struct osl_row *)aft_row,
227 .size = sizeof(aft_row)};
228 return osl(osl_get_row(score_table, SCORECOL_AFT_ROW, &obj, score_row));
229 }
230
231 /**
232 * Loop over all files in the score table.
233 *
234 * \param data A pointer to arbitrary data.
235 * \param func Function to be called for each admissible file.
236 *
237 * \return The return value of the underlying call to osl_rbtree_loop().
238 *
239 * This is used for the ls command. The \a data parameter is passed as the
240 * second argument to \a func.
241 */
242 int admissible_file_loop(void *data, osl_rbtree_loop_func *func)
243 {
244 return osl(osl_rbtree_loop(score_table, SCORECOL_SCORE, data, func));
245 }
246
247 /**
248 * Get the admissible audio file with highest score.
249 *
250 * \param aft_row Points to the row in the aft of the "best" audio file.
251 * \param score Highest score value in the score table.
252 *
253 * \return Positive on success, negative on errors. Possible errors: Errors
254 * returned by osl_rbtree_last_row(), osl_get_object().
255 */
256 int score_get_best(struct osl_row **aft_row, long *score)
257 {
258 struct osl_row *row;
259 struct osl_object obj;
260 int ret = osl(osl_rbtree_last_row(score_table, SCORECOL_SCORE, &row));
261
262 if (ret < 0)
263 return ret;
264 ret = osl(osl_get_object(score_table, row, SCORECOL_AFT_ROW, &obj));
265 if (ret < 0)
266 return ret;
267 *aft_row = obj.data;
268 return get_score_of_row(row, score);
269 }
270
271 /**
272 * Remove an entry from the rbtree of admissible files.
273 *
274 * \param aft_row The file which is no longer admissible.
275 *
276 * \return Positive on success, negative on errors. Possible errors:
277 * Errors returned by osl_get_row() and osl_del_row().
278 *
279 * \sa \ref score_add().
280 */
281 int score_delete(const struct osl_row *aft_row)
282 {
283 struct osl_row *score_row;
284 int ret = get_score_row_from_aft_row(aft_row, &score_row);
285
286 if (ret < 0)
287 return ret;
288 return osl(osl_del_row(score_table, score_row));
289 }
290
291 /**
292 * Find out whether an audio file is contained in the score table.
293 *
294 * \param aft_row The row of the audio file table.
295 * \param rank Result pointer
296 *
297 * \return Positive, if \a aft_row belongs to the audio file table,
298 * zero if not, negative on errors. If \a aft_row was found, and \a rank
299 * is not \p NULL, the rank of \a aft_row is returned in \a rank.
300 */
301 int row_belongs_to_score_table(const struct osl_row *aft_row, unsigned *rank)
302 {
303 struct osl_row *score_row;
304 int ret = get_score_row_from_aft_row(aft_row, &score_row);
305
306 if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
307 return 0;
308 if (ret < 0)
309 return ret;
310 if (!rank)
311 return 1;
312 ret = osl(osl_get_rank(score_table, score_row, SCORECOL_SCORE, rank));
313 if (ret < 0)
314 return ret;
315 return 1;
316 }
317
318 /* Close the score table. */
319 static void score_close(void)
320 {
321 osl_close_table(score_table, OSL_FREE_VOLATILE);
322 score_table = NULL;
323 }
324
325 /**
326 * Open the score table.
327 *
328 * \param dir Unused.
329 *
330 * \return The return value of the underlying call to osl_open_table().
331 */
332 static int score_open(__a_unused const char *dir)
333 {
334 score_table_desc.dir = NULL; /* this table has only volatile columns */
335 return osl(osl_open_table(&score_table_desc, &score_table));
336 }
337
338 /**
339 * Remove all entries from the score table, but keep the table open.
340 *
341 * \return Standard.
342 */
343 int clear_score_table(void)
344 {
345 score_close();
346 return score_open(NULL);
347 }
348
349 static int score_event_handler(__a_unused enum afs_events event,
350 __a_unused struct para_buffer *pb, __a_unused void *data)
351 {
352 return 1;
353 }
354
355 /**
356 * Initialize the scoring subsystem.
357 *
358 * \param t The members of \a t are filled in by the function.
359 */
360 void score_init(struct afs_table *t)
361 {
362 t->name = score_table_desc.name;
363 t->open = score_open;
364 t->close = score_close;
365 t->event_handler = score_event_handler;
366 }