forked from Netflix/dynomite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdyn_ring_queue.c
152 lines (113 loc) · 3.2 KB
/
dyn_ring_queue.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* dyn_ring_queue.c
*
* Created on: May 31, 2014
* Author: mdo
*/
#include "dyn_ring_queue.h"
#include "dyn_array.h"
#include "dyn_core.h"
#include "dyn_gossip.h"
#include "dyn_token.h"
// should use pooling to store struct ring_message so that we can reuse
struct ring_msg *create_ring_msg(void) {
struct ring_msg *msg = dn_alloc(sizeof(*msg));
if (msg == NULL) return NULL;
ring_msg_init(msg, 1, true);
msg->data = NULL;
return msg;
}
struct ring_msg *create_ring_msg_with_data(uint32_t capacity) {
struct ring_msg *msg = dn_alloc(sizeof(*msg));
if (msg == NULL) return NULL;
rstatus_t status = ring_msg_init(msg, 1, true);
if (status != DN_OK) {
dn_free(msg);
return NULL;
}
msg->data = dn_zalloc(sizeof(uint8_t) * capacity);
msg->capacity = capacity;
msg->len = 0;
return msg;
}
struct ring_msg *create_ring_msg_with_size(uint32_t size, bool init_node) {
struct ring_msg *msg = dn_alloc(sizeof(*msg));
if (msg == NULL) return NULL;
rstatus_t status = ring_msg_init(msg, size, init_node);
if (status != DN_OK) {
dn_free(msg);
return NULL;
}
msg->data = NULL;
msg->capacity = 0;
msg->len = 0;
return msg;
}
rstatus_t ring_msg_init(struct ring_msg *msg, uint32_t n, bool init_node) {
if (msg == NULL) return DN_ERROR;
rstatus_t status = array_init(&msg->nodes, n, sizeof(struct gossip_node));
if (status != DN_OK) return status;
if (init_node) {
uint32_t i;
for (i = 0; i < n; i++) {
struct gossip_node *node = array_push(&msg->nodes);
node_init(node);
}
}
return DN_OK;
}
rstatus_t ring_msg_deinit(struct ring_msg *msg) {
if (msg == NULL) return DN_ERROR;
uint32_t i;
for (i = 0; i < array_n(&msg->nodes); i++) {
struct gossip_node *node = array_get(&msg->nodes, i);
node_deinit(node);
}
array_deinit(&msg->nodes);
if (msg->data != NULL) {
dn_free(msg->data);
}
dn_free(msg);
return DN_OK;
}
struct gossip_node *create_node() {
struct gossip_node *result = dn_alloc(sizeof(*result));
node_init(result);
return result;
}
rstatus_t node_init(struct gossip_node *node) {
if (node == NULL) return DN_ERROR;
init_dyn_token(&node->token);
string_init(&node->dc);
string_init(&node->rack);
string_init(&node->name);
string_init(&node->pname);
node->port = 8101;
node->is_local = false;
node->state = INIT;
return DN_OK;
}
rstatus_t node_deinit(struct gossip_node *node) {
if (node == NULL) return DN_ERROR;
// array_deinit(&node->tokens);
string_deinit(&node->dc);
string_deinit(&node->rack);
string_deinit(&node->name);
string_deinit(&node->pname);
deinit_dyn_token(&node->token);
// dn_free(node);
return DN_OK;
}
rstatus_t node_copy(const struct gossip_node *src, struct gossip_node *dst) {
if (src == NULL || dst == NULL) return DN_ERROR;
dst->state = src->state;
dst->is_local = src->is_local;
dst->port = src->port;
dst->is_secure = src->is_secure;
string_copy(&dst->pname, src->pname.data, src->pname.len);
string_copy(&dst->name, src->name.data, src->name.len);
string_copy(&dst->rack, src->rack.data, src->rack.len);
string_copy(&dst->dc, src->dc.data, src->dc.len);
copy_dyn_token(&src->token, &dst->token);
return DN_OK;
}