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