summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/avro/src/datum_skip.c
blob: aa51d7934688f3f177b39c5d42ced0a144da54ae (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
/*
 * 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 "avro/errors.h"
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "encoding.h"
#include "schema.h"

static int skip_array(avro_reader_t reader, const avro_encoding_t * enc,
		      struct avro_array_schema_t *writers_schema)
{
	int rval;
	int64_t i;
	int64_t block_count;
	int64_t block_size;

	check_prefix(rval, enc->read_long(reader, &block_count),
		     "Cannot read array block count: ");

	while (block_count != 0) {
		if (block_count < 0) {
			block_count = block_count * -1;
			check_prefix(rval, enc->read_long(reader, &block_size),
				     "Cannot read array block size: ");
		}

		for (i = 0; i < block_count; i++) {
			check_prefix(rval, avro_skip_data(reader, writers_schema->items),
				     "Cannot skip array element: ");
		}

		check_prefix(rval, enc->read_long(reader, &block_count),
			     "Cannot read array block count: ");
	}
	return 0;
}

static int skip_map(avro_reader_t reader, const avro_encoding_t * enc,
		    struct avro_map_schema_t *writers_schema)
{
	int rval;
	int64_t i, block_count;

	check_prefix(rval, enc->read_long(reader, &block_count),
		     "Cannot read map block count: ");
	while (block_count != 0) {
		int64_t block_size;
		if (block_count < 0) {
			block_count = block_count * -1;
			check_prefix(rval, enc->read_long(reader, &block_size),
				     "Cannot read map block size: ");
		}
		for (i = 0; i < block_count; i++) {
			check_prefix(rval, enc->skip_string(reader),
				     "Cannot skip map key: ");
			check_prefix(rval,
				     avro_skip_data(reader,
						    avro_schema_to_map(writers_schema)->
						    values),
				     "Cannot skip map value: ");
		}
		check_prefix(rval, enc->read_long(reader, &block_count),
			     "Cannot read map block count: ");
	}
	return 0;
}

static int skip_union(avro_reader_t reader, const avro_encoding_t * enc,
		      struct avro_union_schema_t *writers_schema)
{
	int rval;
	int64_t index;
	avro_schema_t branch_schema;

	check_prefix(rval, enc->read_long(reader, &index),
		     "Cannot read union discriminant: ");
	branch_schema = avro_schema_union_branch(&writers_schema->obj, index);
	if (!branch_schema) {
		return EILSEQ;
	}
	return avro_skip_data(reader, branch_schema);
}

static int skip_record(avro_reader_t reader, const avro_encoding_t * enc,
		       struct avro_record_schema_t *writers_schema)
{
	int rval;
	long i;

	AVRO_UNUSED(enc);

	for (i = 0; i < writers_schema->fields->num_entries; i++) {
		avro_schema_t  field_schema;

		field_schema = avro_schema_record_field_get_by_index
		    (&writers_schema->obj, i);
		check_prefix(rval, avro_skip_data(reader, field_schema),
			     "Cannot skip record field: ");
	}
	return 0;
}

int avro_skip_data(avro_reader_t reader, avro_schema_t writers_schema)
{
	check_param(EINVAL, reader, "reader");
	check_param(EINVAL, is_avro_schema(writers_schema), "writer schema");

	int rval = EINVAL;
	const avro_encoding_t *enc = &avro_binary_encoding;

	switch (avro_typeof(writers_schema)) {
	case AVRO_NULL:
		rval = enc->skip_null(reader);
		break;

	case AVRO_BOOLEAN:
		rval = enc->skip_boolean(reader);
		break;

	case AVRO_STRING:
		rval = enc->skip_string(reader);
		break;

	case AVRO_INT32:
		rval = enc->skip_int(reader);
		break;

	case AVRO_INT64:
		rval = enc->skip_long(reader);
		break;

	case AVRO_FLOAT:
		rval = enc->skip_float(reader);
		break;

	case AVRO_DOUBLE:
		rval = enc->skip_double(reader);
		break;

	case AVRO_BYTES:
		rval = enc->skip_bytes(reader);
		break;

	case AVRO_FIXED:
		rval =
		    avro_skip(reader,
			      avro_schema_to_fixed(writers_schema)->size);
		break;

	case AVRO_ENUM:
		rval = enc->skip_long(reader);
		break;

	case AVRO_ARRAY:
		rval =
		    skip_array(reader, enc,
			       avro_schema_to_array(writers_schema));
		break;

	case AVRO_MAP:
		rval =
		    skip_map(reader, enc, avro_schema_to_map(writers_schema));
		break;

	case AVRO_UNION:
		rval =
		    skip_union(reader, enc,
			       avro_schema_to_union(writers_schema));
		break;

	case AVRO_RECORD:
		rval =
		    skip_record(reader, enc,
				avro_schema_to_record(writers_schema));
		break;

	case AVRO_LINK:
		rval =
		    avro_skip_data(reader,
				   (avro_schema_to_link(writers_schema))->to);
		break;
	}

	return rval;
}