1 /* Copyright (C) 2007 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file score.c Scoring functions to determine the audio file streaming order. */
15 static struct osl_table *score_table;
17 static int ptr_compare(const struct osl_object *obj1, const struct osl_object *obj2)
19 void *d1 = *(void **)obj1->data;
20 void *d2 = *(void **)obj2->data;
21 return NUM_COMPARE(d1, d2);
25 * Compare the score of two audio files
27 * \param obj1 Pointer to the first score object.
28 * \param obj2 Pointer to the second score object.
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.
34 static int score_compare(const struct osl_object *obj1, const struct osl_object *obj2)
36 long d1 = *(long *)obj1->data;
37 long d2 = *(long *)obj2->data;
38 int ret = NUM_COMPARE(d2, d1);
42 return NUM_COMPARE(obj2->data, obj1->data);
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.
50 enum score_table_columns {
51 /** The row of the audio file. */
55 /** This table has two columns */
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,
64 .compare_function = ptr_compare,
65 .data_size = sizeof(void *)
68 .storage_type = OSL_NO_STORAGE,
69 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
71 .compare_function = score_compare,
72 .data_size = sizeof(long)
76 static struct osl_table_description score_table_desc = {
78 .num_columns = NUM_SCORE_COLUMNS,
80 .column_descriptions = score_cols
84 * Compute the number of files in score table.
86 * \param num Result is returned here.
88 * \return Positive on success, negative on errors.
90 int get_num_admissible_files(unsigned *num)
92 return osl(osl_get_num_rows(score_table, num));
95 /* On errors (negative return value) the content of score is undefined. */
96 static int get_score_of_row(void *score_row, long *score)
98 struct osl_object obj;
99 int ret = osl(osl_get_object(score_table, score_row, SCORECOL_SCORE, &obj));
102 *score = *(long *)obj.data;
107 * Add an entry to the table of admissible files.
109 * \param aft_row The audio file to be added.
110 * \param score The score for this file.
112 * \return The return value of the underlying call to osl_add_row().
114 int score_add(const struct osl_row *aft_row, long score)
117 struct osl_object score_objs[NUM_SCORE_COLUMNS];
121 size = score_table_desc.column_descriptions[SCORECOL_AFT_ROW].data_size;
122 score_objs[SCORECOL_AFT_ROW].data = (struct osl_row *)aft_row;
123 score_objs[SCORECOL_AFT_ROW].size = size;
125 size = score_table_desc.column_descriptions[SCORECOL_SCORE].data_size;
126 score_objs[SCORECOL_SCORE].data = para_malloc(size);
127 score_objs[SCORECOL_SCORE].size = size;
128 *(long *)(score_objs[SCORECOL_SCORE].data) = score;
130 // PARA_DEBUG_LOG("adding %p\n", *(void **) (score_objs[SCORECOL_AFT_ROW].data));
131 ret = osl(osl_add_row(score_table, score_objs));
133 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
134 free(score_objs[SCORECOL_SCORE].data);
139 static int get_nth_score(unsigned n, long *score)
142 int ret = osl(osl_get_nth_row(score_table, SCORECOL_SCORE, n, &row));
146 return get_score_of_row(row, score);
150 * Replace a row of the score table.
152 * \param aft_row Determines the audio file to change.
153 * \param percent The position to re-insert the audio file.
155 * The percent parameter must be between 0 and 100. A value of zero means to
156 * re-insert the audio file into the score table with a score lower than any
157 * other admissible file.
159 * \return Positive on success, negative on errors.
161 int score_update(const struct osl_row *aft_row, long percent)
166 struct osl_object obj = {.data = (struct osl_row *)aft_row,
167 .size = sizeof(aft_row)};
168 int ret = osl(osl_get_row(score_table, SCORECOL_AFT_ROW, &obj, &row));
170 if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND)) /* not an error */
174 ret = get_num_admissible_files(&n);
177 new_pos = 1 + (n - 1) * percent / 100;
178 ret = get_nth_score(new_pos, &new_score);
182 obj.size = sizeof(long);
183 obj.data = para_malloc(obj.size);
184 *(long *)obj.data = new_score;
185 PARA_DEBUG_LOG("new score: %ld, rank %u/%u\n", new_score, new_pos, n);
186 return osl(osl_update_object(score_table, row, SCORECOL_SCORE, &obj));
190 * Given an admissible file, get the corresponding row in the aft and the score.
192 * \param score_row Determines the admissible file.
193 * \param score Result pointer.
194 * \param aft_row Result pointer.
196 * \return Negative on errors, positive on success. Possible errors: Errors
197 * returned by osl_get_object().
199 int get_score_and_aft_row(struct osl_row *score_row, long *score,
200 struct osl_row **aft_row)
202 struct osl_object obj;
203 int ret = get_score_of_row(score_row, score);
207 ret = osl(osl_get_object(score_table, score_row, SCORECOL_AFT_ROW, &obj));
214 static int get_score_row_from_aft_row(const struct osl_row *aft_row,
215 struct osl_row **score_row)
217 struct osl_object obj = {.data = (struct osl_row *)aft_row,
218 .size = sizeof(aft_row)};
219 return osl(osl_get_row(score_table, SCORECOL_AFT_ROW, &obj, score_row));
223 * Loop over all files in the score table.
225 * \param data A pointer to arbitrary data.
226 * \param func Function to be called for each admissible file.
228 * \return The return value of the underlying call to osl_rbtree_loop().
230 * This is used for the ls command. The \a data parameter is passed as the
231 * second argument to \a func.
233 int admissible_file_loop(void *data, osl_rbtree_loop_func *func)
235 return osl(osl_rbtree_loop(score_table, SCORECOL_SCORE, data, func));
239 * Get the admissible audio file with highest score.
241 * \param aft_row Points to the row in the aft of the "best" audio file.
242 * \param score Highest score value in the score table.
244 * \return Positive on success, negative on errors. Possible errors: Errors
245 * returned by osl_rbtree_last_row(), osl_get_object().
247 int score_get_best(struct osl_row **aft_row, long *score)
250 struct osl_object obj;
251 int ret = osl(osl_rbtree_last_row(score_table, SCORECOL_SCORE, &row));
255 ret = osl(osl_get_object(score_table, row, SCORECOL_AFT_ROW, &obj));
259 return get_score_of_row(row, score);
263 * Remove an entry from the rbtree of admissible files.
265 * \param aft_row The file which is no longer admissible.
267 * \return Positive on success, negative on errors. Possible errors:
268 * Errors returned by osl_get_row() and osl_del_row().
270 * \sa \ref score_add().
272 int score_delete(const struct osl_row *aft_row)
274 struct osl_row *score_row;
275 int ret = get_score_row_from_aft_row(aft_row, &score_row);
279 return osl(osl_del_row(score_table, score_row));
283 * Find out whether an audio file is contained in the score table.
285 * \param aft_row The row of the audio file table.
286 * \param rank Result pointer
288 * \return Positive, if \a aft_row belongs to the audio file table,
289 * zero if not, negative on errors. If \a aft_row was found, and \a rank
290 * is not \p NULL, the rank of \a aft_row is returned in \a rank.
292 int row_belongs_to_score_table(const struct osl_row *aft_row, unsigned *rank)
294 struct osl_row *score_row;
295 int ret = get_score_row_from_aft_row(aft_row, &score_row);
297 if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
303 ret = osl(osl_get_rank(score_table, score_row, SCORECOL_SCORE, rank));
309 /* Close the score table. */
310 static void score_close(void)
312 osl_close_table(score_table, OSL_FREE_VOLATILE);
317 * Open the score table.
321 * \return The return value of the underlying call to osl_open_table().
323 static int score_open(__a_unused const char *dir)
325 score_table_desc.dir = NULL; /* this table has only volatile columns */
326 return osl(osl_open_table(&score_table_desc, &score_table));
330 * Remove all entries from the score table, but keep the table open.
334 int clear_score_table(void)
337 return score_open(NULL);
340 static int score_event_handler(__a_unused enum afs_events event,
341 __a_unused struct para_buffer *pb, __a_unused void *data)
347 * Initialize the scoring subsystem.
349 * \param t The members of \a t are filled in by the function.
351 void score_init(struct afs_table *t)
353 t->name = score_table_desc.name;
354 t->open = score_open;
355 t->close = score_close;
356 t->event_handler = score_event_handler;