rsstats 0.0.1
Redis Enterprise Statistic collector
clusterlst.c
Go to the documentation of this file.
1
21#ifdef HAVE_CONFIG_H
22#include "config.h"
23#endif
24
25#include "clusterlst.h"
26
27#include <stdlib.h>
28#include <stdio.h>
29#include <string.h>
30
31typedef struct clusterrecord_s {
35
36static clusterrecord_t* clusterlistfirst;
37static clusterrecord_t* clusterlistlast;
38static clusterrecord_t* clusterlistcursor;
39
40static void clusterlist_add_postinit (cluster_t* cluster);
41static cluster_t* clusterlist_find_postinit (const char* host);
42static cluster_t* clusterlist_first_postinit();
43static cluster_t* clusterlist_next_postinit();
44static cluster_t* clusterlist_get_postinit();
45
46static void clusterlist_init() {
47 if (NULL==(clusterlistfirst=malloc(sizeof(struct clusterrecord_s)))) {
48 perror("clusterlist_init");
49 exit(EXIT_FAILURE);
50 }
51 /* Initialize chained structure */
52 clusterlistlast = clusterlistfirst;
53 clusterlistcursor = clusterlistfirst;
54 clusterlistlast->next=NULL;
55 clusterlistlast->cluster=NULL;
56
57 /* From now, the application can use the real functions */
58 clusterlist_add = clusterlist_add_postinit;
59 clusterlist_find = clusterlist_find_postinit;
60 clusterlist_first = clusterlist_first_postinit;
61 clusterlist_next = clusterlist_next_postinit;
62 clusterlist_get = clusterlist_get_postinit;
63}
64
65static void clusterlist_add_postinit (cluster_t* cluster) {
66 /* Store the record in the sentinel */
67 clusterlistlast->cluster=cluster;
68
69 /* Create a new sentinel */
70 if (NULL==(clusterlistlast->next=malloc(sizeof(struct clusterrecord_s)))) {
71 perror("clusterlist_add_postinit malloc(sentinel)");
72 exit(EXIT_FAILURE);
73 }
74 clusterlistlast = clusterlistlast->next;
75 clusterlistlast->next=NULL;
76 clusterlistlast->cluster=NULL;
77}
78static void clusterlist_add_preinit (cluster_t* cluster) {
79 clusterlist_init();
81}
82void (*clusterlist_add)(cluster_t* cluster) = clusterlist_add_preinit;
83
84
85static cluster_t* clusterlist_find_postinit(const char* host) {
86 clusterlistcursor = clusterlistfirst;
87 while (clusterlistcursor->cluster&&
88 (
89 ((NULL!=host)&&(NULL==clusterlistcursor->cluster->host))||
90 ((NULL==host)&&(NULL!=clusterlistcursor->cluster->host))||
91 ((NULL!=host)&&(NULL!=clusterlistcursor->cluster->host)&&
92 (strcmp(host,clusterlistcursor->cluster->host)))
93 ))
94 clusterlistcursor = clusterlistcursor->next;
95 return (clusterlistcursor->cluster);
96}
97static cluster_t* clusterlist_find_preinit(const char* host) {
98 clusterlist_init();
99 return clusterlist_find(host);
100}
101cluster_t* (*clusterlist_find)(const char* host) = clusterlist_find_preinit;
102
103
104static cluster_t* clusterlist_first_postinit() {
105 clusterlistcursor = clusterlistfirst;
106 return (clusterlistcursor->cluster);
107}
108static cluster_t* clusterlist_first_preinit() {
109 clusterlist_init();
110 return clusterlist_first();
111}
112cluster_t* (*clusterlist_first)() = clusterlist_first_preinit;
113
114
115static cluster_t* clusterlist_next_postinit() {
116 if (clusterlistcursor->next)
117 clusterlistcursor = clusterlistcursor->next;
118 return (clusterlistcursor->cluster);
119}
120static cluster_t* clusterlist_next_preinit() {
121 clusterlist_init();
122 return clusterlist_next();
123}
124cluster_t* (*clusterlist_next)() = clusterlist_next_preinit;
125
126
127static cluster_t* clusterlist_get_postinit() {
128 return (clusterlistcursor->cluster);
129}
130static cluster_t* clusterlist_get_preinit() {
131 clusterlist_init();
132 return clusterlist_get();
133}
134cluster_t* (*clusterlist_get)() = clusterlist_get_preinit;
135
136/* vim: set tw=80: */
cluster_t *(* clusterlist_first)()
Definition: clusterlst.c:112
cluster_t *(* clusterlist_get)()
Definition: clusterlst.c:134
void(* clusterlist_add)(cluster_t *cluster)
Definition: clusterlst.c:82
struct clusterrecord_s clusterrecord_t
cluster_t *(* clusterlist_next)()
Definition: clusterlst.c:124
cluster_t *(* clusterlist_find)(const char *host)
Definition: clusterlst.c:101
Basic(non-thread-safe) single-chained list of record with sentinal.
#define NULL
Definition: rsstats-opts.c:64
char * host
Definition: cluster.h:26
struct clusterrecord_s * next
Definition: clusterlst.c:32
cluster_t * cluster
Definition: clusterlst.c:33