~ne02ptzero/libfloat

c93ce23cec30a46ea6bd54d341f22cb6d582bbd9 — Louis Solofrizzo a month ago cb70314
log: Add helpers for human readable consistency types

Patch: https://lists.sr.ht/~ne02ptzero/libfloat/patches/25297

Signed-off-by : Louis Solofrizzo <lsolofrizzo@scaleway.com>
Acked-by      : Patrik Cyvot <patrik@ptrk.io>
Acked-by      : Michael Bonfils <bonfils.michael@protonmail.com>
3 files changed, 62 insertions(+), 0 deletions(-)

M libfloat.c
M libfloat.h
M log.h
M libfloat.c => libfloat.c +43 -0
@@ 62,3 62,46 @@ void libfloat_ctx_del(libfloat_ctx_t *ctx)
    kh_destroy(libfloat_node_id_t, ctx->nodes);
    ctx->free(ctx);
}

static const char *types[] = {
    [LIBFLOAT_HOPEFULLY_CONSISTENT] = "hopeful",
    [LIBFLOAT_EVENTUALLY_CONSISTENT] = "eventual",
    [LIBFLOAT_STRONGLY_CONSISTENT] = "strong",
    [LIBFLOAT_ABSOLUTELY_CONSISTENT] = "absolute",
};
_Static_assert(sizeof(types) / sizeof(types[0]) == LIBFLOAT_COMMIT_TYPE_LAST, "Missing a commit type entry");

const char *libfloat_commit_type_to_str(libfloat_commit_type_t t)
{
    return types[t];
}

static int libfloat_strcmp(const char *s1, const char *s2)
{
    size_t      i;

    for (i = 0; s1[i] != '\0' && s2[i] != '\0' && s1[i] == s2[i]; i++)
        ;

    if (s1[i] == '\0' && s2[i] == '\0')
        return 0;

    return s1[i] - s2[i];
}

bool libfloat_str_to_commit_type(const char *t, libfloat_commit_type_t *out)
{
    if (t == NULL || out == NULL)
        return false;

    for (size_t i = 0; i < sizeof(types) / sizeof(types[0]); i++)
    {
        if (libfloat_strcmp(t, types[i]) == 0)
        {
            *out = i;
            return true;
        }
    }

    return false;
}

M libfloat.h => libfloat.h +18 -0
@@ 496,4 496,22 @@ const char *libfloat_get_error_str(void);
 */
bool libfloat_get_last_term(libfloat_ctx_t *ctx, libfloat_entry_id_t *id, libfloat_term_t *term);

/*!
 * \brief Transform a commit-type to a human readable string
 *
 * \param[in] t Commit type
 * \return A string constant of the type
 */
const char *libfloat_commit_type_to_str(libfloat_commit_type_t t);

/*!
 * \brief Transform a string into a libfloat commit type
 *
 * \param[in] t String to read
 * \param[out] out Output type
 *
 * \return true on success, false on failure
 */
bool libfloat_str_to_commit_type(const char *t, libfloat_commit_type_t *out);

#endif /* LIBFLOAT_H */

M log.h => log.h +1 -0
@@ 9,6 9,7 @@ typedef enum {
    LIBFLOAT_EVENTUALLY_CONSISTENT,     /*!< Callback when leader as written log and queued replication */
    LIBFLOAT_STRONGLY_CONSISTENT,       /*!< Callback when a majority of nodes have replicated the log */
    LIBFLOAT_ABSOLUTELY_CONSISTENT,      /*!< Callback when all nodes have replicated the log */
    LIBFLOAT_COMMIT_TYPE_LAST,
} libfloat_commit_type_t;

typedef enum {