diff --git a/src/NEsper.Avro/Support/SupportAvroArrayEvent.cs b/src/NEsper.Avro/Support/SupportAvroArrayEvent.cs new file mode 100644 index 000000000..e2e0b4513 --- /dev/null +++ b/src/NEsper.Avro/Support/SupportAvroArrayEvent.cs @@ -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 \ No newline at end of file diff --git a/src/NEsper.Avro/Support/SupportAvroUtil.cs b/src/NEsper.Avro/Support/SupportAvroUtil.cs new file mode 100644 index 000000000..839a0d66f --- /dev/null +++ b/src/NEsper.Avro/Support/SupportAvroUtil.cs @@ -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(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 reader = new GenericDatumReader(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 names = new HashSet(); + 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 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 diff --git a/src/NEsper.Compiler/internal/compiler/abstraction/CompilerAbstractionRoslyn.cs b/src/NEsper.Compiler/internal/compiler/abstraction/CompilerAbstractionRoslyn.cs new file mode 100644 index 000000000..80fcceeb9 --- /dev/null +++ b/src/NEsper.Compiler/internal/compiler/abstraction/CompilerAbstractionRoslyn.cs @@ -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 classes, + CompilerAbstractionCompilationContext context, + CompilerAbstractionArtifactCollection state) + { + var sourceList = classes + .Select(clazz => new RoslynCompiler.SourceCodegen(clazz)) + .Cast() + .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 sources, + CompilerAbstractionCompilationContext context, + CompilerAbstractionArtifactCollection state) + { + string Filename(int ii) + { + return "provided_" + ii + "_" + CodeGenerationIDGenerator.GenerateClassNameUUID(); + } + + var names = new LinkedHashMap>(); + var sourceList = sources + .Select((_, index) => new RoslynCompiler.SourceBasic(Filename(index), _)) + .Cast() + .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 diff --git a/tst/NEsper.Regression/suite/epl/dataflow/EPLDataflowOpBeaconSource.cs b/tst/NEsper.Regression/suite/epl/dataflow/EPLDataflowOpBeaconSource.cs index 282307aec..1d91ce8d9 100644 --- a/tst/NEsper.Regression/suite/epl/dataflow/EPLDataflowOpBeaconSource.cs +++ b/tst/NEsper.Regression/suite/epl/dataflow/EPLDataflowOpBeaconSource.cs @@ -373,11 +373,7 @@ public void Run(RegressionEnvironment env) }); ClassicAssert.AreEqual(2, output.Length); -#if DEBUG ClassicAssert.Less(delta, 600); -#else - ClassicAssert.Less(delta, 490); -#endif env.UndeployAll(); // BeaconSource with period