summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/avro/src/schema_equal.c
blob: 419ed127894afc9cbc35805b4e49306c00299d2a (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
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to you under the Apache License, Version 2.0 
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 * https://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.  See the License for the specific language governing
 * permissions and limitations under the License. 
 */

#include "avro_private.h"
#include "schema.h"
#include <string.h>

static int
schema_record_equal(struct avro_record_schema_t *a,
		    struct avro_record_schema_t *b)
{
	long i;
	if (strcmp(a->name, b->name)) {
		/*
		 * They have different names 
		 */
		return 0;
	}
	if (nullstrcmp(a->space, b->space)) {
		return 0;
	}
	if (a->fields->num_entries != b->fields->num_entries) {
		/* They have different numbers of fields */
		return 0;
	}
	for (i = 0; i < a->fields->num_entries; i++) {
		union {
			st_data_t data;
			struct avro_record_field_t *f;
		} fa, fb;
		st_lookup(a->fields, i, &fa.data);
		if (!st_lookup(b->fields, i, &fb.data)) {
			return 0;
		}
		if (strcmp(fa.f->name, fb.f->name)) {
			/*
			 * They have fields with different names 
			 */
			return 0;
		}
		if (!avro_schema_equal(fa.f->type, fb.f->type)) {
			/*
			 * They have fields with different schemas 
			 */
			return 0;
		}
	}
	return 1;
}

static int
schema_enum_equal(struct avro_enum_schema_t *a, struct avro_enum_schema_t *b)
{
	long i;
	if (strcmp(a->name, b->name)) {
		/*
		 * They have different names 
		 */
		return 0;
	}
	if (nullstrcmp(a->space, b->space)) {
		return 0;
	}
	for (i = 0; i < a->symbols->num_entries; i++) {
		union {
			st_data_t data;
			char *sym;
		} sa, sb;
		st_lookup(a->symbols, i, &sa.data);
		if (!st_lookup(b->symbols, i, &sb.data)) {
			return 0;
		}
		if (strcmp(sa.sym, sb.sym) != 0) {
			/*
			 * They have different symbol names 
			 */
			return 0;
		}
	}
	return 1;
}

static int
schema_fixed_equal(struct avro_fixed_schema_t *a, struct avro_fixed_schema_t *b)
{
	if (strcmp(a->name, b->name)) {
		/*
		 * They have different names 
		 */
		return 0;
	}
	if (nullstrcmp(a->space, b->space)) {
		return 0;
	}
	return (a->size == b->size);
}

static int
schema_map_equal(struct avro_map_schema_t *a, struct avro_map_schema_t *b)
{
	return avro_schema_equal(a->values, b->values);
}

static int
schema_array_equal(struct avro_array_schema_t *a, struct avro_array_schema_t *b)
{
	return avro_schema_equal(a->items, b->items);
}

static int
schema_union_equal(struct avro_union_schema_t *a, struct avro_union_schema_t *b)
{
	long i;
	for (i = 0; i < a->branches->num_entries; i++) {
		union {
			st_data_t data;
			avro_schema_t schema;
		} ab, bb;
		st_lookup(a->branches, i, &ab.data);
		if (!st_lookup(b->branches, i, &bb.data)) {
			return 0;
		}
		if (!avro_schema_equal(ab.schema, bb.schema)) {
			/*
			 * They don't have the same schema types 
			 */
			return 0;
		}
	}
	return 1;
}

static int
schema_link_equal(struct avro_link_schema_t *a, struct avro_link_schema_t *b)
{
	/*
	 * NOTE: links can only be used for named types. They are used in
	 * recursive schemas so we just check the name of the schema pointed
	 * to instead of a deep check.  Otherwise, we recurse forever... 
	 */
	if (is_avro_record(a->to)) {
		if (!is_avro_record(b->to)) {
			return 0;
		}
		if (nullstrcmp(avro_schema_to_record(a->to)->space,
			       avro_schema_to_record(b->to)->space)) {
			return 0;
		}
	}
	return (strcmp(avro_schema_name(a->to), avro_schema_name(b->to)) == 0);
}

int avro_schema_equal(avro_schema_t a, avro_schema_t b)
{
	if (!a || !b) {
		/*
		 * this is an error. protecting from segfault. 
		 */
		return 0;
	} else if (a == b) {
		/*
		 * an object is equal to itself 
		 */
		return 1;
	} else if (avro_typeof(a) != avro_typeof(b)) {
		return 0;
	} else if (is_avro_record(a)) {
		return schema_record_equal(avro_schema_to_record(a),
					   avro_schema_to_record(b));
	} else if (is_avro_enum(a)) {
		return schema_enum_equal(avro_schema_to_enum(a),
					 avro_schema_to_enum(b));
	} else if (is_avro_fixed(a)) {
		return schema_fixed_equal(avro_schema_to_fixed(a),
					  avro_schema_to_fixed(b));
	} else if (is_avro_map(a)) {
		return schema_map_equal(avro_schema_to_map(a),
					avro_schema_to_map(b));
	} else if (is_avro_array(a)) {
		return schema_array_equal(avro_schema_to_array(a),
					  avro_schema_to_array(b));
	} else if (is_avro_union(a)) {
		return schema_union_equal(avro_schema_to_union(a),
					  avro_schema_to_union(b));
	} else if (is_avro_link(a)) {
		return schema_link_equal(avro_schema_to_link(a),
					 avro_schema_to_link(b));
	}
	return 1;
}