~ne02ptzero/libfloat

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
1

[PATCH] log: Add helpers for human readable consistency types

Louis Solofrizzo
Details
Message ID
<20210920122316.786507-1-lsolofrizzo@scaleway.com>
DKIM signature
pass
Download raw message
Patch: +62 -0
Signed-off-by: Louis Solofrizzo <lsolofrizzo@scaleway.com>
---
 libfloat.c | 43 +++++++++++++++++++++++++++++++++++++++++++
 libfloat.h | 18 ++++++++++++++++++
 log.h      |  1 +
 3 files changed, 62 insertions(+)

diff --git a/libfloat.c b/libfloat.c
index 6763c5e..63eacde 100644
--- a/libfloat.c
+++ b/libfloat.c
@@ -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;
}
diff --git a/libfloat.h b/libfloat.h
index 4eaadc7..9319876 100644
--- a/libfloat.h
+++ b/libfloat.h
@@ -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 */
diff --git a/log.h b/log.h
index ad2a148..1ff2b66 100644
--- a/log.h
+++ b/log.h
@@ -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 {
-- 
2.33.0
Details
Message ID
<20210920124940.gfzbxqlzozzd6voy@scaleway>
In-Reply-To
<20210920122316.786507-1-lsolofrizzo@scaleway.com> (view parent)
DKIM signature
pass
Download raw message
LGTM

---
Patrik Cyvoct
Reply to thread Export thread (mbox)