avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dcrea...@apache.org
Subject svn commit: r1351497 - in /avro/trunk: CHANGES.txt lang/c/src/CMakeLists.txt lang/c/src/avroappend.c
Date Mon, 18 Jun 2012 21:40:26 GMT
Author: dcreager
Date: Mon Jun 18 21:40:26 2012
New Revision: 1351497

URL: http://svn.apache.org/viewvc?rev=1351497&view=rev
Log:
AVRO-1104. C: avroappend utility

Contributed by Lucas Martin-King.

Added:
    avro/trunk/lang/c/src/avroappend.c
Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/c/src/CMakeLists.txt

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1351497&r1=1351496&r2=1351497&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Mon Jun 18 21:40:26 2012
@@ -13,6 +13,8 @@ Avro 1.7.1 (unreleased)
     AVRO-1108. Java: Add support for reflect API to newer mapreduce API.
     (cutting)
 
+    AVRO-1104. C: avroappend utility. (Lucas Martin-King via dcreager)
+
   IMPROVEMENTS
 
   BUG FIXES

Modified: avro/trunk/lang/c/src/CMakeLists.txt
URL: http://svn.apache.org/viewvc/avro/trunk/lang/c/src/CMakeLists.txt?rev=1351497&r1=1351496&r2=1351497&view=diff
==============================================================================
--- avro/trunk/lang/c/src/CMakeLists.txt (original)
+++ avro/trunk/lang/c/src/CMakeLists.txt Mon Jun 18 21:40:26 2012
@@ -148,6 +148,10 @@ add_executable(avrocat avrocat.c)
 target_link_libraries(avrocat avro-static)
 install(TARGETS avrocat RUNTIME DESTINATION bin)
 
+add_executable(avroappend avroappend.c)
+target_link_libraries(avroappend avro-static)
+install(TARGETS avroappend RUNTIME DESTINATION bin)
+
 if (NOT WIN32)
 #TODO: Port getopt() to Windows to compile avropipe.c and avromod.c
 add_executable(avropipe avropipe.c)

Added: avro/trunk/lang/c/src/avroappend.c
URL: http://svn.apache.org/viewvc/avro/trunk/lang/c/src/avroappend.c?rev=1351497&view=auto
==============================================================================
--- avro/trunk/lang/c/src/avroappend.c (added)
+++ avro/trunk/lang/c/src/avroappend.c Mon Jun 18 21:40:26 2012
@@ -0,0 +1,165 @@
+/*
+ * 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 <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "avro.h"
+
+int process_file(const char *in_filename, const char *out_filename)
+{
+	avro_file_reader_t  reader;
+	avro_file_writer_t  writer;
+
+	if (in_filename == NULL) {
+		if (avro_file_reader_fp(stdin, "<stdin>", 0, &reader)) {
+			fprintf(stderr, "Error opening <stdin>:\n  %s\n",
+				avro_strerror());
+			return 1;
+		}
+	} else {
+		if (avro_file_reader(in_filename, &reader)) {
+			fprintf(stderr, "Error opening %s:\n  %s\n",
+				in_filename, avro_strerror());
+			return 1;
+		}
+	}
+
+	avro_schema_t  wschema;
+	wschema = avro_file_reader_get_writer_schema(reader);
+
+	/* Check that the reader schema is the same as the writer schema */
+	{
+		avro_schema_t oschema;
+		avro_file_reader_t oreader;
+
+		if (avro_file_reader(out_filename, &oreader)) {
+			fprintf(stderr, "Error opening %s:\n   %s\n",
+					out_filename, avro_strerror());
+			avro_file_reader_close(reader);
+			return 1;
+		}
+
+		oschema = avro_file_reader_get_writer_schema(oreader);
+
+		if (avro_schema_equal(oschema, wschema) == 0) {
+			fprintf(stderr, "Error: reader and writer schema are not equal.\n");
+			avro_file_reader_close(oreader);
+			avro_file_reader_close(reader);
+			return 1;
+		}
+
+		avro_file_reader_close(oreader);
+	}
+
+	if (avro_file_writer_open(out_filename, &writer)) {
+		fprintf(stderr, "Error opening %s:\n   %s\n",
+				out_filename, avro_strerror());
+		avro_file_reader_close(reader);
+		return 1;
+	}
+
+	avro_value_iface_t  *iface;
+	avro_value_t  value;
+
+	iface = avro_generic_class_from_schema(wschema);
+	avro_generic_value_new(iface, &value);
+
+	while (avro_file_reader_read_value(reader, &value) == 0) {
+		if (avro_file_writer_append_value(writer, &value)) {
+			fprintf(stderr, "Error writing to %s:\n  %s\n",
+				out_filename, avro_strerror());
+			return 1;
+		}
+		avro_value_reset(&value);
+	}
+
+	avro_file_reader_close(reader);
+	avro_file_writer_close(writer);
+	avro_value_decref(&value);
+	avro_value_iface_decref(iface);
+
+	return 0;
+}
+
+static void usage(void)
+{
+	fprintf(stderr,
+		"Usage: avroappend [<input avro file>] <output avro file>\n");
+}
+
+static int check_filenames(const char *in_filename, const char *out_filename)
+{
+	if (in_filename == NULL) {
+		return 0;
+	}
+
+	struct stat in_stat;
+	struct stat out_stat;
+
+	if (stat(in_filename, &in_stat) == -1) {
+		fprintf(stderr, "stat error on %s: %s\n", in_filename, strerror(errno));
+		return 2;
+	}
+
+	if (stat(out_filename, &out_stat) == -1) {
+		fprintf(stderr, "stat error on %s: %s\n", out_filename, strerror(errno));
+		return 2;
+	}
+
+	if (in_stat.st_dev == out_stat.st_dev && in_stat.st_ino == out_stat.st_ino) {
+		return 1;
+	}
+
+	return 0;
+}
+
+int main(int argc, char **argv)
+{
+	char *in_filename;
+	char *out_filename;
+
+	argc--;
+	argv++;
+
+	if (argc == 2) {
+		in_filename = argv[0];
+		out_filename = argv[1];
+	} else if (argc == 1) {
+		in_filename = NULL;
+		out_filename = argv[0];
+	} else {
+		fprintf(stderr, "Not enough arguments\n\n");
+		usage();
+		exit(1);
+	}
+
+	int ret = check_filenames(in_filename, out_filename);
+
+	if (ret == 1) {
+		fprintf(stderr, "Files are the same.\n");
+	}
+
+	if (ret > 0) {
+		exit(1);
+	}
+
+	exit(process_file(in_filename, out_filename));
+}



Mime
View raw message