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