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 * This function first compares the score values. If they are equal, the
26 * addresses of the two objects are compared. Thus, the function returns
27 * "equal" only if the two objects alias each other, i.e., point to the same
30 static int score_compare(const struct osl_object *obj1, const struct osl_object *obj2)
32 long d1 = *(long *)obj1->data;
33 long d2 = *(long *)obj2->data;
34 int ret = NUM_COMPARE(d2, d1);
38 return NUM_COMPARE(obj2->data, obj1->data);
42 * The score table consists of two columns: The \a aft_row column contains
43 * pointers to the rows of the audio file table, and the score column contains
44 * the current score of the audio file associated with that row.
46 enum score_table_columns {
47 /** The row of the audio file. */
51 /** This table has two columns */
55 static struct osl_column_description score_cols[] = {
56 [SCORECOL_AFT_ROW] = {
57 .storage_type = OSL_NO_STORAGE,
58 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE | OSL_DONT_FREE,
60 .compare_function = ptr_compare,
61 .data_size = sizeof(void *)
64 .storage_type = OSL_NO_STORAGE,
65 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
67 .compare_function = score_compare,
68 .data_size = sizeof(long)
72 static struct osl_table_description score_table_desc = {
74 .num_columns = NUM_SCORE_COLUMNS,
76 .column_descriptions = score_cols
80 * Compute the number of files in score table.
82 * \param num Result is returned here.
84 * \return Positive on success, negative on errors.
86 int get_num_admissible_files(unsigned *num)
88 return osl(osl_get_num_rows(score_table, num));
91 /* On errors (negative return value) the content of score is undefined. */
92 static int get_score_of_row(void *score_row, long *score)
94 struct osl_object obj;
95 int ret = osl(osl_get_object(score_table, score_row, SCORECOL_SCORE, &obj));
98 *score = *(long *)obj.data;
103 * Add an entry to the table of admissible files.
105 * \param aft_row The audio file to be added.
106 * \param score The score for this file.
108 * \return The return value of the underlying call to osl_add_row().
110 int score_add(const struct osl_row *aft_row, long score)
113 struct osl_object score_objs[NUM_SCORE_COLUMNS];
117 size = score_table_desc.column_descriptions[SCORECOL_AFT_ROW].data_size;
118 score_objs[SCORECOL_AFT_ROW].data = (struct osl_row *)aft_row;
119 score_objs[SCORECOL_AFT_ROW].size = size;
121 size = score_table_desc.column_descriptions[SCORECOL_SCORE].data_size;
122 score_objs[SCORECOL_SCORE].data = alloc(size);
123 score_objs[SCORECOL_SCORE].size = size;
124 *(long *)(score_objs[SCORECOL_SCORE].data) = score;
126 // PARA_DEBUG_LOG("adding %p\n", *(void **) (score_objs[SCORECOL_AFT_ROW].data));
127 ret = osl(osl_add_row(score_table, score_objs));
129 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
130 free(score_objs[SCORECOL_SCORE].data);
136 * Replace a row of the score table.
138 * \param aft_row Determines the audio file to change.
139 * \param percent The position to re-insert the audio file.
141 * The percent parameter must be between 0 and 100. A value of zero means to
142 * re-insert the audio file into the score table with a score lower than any
143 * other admissible file.
145 * \return Positive on success, negative on errors.
147 int score_update(const struct osl_row *aft_row, long percent)
149 struct osl_row *row, *rrow; /* score row, reference row */
152 struct osl_object obj = {.data = (struct osl_row *)aft_row,
153 .size = sizeof(aft_row)};
154 int ret = osl(osl_get_row(score_table, SCORECOL_AFT_ROW, &obj, &row));
156 if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND)) /* not an error */
160 ret = get_num_admissible_files(&n);
163 new_pos = 1 + (n - 1) * percent / 100;
164 ret = osl(osl_get_nth_row(score_table, SCORECOL_SCORE, new_pos, &rrow));
167 ret = get_score_of_row(rrow, &new_score);
171 obj.size = sizeof(long);
172 obj.data = alloc(obj.size);
173 *(long *)obj.data = new_score;
174 PARA_DEBUG_LOG("new score: %ld, rank %u/%u\n", new_score, new_pos, n);
175 return osl(osl_update_object(score_table, row, SCORECOL_SCORE, &obj));
179 * Given an admissible file, get the corresponding row in the aft and the score.
181 * \param score_row Determines the admissible file.
182 * \param score Result pointer.
183 * \param aft_row Result pointer.
187 int get_score_and_aft_row(struct osl_row *score_row, long *score,
188 struct osl_row **aft_row)
190 struct osl_object obj;
191 int ret = get_score_of_row(score_row, score);
195 ret = osl(osl_get_object(score_table, score_row, SCORECOL_AFT_ROW, &obj));
202 static int get_score_row_from_aft_row(const struct osl_row *aft_row,
203 struct osl_row **score_row)
205 struct osl_object obj = {.data = (struct osl_row *)aft_row,
206 .size = sizeof(aft_row)};
207 return osl(osl_get_row(score_table, SCORECOL_AFT_ROW, &obj, score_row));
211 * Loop over all files in the score table.
213 * \param data A pointer to arbitrary data.
214 * \param func Function to be called for each admissible file.
216 * \return The return value of the underlying call to osl_rbtree_loop().
218 * This is used for the ls command. The \a data parameter is passed as the
219 * second argument to \a func.
221 int admissible_file_loop(void *data, osl_rbtree_loop_func *func)
223 return osl(osl_rbtree_loop(score_table, SCORECOL_SCORE, data, func));
227 * Get the admissible audio file with highest score.
229 * \param aft_row Points to the row in the aft of the "best" audio file.
230 * \param score Highest score value in the score table.
234 int score_get_best(struct osl_row **aft_row, long *score)
237 struct osl_object obj;
238 int ret = osl(osl_rbtree_last_row(score_table, SCORECOL_SCORE, &row));
242 ret = osl(osl_get_object(score_table, row, SCORECOL_AFT_ROW, &obj));
246 return get_score_of_row(row, score);
250 * Remove an entry from the rbtree of admissible files.
252 * \param aft_row The file which is no longer admissible.
256 * \sa \ref score_add().
258 int score_delete(const struct osl_row *aft_row)
260 struct osl_row *score_row;
261 int ret = get_score_row_from_aft_row(aft_row, &score_row);
265 return osl(osl_del_row(score_table, score_row));
269 * Find out whether an audio file is contained in the score table.
271 * \param aft_row The row of the audio file table.
272 * \param rank Result pointer
274 * \return Positive, if \a aft_row belongs to the audio file table,
275 * zero if not, negative on errors. If \a aft_row was found, and \a rank
276 * is not \p NULL, the rank of \a aft_row is returned in \a rank.
278 int row_belongs_to_score_table(const struct osl_row *aft_row, unsigned *rank)
280 struct osl_row *score_row;
281 int ret = get_score_row_from_aft_row(aft_row, &score_row);
283 if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
289 ret = osl(osl_get_rank(score_table, score_row, SCORECOL_SCORE, rank));
295 static void score_close(void)
297 osl_close_table(score_table, OSL_FREE_VOLATILE);
301 static int score_open(__a_unused const char *dir)
303 return osl(osl_open_table(&score_table_desc, &score_table));
307 * Remove all entries from the score table, but keep the table open.
311 int clear_score_table(void)
314 return score_open(NULL);
318 * Initialize the scoring subsystem.
320 * \param t The members of \a t are filled in by the function.
322 void score_init(struct afs_table *t)
324 t->name = score_table_desc.name;
325 t->open = score_open;
326 t->close = score_close;