<commit_before>/*
 * This file is part of Arakoon, a distributed key-value store.
 *
 * Copyright (C) 2010, 2012 Incubaid BVBA
 *
 * Licensees holding a valid Incubaid license may use this file in
 * accordance with Incubaid's Arakoon commercial license agreement. For
 * more information on how to enter into this agreement, please contact
 * Incubaid (contact details can be found on http://www.arakoon.org/licensing).
 *
 * Alternatively, this file may be redistributed and/or modified under
 * the terms of the GNU Affero General Public License version 3, as
 * published by the Free Software Foundation. Under this license, this
 * file is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * See the GNU Affero General Public License for more details.
 * You should have received a copy of the
 * GNU Affero General Public License along with this program (file "COPYING").
 * If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

#include "arakoon.h"
#include "memory.h"
#include "utils.h"

typedef struct Node Node;
struct Node {
        const char *name;
        ArakoonClusterNode *node;
        Node *next;
};

static void client_error_handler(arakoon_rc rc, size_t len, const void *msg) {
        fprintf(stderr, "[CLIENT ERROR] %s: %.*s\n", arakoon_strerror(rc),
                len < INT_MAX ? (int) len : INT_MAX, (const char *) msg);
}

int main(int argc, char **argv) {
        ArakoonCluster *c = NULL;
        arakoon_rc rc = 0;
        ArakoonClientCallOptions *options = NULL;
        ArakoonValueList *r0 = NULL, *r2 = NULL;
        ArakoonValueListIter *iter0 = NULL;
        ArakoonKeyValueList *r1 = NULL;
        ArakoonKeyValueListIter *iter1 = NULL;
        size_t l0 = 0, l1 = 0, l2 = 0;
        void *d0 = NULL;
        const void *v0 = NULL, *v1 = NULL;
        char *s0 = NULL, *s1 = NULL;
        ArakoonSequence *seq = NULL;
        int i = 0;
        uint32_t uint32 = 0;
        int32_t major = 0, minor = 0, patch = 0;
        char *version_info = NULL;

        Node *fst = NULL, *n = NULL, *the_node = NULL;
        const char *name = NULL;

        const char *multi_get_expected_values[] = {
                "bar",
                "testvalue",
                "bar2",
                NULL
        };
        const char **multi_get_expected_value = NULL;

        const ArakoonMemoryHooks hooks = {
                check_arakoon_malloc,
                check_arakoon_free,
                check_arakoon_realloc
        };

        if((argc - 2) % 3 != 0 || argc < 5) {
                fprintf(stderr, "Usage: %s cluster [name host port]*\n",
                        argv[0]);
                return 1;
        }

        printf("Working with crakoon version %u.%u.%u (%s)\n",
                arakoon_library_version_major(),
                arakoon_library_version_minor(),
                arakoon_library_version_micro(),
                arakoon_library_version_info());

        arakoon_memory_set_hooks(&hooks);
        arakoon_log_set_handler(arakoon_log_get_stderr_handler());
        arakoon_log_set_client_error_handler(client_error_handler);

        options = arakoon_client_call_options_new();
        arakoon_client_call_options_set_timeout(options, 400);

        c = arakoon_cluster_new(ARAKOON_PROTOCOL_VERSION_1, argv[1]);
        ABORT_IF_NULL(c, "arakoon_cluster_new");

        for(i = 2; i < argc; i++) {
                the_node = NULL;
                name = argv[i];

                for(n = fst; n != NULL;) {
                        if(strcmp(n->name, name) == 0) {
                                the_node = n;
                                break;
                        }

                        n = n->next;
                }

                if(the_node == NULL) {
                        the_node = (Node *)check_arakoon_malloc(sizeof(Node));
                        ABORT_IF_NULL(the_node, "Allocate Node");

                        the_node->node = arakoon_cluster_node_new(name);
                        ABORT_IF_NULL(the_node->node,
                                "arakoon_cluster_node_new");
                        the_node->name = name;

                        the_node->next = fst;
                        fst = the_node;
                }

                rc = arakoon_cluster_node_add_address_tcp(the_node->node,
                        argv[i + 1], argv[i + 2]);
                ABORT_IF_NOT_SUCCESS(rc,
                        "arakoon_cluster_node_add_address_tcp");

                i += 2;
        }

        for(n = fst; n != NULL;) {
                rc = arakoon_cluster_add_node(c, n->node);
                ABORT_IF_NOT_SUCCESS(rc, "arakoon_cluster_add_node");

                the_node = n;
                n = n->next;
                check_arakoon_free(the_node);
        }

        rc = arakoon_cluster_connect_master(c, options);
        ABORT_IF_NOT_SUCCESS(rc, "arakoon_cluster_connect_master");

        rc = arakoon_get(c, NULL, 3, "foo", &l0, &d0);
        if(rc != ARAKOON_RC_NOT_FOUND) {
                fprintf(stderr, "Unset value found\n");
                abort();
        }

        rc = arakoon_set(c, NULL, 3, "foo", 3, "bar");
        ABORT_IF_NOT_SUCCESS(rc, "arakoon_set");
        rc = arakoon_set(c, options, 4, "foo2", 4, "bar2");
        ABORT_IF_NOT_SUCCESS(rc, "arakoon_set");
        rc = arakoon_set(c, NULL, 7, "testkey", 9, "testvalue");
        ABORT_IF_NOT_SUCCESS(rc, "arakoon_set");

        r0 = arakoon_value_list_new();
        rc = arakoon_value_list_add(r0, 3, "foo");
        ABORT_IF_NOT_SUCCESS(rc, "arakoon_value_list_add");
        rc = arakoon_value_list_add(r0, 7, "testkey");
        ABORT_IF_NOT_SUCCESS(rc, "arakoon_value_list_add");
        rc = arakoon_value_list_add(r0, 4, "foo2");
        ABORT_IF_NOT_SUCCESS(rc, "arakoon_value_list_add");
        rc = arakoon_multi_get(c, options, r0, &r2);
        ABORT_IF_NOT_SUCCESS(rc, "arakoon_multi_get");
        arakoon_value_list_free(r0);

        if(arakoon_value_list_size(r2) != 3) {
                fprintf(stderr, "Unexpected result size: %zu, expected 3\n",
                        arakoon_value_list_size(r2));
                abort();
        }

        iter0 = arakoon_value_list_create_iter(r2);
        ABORT_IF_NULL(iter0, "arakoon_value_list_create_iter");

        multi_get_expected_value = multi_get_expected_values;

        FOR_ARAKOON_VALUE_ITER(iter0, &l0, &v0) {
                s0 = (char *)check_arakoon_malloc((l0 + 1) * sizeof(char));
                ABORT_IF_NULL(s0, "check_arakoon_malloc");
                memcpy(s0, v0, l0);
                s0[l0] = 0;
                printf("Multi-get value: %s\n", s0);

                if(strlen(*multi_get_expected_value) != l0) {
                        fprintf(stderr, "Unexpected result length: %zd\n", l0);
                        abort();
                }

                if(strncmp(s0, *multi_get_expected_value, l0) != 0) {
                        fprintf(stderr, "Unexpected multi_get result: expected \"%s\", got \"%s\"\n",
                                *multi_get_expected_value, s0);
                        abort();
                }

                check_arakoon_free(s0);
                multi_get_expected_value++;
        }<commit_msg>Fix `scan-build` results<commit_after>/*
 * This file is part of Arakoon, a distributed key-value store.
 *
 * Copyright (C) 2010, 2012 Incubaid BVBA
 *
 * Licensees holding a valid Incubaid license may use this file in
 * accordance with Incubaid's Arakoon commercial license agreement. For
 * more information on how to enter into this agreement, please contact
 * Incubaid (contact details can be found on http://www.arakoon.org/licensing).
 *
 * Alternatively, this file may be redistributed and/or modified under
 * the terms of the GNU Affero General Public License version 3, as
 * published by the Free Software Foundation. Under this license, this
 * file is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * See the GNU Affero General Public License for more details.
 * You should have received a copy of the
 * GNU Affero General Public License along with this program (file "COPYING").
 * If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

#include "arakoon.h"
#include "memory.h"
#include "utils.h"

typedef struct Node Node;
struct Node {
        const char *name;
        ArakoonClusterNode *node;
        Node *next;
};

static void client_error_handler(arakoon_rc rc, size_t len, const void *msg) {
        fprintf(stderr, "[CLIENT ERROR] %s: %.*s\n", arakoon_strerror(rc),
                len < INT_MAX ? (int) len : INT_MAX, (const char *) msg);
}

int main(int argc, char **argv) {
        ArakoonCluster *c = NULL;
        arakoon_rc rc = 0;
        ArakoonClientCallOptions *options = NULL;
        ArakoonValueList *r0 = NULL, *r2 = NULL;
        ArakoonValueListIter *iter0 = NULL;
        ArakoonKeyValueList *r1 = NULL;
        ArakoonKeyValueListIter *iter1 = NULL;
        size_t l0 = 0, l1 = 0, l2 = 0;
        void *d0 = NULL;
        const void *v0 = NULL, *v1 = NULL;
        char *s0 = NULL, *s1 = NULL;
        ArakoonSequence *seq = NULL;
        int i = 0;
        uint32_t uint32 = 0;
        int32_t major = 0, minor = 0, patch = 0;
        char *version_info = NULL;

        Node *fst = NULL, *n = NULL, *the_node = NULL;
        const char *name = NULL;

        const char *multi_get_expected_values[] = {
                "bar",
                "testvalue",
                "bar2",
                NULL
        };
        const char **multi_get_expected_value = NULL;

        const ArakoonMemoryHooks hooks = {
                check_arakoon_malloc,
                check_arakoon_free,
                check_arakoon_realloc
        };

        if((argc - 2) % 3 != 0 || argc < 5) {
                fprintf(stderr, "Usage: %s cluster [name host port]*\n",
                        argv[0]);
                return 1;
        }

        printf("Working with crakoon version %u.%u.%u (%s)\n",
                arakoon_library_version_major(),
                arakoon_library_version_minor(),
                arakoon_library_version_micro(),
                arakoon_library_version_info());

        arakoon_memory_set_hooks(&hooks);
        arakoon_log_set_handler(arakoon_log_get_stderr_handler());
        arakoon_log_set_client_error_handler(client_error_handler);

        options = arakoon_client_call_options_new();
        arakoon_client_call_options_set_timeout(options, 400);

        c = arakoon_cluster_new(ARAKOON_PROTOCOL_VERSION_1, argv[1]);
        ABORT_IF_NULL(c, "arakoon_cluster_new");

        for(i = 2; i < argc; i++) {
                the_node = NULL;
                name = argv[i];

                for(n = fst; n != NULL;) {
                        if(strcmp(n->name, name) == 0) {
                                the_node = n;
                                break;
                        }

                        n = n->next;
                }

                if(the_node == NULL) {
                        the_node = (Node *)check_arakoon_malloc(sizeof(Node));
                        ABORT_IF_NULL(the_node, "Allocate Node");

                        the_node->node = arakoon_cluster_node_new(name);
                        ABORT_IF_NULL(the_node->node,
                                "arakoon_cluster_node_new");
                        the_node->name = name;

                        the_node->next = fst;
                        fst = the_node;
                }

                rc = arakoon_cluster_node_add_address_tcp(the_node->node,
                        argv[i + 1], argv[i + 2]);
                ABORT_IF_NOT_SUCCESS(rc,
                        "arakoon_cluster_node_add_address_tcp");

                i += 2;
        }

        for(n = fst; n != NULL;) {
                rc = arakoon_cluster_add_node(c, n->node);
                ABORT_IF_NOT_SUCCESS(rc, "arakoon_cluster_add_node");

                the_node = n;
                n = n->next;
                check_arakoon_free(the_node);
        }

        rc = arakoon_cluster_connect_master(c, options);
        ABORT_IF_NOT_SUCCESS(rc, "arakoon_cluster_connect_master");

        rc = arakoon_get(c, NULL, 3, "foo", &l0, &d0);
        if(rc != ARAKOON_RC_NOT_FOUND) {
                fprintf(stderr, "Unset value found\n");
                abort();
        }

        rc = arakoon_set(c, NULL, 3, "foo", 3, "bar");
        ABORT_IF_NOT_SUCCESS(rc, "arakoon_set");
        rc = arakoon_set(c, options, 4, "foo2", 4, "bar2");
        ABORT_IF_NOT_SUCCESS(rc, "arakoon_set");
        rc = arakoon_set(c, NULL, 7, "testkey", 9, "testvalue");
        ABORT_IF_NOT_SUCCESS(rc, "arakoon_set");

        r0 = arakoon_value_list_new();
        rc = arakoon_value_list_add(r0, 3, "foo");
        ABORT_IF_NOT_SUCCESS(rc, "arakoon_value_list_add");
        rc = arakoon_value_list_add(r0, 7, "testkey");
        ABORT_IF_NOT_SUCCESS(rc, "arakoon_value_list_add");
        rc = arakoon_value_list_add(r0, 4, "foo2");
        ABORT_IF_NOT_SUCCESS(rc, "arakoon_value_list_add");
        rc = arakoon_multi_get(c, options, r0, &r2);
        ABORT_IF_NOT_SUCCESS(rc, "arakoon_multi_get");
        arakoon_value_list_free(r0);

        if(arakoon_value_list_size(r2) != 3) {
                fprintf(stderr, "Unexpected result size: %zu, expected 3\n",
                        arakoon_value_list_size(r2));
                abort();
        }

        iter0 = arakoon_value_list_create_iter(r2);
        ABORT_IF_NULL(iter0, "arakoon_value_list_create_iter");

        multi_get_expected_value = multi_get_expected_values;

        FOR_ARAKOON_VALUE_ITER(iter0, &l0, &v0) {
                s0 = (char *)check_arakoon_malloc((l0 + 1) * sizeof(char));
                ABORT_IF_NULL(s0, "check_arakoon_malloc");
                memcpy(s0, v0, l0);
                s0[l0] = 0;
                printf("Multi-get value: %s\n", s0);

                if(*multi_get_expected_value == NULL || strlen(*multi_get_expected_value) != l0) {
                        fprintf(stderr, "Unexpected result length: %zd\n", l0);
                        abort();
                }

                if(strncmp(s0, *multi_get_expected_value, l0) != 0) {
                        fprintf(stderr, "Unexpected multi_get result: expected \"%s\", got \"%s\"\n",
                                *multi_get_expected_value, s0);
                        abort();
                }

                check_arakoon_free(s0);
                multi_get_expected_value++;
        }
{'fraction_non_alphanumeric': 0.10561676968556839, 'fraction_numerical': 0.013974737973662993, 'mean_word_length': 1.883572258814413, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5080992', 'n_tokens_mistral': 4923, 'n_tokens_neox': 4564, 'n_words': 1064}