summaryrefslogtreecommitdiffstats
path: root/libglusterfs/src/dict.h
blob: 8e81e539b74b909fb5a3fadd9f52fafb798d8114 (plain)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
   Copyright (c) 2006-2011 Gluster, Inc. <http://www.gluster.com>
   This file is part of GlusterFS.

   GlusterFS is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published
   by the Free Software Foundation; either version 3 of the License,
   or (at your option) any later version.

   GlusterFS 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
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see
   <http://www.gnu.org/licenses/>.
*/

#ifndef _DICT_H
#define _DICT_H

#ifndef _CONFIG_H
#define _CONFIG_H
#include "config.h"
#endif

#include <inttypes.h>
#include <sys/uio.h>
#include <pthread.h>

#include "common-utils.h"

typedef struct _data data_t;
typedef struct _dict dict_t;
typedef struct _data_pair data_pair_t;


#define GF_PROTOCOL_DICT_SERIALIZE(this,from_dict,to,len,ope,labl) do { \
                int    ret     = 0;                                     \
                size_t dictlen = 0;                                     \
                                                                        \
                if (!from_dict)                                         \
                        break;                                          \
                                                                        \
                ret = dict_allocate_and_serialize (from_dict, to,       \
                                                   &dictlen);           \
                if (ret < 0) {                                          \
                        gf_log (this->name, GF_LOG_WARNING,             \
                                "failed to get serialized dict (%s)",   \
                                (#from_dict));                          \
                        ope = EINVAL;                                   \
                        goto labl;                                      \
                }                                                       \
                len = dictlen;                                          \
        } while (0)


#define GF_PROTOCOL_DICT_UNSERIALIZE(xl,to,buff,len,ret,ope,labl) do {  \
                char *buf = NULL;                                       \
                if (!len)                                               \
                        break;                                          \
                to = dict_new();                                        \
                GF_VALIDATE_OR_GOTO (xl->name, to, labl);               \
                                                                        \
                buf = memdup (buff, len);                               \
                GF_VALIDATE_OR_GOTO (xl->name, buf, labl);              \
                                                                        \
                ret = dict_unserialize (buf, len, &to);                 \
                if (ret < 0) {                                          \
                        gf_log (xl->name, GF_LOG_WARNING,               \
                                "failed to unserialize dictionary (%s)", \
                                (#to));                                 \
                                                                        \
                        ope = EINVAL;                                   \
                        GF_FREE (buf);                                  \
                        goto labl;                                      \
                }                                                       \
                                                                        \
                to->extra_free = buf;                                   \
        } while (0)

struct _data {
        unsigned char  is_static:1;
        unsigned char  is_const:1;
        unsigned char  is_stdalloc:1;
        int32_t        len;
        struct iovec  *vec;
        char          *data;
        int32_t        refcount;
        gf_lock_t      lock;
};

struct _data_pair {
        struct _data_pair *hash_next;
        struct _data_pair *prev;
        struct _data_pair *next;
        data_t            *value;
        char              *key;
};

struct _dict {
        unsigned char   is_static:1;
        int32_t         hash_size;
        int32_t         count;
        int32_t         refcount;
        data_pair_t   **members;
        data_pair_t    *members_list;
        char           *extra_free;
        char           *extra_stdfree;
        gf_lock_t       lock;
};


int32_t is_data_equal (data_t *one, data_t *two);
void data_destroy (data_t *data);

int32_t dict_set (dict_t *this, char *key, data_t *value);
data_t *dict_get (dict_t *this, char *key);
void dict_del (dict_t *this, char *key);
int dict_reset (dict_t *dict);

int32_t dict_serialized_length (dict_t *dict);
int32_t dict_serialize (dict_t *dict, char *buf);
int32_t dict_unserialize (char *buf, int32_t size, dict_t **fill);

int32_t dict_allocate_and_serialize (dict_t *this, char **buf, size_t *length);

int32_t dict_iovec_len (dict_t *dict);
int32_t dict_to_iovec (dict_t *dict, struct iovec *vec, int32_t count);

void dict_destroy (dict_t *dict);
void dict_unref (dict_t *dict);
dict_t *dict_ref (dict_t *dict);
data_t *data_ref (data_t *data);
void data_unref (data_t *data);

int32_t dict_lookup  (dict_t *this, char *key, data_pair_t **data);
/*
   TODO: provide converts for differnt byte sizes, signedness, and void *
 */
data_t *int_to_data (int64_t value);
data_t *str_to_data (char *value);
data_t *data_from_dynstr (char *value);
data_t *data_from_dynptr (void *value, int32_t len);
data_t *bin_to_data (void *value, int32_t len);
data_t *static_str_to_data (char *value);
data_t *static_bin_to_data (void *value);

int64_t data_to_int64 (data_t *data);
int32_t data_to_int32 (data_t *data);
int16_t data_to_int16 (data_t *data);
int8_t data_to_int8 (data_t *data);

uint64_t data_to_uint64 (data_t *data);
uint32_t data_to_uint32 (data_t *data);
uint16_t data_to_uint16 (data_t *data);
uint8_t data_to_uint8 (data_t *data);

data_t *data_from_ptr (void *value);
data_t *data_from_static_ptr (void *value);

data_t *data_from_int64 (int64_t value);
data_t *data_from_int32 (int32_t value);
data_t *data_from_int16 (int16_t value);
data_t *data_from_int8 (int8_t value);

data_t *data_from_uint64 (uint64_t value);
data_t *data_from_uint32 (uint32_t value);
data_t *data_from_uint16 (uint16_t value);

char *data_to_str (data_t *data);
void *data_to_bin (data_t *data);
void *data_to_ptr (data_t *data);

data_t *get_new_data ();
data_t * data_copy (data_t *old);
dict_t *get_new_dict_full (int size_hint);
dict_t *get_new_dict ();

data_pair_t *get_new_data_pair ();

void dict_foreach (dict_t *this,
		   void (*fn)(dict_t *this,
			      char *key,
			      data_t *value,
			      void *data),
		   void *data);

dict_t *dict_copy (dict_t *this, dict_t *new);

/* CLEANED UP FUNCTIONS DECLARATIONS */
GF_MUST_CHECK dict_t *dict_new (void);
dict_t *dict_copy_with_ref (dict_t *this, dict_t *new);

GF_MUST_CHECK int dict_reset (dict_t *dict);

GF_MUST_CHECK int dict_get_int8 (dict_t *this, char *key, int8_t *val);
GF_MUST_CHECK int dict_set_int8 (dict_t *this, char *key, int8_t val);

GF_MUST_CHECK int dict_get_int16 (dict_t *this, char *key, int16_t *val);
GF_MUST_CHECK int dict_set_int16 (dict_t *this, char *key, int16_t val);

GF_MUST_CHECK int dict_get_int32 (dict_t *this, char *key, int32_t *val);
GF_MUST_CHECK int dict_set_int32 (dict_t *this, char *key, int32_t val);

GF_MUST_CHECK int dict_get_int64 (dict_t *this, char *key, int64_t *val);
GF_MUST_CHECK int dict_set_int64 (dict_t *this, char *key, int64_t val);

GF_MUST_CHECK int dict_get_uint16 (dict_t *this, char *key, uint16_t *val);
GF_MUST_CHECK int dict_set_uint16 (dict_t *this, char *key, uint16_t val);

GF_MUST_CHECK int dict_get_uint32 (dict_t *this, char *key, uint32_t *val);
GF_MUST_CHECK int dict_set_uint32 (dict_t *this, char *key, uint32_t val);

GF_MUST_CHECK int dict_get_uint64 (dict_t *this, char *key, uint64_t *val);
GF_MUST_CHECK int dict_set_uint64 (dict_t *this, char *key, uint64_t val);

GF_MUST_CHECK int dict_get_double (dict_t *this, char *key, double *val);
GF_MUST_CHECK int dict_set_double (dict_t *this, char *key, double val);

GF_MUST_CHECK int dict_set_static_ptr (dict_t *this, char *key, void *ptr);
GF_MUST_CHECK int dict_get_ptr (dict_t *this, char *key, void **ptr);
GF_MUST_CHECK int dict_get_ptr_and_len (dict_t *this, char *key, void **ptr, int *len);
GF_MUST_CHECK int dict_set_ptr (dict_t *this, char *key, void *ptr);
GF_MUST_CHECK int dict_set_dynptr (dict_t *this, char *key, void *ptr, size_t size);

GF_MUST_CHECK int dict_get_bin (dict_t *this, char *key, void **ptr);
GF_MUST_CHECK int dict_set_bin (dict_t *this, char *key, void *ptr, size_t size);
GF_MUST_CHECK int dict_set_static_bin (dict_t *this, char *key, void *ptr, size_t size);

GF_MUST_CHECK int dict_set_str (dict_t *this, char *key, char *str);
GF_MUST_CHECK int dict_set_dynmstr (dict_t *this, char *key, char *str);
GF_MUST_CHECK int dict_set_dynstr (dict_t *this, char *key, char *str);
GF_MUST_CHECK int dict_get_str (dict_t *this, char *key, char **str);

GF_MUST_CHECK int dict_get_str_boolean (dict_t *this, char *key, int default_val);
GF_MUST_CHECK int dict_serialize_value_with_delim (dict_t *this, char *buf, int32_t *serz_len,
                                                    char delimiter);
#endif