Ejemplo n.º 1
0
 public static byte[] Serialize <T>(T thisObj) where T : ISpecificRecord
 {
     using (var ms = new MemoryStream())
     {
         var enc    = new BinaryEncoder(ms);
         var writer = new SpecificDefaultWriter(thisObj.Schema); // Schema comes from pre-compiled, code-gen phase
         writer.Write(thisObj, enc);
         return(ms.ToArray());
     }
 }
Ejemplo n.º 2
0
 public byte[] Serialize <T>(T payload) where T : ISpecificRecord
 {
     using (var ms = new MemoryStream())
     {
         var encoder = new BinaryEncoder(ms);
         var writer  = new SpecificDefaultWriter(Schema);
         writer.Write(Schema, payload, encoder);
         return(ms.ToArray());
     }
 }
Ejemplo n.º 3
0
        public byte[] SerializeAvro(InheritedEntityAvro thisObj)
        {
            using (var ms = new MemoryStream())
            {
                var enc    = new BinaryEncoder(ms);
                var writer = new SpecificDefaultWriter(InheritedEntityAvro._SCHEMA); // Schema comes from pre-compiled, code-gen phase
                writer.Write(thisObj, enc);

                return(ms.ToArray());
            }
        }
Ejemplo n.º 4
0
        public byte[] SerializeAvro(InheritedEntityAvro thisObj)
        {
            using (var ms = new MemoryStream())
            {
                var enc = new BinaryEncoder(ms);
                var writer = new SpecificDefaultWriter(InheritedEntityAvro._SCHEMA); // Schema comes from pre-compiled, code-gen phase
                writer.Write(thisObj, enc);

                return ms.ToArray();
            }
        }
Ejemplo n.º 5
0
        public override dynamic Serialize(object thisObj)
        {
            DoCopyIfNotCheating((T)thisObj);

            using (var ms = new MemoryStream())
            {
                var enc    = new BinaryEncoder(ms);
                var writer = new SpecificDefaultWriter(InheritedEntityAvro._SCHEMA); // Schema comes from pre-compiled, code-gen phase
                writer.Write(CodeGenObjSer, enc);

                SerBytes = ms.ToArray();
                return(SerBytes);
            }
        }
Ejemplo n.º 6
0
        /// <inheritdoc />
        public BaseMessage Serialize(T message)
        {
            var result = new BytesMessage();

            using (var ms = new MemoryStream())
            {
                var enc    = new BinaryEncoder(ms);
                var writer = new SpecificDefaultWriter(message.Schema); // Schema comes from pre-compiled, code-gen phase
                writer.Write(message, enc);
                result.Body = ms.ToArray();
            }

            return(result);
        }
Ejemplo n.º 7
0
        public byte[] Serialize(Type t, object message)
        {
            using (var ms = WriteMemoryStreamFactory())
            {
                var enc = new BinaryEncoder(ms);

                var writerSchema = WriteSchemaLookup(t);
                AssertSchemaNotNull(t, writerSchema, true);

                Log.DebugFormat(CultureInfo.InvariantCulture, "Type {0} writer schema: {1}", t, writerSchema);

                var writer = new SpecificDefaultWriter(writerSchema); // Schema comes from pre-compiled, code-gen phase
                writer.Write(message, enc);
                return(ms.ToArray());
            }
        }
Ejemplo n.º 8
0
    public async Task <int> PublishAvroMessagesAsync(string projectId, string topicId, IEnumerable <AvroUtilities.State> messageStates)
    {
        TopicName       topicName = TopicName.FromProjectTopic(projectId, topicId);
        PublisherClient publisher = await PublisherClient.CreateAsync(topicName);

        PublisherServiceApiClient publishApi = PublisherServiceApiClient.Create();
        var topic = publishApi.GetTopic(topicName);

        int publishedMessageCount = 0;
        var publishTasks          = messageStates.Select(async state =>
        {
            try
            {
                string messageId = null;
                switch (topic.SchemaSettings.Encoding)
                {
                case Encoding.Binary:
                    using (var ms = new MemoryStream())
                    {
                        var encoder = new BinaryEncoder(ms);
                        var writer  = new SpecificDefaultWriter(state.Schema);
                        writer.Write(state, encoder);
                        messageId = await publisher.PublishAsync(ms.ToArray());
                    }
                    break;

                case Encoding.Json:
                    var jsonMessage = AvroUtilities.StateUtils.StateToJsonString(state);
                    messageId       = await publisher.PublishAsync(jsonMessage);
                    break;
                }
                Console.WriteLine($"Published message {messageId}");
                Interlocked.Increment(ref publishedMessageCount);
            }
            catch (Exception exception)
            {
                Console.WriteLine($"An error ocurred when publishing message {state}: {exception.Message}");
            }
        });
        await Task.WhenAll(publishTasks);

        return(publishedMessageCount);
    }
Ejemplo n.º 9
0
        public static void TestSpecific(string str, object[] result)
        {
            Protocol protocol = Protocol.Parse(str);
            var      codegen  = new CodeGen();

            codegen.AddProtocol(protocol);
            var compileUnit = codegen.GenerateCode();

            // add a constructor to the main class using the passed assignment statements
            CodeTypeDeclaration ctd         = compileUnit.Namespaces[0].Types[(int)result[0]];
            CodeConstructor     constructor = new CodeConstructor();

            constructor.Attributes = MemberAttributes.Public;
            CodeSnippetExpression snippet = new CodeSnippetExpression((string)result[2]);

            constructor.Statements.Add(snippet);
            ctd.Members.Add(constructor);

            // add a function to the main class to populate the data
            // This has been moved from constructor, as it was causing some tests to pass that shouldn't when referencing a blank object on READ.

            CodeMemberMethod method = new CodeMemberMethod();

            method.Attributes = MemberAttributes.Public;
            method.Name       = "Populate";
            CodeSnippetExpression snippet2 = new CodeSnippetExpression((string)result[3]);

            method.Statements.Add(snippet2);
            ctd.Members.Add(method);



            // compile
            var comparam = new CompilerParameters(new string[] { "mscorlib.dll" });

            comparam.ReferencedAssemblies.Add("System.dll");
            comparam.ReferencedAssemblies.Add("Avro.dll");
            comparam.GenerateInMemory = true;
            var ccp     = new Microsoft.CSharp.CSharpCodeProvider();
            var units   = new CodeCompileUnit[] { compileUnit };
            var compres = ccp.CompileAssemblyFromDom(comparam, units);

            if (compres == null || compres.Errors.Count > 0)
            {
                for (int i = 0; i < compres.Errors.Count; i++)
                {
                    Console.WriteLine(compres.Errors[i]);
                }
            }
            if (null != compres)
            {
                Assert.IsTrue(compres.Errors.Count == 0);
            }

            // create record
            ISpecificRecord rec = compres.CompiledAssembly.CreateInstance((string)result[1]) as ISpecificRecord;

            // Call populate to put some data in it.
            Type       recType    = rec.GetType();;
            MethodInfo methodInfo = recType.GetMethod("Populate");

            methodInfo.Invoke(rec, null);

            var x1 = compres.CompiledAssembly.FullName;

            Assert.IsFalse(rec == null);

            // serialize
            var stream     = new MemoryStream();
            var binEncoder = new BinaryEncoder(stream);
            var writer     = new SpecificDefaultWriter(rec.Schema);

            writer.Write(rec.Schema, rec, binEncoder);

            // deserialize
            stream.Position = 0;
            var decoder = new BinaryDecoder(stream);
            var reader  = new SpecificDefaultReader(rec.Schema, rec.Schema);
            var rec2    = (ISpecificRecord)reader.Read(null, rec.Schema, rec.Schema, decoder);

            Assert.IsFalse(rec2 == null);
        }
Ejemplo n.º 10
0
        public static void TestSpecific(string str, object[] result)
        {
            Protocol protocol = Protocol.Parse(str);
            var codegen = new CodeGen();
            codegen.AddProtocol(protocol);
            var compileUnit = codegen.GenerateCode();

            // add a constructor to the main class using the passed assignment statements
            CodeTypeDeclaration ctd = compileUnit.Namespaces[0].Types[(int)result[0]];
            CodeConstructor constructor = new CodeConstructor();
            constructor.Attributes = MemberAttributes.Public;
            CodeSnippetExpression snippet = new CodeSnippetExpression((string)result[2]);
            constructor.Statements.Add(snippet);
            ctd.Members.Add(constructor);

            // add a function to the main class to populate the data
            // This has been moved from constructor, as it was causing some tests to pass that shouldn't when referencing a blank object on READ.

            CodeMemberMethod method = new CodeMemberMethod();
            method.Attributes = MemberAttributes.Public;
            method.Name = "Populate";
            CodeSnippetExpression snippet2 = new CodeSnippetExpression((string)result[3]);
            method.Statements.Add(snippet2);
            ctd.Members.Add(method);

            // compile
            var comparam = new CompilerParameters(new string[] { "mscorlib.dll" });
            comparam.ReferencedAssemblies.Add("System.dll");
            comparam.ReferencedAssemblies.Add("Avro.dll");
            comparam.GenerateInMemory = true;
            var ccp = new Microsoft.CSharp.CSharpCodeProvider();
            var units = new CodeCompileUnit[] { compileUnit };
            var compres = ccp.CompileAssemblyFromDom(comparam, units);
            if (compres == null || compres.Errors.Count > 0)
            {
                for (int i = 0; i < compres.Errors.Count; i++)
                    Console.WriteLine(compres.Errors[i]);
            }
            if (null != compres)
                Assert.IsTrue(compres.Errors.Count == 0);

            // create record
            ISpecificRecord rec = compres.CompiledAssembly.CreateInstance((string)result[1]) as ISpecificRecord;

            // Call populate to put some data in it.
            Type recType = rec.GetType(); ;
            MethodInfo methodInfo = recType.GetMethod("Populate");
            methodInfo.Invoke(rec, null);

            var x1 = compres.CompiledAssembly.FullName;

            Assert.IsFalse(rec == null);

            // serialize
            var stream = new MemoryStream();
            var binEncoder = new BinaryEncoder(stream);
            var writer = new SpecificDefaultWriter(rec.Schema);
            writer.Write(rec.Schema, rec, binEncoder);

            // deserialize
            stream.Position = 0;
            var decoder = new BinaryDecoder(stream);
            var reader = new SpecificDefaultReader(rec.Schema, rec.Schema);
            var rec2 = (ISpecificRecord)reader.Read(null, rec.Schema, rec.Schema, decoder);
            Assert.IsFalse(rec2 == null);
        }
Ejemplo n.º 11
0
        public static void TestSpecific(string str, object[] result)
        {
            Protocol protocol = Protocol.Parse(str);
            var      codegen  = new CodeGen();

            codegen.AddProtocol(protocol);
            var compileUnit = codegen.GenerateCode();

            // add a constructor to the main class using the passed assignment statements
            CodeTypeDeclaration ctd         = compileUnit.Namespaces[0].Types[(int)result[0]];
            CodeConstructor     constructor = new CodeConstructor();

            constructor.Attributes = MemberAttributes.Public;
            CodeSnippetExpression snippet = new CodeSnippetExpression((string)result[2]);

            constructor.Statements.Add(snippet);
            ctd.Members.Add(constructor);

            // compile
            var comparam = new CompilerParameters(new string[] { "mscorlib.dll" });

            comparam.ReferencedAssemblies.Add("System.dll");
            comparam.ReferencedAssemblies.Add("System.Core.dll");
            comparam.ReferencedAssemblies.Add(Type.GetType("Mono.Runtime") != null ? "Mono.CSharp.dll" : "Microsoft.CSharp.dll");
            comparam.ReferencedAssemblies.Add("Avro.dll");
            comparam.GenerateInMemory = true;
            var ccp     = new Microsoft.CSharp.CSharpCodeProvider();
            var units   = new CodeCompileUnit[] { compileUnit };
            var compres = ccp.CompileAssemblyFromDom(comparam, units);

            if (compres == null || compres.Errors.Count > 0)
            {
                for (int i = 0; i < compres.Errors.Count; i++)
                {
                    Console.WriteLine(compres.Errors[i]);
                }
            }
            if (null != compres)
            {
                Assert.IsTrue(compres.Errors.Count == 0);
            }

            // create record
            ISpecificRecord rec = compres.CompiledAssembly.CreateInstance((string)result[1]) as ISpecificRecord;

            Assert.IsFalse(rec == null);

            // serialize
            var stream     = new MemoryStream();
            var binEncoder = new BinaryEncoder(stream);
            var writer     = new SpecificDefaultWriter(rec.Schema);

            writer.Write(rec.Schema, rec, binEncoder);

            // deserialize
            stream.Position = 0;
            var decoder = new BinaryDecoder(stream);
            var reader  = new SpecificDefaultReader(rec.Schema, rec.Schema);
            var rec2    = (ISpecificRecord)reader.Read(null, rec.Schema, rec.Schema, decoder);

            Assert.IsFalse(rec2 == null);
        }
Ejemplo n.º 12
0
        public static void TestSpecific(string str, object[] result)
        {
            Protocol protocol = Protocol.Parse(str);
            var codegen = new CodeGen();
            codegen.AddProtocol(protocol);
            var compileUnit = codegen.GenerateCode();

            // add a constructor to the main class using the passed assignment statements
            CodeTypeDeclaration ctd = compileUnit.Namespaces[0].Types[(int)result[0]];
            CodeConstructor constructor = new CodeConstructor();
            constructor.Attributes = MemberAttributes.Public;
            CodeSnippetExpression snippet = new CodeSnippetExpression((string)result[2]);
            constructor.Statements.Add(snippet);
            ctd.Members.Add(constructor);

            // compile
            var comparam = new CompilerParameters(new string[] { "mscorlib.dll" });
            comparam.ReferencedAssemblies.Add("System.dll");
            comparam.ReferencedAssemblies.Add("System.Core.dll");
            comparam.ReferencedAssemblies.Add(Type.GetType("Mono.Runtime") != null ? "Mono.CSharp.dll" : "Microsoft.CSharp.dll");
            comparam.ReferencedAssemblies.Add("Avro.dll");
            comparam.GenerateInMemory = true;
            var ccp = new Microsoft.CSharp.CSharpCodeProvider();
            var units = new CodeCompileUnit[] { compileUnit };
            var compres = ccp.CompileAssemblyFromDom(comparam, units);
            if (compres == null || compres.Errors.Count > 0)
            {
                for (int i = 0; i < compres.Errors.Count; i++)
                    Console.WriteLine(compres.Errors[i]);
            }
            if (null != compres)
                Assert.IsTrue(compres.Errors.Count == 0);

            // create record
            ISpecificRecord rec = compres.CompiledAssembly.CreateInstance((string)result[1]) as ISpecificRecord;
            Assert.IsFalse(rec == null);

            // serialize
            var stream = new MemoryStream();
            var binEncoder = new BinaryEncoder(stream);
            var writer = new SpecificDefaultWriter(rec.Schema);
            writer.Write(rec.Schema, rec, binEncoder);

            // deserialize
            stream.Position = 0;
            var decoder = new BinaryDecoder(stream);
            var reader = new SpecificDefaultReader(rec.Schema, rec.Schema);
            var rec2 = (ISpecificRecord)reader.Read(null, rec.Schema, rec.Schema, decoder);
            Assert.IsFalse(rec2 == null);
        }