paraslash 0.7.3
[paraslash.git] / score.c
diff --git a/score.c b/score.c
index f5fe4a833f6f6f99ca3d9afca49175113b1eac78..c03e3472da306a1d535b201ea2d23c90ccf8f90a 100644 (file)
--- a/score.c
+++ b/score.c
@@ -1,12 +1,9 @@
-/*
- * Copyright (C) 2007-2011 Andre Noll <maan@systemlinux.org>
- *
- * Licensed under the GPL v2. For licencing details see COPYING.
- */
+/* Copyright (C) 2007 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
 
 /** \file score.c Scoring functions to determine the audio file streaming order. */
 #include <regex.h>
 #include <osl.h>
+#include <lopsub.h>
 
 #include "para.h"
 #include "error.h"
@@ -24,17 +21,11 @@ static int ptr_compare(const struct osl_object *obj1, const struct osl_object *o
        return NUM_COMPARE(d1, d2);
 }
 
-/**
- * Compare the score of two audio files
- *
- * \param obj1 Pointer to the first score object.
- * \param obj2 Pointer to the second score object.
- *
- * This function first compares the score values as usual integers. If they compare as
- * equal, the address of \a obj1 and \a obj2 are compared. So this compare function
- * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
- *
- * \sa osl_compare_function.
+/*
+ * This function first compares the score values. If they are equal, the
+ * addresses of the two objects are compared. Thus, the function returns
+ * "equal" only if the two objects alias each other, i.e., point to the same
+ * memory address.
  */
 static int score_compare(const struct osl_object *obj1, const struct osl_object *obj2)
 {
@@ -71,7 +62,7 @@ static struct osl_column_description score_cols[] = {
        },
        [SCORECOL_SCORE] = {
                .storage_type = OSL_NO_STORAGE,
-               .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
+               .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
                .name = "score",
                .compare_function = score_compare,
                .data_size = sizeof(long)
@@ -85,34 +76,11 @@ static struct osl_table_description score_table_desc = {
        .column_descriptions = score_cols
 };
 
-/**
- * Compute the number of files in score table.
- *
- * \param num Result is returned here.
- *
- * \return Positive on success, negative on errors.
- *
- * \sa osl_get_num_rows().
- */
-int get_num_admissible_files(unsigned *num)
-{
-       return osl(osl_get_num_rows(score_table, num));
-}
-
-/**
- * Get the score of the audio file associated with given row of the score table.
- *
- * \param score_row Pointer to the row in the score table.
- * \param score Result is returned here on success.
- *
- * On errors (negative return value) the content of \a score is undefined.
- *
- * \return The return value of the underlying call to osl_get_object().
- */
-static int get_score_of_row(void *score_row, long *score)
+/* On errors (negative return value) the content of score is undefined. */
+static int get_score_of_row(struct osl_table *t, void *score_row, long *score)
 {
        struct osl_object obj;
-       int ret = osl(osl_get_object(score_table, score_row, SCORECOL_SCORE, &obj));
+       int ret = osl(osl_get_object(t, score_row, SCORECOL_SCORE, &obj));
 
        if (ret >= 0)
                *score = *(long *)obj.data;
@@ -120,14 +88,15 @@ static int get_score_of_row(void *score_row, long *score)
 }
 
 /**
- * Add an entry to the table of admissible files.
+ * Add a (row, score) pair to the score table.
  *
- * \param aft_row The audio file to be added.
- * \param score The score for this file.
+ * \param aft_row Identifies the audio file to be added.
+ * \param score The score value of the audio file.
+ * \param t NULL means to operate on the currently active table.
  *
  * \return The return value of the underlying call to osl_add_row().
  */
-int score_add(const struct osl_row *aft_row, long score)
+int score_add(const struct osl_row *aft_row, long score, struct osl_table *t)
 {
        int ret;
        struct osl_object score_objs[NUM_SCORE_COLUMNS];
@@ -139,12 +108,12 @@ int score_add(const struct osl_row *aft_row, long score)
        score_objs[SCORECOL_AFT_ROW].size = size;
 
        size = score_table_desc.column_descriptions[SCORECOL_SCORE].data_size;
-       score_objs[SCORECOL_SCORE].data = para_malloc(size);
+       score_objs[SCORECOL_SCORE].data = alloc(size);
        score_objs[SCORECOL_SCORE].size = size;
        *(long *)(score_objs[SCORECOL_SCORE].data) = score;
 
 //     PARA_DEBUG_LOG("adding %p\n", *(void **) (score_objs[SCORECOL_AFT_ROW].data));
-       ret = osl(osl_add_row(score_table, score_objs));
+       ret = osl(osl_add_row(t? t : score_table, score_objs));
        if (ret < 0) {
                PARA_ERROR_LOG("%s\n", para_strerror(-ret));
                free(score_objs[SCORECOL_SCORE].data);
@@ -152,33 +121,21 @@ int score_add(const struct osl_row *aft_row, long score)
        return ret;
 }
 
-static int get_nth_score(unsigned n, long *score)
-{
-       struct osl_row *row;
-       int ret = osl(osl_get_nth_row(score_table, SCORECOL_SCORE, n, &row));
-
-       if (ret < 0)
-               return ret;
-       return get_score_of_row(row, score);
-}
-
 /**
  * Replace a row of the score table.
  *
  * \param aft_row Determines the audio file to change.
  * \param percent The position to re-insert the audio file.
  *
- * The percent parameter must be between \p 0 and 100 and. A value of zero
- * means to re-insert the audio file into the score table with a score lower
- * than any other admissible file.
+ * The percent parameter must be between 0 and 100. A value of zero means to
+ * re-insert the audio file into the score table with a score lower than any
+ * other admissible file.
  *
- * \return Positive on success, negative on errors. Possible errors: Errors
- * returned by osl_get_row(), get_num_admissible_files(), osl_get_nth_row(),
- * osl_get_object(), osl_update_object().
+ * \return Positive on success, negative on errors.
  */
 int score_update(const struct osl_row *aft_row, long percent)
 {
-       struct osl_row *row;
+       struct osl_row *row, *rrow; /* score row, reference row */
        long new_score;
        unsigned n, new_pos;
        struct osl_object obj = {.data = (struct osl_row *)aft_row,
@@ -189,16 +146,19 @@ int score_update(const struct osl_row *aft_row, long percent)
                return 1;
        if (ret < 0)
                return ret;
-       ret = get_num_admissible_files(&n);
+       ret = osl(osl_get_num_rows(score_table, &n));
        if (ret < 0)
                return ret;
        new_pos = 1 + (n - 1) * percent / 100;
-       ret = get_nth_score(new_pos, &new_score);
+       ret = osl(osl_get_nth_row(score_table, SCORECOL_SCORE, new_pos, &rrow));
+       if (ret < 0)
+               return ret;
+       ret = get_score_of_row(score_table, rrow, &new_score);
        if (ret < 0)
                return ret;
        new_score--;
        obj.size = sizeof(long);
-       obj.data = para_malloc(obj.size);
+       obj.data = alloc(obj.size);
        *(long *)obj.data = new_score;
        PARA_DEBUG_LOG("new score: %ld, rank %u/%u\n", new_score, new_pos, n);
        return osl(osl_update_object(score_table, row, SCORECOL_SCORE, &obj));
@@ -211,14 +171,13 @@ int score_update(const struct osl_row *aft_row, long percent)
  * \param score Result pointer.
  * \param aft_row Result pointer.
  *
- * \return Negative on errors, positive on success. Possible errors: Errors
- * returned by osl_get_object().
+ * \return Standard.
  */
 int get_score_and_aft_row(struct osl_row *score_row, long *score,
                struct osl_row **aft_row)
 {
        struct osl_object obj;
-       int ret = get_score_of_row(score_row, score);
+       int ret = get_score_of_row(score_table, score_row, score);
 
        if (ret < 0)
                return ret;
@@ -229,45 +188,28 @@ int get_score_and_aft_row(struct osl_row *score_row, long *score,
        return 1;
 }
 
-static int get_score_row_from_aft_row(const struct osl_row *aft_row,
-               struct osl_row **score_row)
+static int get_score_row_from_aft_row(struct osl_table *t,
+               const struct osl_row *aft_row, struct osl_row **score_row)
 {
        struct osl_object obj = {.data = (struct osl_row *)aft_row,
                .size = sizeof(aft_row)};
-       return osl(osl_get_row(score_table, SCORECOL_AFT_ROW, &obj, score_row));
+       return osl(osl_get_row(t, SCORECOL_AFT_ROW, &obj, score_row));
 }
 
 /**
- * Loop over all files in the score table.
- *
- * \param data A pointer to arbitrary data.
- * \param func Function to be called for each admissible file.
+ * Call the given function for each row of the score table.
  *
- * \return The return value of the underlying call to osl_rbtree_loop().
+ * \param func Callback, called once per row.
+ * \param t NULL means to use the currently active score table.
+ * \param data Passed verbatim to the callback.
  *
- * This is used for the ls command. The \a data parameter is passed as the
- * second argument to \a func.
- *
- * \sa admissible_file_loop_reverse().
- */
-int admissible_file_loop(void *data, osl_rbtree_loop_func *func)
-{
-       return osl(osl_rbtree_loop(score_table, SCORECOL_SCORE, data, func));
-}
-
-/**
- * Loop over all files in the score table in reverse order.
- *
- * \param data As in admissible_file_loop().
- * \param func As in admissible_file_loop().
- *
- * \return Same return value as admissible_file_loop().
- *
- * \sa admissible_file_loop(), osl_rbtree_loop_reverse().
+ * \return The return value of the underlying call to osl_rbtree_loop(). The
+ * loop terminates early if the callback returns negative.
  */
-int admissible_file_loop_reverse(void *data, osl_rbtree_loop_func *func)
+int score_loop(osl_rbtree_loop_func *func, struct osl_table *t, void *data)
 {
-       return osl(osl_rbtree_loop_reverse(score_table, SCORECOL_SCORE, data, func));
+       return osl(osl_rbtree_loop(t? t : score_table, SCORECOL_SCORE, data,
+               func));
 }
 
 /**
@@ -276,8 +218,7 @@ int admissible_file_loop_reverse(void *data, osl_rbtree_loop_func *func)
  * \param aft_row Points to the row in the aft of the "best" audio file.
  * \param score Highest score value in the score table.
  *
- * \return Positive on success, negative on errors. Possible errors: Errors
- * returned by osl_rbtree_last_row(), osl_get_object().
+ * \return Standard.
  */
 int score_get_best(struct osl_row **aft_row, long *score)
 {
@@ -291,7 +232,7 @@ int score_get_best(struct osl_row **aft_row, long *score)
        if (ret < 0)
                return ret;
        *aft_row = obj.data;
-       return get_score_of_row(row, score);
+       return get_score_of_row(score_table, row, score);
 }
 
 /**
@@ -299,15 +240,14 @@ int score_get_best(struct osl_row **aft_row, long *score)
  *
  * \param aft_row The file which is no longer admissible.
  *
- * \return Positive on success, negative on errors. Possible errors:
- * Errors returned by osl_get_row() and osl_del_row().
+ * \return Standard.
  *
- * \sa score_add(), score_shutdown().
+ * \sa \ref score_add().
  */
 int score_delete(const struct osl_row *aft_row)
 {
        struct osl_row *score_row;
-       int ret = get_score_row_from_aft_row(aft_row, &score_row);
+       int ret = get_score_row_from_aft_row(score_table, aft_row, &score_row);
 
        if (ret < 0)
                return ret;
@@ -318,75 +258,72 @@ int score_delete(const struct osl_row *aft_row)
  * Find out whether an audio file is contained in the score table.
  *
  * \param aft_row The row of the audio file table.
- * \param rank Result pointer
  *
- * \return Positive, if \a aft_row belongs to the audio file table,
- * zero if not, negative on errors. If \a aft_row was found, and \a rank
- * is not \p NULL, the rank of \a aft_row is returned in \a rank.
+ * \return If the lookup operation fails for any other reason than "not found",
+ * the function aborts the current process (afs), since this is considered a
+ * fatal error that should never happen.
  */
-int row_belongs_to_score_table(const struct osl_row *aft_row, unsigned *rank)
+bool row_belongs_to_score_table(const struct osl_row *aft_row)
 {
        struct osl_row *score_row;
-       int ret = get_score_row_from_aft_row(aft_row, &score_row);
+       int ret = get_score_row_from_aft_row(score_table, aft_row, &score_row);
 
        if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
-               return 0;
-       if (ret < 0)
-               return ret;
-       if (!rank)
-               return 1;
-       ret = osl(osl_get_rank(score_table, score_row, SCORECOL_SCORE, rank));
-       if (ret < 0)
-               return ret;
-       return 1;
-}
-
-/* Close the score table. */
-static void score_close(void)
-{
-       osl_close_table(score_table, OSL_FREE_VOLATILE);
-       score_table = NULL;
+               return false;
+       assert(ret >= 0);
+       return true;
 }
 
 /**
- * Open the score table.
+ * Free all volatile objects, then close the table.
  *
- * \param dir Unused.
+ * \param t As returned from \ref score_open().
  *
- * \return The return value of the underlying call to osl_open_table().
+ * This either succeeds or terminates the calling process.
  */
-static int score_open(__a_unused const char *dir)
+void score_close(struct osl_table *t)
 {
-       score_table_desc.dir = NULL; /* this table has only volatile columns */
-       return osl(osl_open_table(&score_table_desc, &score_table));
+       assert(osl_close_table(t? t : score_table, OSL_FREE_VOLATILE) >= 0);
 }
 
-/**
- * Remove all entries from the score table, but keep the table open.
- *
- * \return Standard.
- */
-int clear_score_table(void)
+static void close_global_table(void)
 {
-       score_close();
-       return score_open(NULL);
+       score_close(NULL);
 }
 
-static int score_event_handler(__a_unused enum afs_events event,
-               __a_unused struct para_buffer *pb, __a_unused void *data)
+static int open_global_table(__a_unused const char *dir)
 {
+       assert(osl(osl_open_table(&score_table_desc, &score_table)) >= 0);
        return 1;
 }
 
 /**
- * Initialize the scoring subsystem.
+ * Allocate a score table instance.
+ *
+ * \param result NULL means to open the currently active score table.
  *
- * \param t The members of \a t are filled in by the function.
+ * Since the score table does no filesystem I/O, this function always succeeds.
+ * \sa \ref score_close().
  */
-void score_init(struct afs_table *t)
+void score_open(struct osl_table **result)
 {
-       t->name = score_table_desc.name;
-       t->open = score_open;
-       t->close = score_close;
-       t->event_handler = score_event_handler;
+       if (result)
+               assert(osl(osl_open_table(&score_table_desc, result)) >= 0);
+       else
+               open_global_table(NULL);
 }
+
+/**
+ * Remove all entries from the score table, but keep the table open.
+ */
+void score_clear(void)
+{
+       close_global_table();
+       open_global_table(NULL);
+}
+
+/** The score table stores (aft row, score) pairs in memory. */
+const struct afs_table_operations score_ops = {
+       .open = open_global_table,
+       .close = close_global_table,
+};