You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by ma...@apache.org on 2010/01/20 23:58:23 UTC
svn commit: r901421 [2/3] - in /hadoop/avro/trunk: ./ lang/c/ lang/c/src/
lang/c/tests/
Modified: hadoop/avro/trunk/lang/c/src/encoding_binary.c
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/src/encoding_binary.c?rev=901421&r1=901420&r2=901421&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/encoding_binary.c (original)
+++ hadoop/avro/trunk/lang/c/src/encoding_binary.c Wed Jan 20 22:58:22 2010
@@ -1,21 +1,19 @@
/*
-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
-
-http://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.
-*/
+ * 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
+ *
+ * http://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 "encoding.h"
#include <stdlib.h>
#include <limits.h>
@@ -24,346 +22,330 @@
#define MAX_VARINT_BUF_SIZE 10
-static int
-read_long (avro_reader_t reader, int64_t * l)
+static int read_long(avro_reader_t reader, int64_t * l)
{
- uint64_t value = 0;
- uint8_t b;
- int offset = 0;
- do
- {
- if (offset == MAX_VARINT_BUF_SIZE)
- {
- /* illegal byte sequence */
- return EILSEQ;
+ uint64_t value = 0;
+ uint8_t b;
+ int offset = 0;
+ do {
+ if (offset == MAX_VARINT_BUF_SIZE) {
+ /*
+ * illegal byte sequence
+ */
+ return EILSEQ;
+ }
+ AVRO_READ(reader, &b, 1);
+ value |= (int64_t) (b & 0x7F) << (7 * offset);
+ ++offset;
}
- AVRO_READ (reader, &b, 1);
- value |= (int64_t) (b & 0x7F) << (7 * offset);
- ++offset;
- }
- while (b & 0x80);
- *l = ((value >> 1) ^ -(value & 1));
- return 0;
+ while (b & 0x80);
+ *l = ((value >> 1) ^ -(value & 1));
+ return 0;
}
-static int
-skip_long (avro_reader_t reader)
-{
- uint8_t b;
- int offset = 0;
- do
- {
- if (offset == MAX_VARINT_BUF_SIZE)
- {
- return EILSEQ;
+static int skip_long(avro_reader_t reader)
+{
+ uint8_t b;
+ int offset = 0;
+ do {
+ if (offset == MAX_VARINT_BUF_SIZE) {
+ return EILSEQ;
+ }
+ AVRO_READ(reader, &b, 1);
+ ++offset;
}
- AVRO_READ (reader, &b, 1);
- ++offset;
- }
- while (b & 0x80);
- return 0;
+ while (b & 0x80);
+ return 0;
}
-static int
-write_long (avro_writer_t writer, int64_t l)
+static int write_long(avro_writer_t writer, int64_t l)
{
- uint8_t b;
- uint64_t n = (l << 1) ^ (l >> 63);
- while (n & ~0x7F)
- {
- b = ((((uint8_t) n) & 0x7F) | 0x80);
- AVRO_WRITE (writer, &b, 1);
- n >>= 7;
- }
- b = (uint8_t) n;
- AVRO_WRITE (writer, &b, 1);
- return 0;
+ uint8_t b;
+ uint64_t n = (l << 1) ^ (l >> 63);
+ while (n & ~0x7F) {
+ b = ((((uint8_t) n) & 0x7F) | 0x80);
+ AVRO_WRITE(writer, &b, 1);
+ n >>= 7;
+ }
+ b = (uint8_t) n;
+ AVRO_WRITE(writer, &b, 1);
+ return 0;
}
-static int
-read_int (avro_reader_t reader, int32_t * i)
+static int read_int(avro_reader_t reader, int32_t * i)
{
- int64_t l;
- int rval = read_long (reader, &l);
- if (rval)
- {
- return rval;
- }
- if (!(INT_MIN <= l && l <= INT_MAX))
- {
- return ERANGE;
- }
- *i = l;
- return 0;
+ int64_t l;
+ int rval = read_long(reader, &l);
+ if (rval) {
+ return rval;
+ }
+ if (!(INT_MIN <= l && l <= INT_MAX)) {
+ return ERANGE;
+ }
+ *i = l;
+ return 0;
}
-static int
-skip_int (avro_reader_t reader)
+static int skip_int(avro_reader_t reader)
{
- return skip_long (reader);
+ return skip_long(reader);
}
-static int
-write_int (avro_writer_t writer, const int32_t i)
+static int write_int(avro_writer_t writer, const int32_t i)
{
- int64_t l = i;
- return write_long (writer, l);
+ int64_t l = i;
+ return write_long(writer, l);
}
-static int
-read_bytes (avro_reader_t reader, char **bytes, int64_t * len)
+static int read_bytes(avro_reader_t reader, char **bytes, int64_t * len)
{
- int rval = read_long (reader, len);
- if (rval)
- {
- return rval;
- }
- *bytes = malloc (*len + 1);
- if (!*bytes)
- {
- return ENOMEM;
- }
- (*bytes)[*len] = '\0';
- AVRO_READ (reader, *bytes, *len);
- return 0;
+ int rval = read_long(reader, len);
+ if (rval) {
+ return rval;
+ }
+ *bytes = malloc(*len + 1);
+ if (!*bytes) {
+ return ENOMEM;
+ }
+ (*bytes)[*len] = '\0';
+ AVRO_READ(reader, *bytes, *len);
+ return 0;
}
-static int
-skip_bytes (avro_reader_t reader)
+static int skip_bytes(avro_reader_t reader)
{
- int64_t len;
- int rval = read_long (reader, &len);
- if (rval)
- {
- return rval;
- }
- AVRO_SKIP (reader, len);
- return 0;
+ int64_t len;
+ int rval = read_long(reader, &len);
+ if (rval) {
+ return rval;
+ }
+ AVRO_SKIP(reader, len);
+ return 0;
}
static int
-write_bytes (avro_writer_t writer, const char *bytes, const int64_t len)
+write_bytes(avro_writer_t writer, const char *bytes, const int64_t len)
{
- int rval;
- if (len < 0)
- {
- return EINVAL;
- }
- rval = write_long (writer, len);
- if (rval)
- {
- return rval;
- }
- AVRO_WRITE (writer, (char *) bytes, len);
- return 0;
+ int rval;
+ if (len < 0) {
+ return EINVAL;
+ }
+ rval = write_long(writer, len);
+ if (rval) {
+ return rval;
+ }
+ AVRO_WRITE(writer, (char *)bytes, len);
+ return 0;
}
-static int
-read_string (avro_reader_t reader, char **s)
+static int read_string(avro_reader_t reader, char **s)
{
- int64_t len;
- return read_bytes (reader, s, &len);
+ int64_t len;
+ return read_bytes(reader, s, &len);
}
-static int
-skip_string (avro_reader_t reader)
+static int skip_string(avro_reader_t reader)
{
- return skip_bytes (reader);
+ return skip_bytes(reader);
}
-static int
-write_string (avro_writer_t writer, const char *s)
+static int write_string(avro_writer_t writer, const char *s)
{
- int64_t len = strlen (s);
- return write_bytes (writer, s, len);
+ int64_t len = strlen(s);
+ return write_bytes(writer, s, len);
}
-static int
-read_float (avro_reader_t reader, float *f)
+static int read_float(avro_reader_t reader, float *f)
{
#if WORDS_BIGENDIAN
- uint8_t buf[4];
+ uint8_t buf[4];
#endif
- union
- {
- float f;
- int32_t i;
- } v;
+ union {
+ float f;
+ int32_t i;
+ } v;
#if WORDS_BIGENDIAN
- AVRO_READ (avro, buf, 4);
- v.i = ((int32_t) buf[0] << 0)
- | ((int32_t) buf[1] << 8)
- | ((int32_t) buf[2] << 16) | ((int32_t) buf[3] << 24);
+ AVRO_READ(avro, buf, 4);
+ v.i = ((int32_t) buf[0] << 0)
+ | ((int32_t) buf[1] << 8)
+ | ((int32_t) buf[2] << 16) | ((int32_t) buf[3] << 24);
#else
- AVRO_READ (reader, (void *) &v.i, 4);
+ AVRO_READ(reader, (void *)&v.i, 4);
#endif
- *f = v.f;
- return 0;
+ *f = v.f;
+ return 0;
}
-static int
-skip_float (avro_reader_t reader)
+static int skip_float(avro_reader_t reader)
{
- AVRO_SKIP (reader, 4);
- return 0;
+ AVRO_SKIP(reader, 4);
+ return 0;
}
-static int
-write_float (avro_writer_t writer, const float f)
+static int write_float(avro_writer_t writer, const float f)
{
#if WORDS_BIGENDIAN
- uint8_t buf[4];
+ uint8_t buf[4];
#endif
- union
- {
- float f;
- int32_t i;
- } v;
+ union {
+ float f;
+ int32_t i;
+ } v;
- v.f = f;
+ v.f = f;
#if WORDS_BIGENDIAN
- buf[0] = (uint8_t) (v.i >> 0);
- buf[1] = (uint8_t) (v.i >> 8);
- buf[2] = (uint8_t) (v.i >> 16);
- buf[3] = (uint8_t) (v.i >> 24);
- AVRO_WRITE (writer, buf, 4);
+ buf[0] = (uint8_t) (v.i >> 0);
+ buf[1] = (uint8_t) (v.i >> 8);
+ buf[2] = (uint8_t) (v.i >> 16);
+ buf[3] = (uint8_t) (v.i >> 24);
+ AVRO_WRITE(writer, buf, 4);
#else
- AVRO_WRITE (writer, (void *) &v.i, 4);
+ AVRO_WRITE(writer, (void *)&v.i, 4);
#endif
- return 0;
+ return 0;
}
-static int
-read_double (avro_reader_t reader, double *d)
+static int read_double(avro_reader_t reader, double *d)
{
#if WORDS_BIGENDIAN
- uint8_t buf[8];
+ uint8_t buf[8];
#endif
- union
- {
- double d;
- int64_t l;
- } v;
+ union {
+ double d;
+ int64_t l;
+ } v;
#if WORDS_BIGENDIAN
- AVRO_READ (avro, buf, 8);
- v.l = ((int64_t) buf[0] << 0)
- | ((int64_t) buf[1] << 8)
- | ((int64_t) buf[2] << 16)
- | ((int64_t) buf[3] << 24)
- | ((int64_t) buf[4] << 32)
- | ((int64_t) buf[5] << 40)
- | ((int64_t) buf[6] << 48) | ((int64_t) buf[7] << 56);
+ AVRO_READ(avro, buf, 8);
+ v.l = ((int64_t) buf[0] << 0)
+ | ((int64_t) buf[1] << 8)
+ | ((int64_t) buf[2] << 16)
+ | ((int64_t) buf[3] << 24)
+ | ((int64_t) buf[4] << 32)
+ | ((int64_t) buf[5] << 40)
+ | ((int64_t) buf[6] << 48) | ((int64_t) buf[7] << 56);
#else
- AVRO_READ (reader, (void *) &v.l, 8);
+ AVRO_READ(reader, (void *)&v.l, 8);
#endif
- *d = v.d;
- return 0;
+ *d = v.d;
+ return 0;
}
-static int
-skip_double (avro_reader_t reader)
+static int skip_double(avro_reader_t reader)
{
- AVRO_SKIP (reader, 8);
- return 0;
+ AVRO_SKIP(reader, 8);
+ return 0;
}
-static int
-write_double (avro_writer_t writer, const double d)
+static int write_double(avro_writer_t writer, const double d)
{
#if WORDS_BIGENDIAN
- uint8_t buf[8];
+ uint8_t buf[8];
#endif
- union
- {
- double d;
- int64_t l;
- } v;
+ union {
+ double d;
+ int64_t l;
+ } v;
- v.d = d;
+ v.d = d;
#if WORDS_BIGENDIAN
- buf[0] = (uint8_t) (v.l >> 0);
- buf[1] = (uint8_t) (v.l >> 8);
- buf[2] = (uint8_t) (v.l >> 16);
- buf[3] = (uint8_t) (v.l >> 24);
- buf[4] = (uint8_t) (v.l >> 32);
- buf[5] = (uint8_t) (v.l >> 40);
- buf[6] = (uint8_t) (v.l >> 48);
- buf[7] = (uint8_t) (v.l >> 56);
- AVRO_WRITE (writer, buf, 8);
+ buf[0] = (uint8_t) (v.l >> 0);
+ buf[1] = (uint8_t) (v.l >> 8);
+ buf[2] = (uint8_t) (v.l >> 16);
+ buf[3] = (uint8_t) (v.l >> 24);
+ buf[4] = (uint8_t) (v.l >> 32);
+ buf[5] = (uint8_t) (v.l >> 40);
+ buf[6] = (uint8_t) (v.l >> 48);
+ buf[7] = (uint8_t) (v.l >> 56);
+ AVRO_WRITE(writer, buf, 8);
#else
- AVRO_WRITE (writer, (void *) &v.l, 8);
+ AVRO_WRITE(writer, (void *)&v.l, 8);
#endif
- return 0;
+ return 0;
}
-static int
-read_boolean (avro_reader_t reader, int8_t * b)
+static int read_boolean(avro_reader_t reader, int8_t * b)
{
- AVRO_READ (reader, b, 1);
- return 0;
+ AVRO_READ(reader, b, 1);
+ return 0;
}
-static int
-skip_boolean (avro_reader_t reader)
+static int skip_boolean(avro_reader_t reader)
{
- AVRO_SKIP (reader, 1);
- return 0;
+ AVRO_SKIP(reader, 1);
+ return 0;
}
-static int
-write_boolean (avro_writer_t writer, const int8_t b)
+static int write_boolean(avro_writer_t writer, const int8_t b)
{
- AVRO_WRITE (writer, (char *) &b, 1);
- return 0;
+ AVRO_WRITE(writer, (char *)&b, 1);
+ return 0;
}
-static int
-read_skip_null (avro_reader_t reader)
+static int read_skip_null(avro_reader_t reader)
{
- /* no-op */
- return 0;
+ /*
+ * no-op
+ */
+ return 0;
}
-static int
-write_null (avro_writer_t writer)
+static int write_null(avro_writer_t writer)
{
- /* no-op */
- return 0;
+ /*
+ * no-op
+ */
+ return 0;
}
const avro_encoding_t avro_binary_encoding = {
- .description = "BINARY FORMAT",
- /* string */
- .read_string = read_string,
- .skip_string = skip_string,
- .write_string = write_string,
- /* bytes */
- .read_bytes = read_bytes,
- .skip_bytes = skip_bytes,
- .write_bytes = write_bytes,
- /* int */
- .read_int = read_int,
- .skip_int = skip_int,
- .write_int = write_int,
- /* long */
- .read_long = read_long,
- .skip_long = skip_long,
- .write_long = write_long,
- /* float */
- .read_float = read_float,
- .skip_float = skip_float,
- .write_float = write_float,
- /* double */
- .read_double = read_double,
- .skip_double = skip_double,
- .write_double = write_double,
- /* boolean */
- .read_boolean = read_boolean,
- .skip_boolean = skip_boolean,
- .write_boolean = write_boolean,
- /* null */
- .read_null = read_skip_null,
- .skip_null = read_skip_null,
- .write_null = write_null
+ .description = "BINARY FORMAT",
+ /*
+ * string
+ */
+ .read_string = read_string,
+ .skip_string = skip_string,
+ .write_string = write_string,
+ /*
+ * bytes
+ */
+ .read_bytes = read_bytes,
+ .skip_bytes = skip_bytes,
+ .write_bytes = write_bytes,
+ /*
+ * int
+ */
+ .read_int = read_int,
+ .skip_int = skip_int,
+ .write_int = write_int,
+ /*
+ * long
+ */
+ .read_long = read_long,
+ .skip_long = skip_long,
+ .write_long = write_long,
+ /*
+ * float
+ */
+ .read_float = read_float,
+ .skip_float = skip_float,
+ .write_float = write_float,
+ /*
+ * double
+ */
+ .read_double = read_double,
+ .skip_double = skip_double,
+ .write_double = write_double,
+ /*
+ * boolean
+ */
+ .read_boolean = read_boolean,
+ .skip_boolean = skip_boolean,
+ .write_boolean = write_boolean,
+ /*
+ * null
+ */
+ .read_null = read_skip_null,
+ .skip_null = read_skip_null,
+ .write_null = write_null
};
Modified: hadoop/avro/trunk/lang/c/src/io.c
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/src/io.c?rev=901421&r1=901420&r2=901421&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/io.c (original)
+++ hadoop/avro/trunk/lang/c/src/io.c Wed Jan 20 22:58:22 2010
@@ -1,21 +1,19 @@
/*
-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
-
-http://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.
-*/
+ * 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
+ *
+ * http://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 <stdio.h>
#include <stdlib.h>
#include <stdint.h>
@@ -24,51 +22,44 @@
#include "avro.h"
#include "container_of.h"
-enum avro_io_type_t
-{
- AVRO_FILE_IO,
- AVRO_MEMORY_IO
+enum avro_io_type_t {
+ AVRO_FILE_IO,
+ AVRO_MEMORY_IO
};
typedef enum avro_io_type_t avro_io_type_t;
-struct avro_reader_t
-{
- avro_io_type_t type;
- unsigned long refcount;
+struct avro_reader_t {
+ avro_io_type_t type;
+ unsigned long refcount;
};
-struct avro_writer_t
-{
- avro_io_type_t type;
- unsigned long refcount;
+struct avro_writer_t {
+ avro_io_type_t type;
+ unsigned long refcount;
};
-struct avro_file_reader_t
-{
- struct avro_reader_t reader;
- FILE *fp;
+struct avro_file_reader_t {
+ struct avro_reader_t reader;
+ FILE *fp;
};
-struct avro_file_writer_t
-{
- struct avro_writer_t writer;
- FILE *fp;
+struct avro_file_writer_t {
+ struct avro_writer_t writer;
+ FILE *fp;
};
-struct avro_memory_reader_t
-{
- struct avro_reader_t reader;
- const char *buf;
- int64_t len;
- int64_t read;
+struct avro_memory_reader_t {
+ struct avro_reader_t reader;
+ const char *buf;
+ int64_t len;
+ int64_t read;
};
-struct avro_memory_writer_t
-{
- struct avro_writer_t writer;
- const char *buf;
- int64_t len;
- int64_t written;
+struct avro_memory_writer_t {
+ struct avro_writer_t writer;
+ const char *buf;
+ int64_t len;
+ int64_t written;
};
#define avro_io_typeof(obj) ((obj)->type)
@@ -80,188 +71,164 @@
#define avro_writer_to_memory(writer_) container_of(writer_, struct avro_memory_writer_t, writer)
#define avro_writer_to_file(writer_) container_of(writer_, struct avro_file_writer_t, writer)
-static void
-reader_init (avro_reader_t reader, avro_io_type_t type)
+static void reader_init(avro_reader_t reader, avro_io_type_t type)
{
- reader->type = type;
- reader->refcount = 1;
+ reader->type = type;
+ reader->refcount = 1;
}
-static void
-writer_init (avro_writer_t writer, avro_io_type_t type)
-{
- writer->type = type;
- writer->refcount = 1;
-}
-
-avro_reader_t
-avro_reader_file (FILE * fp)
-{
- struct avro_file_reader_t *file_reader =
- malloc (sizeof (struct avro_file_reader_t));
- if (!file_reader)
- {
- return NULL;
- }
- file_reader->fp = fp;
- reader_init (&file_reader->reader, AVRO_FILE_IO);
- return &file_reader->reader;
-}
-
-avro_writer_t
-avro_writer_file (FILE * fp)
-{
- struct avro_file_writer_t *file_writer =
- malloc (sizeof (struct avro_file_writer_t));
- if (!file_writer)
- {
- return NULL;
- }
- file_writer->fp = fp;
- writer_init (&file_writer->writer, AVRO_FILE_IO);
- return &file_writer->writer;
-}
-
-avro_reader_t
-avro_reader_memory (const char *buf, int64_t len)
-{
- struct avro_memory_reader_t *mem_reader =
- malloc (sizeof (struct avro_memory_reader_t));
- if (!mem_reader)
- {
- return NULL;
- }
- mem_reader->buf = buf;
- mem_reader->len = len;
- mem_reader->read = 0;
- reader_init (&mem_reader->reader, AVRO_MEMORY_IO);
- return &mem_reader->reader;
-}
-
-avro_writer_t
-avro_writer_memory (const char *buf, int64_t len)
-{
- struct avro_memory_writer_t *mem_writer =
- malloc (sizeof (struct avro_memory_writer_t));
- if (!mem_writer)
- {
- return NULL;
- }
- mem_writer->buf = buf;
- mem_writer->len = len;
- mem_writer->written = 0;
- writer_init (&mem_writer->writer, AVRO_MEMORY_IO);
- return &mem_writer->writer;
+static void writer_init(avro_writer_t writer, avro_io_type_t type)
+{
+ writer->type = type;
+ writer->refcount = 1;
}
-static int
-avro_read_memory (struct avro_memory_reader_t *reader, void *buf, int64_t len)
+avro_reader_t avro_reader_file(FILE * fp)
{
- if (len)
- {
- if ((reader->len - reader->read) < len)
- {
- return ENOSPC;
+ struct avro_file_reader_t *file_reader =
+ malloc(sizeof(struct avro_file_reader_t));
+ if (!file_reader) {
+ return NULL;
}
- memcpy (buf, reader->buf + reader->read, len);
- reader->read += len;
- }
- return 0;
+ file_reader->fp = fp;
+ reader_init(&file_reader->reader, AVRO_FILE_IO);
+ return &file_reader->reader;
}
-static int
-avro_read_file (struct avro_file_reader_t *reader, void *buf, int64_t len)
+avro_writer_t avro_writer_file(FILE * fp)
{
- /* TODO */
- return -1;
+ struct avro_file_writer_t *file_writer =
+ malloc(sizeof(struct avro_file_writer_t));
+ if (!file_writer) {
+ return NULL;
+ }
+ file_writer->fp = fp;
+ writer_init(&file_writer->writer, AVRO_FILE_IO);
+ return &file_writer->writer;
}
-int
-avro_read (avro_reader_t reader, void *buf, int64_t len)
+avro_reader_t avro_reader_memory(const char *buf, int64_t len)
{
- if (buf && len >= 0)
- {
- if (is_memory_io (reader))
- {
- return avro_read_memory (avro_reader_to_memory (reader), buf, len);
+ struct avro_memory_reader_t *mem_reader =
+ malloc(sizeof(struct avro_memory_reader_t));
+ if (!mem_reader) {
+ return NULL;
}
- else if (is_file_io (reader))
- {
- return avro_read_file (avro_reader_to_file (reader), buf, len);
+ mem_reader->buf = buf;
+ mem_reader->len = len;
+ mem_reader->read = 0;
+ reader_init(&mem_reader->reader, AVRO_MEMORY_IO);
+ return &mem_reader->reader;
+}
+
+avro_writer_t avro_writer_memory(const char *buf, int64_t len)
+{
+ struct avro_memory_writer_t *mem_writer =
+ malloc(sizeof(struct avro_memory_writer_t));
+ if (!mem_writer) {
+ return NULL;
}
- }
- return EINVAL;
+ mem_writer->buf = buf;
+ mem_writer->len = len;
+ mem_writer->written = 0;
+ writer_init(&mem_writer->writer, AVRO_MEMORY_IO);
+ return &mem_writer->writer;
}
static int
-avro_write_memory (struct avro_memory_writer_t *writer, void *buf,
- int64_t len)
+avro_read_memory(struct avro_memory_reader_t *reader, void *buf, int64_t len)
{
- if (len)
- {
- if ((writer->len - writer->written) < len)
- {
- return ENOSPC;
+ if (len) {
+ if ((reader->len - reader->read) < len) {
+ return ENOSPC;
+ }
+ memcpy(buf, reader->buf + reader->read, len);
+ reader->read += len;
}
- memcpy ((void *) (writer->buf + writer->written), buf, len);
- writer->written += len;
- }
- return 0;
+ return 0;
}
static int
-avro_write_file (struct avro_file_writer_t *writer, void *buf, int64_t len)
+avro_read_file(struct avro_file_reader_t *reader, void *buf, int64_t len)
{
- /* TODO */
- return -1;
+ /*
+ * TODO
+ */
+ return -1;
+}
+
+int avro_read(avro_reader_t reader, void *buf, int64_t len)
+{
+ if (buf && len >= 0) {
+ if (is_memory_io(reader)) {
+ return avro_read_memory(avro_reader_to_memory(reader),
+ buf, len);
+ } else if (is_file_io(reader)) {
+ return avro_read_file(avro_reader_to_file(reader), buf,
+ len);
+ }
+ }
+ return EINVAL;
}
-int
-avro_write (avro_writer_t writer, void *buf, int64_t len)
+static int
+avro_write_memory(struct avro_memory_writer_t *writer, void *buf, int64_t len)
{
- if (buf && len >= 0)
- {
- if (is_memory_io (writer))
- {
- return avro_write_memory (avro_writer_to_memory (writer), buf, len);
+ if (len) {
+ if ((writer->len - writer->written) < len) {
+ return ENOSPC;
+ }
+ memcpy((void *)(writer->buf + writer->written), buf, len);
+ writer->written += len;
}
- else if (is_memory_io (writer))
- {
- return avro_write_file (avro_writer_to_file (writer), buf, len);
+ return 0;
+}
+
+static int
+avro_write_file(struct avro_file_writer_t *writer, void *buf, int64_t len)
+{
+ /*
+ * TODO
+ */
+ return -1;
+}
+
+int avro_write(avro_writer_t writer, void *buf, int64_t len)
+{
+ if (buf && len >= 0) {
+ if (is_memory_io(writer)) {
+ return avro_write_memory(avro_writer_to_memory(writer),
+ buf, len);
+ } else if (is_memory_io(writer)) {
+ return avro_write_file(avro_writer_to_file(writer), buf,
+ len);
+ }
}
- }
- return EINVAL;
+ return EINVAL;
}
-int
-avro_skip (avro_reader_t reader, int64_t len)
+int avro_skip(avro_reader_t reader, int64_t len)
{
- /* TODO */
- return -1;
+ /*
+ * TODO
+ */
+ return -1;
}
-void
-avro_reader_free (avro_reader_t reader)
+void avro_reader_free(avro_reader_t reader)
{
- if (is_memory_io (reader))
- {
- free (avro_reader_to_memory (reader));
- }
- else if (is_file_io (reader))
- {
- free (avro_reader_to_file (reader));
- }
+ if (is_memory_io(reader)) {
+ free(avro_reader_to_memory(reader));
+ } else if (is_file_io(reader)) {
+ free(avro_reader_to_file(reader));
+ }
}
-void
-avro_writer_free (avro_writer_t writer)
+void avro_writer_free(avro_writer_t writer)
{
- if (is_memory_io (writer))
- {
- free (avro_writer_to_memory (writer));
- }
- else if (is_file_io (writer))
- {
- free (avro_writer_to_file (writer));
- }
+ if (is_memory_io(writer)) {
+ free(avro_writer_to_memory(writer));
+ } else if (is_file_io(writer)) {
+ free(avro_writer_to_file(writer));
+ }
}
Modified: hadoop/avro/trunk/lang/c/src/queue.h
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/src/queue.h?rev=901421&r1=901420&r2=901421&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/queue.h (original)
+++ hadoop/avro/trunk/lang/c/src/queue.h Wed Jan 20 22:58:22 2010
@@ -1,6 +1,6 @@
/*
* Copyright (c) 1991, 1993
- * The Regents of the University of California. All rights reserved.
+ * The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
- * @(#)queue.h 8.5 (Berkeley) 8/20/94
+ * @(#)queue.h 8.5 (Berkeley) 8/20/94
*/
#ifndef _SYS_QUEUE_H_
@@ -143,7 +143,6 @@
#define LIST_FIRST(head) ((head)->lh_first)
#define LIST_NEXT(elm, field) ((elm)->field.le_next)
-
/*
* Singly-linked List definitions.
*/
@@ -204,7 +203,6 @@
#define SLIST_FIRST(head) ((head)->slh_first)
#define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
-
/*
* Singly-linked Tail queue declarations.
*/
@@ -278,7 +276,6 @@
#define STAILQ_FIRST(head) ((head)->stqh_first)
#define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
-
/*
* Simple queue definitions.
*/
@@ -352,7 +349,6 @@
#define SIMPLEQ_FIRST(head) ((head)->sqh_first)
#define SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
-
/*
* Tail queue definitions.
*/
@@ -446,7 +442,6 @@
#define TAILQ_PREV(elm, headname, field) \
(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
-
/*
* Circular queue definitions.
*/
@@ -554,4 +549,4 @@
? ((head)->cqh_last) \
: (elm->field.cqe_prev))
-#endif /* sys/queue.h */
+#endif /* sys/queue.h */
Modified: hadoop/avro/trunk/lang/c/src/schema.c
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/src/schema.c?rev=901421&r1=901420&r2=901421&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/schema.c (original)
+++ hadoop/avro/trunk/lang/c/src/schema.c Wed Jan 20 22:58:22 2010
@@ -1,21 +1,19 @@
/*
-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
-
-http://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.
-*/
+ * 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
+ *
+ * http://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 <stdlib.h>
#include <string.h>
#include <errno.h>
@@ -26,890 +24,803 @@
#include "st.h"
#include "schema.h"
-struct avro_schema_error_t
-{
- st_table *named_schemas;
- json_error_t json_error;
+struct avro_schema_error_t {
+ st_table *named_schemas;
+ json_error_t json_error;
};
-static void
-avro_schema_init (avro_schema_t schema, avro_type_t type)
+static void avro_schema_init(avro_schema_t schema, avro_type_t type)
{
- schema->type = type;
- schema->class_type = AVRO_SCHEMA;
- schema->refcount = 1;
+ schema->type = type;
+ schema->class_type = AVRO_SCHEMA;
+ schema->refcount = 1;
+}
+
+static int is_avro_id(const char *name)
+{
+ size_t i, len;
+ if (name) {
+ len = strlen(name);
+ if (len < 1) {
+ return 0;
+ }
+ for (i = 0; i < len; i++) {
+ if (!(isalpha(name[i])
+ || name[i] == '_' || (i && isdigit(name[i])))) {
+ return 0;
+ }
+ }
+ /*
+ * starts with [A-Za-z_] subsequent [A-Za-z0-9_]
+ */
+ return 1;
+ }
+ return 0;
}
-static int
-is_avro_id (const char *name)
+/*
+ * TODO
+ */
+void avro_schema_free(avro_schema_t schema)
+{
+ return;
+}
+
+avro_schema_t avro_schema_string(void)
+{
+ static struct avro_obj_t obj = {
+ .type = AVRO_STRING,
+ .class_type = AVRO_SCHEMA,
+ .refcount = 1
+ };
+ return &obj;
+}
+
+avro_schema_t avro_schema_bytes(void)
+{
+ static struct avro_obj_t obj = {
+ .type = AVRO_BYTES,
+ .class_type = AVRO_SCHEMA,
+ .refcount = 1
+ };
+ return &obj;
+}
+
+avro_schema_t avro_schema_int(void)
+{
+ static struct avro_obj_t obj = {
+ .type = AVRO_INT,
+ .class_type = AVRO_SCHEMA,
+ .refcount = 1
+ };
+ return &obj;
+}
+
+avro_schema_t avro_schema_long(void)
+{
+ static struct avro_obj_t obj = {
+ .type = AVRO_LONG,
+ .class_type = AVRO_SCHEMA,
+ .refcount = 1
+ };
+ return &obj;
+}
+
+avro_schema_t avro_schema_float(void)
{
- size_t i, len;
- if (name)
- {
- len = strlen (name);
- if (len < 1)
- {
- return 0;
- }
- for (i = 0; i < len; i++)
- {
- if (!
- (isalpha (name[i]) || name[i] == '_'
- || (i && isdigit (name[i]))))
- {
- return 0;
- }
- }
- /* starts with [A-Za-z_] subsequent [A-Za-z0-9_] */
- return 1;
- }
- return 0;
-}
-
-/* TODO */
-void
-avro_schema_free (avro_schema_t schema)
-{
- return;
-}
-
-avro_schema_t
-avro_schema_string (void)
-{
- static struct avro_obj_t obj = {
- .type = AVRO_STRING,
- .class_type = AVRO_SCHEMA,
- .refcount = 1
- };
- return &obj;
-}
-
-avro_schema_t
-avro_schema_bytes (void)
-{
- static struct avro_obj_t obj = {
- .type = AVRO_BYTES,
- .class_type = AVRO_SCHEMA,
- .refcount = 1
- };
- return &obj;
-}
-
-avro_schema_t
-avro_schema_int (void)
-{
- static struct avro_obj_t obj = {
- .type = AVRO_INT,
- .class_type = AVRO_SCHEMA,
- .refcount = 1
- };
- return &obj;
-}
-
-avro_schema_t
-avro_schema_long (void)
-{
- static struct avro_obj_t obj = {
- .type = AVRO_LONG,
- .class_type = AVRO_SCHEMA,
- .refcount = 1
- };
- return &obj;
-}
-
-avro_schema_t
-avro_schema_float (void)
-{
- static struct avro_obj_t obj = {
- .type = AVRO_FLOAT,
- .class_type = AVRO_SCHEMA,
- .refcount = 1
- };
- return &obj;
-}
-
-avro_schema_t
-avro_schema_double (void)
-{
- static struct avro_obj_t obj = {
- .type = AVRO_DOUBLE,
- .class_type = AVRO_SCHEMA,
- .refcount = 1
- };
- return &obj;
-}
-
-avro_schema_t
-avro_schema_boolean (void)
-{
- static struct avro_obj_t obj = {
- .type = AVRO_BOOLEAN,
- .class_type = AVRO_SCHEMA,
- .refcount = 1
- };
- return &obj;
-}
-
-avro_schema_t
-avro_schema_null (void)
-{
- static struct avro_obj_t obj = {
- .type = AVRO_NULL,
- .class_type = AVRO_SCHEMA,
- .refcount = 1
- };
- return &obj;
-}
-
-avro_schema_t
-avro_schema_fixed (const char *name, const int64_t size)
-{
- struct avro_fixed_schema_t *fixed =
- malloc (sizeof (struct avro_fixed_schema_t));
- if (!fixed)
- {
- return NULL;
- }
- if (!is_avro_id (name))
- {
- return NULL;
- }
- fixed->name = strdup (name);
- fixed->size = size;
- avro_schema_init (&fixed->obj, AVRO_FIXED);
- return &fixed->obj;
-}
-
-avro_schema_t
-avro_schema_union (void)
-{
- struct avro_union_schema_t *schema =
- malloc (sizeof (struct avro_union_schema_t));
- if (!schema)
- {
- return NULL;
- }
- STAILQ_INIT (&schema->branches);
- avro_schema_init (&schema->obj, AVRO_UNION);
- return &schema->obj;
+ static struct avro_obj_t obj = {
+ .type = AVRO_FLOAT,
+ .class_type = AVRO_SCHEMA,
+ .refcount = 1
+ };
+ return &obj;
+}
+
+avro_schema_t avro_schema_double(void)
+{
+ static struct avro_obj_t obj = {
+ .type = AVRO_DOUBLE,
+ .class_type = AVRO_SCHEMA,
+ .refcount = 1
+ };
+ return &obj;
+}
+
+avro_schema_t avro_schema_boolean(void)
+{
+ static struct avro_obj_t obj = {
+ .type = AVRO_BOOLEAN,
+ .class_type = AVRO_SCHEMA,
+ .refcount = 1
+ };
+ return &obj;
+}
+
+avro_schema_t avro_schema_null(void)
+{
+ static struct avro_obj_t obj = {
+ .type = AVRO_NULL,
+ .class_type = AVRO_SCHEMA,
+ .refcount = 1
+ };
+ return &obj;
+}
+
+avro_schema_t avro_schema_fixed(const char *name, const int64_t size)
+{
+ struct avro_fixed_schema_t *fixed =
+ malloc(sizeof(struct avro_fixed_schema_t));
+ if (!fixed) {
+ return NULL;
+ }
+ if (!is_avro_id(name)) {
+ return NULL;
+ }
+ fixed->name = strdup(name);
+ fixed->size = size;
+ avro_schema_init(&fixed->obj, AVRO_FIXED);
+ return &fixed->obj;
+}
+
+avro_schema_t avro_schema_union(void)
+{
+ struct avro_union_schema_t *schema =
+ malloc(sizeof(struct avro_union_schema_t));
+ if (!schema) {
+ return NULL;
+ }
+ STAILQ_INIT(&schema->branches);
+ avro_schema_init(&schema->obj, AVRO_UNION);
+ return &schema->obj;
}
int
-avro_schema_union_append (const avro_schema_t union_schema,
- const avro_schema_t schema)
+avro_schema_union_append(const avro_schema_t union_schema,
+ const avro_schema_t schema)
{
- struct avro_union_schema_t *unionp;
- struct avro_union_branch_t *s;
+ struct avro_union_schema_t *unionp;
+ struct avro_union_branch_t *s;
- if (!union_schema || !schema || !is_avro_union (union_schema))
- {
- return EINVAL;
- }
- unionp = avro_schema_to_union (union_schema);
- s = malloc (sizeof (struct avro_union_branch_t));
- if (!s)
- {
- return ENOMEM;
- }
- s->schema = schema;
- STAILQ_INSERT_TAIL (&unionp->branches, s, branches);
- return 0;
-}
-
-avro_schema_t
-avro_schema_array (const avro_schema_t items)
-{
- struct avro_array_schema_t *array =
- malloc (sizeof (struct avro_array_schema_t));
- if (!array)
- {
- return NULL;
- }
- array->items = avro_schema_incref (items);
- avro_schema_init (&array->obj, AVRO_ARRAY);
- return &array->obj;
-}
-
-avro_schema_t
-avro_schema_map (const avro_schema_t values)
-{
- struct avro_map_schema_t *map = malloc (sizeof (struct avro_map_schema_t));
- if (!map)
- {
- return NULL;
- }
- map->values = avro_schema_incref (values);
- avro_schema_init (&map->obj, AVRO_MAP);
- return &map->obj;
-}
-
-avro_schema_t
-avro_schema_enum (const char *name)
-{
- struct avro_enum_schema_t *enump =
- malloc (sizeof (struct avro_enum_schema_t));
- if (!enump)
- {
- return NULL;
- }
- if (!is_avro_id (name))
- {
- return NULL;
- }
- enump->name = strdup (name);
- if (!enump->name)
- {
- return NULL;
- }
- STAILQ_INIT (&enump->symbols);
- avro_schema_init (&enump->obj, AVRO_ENUM);
- return &enump->obj;
+ if (!union_schema || !schema || !is_avro_union(union_schema)) {
+ return EINVAL;
+ }
+ unionp = avro_schema_to_union(union_schema);
+ s = malloc(sizeof(struct avro_union_branch_t));
+ if (!s) {
+ return ENOMEM;
+ }
+ s->schema = schema;
+ STAILQ_INSERT_TAIL(&unionp->branches, s, branches);
+ return 0;
+}
+
+avro_schema_t avro_schema_array(const avro_schema_t items)
+{
+ struct avro_array_schema_t *array =
+ malloc(sizeof(struct avro_array_schema_t));
+ if (!array) {
+ return NULL;
+ }
+ array->items = avro_schema_incref(items);
+ avro_schema_init(&array->obj, AVRO_ARRAY);
+ return &array->obj;
+}
+
+avro_schema_t avro_schema_map(const avro_schema_t values)
+{
+ struct avro_map_schema_t *map =
+ malloc(sizeof(struct avro_map_schema_t));
+ if (!map) {
+ return NULL;
+ }
+ map->values = avro_schema_incref(values);
+ avro_schema_init(&map->obj, AVRO_MAP);
+ return &map->obj;
+}
+
+avro_schema_t avro_schema_enum(const char *name)
+{
+ struct avro_enum_schema_t *enump =
+ malloc(sizeof(struct avro_enum_schema_t));
+ if (!enump) {
+ return NULL;
+ }
+ if (!is_avro_id(name)) {
+ return NULL;
+ }
+ enump->name = strdup(name);
+ if (!enump->name) {
+ return NULL;
+ }
+ STAILQ_INIT(&enump->symbols);
+ avro_schema_init(&enump->obj, AVRO_ENUM);
+ return &enump->obj;
}
int
-avro_schema_enum_symbol_append (const avro_schema_t enum_schema,
- const char *symbol)
+avro_schema_enum_symbol_append(const avro_schema_t enum_schema,
+ const char *symbol)
{
- struct avro_enum_schema_t *enump;
- struct avro_enum_symbol_t *enum_symbol;
- if (!enum_schema || !symbol || !is_avro_enum (enum_schema))
- {
- return EINVAL;
- }
- enump = avro_schema_to_enum (enum_schema);
- enum_symbol = malloc (sizeof (struct avro_enum_symbol_t));
- if (!enum_symbol)
- {
- return ENOMEM;
- }
- enum_symbol->symbol = strdup (symbol);
- STAILQ_INSERT_TAIL (&enump->symbols, enum_symbol, symbols);
- return 0;
+ struct avro_enum_schema_t *enump;
+ struct avro_enum_symbol_t *enum_symbol;
+ if (!enum_schema || !symbol || !is_avro_enum(enum_schema)) {
+ return EINVAL;
+ }
+ enump = avro_schema_to_enum(enum_schema);
+ enum_symbol = malloc(sizeof(struct avro_enum_symbol_t));
+ if (!enum_symbol) {
+ return ENOMEM;
+ }
+ enum_symbol->symbol = strdup(symbol);
+ STAILQ_INSERT_TAIL(&enump->symbols, enum_symbol, symbols);
+ return 0;
}
int
-avro_schema_record_field_append (const avro_schema_t record_schema,
- const char *field_name,
- const avro_schema_t field_schema)
-{
- struct avro_record_schema_t *record;
- struct avro_record_field_t *new_field;
- if (!record_schema || !field_name || !field_schema
- || !is_avro_record (record_schema) || record_schema == field_schema
- || !is_avro_id (field_name))
- {
- return EINVAL;
- }
- record = avro_schema_to_record (record_schema);
- new_field = malloc (sizeof (struct avro_record_field_t));
- if (!new_field)
- {
- return ENOMEM;
- }
- new_field->name = strdup (field_name);
- new_field->type = avro_schema_incref (field_schema);
- STAILQ_INSERT_TAIL (&record->fields, new_field, fields);
- return 0;
-}
-
-avro_schema_t
-avro_schema_record (const char *name)
-{
- struct avro_record_schema_t *record =
- malloc (sizeof (struct avro_record_schema_t));
- if (!record)
- {
- return NULL;
- }
- if (!is_avro_id (name))
- {
- return NULL;
- }
- record->name = strdup (name);
- STAILQ_INIT (&record->fields);
- avro_schema_init (&record->obj, AVRO_RECORD);
- return &record->obj;
+avro_schema_record_field_append(const avro_schema_t record_schema,
+ const char *field_name,
+ const avro_schema_t field_schema)
+{
+ struct avro_record_schema_t *record;
+ struct avro_record_field_t *new_field;
+ if (!record_schema || !field_name || !field_schema
+ || !is_avro_record(record_schema) || record_schema == field_schema
+ || !is_avro_id(field_name)) {
+ return EINVAL;
+ }
+ record = avro_schema_to_record(record_schema);
+ new_field = malloc(sizeof(struct avro_record_field_t));
+ if (!new_field) {
+ return ENOMEM;
+ }
+ new_field->name = strdup(field_name);
+ new_field->type = avro_schema_incref(field_schema);
+ STAILQ_INSERT_TAIL(&record->fields, new_field, fields);
+ return 0;
+}
+
+avro_schema_t avro_schema_record(const char *name)
+{
+ struct avro_record_schema_t *record =
+ malloc(sizeof(struct avro_record_schema_t));
+ if (!record) {
+ return NULL;
+ }
+ if (!is_avro_id(name)) {
+ return NULL;
+ }
+ record->name = strdup(name);
+ STAILQ_INIT(&record->fields);
+ avro_schema_init(&record->obj, AVRO_RECORD);
+ return &record->obj;
}
static int
-save_named_schemas (const char *name, avro_schema_t schema,
- avro_schema_error_t * error)
+save_named_schemas(const char *name, avro_schema_t schema,
+ avro_schema_error_t * error)
{
- st_table *st = (*error)->named_schemas;
- return st_insert (st, (st_data_t) name, (st_data_t) schema);
+ st_table *st = (*error)->named_schemas;
+ return st_insert(st, (st_data_t) name, (st_data_t) schema);
}
static avro_schema_t
-find_named_schemas (const char *name, avro_schema_error_t * error)
+find_named_schemas(const char *name, avro_schema_error_t * error)
{
- st_table *st = (*error)->named_schemas;
- avro_schema_t schema;
- if (st_lookup (st, (st_data_t) name, (st_data_t *) & schema))
- {
- return schema;
- }
- return NULL;
+ st_table *st = (*error)->named_schemas;
+ avro_schema_t schema;
+ if (st_lookup(st, (st_data_t) name, (st_data_t *) & schema)) {
+ return schema;
+ }
+ return NULL;
};
-avro_schema_t
-avro_schema_link (avro_schema_t to)
+avro_schema_t avro_schema_link(avro_schema_t to)
{
- struct avro_link_schema_t *link;
- if (!is_avro_named_type (to))
- {
- return NULL;
- }
- link = malloc (sizeof (struct avro_link_schema_t));
- if (!link)
- {
- return NULL;
- }
- link->to = avro_schema_incref (to);
- avro_schema_init (&link->obj, AVRO_LINK);
- return &link->obj;
-}
-
-static int
-avro_type_from_json_t (json_t * json, avro_type_t * type,
- avro_schema_error_t * error,
- avro_schema_t * named_type)
-{
- json_t *json_type;
- const char *type_str;
-
- if (json_is_array (json))
- {
- *type = AVRO_UNION;
- return 0;
- }
- else if (json_is_object (json))
- {
- json_type = json_object_get (json, "type");
- }
- else
- {
- json_type = json;
- }
- if (!json_is_string (json_type))
- {
- return EINVAL;
- }
- type_str = json_string_value (json_type);
- if (!type_str)
- {
- return EINVAL;
- }
- /* TODO: gperf/re2c this */
- if (strcmp (type_str, "string") == 0)
- {
- *type = AVRO_STRING;
- }
- else if (strcmp (type_str, "bytes") == 0)
- {
- *type = AVRO_BYTES;
- }
- else if (strcmp (type_str, "int") == 0)
- {
- *type = AVRO_INT;
- }
- else if (strcmp (type_str, "long") == 0)
- {
- *type = AVRO_LONG;
- }
- else if (strcmp (type_str, "float") == 0)
- {
- *type = AVRO_FLOAT;
- }
- else if (strcmp (type_str, "double") == 0)
- {
- *type = AVRO_DOUBLE;
- }
- else if (strcmp (type_str, "boolean") == 0)
- {
- *type = AVRO_BOOLEAN;
- }
- else if (strcmp (type_str, "null") == 0)
- {
- *type = AVRO_NULL;
- }
- else if (strcmp (type_str, "record") == 0)
- {
- *type = AVRO_RECORD;
- }
- else if (strcmp (type_str, "enum") == 0)
- {
- *type = AVRO_ENUM;
- }
- else if (strcmp (type_str, "array") == 0)
- {
- *type = AVRO_ARRAY;
- }
- else if (strcmp (type_str, "map") == 0)
- {
- *type = AVRO_MAP;
- }
- else if (strcmp (type_str, "fixed") == 0)
- {
- *type = AVRO_FIXED;
- }
- else if ((*named_type = find_named_schemas (type_str, error)))
- {
- *type = AVRO_LINK;
- }
- else
- {
- return EINVAL;
- }
- return 0;
+ struct avro_link_schema_t *link;
+ if (!is_avro_named_type(to)) {
+ return NULL;
+ }
+ link = malloc(sizeof(struct avro_link_schema_t));
+ if (!link) {
+ return NULL;
+ }
+ link->to = avro_schema_incref(to);
+ avro_schema_init(&link->obj, AVRO_LINK);
+ return &link->obj;
}
static int
-avro_schema_from_json_t (json_t * json, avro_schema_t * schema,
- avro_schema_error_t * error)
+avro_type_from_json_t(json_t * json, avro_type_t * type,
+ avro_schema_error_t * error, avro_schema_t * named_type)
{
- avro_type_t type = 0;
- unsigned int i;
- avro_schema_t named_schemas = NULL;
-
- if (avro_type_from_json_t (json, &type, error, &named_schemas))
- {
- return EINVAL;
- }
-
- switch (type)
- {
- case AVRO_LINK:
- *schema = avro_schema_link (named_schemas);
- break;
-
- case AVRO_STRING:
- *schema = avro_schema_string ();
- break;
-
- case AVRO_BYTES:
- *schema = avro_schema_bytes ();
- break;
-
- case AVRO_INT:
- *schema = avro_schema_int ();
- break;
-
- case AVRO_LONG:
- *schema = avro_schema_long ();
- break;
-
- case AVRO_FLOAT:
- *schema = avro_schema_float ();
- break;
-
- case AVRO_DOUBLE:
- *schema = avro_schema_double ();
- break;
-
- case AVRO_BOOLEAN:
- *schema = avro_schema_boolean ();
- break;
-
- case AVRO_NULL:
- *schema = avro_schema_null ();
- break;
-
- case AVRO_RECORD:
- {
- json_t *json_name = json_object_get (json, "name");
- json_t *json_fields = json_object_get (json, "fields");
- unsigned int num_fields;
- const char *record_name;
-
- if (!json_is_string (json_name))
- {
- return EINVAL;
- }
- if (!json_is_array (json_fields))
- {
- return EINVAL;
- }
- num_fields = json_array_size (json_fields);
- if (num_fields == 0)
- {
- return EINVAL;
- }
- record_name = json_string_value (json_name);
- if (!record_name)
- {
- return EINVAL;
- }
- *schema = avro_schema_record (record_name);
- if (save_named_schemas (record_name, *schema, error))
- {
- return ENOMEM;
- }
- for (i = 0; i < num_fields; i++)
- {
- json_t *json_field = json_array_get (json_fields, i);
- json_t *json_field_name;
- json_t *json_field_type;
- avro_schema_t json_field_type_schema;
- int field_rval;
-
- if (!json_is_object (json_field))
- {
- avro_schema_decref (*schema);
+ json_t *json_type;
+ const char *type_str;
+
+ if (json_is_array(json)) {
+ *type = AVRO_UNION;
+ return 0;
+ } else if (json_is_object(json)) {
+ json_type = json_object_get(json, "type");
+ } else {
+ json_type = json;
+ }
+ if (!json_is_string(json_type)) {
return EINVAL;
- }
- json_field_name = json_object_get (json_field, "name");
- if (!json_field_name)
- {
- avro_schema_decref (*schema);
+ }
+ type_str = json_string_value(json_type);
+ if (!type_str) {
return EINVAL;
- }
- json_field_type = json_object_get (json_field, "type");
- if (!json_field_type)
- {
- avro_schema_decref (*schema);
+ }
+ /*
+ * TODO: gperf/re2c this
+ */
+ if (strcmp(type_str, "string") == 0) {
+ *type = AVRO_STRING;
+ } else if (strcmp(type_str, "bytes") == 0) {
+ *type = AVRO_BYTES;
+ } else if (strcmp(type_str, "int") == 0) {
+ *type = AVRO_INT;
+ } else if (strcmp(type_str, "long") == 0) {
+ *type = AVRO_LONG;
+ } else if (strcmp(type_str, "float") == 0) {
+ *type = AVRO_FLOAT;
+ } else if (strcmp(type_str, "double") == 0) {
+ *type = AVRO_DOUBLE;
+ } else if (strcmp(type_str, "boolean") == 0) {
+ *type = AVRO_BOOLEAN;
+ } else if (strcmp(type_str, "null") == 0) {
+ *type = AVRO_NULL;
+ } else if (strcmp(type_str, "record") == 0) {
+ *type = AVRO_RECORD;
+ } else if (strcmp(type_str, "enum") == 0) {
+ *type = AVRO_ENUM;
+ } else if (strcmp(type_str, "array") == 0) {
+ *type = AVRO_ARRAY;
+ } else if (strcmp(type_str, "map") == 0) {
+ *type = AVRO_MAP;
+ } else if (strcmp(type_str, "fixed") == 0) {
+ *type = AVRO_FIXED;
+ } else if ((*named_type = find_named_schemas(type_str, error))) {
+ *type = AVRO_LINK;
+ } else {
return EINVAL;
- }
- field_rval =
- avro_schema_from_json_t (json_field_type,
- &json_field_type_schema, error);
- if (field_rval)
- {
- avro_schema_decref (*schema);
- return field_rval;
- }
- field_rval =
- avro_schema_record_field_append (*schema,
- json_string_value
- (json_field_name),
- json_field_type_schema);
- if (field_rval != 0)
- {
- avro_schema_decref (*schema);
- return field_rval;
- }
- }
- }
- break;
-
- case AVRO_ENUM:
- {
- json_t *json_name = json_object_get (json, "name");
- json_t *json_symbols = json_object_get (json, "symbols");
- const char *name;
- unsigned int num_symbols;
-
- if (!json_is_string (json_name))
- {
- return EINVAL;
- }
- if (!json_is_array (json_symbols))
- {
- return EINVAL;
- }
-
- name = json_string_value (json_name);
- if (!name)
- {
- return EINVAL;
- }
- num_symbols = json_array_size (json_symbols);
- if (num_symbols == 0)
- {
- return EINVAL;
- }
- *schema = avro_schema_enum (name);
- if (save_named_schemas (name, *schema, error))
- {
- return ENOMEM;
- }
- for (i = 0; i < num_symbols; i++)
- {
- int enum_rval;
- json_t *json_symbol = json_array_get (json_symbols, i);
- const char *symbol;
- if (!json_is_string (json_symbol))
- {
- avro_schema_decref (*schema);
+ }
+ return 0;
+}
+
+static int
+avro_schema_from_json_t(json_t * json, avro_schema_t * schema,
+ avro_schema_error_t * error)
+{
+ avro_type_t type = 0;
+ unsigned int i;
+ avro_schema_t named_schemas = NULL;
+
+ if (avro_type_from_json_t(json, &type, error, &named_schemas)) {
return EINVAL;
- }
- symbol = json_string_value (json_symbol);
- enum_rval = avro_schema_enum_symbol_append (*schema, symbol);
- if (enum_rval != 0)
- {
- avro_schema_decref (*schema);
- return enum_rval;
- }
- }
- }
- break;
-
- case AVRO_ARRAY:
- {
- int items_rval;
- json_t *json_items = json_object_get (json, "items");
- avro_schema_t items_schema;
- if (!json_items)
- {
- return EINVAL;
- }
- items_rval =
- avro_schema_from_json_t (json_items, &items_schema, error);
- if (items_rval)
- {
- return items_rval;
- }
- *schema = avro_schema_array (items_schema);
- }
- break;
-
- case AVRO_MAP:
- {
- int values_rval;
- json_t *json_values = json_object_get (json, "values");
- avro_schema_t values_schema;
-
- if (!json_values)
- {
- return EINVAL;
- }
- values_rval =
- avro_schema_from_json_t (json_values, &values_schema, error);
- if (values_rval)
- {
- return values_rval;
- }
- *schema = avro_schema_map (values_schema);
- }
- break;
-
- case AVRO_UNION:
- {
- unsigned int num_schemas = json_array_size (json);
- avro_schema_t s;
- if (num_schemas == 0)
- {
- return EINVAL;
- }
- *schema = avro_schema_union ();
- for (i = 0; i < num_schemas; i++)
- {
- int schema_rval;
- json_t *schema_json = json_array_get (json, i);
- if (!schema_json)
- {
+ }
+
+ switch (type) {
+ case AVRO_LINK:
+ *schema = avro_schema_link(named_schemas);
+ break;
+
+ case AVRO_STRING:
+ *schema = avro_schema_string();
+ break;
+
+ case AVRO_BYTES:
+ *schema = avro_schema_bytes();
+ break;
+
+ case AVRO_INT:
+ *schema = avro_schema_int();
+ break;
+
+ case AVRO_LONG:
+ *schema = avro_schema_long();
+ break;
+
+ case AVRO_FLOAT:
+ *schema = avro_schema_float();
+ break;
+
+ case AVRO_DOUBLE:
+ *schema = avro_schema_double();
+ break;
+
+ case AVRO_BOOLEAN:
+ *schema = avro_schema_boolean();
+ break;
+
+ case AVRO_NULL:
+ *schema = avro_schema_null();
+ break;
+
+ case AVRO_RECORD:
+ {
+ json_t *json_name = json_object_get(json, "name");
+ json_t *json_fields = json_object_get(json, "fields");
+ unsigned int num_fields;
+ const char *record_name;
+
+ if (!json_is_string(json_name)) {
+ return EINVAL;
+ }
+ if (!json_is_array(json_fields)) {
+ return EINVAL;
+ }
+ num_fields = json_array_size(json_fields);
+ if (num_fields == 0) {
+ return EINVAL;
+ }
+ record_name = json_string_value(json_name);
+ if (!record_name) {
+ return EINVAL;
+ }
+ *schema = avro_schema_record(record_name);
+ if (save_named_schemas(record_name, *schema, error)) {
+ return ENOMEM;
+ }
+ for (i = 0; i < num_fields; i++) {
+ json_t *json_field =
+ json_array_get(json_fields, i);
+ json_t *json_field_name;
+ json_t *json_field_type;
+ avro_schema_t json_field_type_schema;
+ int field_rval;
+
+ if (!json_is_object(json_field)) {
+ avro_schema_decref(*schema);
+ return EINVAL;
+ }
+ json_field_name =
+ json_object_get(json_field, "name");
+ if (!json_field_name) {
+ avro_schema_decref(*schema);
+ return EINVAL;
+ }
+ json_field_type =
+ json_object_get(json_field, "type");
+ if (!json_field_type) {
+ avro_schema_decref(*schema);
+ return EINVAL;
+ }
+ field_rval =
+ avro_schema_from_json_t(json_field_type,
+ &json_field_type_schema,
+ error);
+ if (field_rval) {
+ avro_schema_decref(*schema);
+ return field_rval;
+ }
+ field_rval =
+ avro_schema_record_field_append(*schema,
+ json_string_value
+ (json_field_name),
+ json_field_type_schema);
+ if (field_rval != 0) {
+ avro_schema_decref(*schema);
+ return field_rval;
+ }
+ }
+ }
+ break;
+
+ case AVRO_ENUM:
+ {
+ json_t *json_name = json_object_get(json, "name");
+ json_t *json_symbols = json_object_get(json, "symbols");
+ const char *name;
+ unsigned int num_symbols;
+
+ if (!json_is_string(json_name)) {
+ return EINVAL;
+ }
+ if (!json_is_array(json_symbols)) {
+ return EINVAL;
+ }
+
+ name = json_string_value(json_name);
+ if (!name) {
+ return EINVAL;
+ }
+ num_symbols = json_array_size(json_symbols);
+ if (num_symbols == 0) {
+ return EINVAL;
+ }
+ *schema = avro_schema_enum(name);
+ if (save_named_schemas(name, *schema, error)) {
+ return ENOMEM;
+ }
+ for (i = 0; i < num_symbols; i++) {
+ int enum_rval;
+ json_t *json_symbol =
+ json_array_get(json_symbols, i);
+ const char *symbol;
+ if (!json_is_string(json_symbol)) {
+ avro_schema_decref(*schema);
+ return EINVAL;
+ }
+ symbol = json_string_value(json_symbol);
+ enum_rval =
+ avro_schema_enum_symbol_append(*schema,
+ symbol);
+ if (enum_rval != 0) {
+ avro_schema_decref(*schema);
+ return enum_rval;
+ }
+ }
+ }
+ break;
+
+ case AVRO_ARRAY:
+ {
+ int items_rval;
+ json_t *json_items = json_object_get(json, "items");
+ avro_schema_t items_schema;
+ if (!json_items) {
+ return EINVAL;
+ }
+ items_rval =
+ avro_schema_from_json_t(json_items, &items_schema,
+ error);
+ if (items_rval) {
+ return items_rval;
+ }
+ *schema = avro_schema_array(items_schema);
+ }
+ break;
+
+ case AVRO_MAP:
+ {
+ int values_rval;
+ json_t *json_values = json_object_get(json, "values");
+ avro_schema_t values_schema;
+
+ if (!json_values) {
+ return EINVAL;
+ }
+ values_rval =
+ avro_schema_from_json_t(json_values, &values_schema,
+ error);
+ if (values_rval) {
+ return values_rval;
+ }
+ *schema = avro_schema_map(values_schema);
+ }
+ break;
+
+ case AVRO_UNION:
+ {
+ unsigned int num_schemas = json_array_size(json);
+ avro_schema_t s;
+ if (num_schemas == 0) {
+ return EINVAL;
+ }
+ *schema = avro_schema_union();
+ for (i = 0; i < num_schemas; i++) {
+ int schema_rval;
+ json_t *schema_json = json_array_get(json, i);
+ if (!schema_json) {
+ return EINVAL;
+ }
+ schema_rval =
+ avro_schema_from_json_t(schema_json, &s,
+ error);
+ if (schema_rval != 0) {
+ avro_schema_decref(*schema);
+ return schema_rval;
+ }
+ schema_rval =
+ avro_schema_union_append(*schema, s);
+ if (schema_rval != 0) {
+ avro_schema_decref(*schema);
+ return schema_rval;
+ }
+ }
+ }
+ break;
+
+ case AVRO_FIXED:
+ {
+ json_t *json_size = json_object_get(json, "size");
+ json_t *json_name = json_object_get(json, "name");
+ int size;
+ const char *name;
+ if (!json_is_integer(json_size)) {
+ return EINVAL;
+ }
+ if (!json_is_string(json_name)) {
+ return EINVAL;
+ }
+ size = json_integer_value(json_size);
+ name = json_string_value(json_name);
+ *schema = avro_schema_fixed(name, size);
+ if (save_named_schemas(name, *schema, error)) {
+ return ENOMEM;
+ }
+ }
+ break;
+
+ default:
return EINVAL;
- }
- schema_rval = avro_schema_from_json_t (schema_json, &s, error);
- if (schema_rval != 0)
- {
- avro_schema_decref (*schema);
- return schema_rval;
- }
- schema_rval = avro_schema_union_append (*schema, s);
- if (schema_rval != 0)
- {
- avro_schema_decref (*schema);
- return schema_rval;
- }
- }
- }
- break;
-
- case AVRO_FIXED:
- {
- json_t *json_size = json_object_get (json, "size");
- json_t *json_name = json_object_get (json, "name");
- int size;
- const char *name;
- if (!json_is_integer (json_size))
- {
- return EINVAL;
- }
- if (!json_is_string (json_name))
- {
- return EINVAL;
- }
- size = json_integer_value (json_size);
- name = json_string_value (json_name);
- *schema = avro_schema_fixed (name, size);
- if (save_named_schemas (name, *schema, error))
- {
- return ENOMEM;
- }
- }
- break;
-
- default:
- return EINVAL;
- }
- return 0;
+ }
+ return 0;
}
int
-avro_schema_from_json (const char *jsontext, const int32_t len,
- avro_schema_t * schema, avro_schema_error_t * e)
+avro_schema_from_json(const char *jsontext, const int32_t len,
+ avro_schema_t * schema, avro_schema_error_t * e)
{
- json_t *root;
- int rval = 0;
- struct avro_schema_error_t *error;
-
- if (!jsontext || !schema)
- {
- return EINVAL;
- }
-
- error = malloc (sizeof (struct avro_schema_error_t));
- if (!error)
- {
- return ENOMEM;
- }
- *e = error;
-
- error->named_schemas = st_init_strtable ();
- if (!error->named_schemas)
- {
- free (error);
- return ENOMEM;
- }
-
- root = json_loads (jsontext, &error->json_error);
- if (!root)
- {
- return EINVAL;
- }
-
- /*json_dumpf(root, stderr, 0); */
- rval = avro_schema_from_json_t (root, schema, e);
- json_decref (root);
- return rval;
-}
-
-avro_schema_t
-avro_schema_copy (avro_schema_t schema)
-{
- avro_schema_t new_schema = NULL;
- if (!schema)
- {
- return NULL;
- }
- switch (avro_typeof (schema))
- {
- case AVRO_STRING:
- case AVRO_BYTES:
- case AVRO_INT:
- case AVRO_LONG:
- case AVRO_FLOAT:
- case AVRO_DOUBLE:
- case AVRO_BOOLEAN:
- case AVRO_NULL:
- /* No need to copy primitives since they're static */
- new_schema = schema;
- break;
- case AVRO_RECORD:
- {
- struct avro_record_schema_t *record_schema =
- avro_schema_to_record (schema);
- struct avro_record_field_t *field;
- new_schema = avro_schema_record (record_schema->name);
- for (field = STAILQ_FIRST (&record_schema->fields);
- field != NULL; field = STAILQ_NEXT (field, fields))
- {
- avro_schema_t field_copy = avro_schema_copy (field->type);
- if (!field_copy)
- {
- avro_schema_decref (new_schema);
- return NULL;
- }
- if (avro_schema_record_field_append
- (new_schema, field->name, field_copy))
- {
- avro_schema_decref (new_schema);
- return NULL;
- }
- }
- }
- break;
- case AVRO_ENUM:
- {
- struct avro_enum_schema_t *enum_schema = avro_schema_to_enum (schema);
- new_schema = avro_schema_enum (enum_schema->name);
- struct avro_enum_symbol_t *enum_symbol;
- for (enum_symbol = STAILQ_FIRST (&enum_schema->symbols);
- enum_symbol != NULL;
- enum_symbol = STAILQ_NEXT (enum_symbol, symbols))
- {
- if (avro_schema_enum_symbol_append
- (new_schema, enum_symbol->symbol))
- {
- avro_schema_decref (new_schema);
- return NULL;
- }
- }
- }
- break;
- case AVRO_FIXED:
- {
- struct avro_fixed_schema_t *fixed_schema =
- avro_schema_to_fixed (schema);
- new_schema =
- avro_schema_fixed (fixed_schema->name, fixed_schema->size);
- }
- break;
- case AVRO_MAP:
- {
- struct avro_map_schema_t *map_schema = avro_schema_to_map (schema);
- avro_schema_t values_copy = avro_schema_copy (map_schema->values);
- if (!values_copy)
- {
- return NULL;
- }
- new_schema = avro_schema_map (values_copy);
- }
- break;
- case AVRO_ARRAY:
- {
- struct avro_array_schema_t *array_schema =
- avro_schema_to_array (schema);
- avro_schema_t items_copy = avro_schema_copy (array_schema->items);
- if (!items_copy)
- {
- return NULL;
- }
- new_schema = avro_schema_array (items_copy);
- }
- break;
- case AVRO_UNION:
- {
- struct avro_union_schema_t *union_schema =
- avro_schema_to_union (schema);
- struct avro_union_branch_t *s;
+ json_t *root;
+ int rval = 0;
+ struct avro_schema_error_t *error;
- new_schema = avro_schema_union ();
+ if (!jsontext || !schema) {
+ return EINVAL;
+ }
- for (s = STAILQ_FIRST (&union_schema->branches);
- s != NULL; s = STAILQ_NEXT (s, branches))
- {
- avro_schema_t schema_copy = avro_schema_copy (s->schema);
- if (avro_schema_union_append (new_schema, schema_copy))
- {
- avro_schema_decref (new_schema);
- return NULL;
- }
- }
- }
- break;
-
- case AVRO_LINK:
- {
- struct avro_link_schema_t *link_schema = avro_schema_to_link (schema);
- /* TODO: use an avro_schema_copy of to instead of pointing to the same reference */
- avro_schema_incref (link_schema->to);
- new_schema = avro_schema_link (link_schema->to);
- }
- break;
-
- default:
- return NULL;
- }
- return new_schema;
+ error = malloc(sizeof(struct avro_schema_error_t));
+ if (!error) {
+ return ENOMEM;
+ }
+ *e = error;
+
+ error->named_schemas = st_init_strtable();
+ if (!error->named_schemas) {
+ free(error);
+ return ENOMEM;
+ }
+
+ root = json_loads(jsontext, &error->json_error);
+ if (!root) {
+ return EINVAL;
+ }
+
+ /*
+ * json_dumpf(root, stderr, 0);
+ */
+ rval = avro_schema_from_json_t(root, schema, e);
+ json_decref(root);
+ return rval;
+}
+
+avro_schema_t avro_schema_copy(avro_schema_t schema)
+{
+ avro_schema_t new_schema = NULL;
+ if (!schema) {
+ return NULL;
+ }
+ switch (avro_typeof(schema)) {
+ case AVRO_STRING:
+ case AVRO_BYTES:
+ case AVRO_INT:
+ case AVRO_LONG:
+ case AVRO_FLOAT:
+ case AVRO_DOUBLE:
+ case AVRO_BOOLEAN:
+ case AVRO_NULL:
+ /*
+ * No need to copy primitives since they're static
+ */
+ new_schema = schema;
+ break;
+
+ case AVRO_RECORD:
+ {
+ struct avro_record_schema_t *record_schema =
+ avro_schema_to_record(schema);
+ struct avro_record_field_t *field;
+ new_schema = avro_schema_record(record_schema->name);
+ for (field = STAILQ_FIRST(&record_schema->fields);
+ field != NULL;
+ field = STAILQ_NEXT(field, fields)) {
+ avro_schema_t field_copy =
+ avro_schema_copy(field->type);
+ if (!field_copy) {
+ avro_schema_decref(new_schema);
+ return NULL;
+ }
+ if (avro_schema_record_field_append
+ (new_schema, field->name, field_copy)) {
+ avro_schema_decref(new_schema);
+ return NULL;
+ }
+ }
+ }
+ break;
+
+ case AVRO_ENUM:
+ {
+ struct avro_enum_schema_t *enum_schema =
+ avro_schema_to_enum(schema);
+ new_schema = avro_schema_enum(enum_schema->name);
+ struct avro_enum_symbol_t *enum_symbol;
+ for (enum_symbol = STAILQ_FIRST(&enum_schema->symbols);
+ enum_symbol != NULL;
+ enum_symbol = STAILQ_NEXT(enum_symbol, symbols)) {
+ if (avro_schema_enum_symbol_append
+ (new_schema, enum_symbol->symbol)) {
+ avro_schema_decref(new_schema);
+ return NULL;
+ }
+ }
+ }
+ break;
+
+ case AVRO_FIXED:
+ {
+ struct avro_fixed_schema_t *fixed_schema =
+ avro_schema_to_fixed(schema);
+ new_schema =
+ avro_schema_fixed(fixed_schema->name,
+ fixed_schema->size);
+ }
+ break;
+
+ case AVRO_MAP:
+ {
+ struct avro_map_schema_t *map_schema =
+ avro_schema_to_map(schema);
+ avro_schema_t values_copy =
+ avro_schema_copy(map_schema->values);
+ if (!values_copy) {
+ return NULL;
+ }
+ new_schema = avro_schema_map(values_copy);
+ }
+ break;
+
+ case AVRO_ARRAY:
+ {
+ struct avro_array_schema_t *array_schema =
+ avro_schema_to_array(schema);
+ avro_schema_t items_copy =
+ avro_schema_copy(array_schema->items);
+ if (!items_copy) {
+ return NULL;
+ }
+ new_schema = avro_schema_array(items_copy);
+ }
+ break;
+
+ case AVRO_UNION:
+ {
+ struct avro_union_schema_t *union_schema =
+ avro_schema_to_union(schema);
+ struct avro_union_branch_t *s;
+
+ new_schema = avro_schema_union();
+
+ for (s = STAILQ_FIRST(&union_schema->branches);
+ s != NULL; s = STAILQ_NEXT(s, branches)) {
+ avro_schema_t schema_copy =
+ avro_schema_copy(s->schema);
+ if (avro_schema_union_append
+ (new_schema, schema_copy)) {
+ avro_schema_decref(new_schema);
+ return NULL;
+ }
+ }
+ }
+ break;
+
+ case AVRO_LINK:
+ {
+ struct avro_link_schema_t *link_schema =
+ avro_schema_to_link(schema);
+ /*
+ * TODO: use an avro_schema_copy of to instead of pointing to
+ * the same reference
+ */
+ avro_schema_incref(link_schema->to);
+ new_schema = avro_schema_link(link_schema->to);
+ }
+ break;
+
+ default:
+ return NULL;
+ }
+ return new_schema;
}
Modified: hadoop/avro/trunk/lang/c/src/schema.h
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/src/schema.h?rev=901421&r1=901420&r2=901421&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/schema.h (original)
+++ hadoop/avro/trunk/lang/c/src/schema.h Wed Jan 20 22:58:22 2010
@@ -1,21 +1,19 @@
/*
-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
-
-http://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.
-*/
+ * 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
+ *
+ * http://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.
+ */
#ifndef AVRO_SCHEMA_H
#define AVRO_SCHEMA_H
@@ -23,69 +21,61 @@
#include "container_of.h"
#include "queue.h"
-struct avro_record_field_t
-{
- char *name;
- avro_schema_t type;
- /* TODO: default values */
- STAILQ_ENTRY (avro_record_field_t) fields;
+struct avro_record_field_t {
+ char *name;
+ avro_schema_t type;
+ /*
+ * TODO: default values
+ */
+ STAILQ_ENTRY(avro_record_field_t) fields;
};
-struct avro_record_schema_t
-{
- struct avro_obj_t obj;
- char *name;
- STAILQ_HEAD (fields, avro_record_field_t) fields;
+struct avro_record_schema_t {
+ struct avro_obj_t obj;
+ char *name;
+ STAILQ_HEAD(fields, avro_record_field_t) fields;
};
-struct avro_enum_symbol_t
-{
- char *symbol;
- STAILQ_ENTRY (avro_enum_symbol_t) symbols;
+struct avro_enum_symbol_t {
+ char *symbol;
+ STAILQ_ENTRY(avro_enum_symbol_t) symbols;
};
-struct avro_enum_schema_t
-{
- struct avro_obj_t obj;
- char *name;
- STAILQ_HEAD (symbols, avro_enum_symbol_t) symbols;
+struct avro_enum_schema_t {
+ struct avro_obj_t obj;
+ char *name;
+ STAILQ_HEAD(symbols, avro_enum_symbol_t) symbols;
};
-struct avro_array_schema_t
-{
- struct avro_obj_t obj;
- avro_schema_t items;
+struct avro_array_schema_t {
+ struct avro_obj_t obj;
+ avro_schema_t items;
};
-struct avro_map_schema_t
-{
- struct avro_obj_t obj;
- avro_schema_t values;
+struct avro_map_schema_t {
+ struct avro_obj_t obj;
+ avro_schema_t values;
};
-struct avro_union_branch_t
-{
- avro_schema_t schema;
- STAILQ_ENTRY (avro_union_branch_t) branches;
+struct avro_union_branch_t {
+ avro_schema_t schema;
+ STAILQ_ENTRY(avro_union_branch_t) branches;
};
-struct avro_union_schema_t
-{
- struct avro_obj_t obj;
- STAILQ_HEAD (branch, avro_union_branch_t) branches;
+struct avro_union_schema_t {
+ struct avro_obj_t obj;
+ STAILQ_HEAD(branch, avro_union_branch_t) branches;
};
-struct avro_fixed_schema_t
-{
- struct avro_obj_t obj;
- const char *name;
- size_t size;
+struct avro_fixed_schema_t {
+ struct avro_obj_t obj;
+ const char *name;
+ size_t size;
};
-struct avro_link_schema_t
-{
- struct avro_obj_t obj;
- avro_schema_t to;
+struct avro_link_schema_t {
+ struct avro_obj_t obj;
+ avro_schema_t to;
};
#define avro_schema_to_record(schema_) (container_of(schema_, struct avro_record_schema_t, obj))
@@ -96,26 +86,22 @@
#define avro_schema_to_fixed(schema_) (container_of(schema_, struct avro_fixed_schema_t, obj))
#define avro_schema_to_link(schema_) (container_of(schema_, struct avro_link_schema_t, obj))
-static inline avro_schema_t
-avro_schema_incref (avro_schema_t schema)
+static inline avro_schema_t avro_schema_incref(avro_schema_t schema)
{
- if (schema && schema->refcount != (unsigned int) -1)
- {
- ++schema->refcount;
- }
- return schema;
+ if (schema && schema->refcount != (unsigned int)-1) {
+ ++schema->refcount;
+ }
+ return schema;
}
-void avro_schema_free (avro_schema_t schema);
+void avro_schema_free(avro_schema_t schema);
-static inline void
-avro_schema_decref (avro_schema_t schema)
+static inline void avro_schema_decref(avro_schema_t schema)
{
- if (schema && schema->refcount != (unsigned int) -1
- && --schema->refcount == 0)
- {
- avro_schema_free (schema);
- }
+ if (schema && schema->refcount != (unsigned int)-1
+ && --schema->refcount == 0) {
+ avro_schema_free(schema);
+ }
}
#endif
Modified: hadoop/avro/trunk/lang/c/src/schema_equal.c
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/src/schema_equal.c?rev=901421&r1=901420&r2=901421&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/schema_equal.c (original)
+++ hadoop/avro/trunk/lang/c/src/schema_equal.c Wed Jan 20 22:58:22 2010
@@ -1,200 +1,181 @@
/*
-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
-
-http://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.
-*/
+ * 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
+ *
+ * http://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 "schema.h"
#include <string.h>
static int
-schema_record_equal (struct avro_record_schema_t *a,
- struct avro_record_schema_t *b)
+schema_record_equal(struct avro_record_schema_t *a,
+ struct avro_record_schema_t *b)
{
- struct avro_record_field_t *field_a, *field_b;
- if (strcmp (a->name, b->name))
- {
- /* They have different names */
- return 0;
- }
-
- for (field_a = STAILQ_FIRST (&a->fields),
- field_b = STAILQ_FIRST (&b->fields);
- !(field_a == NULL && field_b == NULL);
- field_a = STAILQ_NEXT (field_a, fields),
- field_b = STAILQ_NEXT (field_b, fields))
- {
- if (field_a == NULL || field_b == NULL)
- {
- return 0; /* different num fields */
- }
- if (strcmp (field_a->name, field_b->name))
- {
- /* They have fields with different names */
- return 0;
- }
- if (!avro_schema_equal (field_a->type, field_b->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)
-{
- struct avro_enum_symbol_t *sym_a, *sym_b;
-
- if (strcmp (a->name, b->name))
- {
- /* They have different names */
- return 0;
- }
- for (sym_a = STAILQ_FIRST (&a->symbols),
- sym_b = STAILQ_FIRST (&b->symbols);
- !(sym_a == NULL && sym_b == NULL);
- sym_a = STAILQ_NEXT (sym_a, symbols),
- sym_b = STAILQ_NEXT (sym_b, symbols))
- {
- if (sym_a == NULL || sym_b == NULL)
- {
- return 0; /* different num symbols */
- }
- if (strcmp (sym_a->symbol, sym_b->symbol))
- {
- /* 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;
- }
- 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)
-{
- struct avro_union_branch_t *branch_a, *branch_b;
-
- for (branch_a = STAILQ_FIRST (&a->branches),
- branch_b = STAILQ_FIRST (&b->branches);
- !(branch_a == NULL && branch_b == NULL);
- branch_a = STAILQ_NEXT (branch_a, branches),
- branch_b = STAILQ_NEXT (branch_b, branches))
- {
- if (branch_a == NULL || branch_b == NULL)
- {
- return 0; /* different num symbols */
- }
- if (!avro_schema_equal (branch_a->schema, branch_b->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... */
- 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;
+ struct avro_record_field_t *field_a, *field_b;
+ if (strcmp(a->name, b->name)) {
+ /*
+ * They have different names
+ */
+ return 0;
+ }
+
+ for (field_a = STAILQ_FIRST(&a->fields),
+ field_b = STAILQ_FIRST(&b->fields);
+ !(field_a == NULL && field_b == NULL);
+ field_a = STAILQ_NEXT(field_a, fields),
+ field_b = STAILQ_NEXT(field_b, fields)) {
+ if (field_a == NULL || field_b == NULL) {
+ return 0; /* different num fields */
+ }
+ if (strcmp(field_a->name, field_b->name)) {
+ /*
+ * They have fields with different names
+ */
+ return 0;
+ }
+ if (!avro_schema_equal(field_a->type, field_b->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)
+{
+ struct avro_enum_symbol_t *sym_a, *sym_b;
+
+ if (strcmp(a->name, b->name)) {
+ /*
+ * They have different names
+ */
+ return 0;
+ }
+ for (sym_a = STAILQ_FIRST(&a->symbols),
+ sym_b = STAILQ_FIRST(&b->symbols);
+ !(sym_a == NULL && sym_b == NULL);
+ sym_a = STAILQ_NEXT(sym_a, symbols),
+ sym_b = STAILQ_NEXT(sym_b, symbols)) {
+ if (sym_a == NULL || sym_b == NULL) {
+ return 0; /* different num symbols */
+ }
+ if (strcmp(sym_a->symbol, sym_b->symbol)) {
+ /*
+ * 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;
+ }
+ 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)
+{
+ struct avro_union_branch_t *branch_a, *branch_b;
+
+ for (branch_a = STAILQ_FIRST(&a->branches),
+ branch_b = STAILQ_FIRST(&b->branches);
+ !(branch_a == NULL && branch_b == NULL);
+ branch_a = STAILQ_NEXT(branch_a, branches),
+ branch_b = STAILQ_NEXT(branch_b, branches)) {
+ if (branch_a == NULL || branch_b == NULL) {
+ return 0; /* different num symbols */
+ }
+ if (!avro_schema_equal(branch_a->schema, branch_b->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...
+ */
+ 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;
}