Skip to content

Commit

Permalink
initial code
Browse files Browse the repository at this point in the history
Signed-off-by: radai-rosenblatt <radai.rosenblatt@gmail.com>
  • Loading branch information
radai-rosenblatt committed Mar 15, 2019
0 parents commit 659f0f4
Show file tree
Hide file tree
Showing 73 changed files with 5,275 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#gradle artifacts
.gradle/
build/

#IDEA artifacts
out/
.idea/
*.iml
14 changes: 14 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Contribution Agreement
======================

As a contributor, you represent that the code you submit is your
original work or that of your employer (in which case you represent you
have the right to bind your employer). By submitting code, you (and, if
applicable, your employer) are licensing the submitted code to LinkedIn
and the open source community subject to the BSD 2-Clause license.

Responsible Disclosure of Security Vulnerabilities
==================================================

Please do not file reports on Github for security issues.
Please review the guidelines at https://www.linkedin.com/help/linkedin/answer/62924/security-vulnerabilities?lang=en
28 changes: 28 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
BSD 2-CLAUSE LICENSE

Copyright 2018 LinkedIn Corporation.
All Rights Reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the
distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5 changes: 5 additions & 0 deletions NOTICE
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Copyright 2018 LinkedIn Corporation
All Rights Reserved.

Licensed under the BSD 2-Clause License (the "License").
See License in the project root for license information.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Avro-Util
=========

A collection of utilities and helper code to allow java projects to work
across a wide version of avro versions

### Background ###

Apache Avro is a widely used serialization format.

Unfortunately it is not always possible to write java code that "just works"
across multiple versions of avro, as there have been breaking changes to both
the API and wire format.

This project provides utility code to enable java developers to write code
that is compatible across a wide range of avro versions
36 changes: 36 additions & 0 deletions avro-migration-helper/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2018 LinkedIn Corp.
* Licensed under the BSD 2-Clause License (the "License").

* See License in the project root for license information.
*/

apply plugin: 'java-library'

dependencies {

compileOnly ('org.apache.avro:avro:1.4.1') {
exclude group: "org.mortbay.jetty"
exclude group: "org.apache.velocity"
exclude group: "commons-lang"
exclude group: "org.jboss.netty"
exclude group: "com.thoughtworks.paranamer", module: "paranamer-ant"
}
//when run with #avro17
//compileOnly 'org.apache.avro:avro-compiler:1.7.7'

compile 'org.json:json:20170516'

testCompile ('org.apache.avro:avro:1.4.1') {
exclude group: "org.mortbay.jetty"
exclude group: "org.apache.velocity"
exclude group: "commons-lang"
exclude group: "org.jboss.netty"
exclude group: "com.thoughtworks.paranamer", module: "paranamer-ant"
}
//when run with #avro17
//compileOnly 'org.apache.avro:avro-compiler:1.7.7'
testCompile 'org.testng:testng:6.14.3'
testCompile 'commons-io:commons-io:2.6'
testCompile 'org.skyscreamer:jsonassert:1.5.0'
testCompile 'net.openhft:compiler:2.3.1'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright 2018 LinkedIn Corp.
* Licensed under the BSD 2-Clause License (the "License").

* See License in the project root for license information.
*/

package com.linkedin.avro.compatibility;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.avro.Schema;


//package private ON PURPOSE
abstract class AbstractAvroFactory implements AvroFactory {
//common fields
protected final Constructor _enumSymbolCtr;
protected final Constructor _fixedCtr;

//common compiler fields
protected Constructor _specificCompilerCtr;
protected Method _compilerEnqueueMethod;
protected Method _compilerCompileMethod;
protected Field _outputFilePathField;
protected Field _outputFileContentsField;

public AbstractAvroFactory(Constructor enumSymbolCtr, Constructor fixedCtr) throws Exception {
_enumSymbolCtr = enumSymbolCtr;
_fixedCtr = fixedCtr;
}

@Override
public Collection<AvroGeneratedSourceCode> compile(Collection<Schema> toCompile, AvroVersion compatibilityTarget) {
if (toCompile == null || toCompile.isEmpty()) {
return Collections.emptyList();
}
Iterator<Schema> schemaIter = toCompile.iterator();
Schema first = schemaIter.next();
try {
Object compilerInstance = getCompilerInstance(first, compatibilityTarget);

while (schemaIter.hasNext()) {
_compilerEnqueueMethod.invoke(compilerInstance, schemaIter.next());
}
Collection<?> outputFiles = (Collection<?>) _compilerCompileMethod.invoke(compilerInstance);
List<AvroGeneratedSourceCode> translated = outputFiles.stream()
.map(o -> new AvroGeneratedSourceCode(getPath(o), getContents(o)))
.collect(Collectors.toList());

return transform(translated, compatibilityTarget);
} catch (UnsupportedOperationException e) {
throw e; //as-is
} catch (Exception e) {
throw new IllegalStateException(e);
}
}

protected Object getCompilerInstance(Schema firstSchema, AvroVersion compatibilityTarget) throws Exception {
return _specificCompilerCtr.newInstance(firstSchema);
}

protected List<AvroGeneratedSourceCode> transform(List<AvroGeneratedSourceCode> avroCodegenOutput, AvroVersion compatibilityLevel) {
return avroCodegenOutput; //nop
}

protected String getPath(Object shouldBeOutputFile) {
try {
return (String) _outputFilePathField.get(shouldBeOutputFile);
} catch (Exception e) {
throw new IllegalStateException("cant extract path from avro OutputFile", e);
}
}

protected String getContents(Object shouldBeOutputFile) {
try {
return (String) _outputFileContentsField.get(shouldBeOutputFile);
} catch (Exception e) {
throw new IllegalStateException("cant extract contents from avro OutputFile", e);
}
}
}
Loading

0 comments on commit 659f0f4

Please sign in to comment.