-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding files that were not previously added to the repository (but
required)
- Loading branch information
Showing
4 changed files
with
274 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/////////////////////////////////////////////////////////////////////////////////////// | ||
// Copyright (C) 2006-2024 Esper Team. All rights reserved. / | ||
// http://esper.codehaus.org / | ||
// ---------------------------------------------------------------------------------- / | ||
// The software in this package is published under the terms of the GPL license / | ||
// a copy of which has been included with this distribution in the license.txt file. / | ||
/////////////////////////////////////////////////////////////////////////////////////// | ||
|
||
using Avro.Generic; | ||
|
||
namespace NEsper.Avro.Support | ||
{ | ||
public class SupportAvroArrayEvent | ||
{ | ||
public SupportAvroArrayEvent(GenericRecord[] someAvroArray) | ||
{ | ||
SomeAvroArray = someAvroArray; | ||
} | ||
|
||
public GenericRecord[] SomeAvroArray { get; } | ||
} | ||
} // end of namespace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/////////////////////////////////////////////////////////////////////////////////////// | ||
// Copyright (C) 2006-2024 Esper Team. All rights reserved. / | ||
// http://esper.codehaus.org / | ||
// ---------------------------------------------------------------------------------- / | ||
// The software in this package is published under the terms of the GPL license / | ||
// a copy of which has been included with this distribution in the license.txt file. / | ||
/////////////////////////////////////////////////////////////////////////////////////// | ||
|
||
using System.Collections.Generic; | ||
using System.IO; | ||
|
||
using Avro; | ||
using Avro.Generic; | ||
using Avro.IO; | ||
|
||
using com.espertech.esper.common.client; | ||
using com.espertech.esper.common.client.meta; | ||
using com.espertech.esper.common.client.util; | ||
using com.espertech.esper.common.@internal.epl.expression.ops; | ||
using com.espertech.esper.common.@internal.@event.avro; | ||
using com.espertech.esper.compat; | ||
|
||
using NEsper.Avro.Core; | ||
using NEsper.Avro.Extensions; | ||
using NEsper.Avro.IO; | ||
|
||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace NEsper.Avro.Support | ||
{ | ||
public class SupportAvroUtil | ||
{ | ||
public static string AvroToJson(EventBean theEvent) | ||
{ | ||
Schema schema = (Schema)((AvroSchemaEventType)theEvent.EventType).Schema; | ||
GenericRecord record = (GenericRecord)theEvent.Underlying; | ||
return AvroToJsonX(schema, record); | ||
} | ||
|
||
public static string AvroToJson( | ||
Schema schema, | ||
GenericRecord datum) | ||
{ | ||
var writer = new GenericDatumWriter<object>(schema); | ||
var textWriter = new StringWriter(); | ||
var encoder = new Avro.IO.JsonEncoder(textWriter); | ||
writer.Write(datum, encoder); | ||
return writer.ToString(); | ||
} | ||
|
||
public static string AvroToJsonX( | ||
Schema schema, | ||
GenericRecord datum) | ||
{ | ||
var converter = new GenericRecordToJsonConverter(); | ||
var encodedResult = converter.Encode(datum); | ||
return JsonConvert.SerializeObject(encodedResult, Formatting.Indented); | ||
} | ||
|
||
public static GenericRecord ParseQuoted( | ||
Schema schema, | ||
string json) | ||
{ | ||
return Parse(schema, json.Replace("'", "\"")); | ||
} | ||
|
||
public static GenericRecord Parse( | ||
Schema schema, | ||
string json) | ||
{ | ||
var input = new MemoryStream(json.GetUTF8Bytes()); | ||
try { | ||
Decoder decoder = new BinaryDecoder(input); | ||
DatumReader<object> reader = new GenericDatumReader<object>(schema, schema); | ||
return (GenericRecord)reader.Read(null, decoder); | ||
} | ||
catch (IOException ex) { | ||
throw new EPRuntimeException("Failed to parse json: " + ex.Message, ex); | ||
} | ||
} | ||
|
||
public static string CompareSchemas( | ||
Schema schemaOne, | ||
Schema schemaTwo) | ||
{ | ||
ISet<string> names = new HashSet<string>(); | ||
AddSchemaFieldNames(names, schemaOne); | ||
AddSchemaFieldNames(names, schemaTwo); | ||
|
||
foreach (string name in names) { | ||
var fieldOne = schemaOne.GetField(name); | ||
var fieldTwo = schemaTwo.GetField(name); | ||
if (fieldOne == null) { | ||
return "Failed to find field '" + name + " in schema-one"; | ||
} | ||
|
||
if (fieldTwo == null) { | ||
return "Failed to find field '" + name + " in schema-one"; | ||
} | ||
|
||
var fieldOneSchema = fieldOne.Schema; | ||
var fieldTwoSchema = fieldTwo.Schema; | ||
if (!Equals(fieldOneSchema, fieldTwoSchema)) { | ||
return "\nSchema-One: " + | ||
fieldOneSchema + | ||
"\n" + | ||
"Schema-Two: " + | ||
fieldTwoSchema; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static AvroEventType MakeAvroSupportEventType(Schema schema) | ||
{ | ||
EventTypeMetadata metadata = new EventTypeMetadata( | ||
"typename", | ||
null, | ||
EventTypeTypeClass.STREAM, | ||
EventTypeApplicationType.AVRO, | ||
NameAccessModifier.TRANSIENT, | ||
EventTypeBusModifier.NONBUS, | ||
false, | ||
EventTypeIdPair.Unassigned()); | ||
return new AvroEventType(metadata, schema, null, null, null, null, null, new EventTypeAvroHandlerImpl()); | ||
} | ||
|
||
private static void AddSchemaFieldNames( | ||
ISet<string> names, | ||
Schema schema) | ||
{ | ||
foreach (var field in schema.AsRecordSchema().Fields) { | ||
names.Add(field.Name); | ||
} | ||
} | ||
|
||
public static Schema GetAvroSchema(EventBean @event) | ||
{ | ||
return GetAvroSchema(@event.EventType); | ||
} | ||
|
||
public static Schema GetAvroSchema(EventType eventType) | ||
{ | ||
return ((AvroEventType)eventType).SchemaAvro; | ||
} | ||
} | ||
} // end of namespace |
103 changes: 103 additions & 0 deletions
103
src/NEsper.Compiler/internal/compiler/abstraction/CompilerAbstractionRoslyn.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/////////////////////////////////////////////////////////////////////////////////////// | ||
// Copyright (C) 2006-2024 Esper Team. All rights reserved. / | ||
// http://esper.codehaus.org / | ||
// ---------------------------------------------------------------------------------- / | ||
// The software in this package is published under the terms of the GPL license / | ||
// a copy of which has been included with this distribution in the license.txt file. / | ||
/////////////////////////////////////////////////////////////////////////////////////// | ||
|
||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
using com.espertech.esper.common.client.artifact; | ||
using com.espertech.esper.common.@internal.bytecodemodel.core; | ||
using com.espertech.esper.common.@internal.compile.compiler; | ||
using com.espertech.esper.compat.collections; | ||
using com.espertech.esper.compiler.@internal.util; | ||
|
||
namespace com.espertech.esper.compiler.@internal.compiler.abstraction | ||
{ | ||
public class CompilerAbstractionRoslyn : CompilerAbstraction | ||
{ | ||
public static readonly CompilerAbstractionRoslyn INSTANCE = new CompilerAbstractionRoslyn(); | ||
|
||
private CompilerAbstractionRoslyn() | ||
{ | ||
} | ||
|
||
public CompilerAbstractionArtifactCollection NewArtifactCollection() | ||
{ | ||
return new CompilerAbstractionArtifactCollectionImpl(); | ||
} | ||
|
||
public ICompileArtifact CompileClasses( | ||
IList<CodegenClass> classes, | ||
CompilerAbstractionCompilationContext context, | ||
CompilerAbstractionArtifactCollection state) | ||
{ | ||
var sourceList = classes | ||
.Select(clazz => new RoslynCompiler.SourceCodegen(clazz)) | ||
.Cast<RoslynCompiler.Source>() | ||
.ToList(); | ||
|
||
var container = context.Container; | ||
var configuration = context.Services.Configuration.Compiler; | ||
var repository = container.ArtifactRepositoryManager().DefaultRepository; | ||
var compiler = container | ||
.RoslynCompiler() | ||
.WithMetaDataReferences(repository.AllMetadataReferences) | ||
.WithMetaDataReferences(container.MetadataReferenceProvider()?.Invoke()) | ||
.WithDebugOptimization(configuration.IsDebugOptimization) | ||
.WithCodeLogging(configuration.Logging.IsEnableCode) | ||
.WithCodeAuditDirectory(configuration.Logging.AuditDirectory) | ||
.WithSources(sourceList); | ||
|
||
return repository.Register(compiler.Compile()); | ||
} | ||
|
||
public CompilerAbstractionCompileSourcesResult CompileSources( | ||
IList<string> sources, | ||
CompilerAbstractionCompilationContext context, | ||
CompilerAbstractionArtifactCollection state) | ||
{ | ||
string Filename(int ii) | ||
{ | ||
return "provided_" + ii + "_" + CodeGenerationIDGenerator.GenerateClassNameUUID(); | ||
} | ||
|
||
var names = new LinkedHashMap<string, IList<string>>(); | ||
var sourceList = sources | ||
.Select((_, index) => new RoslynCompiler.SourceBasic(Filename(index), _)) | ||
.Cast<RoslynCompiler.Source>() | ||
.ToList(); | ||
|
||
var container = context.Container; | ||
var configuration = context.Services.Configuration.Compiler; | ||
var repository = container.ArtifactRepositoryManager().DefaultRepository; | ||
var compiler = container | ||
.RoslynCompiler() | ||
.WithMetaDataReferences(repository.AllMetadataReferences) | ||
.WithMetaDataReferences(container.MetadataReferenceProvider()?.Invoke()) | ||
.WithDebugOptimization(configuration.IsDebugOptimization) | ||
.WithCodeLogging(configuration.Logging.IsEnableCode) | ||
.WithCodeAuditDirectory(configuration.Logging.AuditDirectory) | ||
.WithSources(sourceList); | ||
|
||
var artifact = repository.Register(compiler.Compile()); | ||
state.Add(new IArtifact[] { artifact }); | ||
|
||
// invoke the compile result consumer if one has been provided | ||
context.CompileResultConsumer?.Invoke(artifact); | ||
|
||
// JaninoCompiler.Compile( | ||
// classText, | ||
// filename, | ||
// state.Classes, | ||
// output, | ||
// context.CompileResultConsumer, | ||
// context.Services); | ||
|
||
return new CompilerAbstractionCompileSourcesResult(names, artifact); | ||
} | ||
} | ||
} // end of namespace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters