| 1 | /* |
| 2 | * node.h |
| 3 | * |
| 4 | * Copyright (C) 2017 Aerospike, Inc. |
| 5 | * |
| 6 | * Portions may be licensed to Aerospike, Inc. under one or more contributor |
| 7 | * license agreements. |
| 8 | * |
| 9 | * This program is free software: you can redistribute it and/or modify it under |
| 10 | * the terms of the GNU Affero General Public License as published by the Free |
| 11 | * Software Foundation, either version 3 of the License, or (at your option) any |
| 12 | * later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 15 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 16 | * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more |
| 17 | * details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see http://www.gnu.org/licenses/ |
| 21 | */ |
| 22 | |
| 23 | #pragma once |
| 24 | |
| 25 | #include <stdbool.h> |
| 26 | #include <stdint.h> |
| 27 | |
| 28 | #include "compare.h" |
| 29 | |
| 30 | typedef uint64_t cf_node; |
| 31 | |
| 32 | uint32_t cf_nodeid_shash_fn(const void *key); |
| 33 | uint32_t cf_nodeid_rchash_fn(const void *key, uint32_t key_size); |
| 34 | char *cf_node_name(); |
| 35 | |
| 36 | static inline int |
| 37 | index_of_node(const cf_node* nodes, uint32_t n_nodes, cf_node node) |
| 38 | { |
| 39 | for (uint32_t n = 0; n < n_nodes; n++) { |
| 40 | if (node == nodes[n]) { |
| 41 | return (int)n; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | return -1; |
| 46 | } |
| 47 | |
| 48 | static inline bool |
| 49 | contains_node(const cf_node* nodes, uint32_t n_nodes, cf_node node) |
| 50 | { |
| 51 | return index_of_node(nodes, n_nodes, node) != -1; |
| 52 | } |
| 53 | |
| 54 | static inline uint32_t |
| 55 | remove_node(cf_node* nodes, uint32_t n_nodes, cf_node node) |
| 56 | { |
| 57 | int n = index_of_node(nodes, n_nodes, node); |
| 58 | |
| 59 | if (n != -1) { |
| 60 | nodes[n] = nodes[--n_nodes]; |
| 61 | } |
| 62 | |
| 63 | return n_nodes; |
| 64 | } |
| 65 | |
| 66 | static inline int |
| 67 | cf_node_compare_desc(const void* pa, const void* pb) |
| 68 | { |
| 69 | // Relies on cf_node being uint64_t. |
| 70 | return cf_compare_uint64_desc(pa, pb); |
| 71 | } |
| 72 | |