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