]> git.tuebingen.mpg.de Git - paraslash.git/blob - score.c
paraslash 0.7.3
[paraslash.git] / score.c
1 /* Copyright (C) 2007 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file score.c Scoring functions to determine the audio file streaming order. */
4 #include <regex.h>
5 #include <osl.h>
6 #include <lopsub.h>
7
8 #include "para.h"
9 #include "error.h"
10 #include "string.h"
11 #include "afh.h"
12 #include "afs.h"
13 #include "list.h"
14
15 static struct osl_table *score_table;
16
17 static int ptr_compare(const struct osl_object *obj1, const struct osl_object *obj2)
18 {
19         void *d1 = *(void **)obj1->data;
20         void *d2 = *(void **)obj2->data;
21         return NUM_COMPARE(d1, d2);
22 }
23
24 /*
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
28  * memory address.
29  */
30 static int score_compare(const struct osl_object *obj1, const struct osl_object *obj2)
31 {
32         long d1 = *(long *)obj1->data;
33         long d2 = *(long *)obj2->data;
34         int ret = NUM_COMPARE(d2, d1);
35
36         if (ret)
37                 return ret;
38         return NUM_COMPARE(obj2->data, obj1->data);
39 }
40
41 /**
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.
45  */
46 enum score_table_columns {
47         /** The row of the audio file. */
48         SCORECOL_AFT_ROW,
49         /** The score */
50         SCORECOL_SCORE,
51         /** This table has two columns */
52         NUM_SCORE_COLUMNS
53 };
54
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,
59                 .name = "aft_row",
60                 .compare_function = ptr_compare,
61                 .data_size = sizeof(void *)
62         },
63         [SCORECOL_SCORE] = {
64                 .storage_type = OSL_NO_STORAGE,
65                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
66                 .name = "score",
67                 .compare_function = score_compare,
68                 .data_size = sizeof(long)
69         }
70 };
71
72 static struct osl_table_description score_table_desc = {
73         .name = "score",
74         .num_columns = NUM_SCORE_COLUMNS,
75         .flags = 0,
76         .column_descriptions = score_cols
77 };
78
79 /* On errors (negative return value) the content of score is undefined. */
80 static int get_score_of_row(struct osl_table *t, void *score_row, long *score)
81 {
82         struct osl_object obj;
83         int ret = osl(osl_get_object(t, score_row, SCORECOL_SCORE, &obj));
84
85         if (ret >= 0)
86                 *score = *(long *)obj.data;
87         return ret;
88 }
89
90 /**
91  * Add a (row, score) pair to the score table.
92  *
93  * \param aft_row Identifies the audio file to be added.
94  * \param score The score value of the audio file.
95  * \param t NULL means to operate on the currently active table.
96  *
97  * \return The return value of the underlying call to osl_add_row().
98  */
99 int score_add(const struct osl_row *aft_row, long score, struct osl_table *t)
100 {
101         int ret;
102         struct osl_object score_objs[NUM_SCORE_COLUMNS];
103         size_t size;
104
105         assert(aft_row);
106         size = score_table_desc.column_descriptions[SCORECOL_AFT_ROW].data_size;
107         score_objs[SCORECOL_AFT_ROW].data = (struct osl_row *)aft_row;
108         score_objs[SCORECOL_AFT_ROW].size = size;
109
110         size = score_table_desc.column_descriptions[SCORECOL_SCORE].data_size;
111         score_objs[SCORECOL_SCORE].data = alloc(size);
112         score_objs[SCORECOL_SCORE].size = size;
113         *(long *)(score_objs[SCORECOL_SCORE].data) = score;
114
115 //      PARA_DEBUG_LOG("adding %p\n", *(void **) (score_objs[SCORECOL_AFT_ROW].data));
116         ret = osl(osl_add_row(t? t : score_table, score_objs));
117         if (ret < 0) {
118                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
119                 free(score_objs[SCORECOL_SCORE].data);
120         }
121         return ret;
122 }
123
124 /**
125  * Replace a row of the score table.
126  *
127  * \param aft_row Determines the audio file to change.
128  * \param percent The position to re-insert the audio file.
129  *
130  * The percent parameter must be between 0 and 100. A value of zero means to
131  * re-insert the audio file into the score table with a score lower than any
132  * other admissible file.
133  *
134  * \return Positive on success, negative on errors.
135  */
136 int score_update(const struct osl_row *aft_row, long percent)
137 {
138         struct osl_row *row, *rrow; /* score row, reference row */
139         long new_score;
140         unsigned n, new_pos;
141         struct osl_object obj = {.data = (struct osl_row *)aft_row,
142                 .size = sizeof(aft_row)};
143         int ret = osl(osl_get_row(score_table, SCORECOL_AFT_ROW, &obj, &row));
144
145         if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND)) /* not an error */
146                 return 1;
147         if (ret < 0)
148                 return ret;
149         ret = osl(osl_get_num_rows(score_table, &n));
150         if (ret < 0)
151                 return ret;
152         new_pos = 1 + (n - 1) * percent / 100;
153         ret = osl(osl_get_nth_row(score_table, SCORECOL_SCORE, new_pos, &rrow));
154         if (ret < 0)
155                 return ret;
156         ret = get_score_of_row(score_table, rrow, &new_score);
157         if (ret < 0)
158                 return ret;
159         new_score--;
160         obj.size = sizeof(long);
161         obj.data = alloc(obj.size);
162         *(long *)obj.data = new_score;
163         PARA_DEBUG_LOG("new score: %ld, rank %u/%u\n", new_score, new_pos, n);
164         return osl(osl_update_object(score_table, row, SCORECOL_SCORE, &obj));
165 }
166
167 /**
168  * Given an admissible file, get the corresponding row in the aft and the score.
169  *
170  * \param score_row Determines the admissible file.
171  * \param score Result pointer.
172  * \param aft_row Result pointer.
173  *
174  * \return Standard.
175  */
176 int get_score_and_aft_row(struct osl_row *score_row, long *score,
177                 struct osl_row **aft_row)
178 {
179         struct osl_object obj;
180         int ret = get_score_of_row(score_table, score_row, score);
181
182         if (ret < 0)
183                 return ret;
184         ret = osl(osl_get_object(score_table, score_row, SCORECOL_AFT_ROW, &obj));
185         if (ret < 0)
186                 return ret;
187         *aft_row = obj.data;
188         return 1;
189 }
190
191 static int get_score_row_from_aft_row(struct osl_table *t,
192                 const struct osl_row *aft_row, struct osl_row **score_row)
193 {
194         struct osl_object obj = {.data = (struct osl_row *)aft_row,
195                 .size = sizeof(aft_row)};
196         return osl(osl_get_row(t, SCORECOL_AFT_ROW, &obj, score_row));
197 }
198
199 /**
200  * Call the given function for each row of the score table.
201  *
202  * \param func Callback, called once per row.
203  * \param t NULL means to use the currently active score table.
204  * \param data Passed verbatim to the callback.
205  *
206  * \return The return value of the underlying call to osl_rbtree_loop(). The
207  * loop terminates early if the callback returns negative.
208  */
209 int score_loop(osl_rbtree_loop_func *func, struct osl_table *t, void *data)
210 {
211         return osl(osl_rbtree_loop(t? t : score_table, SCORECOL_SCORE, data,
212                 func));
213 }
214
215 /**
216  * Get the admissible audio file with highest score.
217  *
218  * \param aft_row Points to the row in the aft of the "best" audio file.
219  * \param score Highest score value in the score table.
220  *
221  * \return Standard.
222  */
223 int score_get_best(struct osl_row **aft_row, long *score)
224 {
225         struct osl_row *row;
226         struct osl_object obj;
227         int ret = osl(osl_rbtree_last_row(score_table, SCORECOL_SCORE, &row));
228
229         if (ret < 0)
230                 return ret;
231         ret = osl(osl_get_object(score_table, row, SCORECOL_AFT_ROW, &obj));
232         if (ret < 0)
233                 return ret;
234         *aft_row = obj.data;
235         return get_score_of_row(score_table, row, score);
236 }
237
238 /**
239  * Remove an entry from the rbtree of admissible files.
240  *
241  * \param aft_row The file which is no longer admissible.
242  *
243  * \return Standard.
244  *
245  * \sa \ref score_add().
246  */
247 int score_delete(const struct osl_row *aft_row)
248 {
249         struct osl_row *score_row;
250         int ret = get_score_row_from_aft_row(score_table, aft_row, &score_row);
251
252         if (ret < 0)
253                 return ret;
254         return osl(osl_del_row(score_table, score_row));
255 }
256
257 /**
258  * Find out whether an audio file is contained in the score table.
259  *
260  * \param aft_row The row of the audio file table.
261  *
262  * \return If the lookup operation fails for any other reason than "not found",
263  * the function aborts the current process (afs), since this is considered a
264  * fatal error that should never happen.
265  */
266 bool row_belongs_to_score_table(const struct osl_row *aft_row)
267 {
268         struct osl_row *score_row;
269         int ret = get_score_row_from_aft_row(score_table, aft_row, &score_row);
270
271         if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
272                 return false;
273         assert(ret >= 0);
274         return true;
275 }
276
277 /**
278  * Free all volatile objects, then close the table.
279  *
280  * \param t As returned from \ref score_open().
281  *
282  * This either succeeds or terminates the calling process.
283  */
284 void score_close(struct osl_table *t)
285 {
286         assert(osl_close_table(t? t : score_table, OSL_FREE_VOLATILE) >= 0);
287 }
288
289 static void close_global_table(void)
290 {
291         score_close(NULL);
292 }
293
294 static int open_global_table(__a_unused const char *dir)
295 {
296         assert(osl(osl_open_table(&score_table_desc, &score_table)) >= 0);
297         return 1;
298 }
299
300 /**
301  * Allocate a score table instance.
302  *
303  * \param result NULL means to open the currently active score table.
304  *
305  * Since the score table does no filesystem I/O, this function always succeeds.
306  * \sa \ref score_close().
307  */
308 void score_open(struct osl_table **result)
309 {
310         if (result)
311                 assert(osl(osl_open_table(&score_table_desc, result)) >= 0);
312         else
313                 open_global_table(NULL);
314 }
315
316 /**
317  * Remove all entries from the score table, but keep the table open.
318  */
319 void score_clear(void)
320 {
321         close_global_table();
322         open_global_table(NULL);
323 }
324
325 /** The score table stores (aft row, score) pairs in memory. */
326 const struct afs_table_operations score_ops = {
327         .open = open_global_table,
328         .close = close_global_table,
329 };