14
14
#define MUR_SEED 13
15
15
#include "third_party/msgpuck.h"
16
16
17
- int mh_indexcmp_eq (
18
- const struct schema_index_value * * lval ,
19
- const struct schema_index_value * * rval ,
20
- void * arg
21
- ) {
22
- (void )arg ;
23
- if ((* lval )-> key .id_len != (* rval )-> key .id_len )
24
- return 0 ;
25
- return !memcmp ((* lval )-> key .id , (* rval )-> key .id , (* rval )-> key .id_len );
26
- }
17
+ #define MH_DEFINE_CMPFUNC (NAME , TYPE ) \
18
+ int mh_##NAME##cmp_eq(const TYPE **lval, const TYPE **rval, \
19
+ void *arg) { \
20
+ (void *) arg; \
21
+ if ((*lval)->key.id_len != (*rval)->key.id_len) \
22
+ return 0; \
23
+ return !memcmp((*lval)->key.id, (*rval)->key.id, \
24
+ (*rval)->key.id_len); \
25
+ } \
26
+ \
27
+ int mh_##NAME##cmp_key_eq(const struct schema_key *key, \
28
+ const TYPE **val, void *arg) { \
29
+ (void *) arg; \
30
+ if (key->id_len != (*val)->key.id_len) \
31
+ return 0; \
32
+ return !memcmp(key->id, (*val)->key.id, key->id_len); \
33
+ }
27
34
28
- int mh_indexcmp_key_eq (
29
- const struct schema_key * key ,
30
- const struct schema_index_value * * val ,
31
- void * arg
32
- ) {
33
- (void )arg ;
34
- if (key -> id_len != (* val )-> key .id_len )
35
- return 0 ;
36
- return !memcmp (key -> id , (* val )-> key .id , key -> id_len );
37
- }
35
+ MH_DEFINE_CMPFUNC (field , struct schema_field_value );
36
+ MH_DEFINE_CMPFUNC (index , struct schema_index_value );
37
+ MH_DEFINE_CMPFUNC (space , struct schema_space_value );
38
+ #undef MH_DEFINE_CMPFUNC
39
+
40
+ #define mh_arg_t void *
41
+
42
+ #define mh_eq (a , b , arg ) mh_fieldcmp_eq(a, b, arg)
43
+ #define mh_eq_key (a , b , arg ) mh_fieldcmp_key_eq(a, b, arg)
44
+ #define mh_hash (x , arg ) PMurHash32(MUR_SEED, (*x)->key.id, \
45
+ (*x)->key.id_len)
46
+ #define mh_hash_key (x , arg ) PMurHash32(MUR_SEED, (x)->id, (x)->id_len);
47
+
48
+ #define mh_node_t struct schema_field_value *
49
+ #define mh_key_t struct schema_key *
50
+
51
+ #define MH_CALLOC (x , y ) pecalloc((x), (y), 1)
52
+ #define MH_FREE (x ) pefree((x), 1)
53
+
54
+ #define mh_name _schema_field
55
+ #define MH_SOURCE 1
56
+ #include "third_party/mhash.h"
38
57
39
58
#define mh_arg_t void *
40
59
41
60
#define mh_eq (a , b , arg ) mh_indexcmp_eq(a, b, arg)
42
61
#define mh_eq_key (a , b , arg ) mh_indexcmp_key_eq(a, b, arg)
43
- #define mh_hash (x , arg ) PMurHash32(MUR_SEED, (*x)->key.id, (*x)->key.id_len)
62
+ #define mh_hash (x , arg ) PMurHash32(MUR_SEED, (*x)->key.id, \
63
+ (*x)->key.id_len)
44
64
#define mh_hash_key (x , arg ) PMurHash32(MUR_SEED, (x)->id, (x)->id_len);
45
65
46
66
#define mh_node_t struct schema_index_value *
@@ -79,7 +99,7 @@ schema_index_free(struct mh_schema_index_t *schema) {
79
99
do {
80
100
struct schema_key key_number = {
81
101
(void * )& (ivalue -> index_number ),
82
- sizeof (uint32_t ), 0
102
+ sizeof (ivalue -> index_number ), 0
83
103
};
84
104
index_slot = mh_schema_index_find (schema , & key_number ,
85
105
NULL );
@@ -106,33 +126,12 @@ schema_index_free(struct mh_schema_index_t *schema) {
106
126
}
107
127
}
108
128
109
- int
110
- mh_spacecmp_eq (
111
- const struct schema_space_value * * lval ,
112
- const struct schema_space_value * * rval ,
113
- void * arg ) {
114
- (void )arg ;
115
- if ((* lval )-> key .id_len != (* rval )-> key .id_len )
116
- return 0 ;
117
- return !memcmp ((* lval )-> key .id , (* rval )-> key .id , (* rval )-> key .id_len );
118
- }
119
-
120
- int
121
- mh_spacecmp_key_eq (
122
- const struct schema_key * key ,
123
- const struct schema_space_value * * val ,
124
- void * arg ) {
125
- (void )arg ;
126
- if (key -> id_len != (* val )-> key .id_len )
127
- return 0 ;
128
- return !memcmp (key -> id , (* val )-> key .id , key -> id_len );
129
- }
130
-
131
129
#define mh_arg_t void *
132
130
133
131
#define mh_eq (a , b , arg ) mh_spacecmp_eq(a, b, arg)
134
132
#define mh_eq_key (a , b , arg ) mh_spacecmp_key_eq(a, b, arg)
135
- #define mh_hash (x , arg ) PMurHash32(MUR_SEED, (*x)->key.id, (*x)->key.id_len)
133
+ #define mh_hash (x , arg ) PMurHash32(MUR_SEED, (*x)->key.id, \
134
+ (*x)->key.id_len)
136
135
#define mh_hash_key (x , arg ) PMurHash32(MUR_SEED, x->id, x->id_len);
137
136
138
137
#define mh_node_t struct schema_space_value *
@@ -158,6 +157,9 @@ schema_space_value_free(const struct schema_space_value *val) {
158
157
schema_index_free (val -> index_hash );
159
158
mh_schema_index_delete (val -> index_hash );
160
159
}
160
+ if (val -> schema_hash ) {
161
+ mh_schema_field_delete (val -> schema_hash );
162
+ }
161
163
}
162
164
}
163
165
@@ -166,7 +168,8 @@ schema_space_free(struct mh_schema_space_t *schema) {
166
168
int pos = 0 ;
167
169
mh_int_t space_slot = 0 ;
168
170
mh_foreach (schema , pos ) {
169
- const struct schema_space_value * svalue , * sv1 = NULL , * sv2 = NULL ;
171
+ const struct schema_space_value * svalue ;
172
+ const struct schema_space_value * sv1 = NULL , * sv2 = NULL ;
170
173
svalue = * mh_schema_space_node (schema , pos );
171
174
do {
172
175
struct schema_key key_number = {
@@ -254,7 +257,8 @@ parse_schema_space_value(struct schema_space_value *space_string,
254
257
goto error ;
255
258
int i = 0 ;
256
259
for (i = 0 ; i < fmt_len ; ++ i ) {
257
- struct schema_field_value * val = & (space_string -> schema_list [i ]);
260
+ struct schema_field_value * val =
261
+ & (space_string -> schema_list [i ]);
258
262
if (mp_typeof (* * tuple ) != MP_MAP )
259
263
goto error ;
260
264
uint32_t arrsz = mp_decode_map (tuple );
@@ -263,6 +267,11 @@ parse_schema_space_value(struct schema_space_value *space_string,
263
267
goto error ;
264
268
}
265
269
val -> field_number = i ;
270
+ val -> key .id = val -> field_name ;
271
+ val -> key .id_len = val -> field_name_len ;
272
+ mh_schema_field_put (space_string -> schema_hash ,
273
+ (const struct schema_field_value * * )& val ,
274
+ NULL , NULL );
266
275
}
267
276
return 0 ;
268
277
error :
@@ -289,7 +298,8 @@ parse_schema_index_value(struct schema_index_value *index_string,
289
298
goto error ;
290
299
if (mp_decode_array (tuple ) != 2 )
291
300
goto error ;
292
- struct schema_field_value * val = & (index_string -> index_parts [i ]);
301
+ struct schema_field_value * val =
302
+ & (index_string -> index_parts [i ]);
293
303
if (mp_typeof (* * tuple ) != MP_UINT )
294
304
goto error ;
295
305
val -> field_number = mp_decode_uint (tuple );
@@ -367,6 +377,7 @@ schema_add_space(
367
377
* }
368
378
*/
369
379
case 6 :
380
+ space_string -> schema_hash = mh_schema_field_new ();
370
381
if (parse_schema_space_value (space_string , & tuple ) < 0 )
371
382
goto error ;
372
383
break ;
@@ -590,27 +601,26 @@ tarantool_schema_get_fid_by_string(
590
601
struct tarantool_schema * schema_obj , uint32_t sid ,
591
602
const char * field_name , uint32_t field_name_len
592
603
) {
593
- struct mh_schema_space_t * schema = schema_obj -> space_hash ;
594
- struct schema_key space_key = {
595
- (void * )& sid ,
596
- sizeof (uint32_t ), 0
597
- };
598
- mh_int_t space_slot = mh_schema_space_find (schema , & space_key , NULL );
599
- if (space_slot == mh_end (schema ))
604
+ struct mh_schema_space_t * sschema = schema_obj -> space_hash ;
605
+ struct schema_key space_key = { (void * )& sid , sizeof (uint32_t ), 0 };
606
+ mh_int_t space_slot = mh_schema_space_find (sschema , & space_key , NULL );
607
+ if (space_slot == mh_end (sschema ))
600
608
return -1 ;
601
- const struct schema_space_value * space = * mh_schema_space_node (schema ,
609
+ const struct schema_space_value * space = * mh_schema_space_node (sschema ,
602
610
space_slot );
603
- int i = 0 ;
604
- for (i = 0 ; i < space -> schema_list_len ; ++ i ) {
605
- struct schema_field_value * val = & space -> schema_list [i ];
606
- if (strncmp (val -> field_name , field_name , field_name_len ) == 0 )
607
- return val -> field_number ;
608
- }
609
- return -1 ;
611
+ struct schema_key field_key = { field_name , field_name_len , 0 };
612
+ struct mh_schema_field_t * fschema = space -> schema_hash ;
613
+ mh_int_t field_slot = mh_schema_field_find (fschema , & field_key , NULL );
614
+ if (field_slot == mh_end (fschema ))
615
+ return -1 ;
616
+ const struct schema_field_value * field = * mh_schema_field_node (fschema ,
617
+ field_slot );
618
+ return field -> field_number ;
610
619
}
611
620
612
621
struct tarantool_schema * tarantool_schema_new (int is_persistent ) {
613
- struct tarantool_schema * obj = pemalloc (sizeof (struct tarantool_schema * ), 1 );
622
+ struct tarantool_schema * obj = NULL ;
623
+ obj = pemalloc (sizeof (struct tarantool_schema * ), 1 );
614
624
obj -> space_hash = mh_schema_space_new ();
615
625
return obj ;
616
626
}
0 commit comments