<commit_before>﻿using IFramework.Message;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using IFramework.Infrastructure;
using IFramework.Infrastructure.Logging;
using IFramework.Event;
using IFramework.EntityFramework;
using IFramework.IoC;
using IFramework.Message.Impl;

namespace IFramework.MessageStoring
{
    public class MessageStore : MSDbContext, IMessageStore
    {
        protected readonly ILogger _logger;

        public DbSet<Command> Commands { get; set; }
        public DbSet<Event> Events { get; set; }

        public DbSet<HandledEvent> HandledEvents { get; set; }
        public DbSet<FailHandledEvent> FailHandledEvents { get; set; }
        public DbSet<UnSentCommand> UnSentCommands { get; set; }
        public DbSet<UnPublishedEvent> UnPublishedEvents { get; set; }

        static object EventLock = new object();

        public MessageStore(string connectionString = null)
            : base(connectionString ?? "MessageStore")
        {
            if (IoCFactory.IsInit())
            {
                _logger = IoCFactory.Resolve<ILoggerFactory>().Create(this.GetType());
            }
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<HandledEvent>().HasKey(e => new { e.Id, e.SubscriptionName });

            //modelBuilder.Entity<Message>()
            //    .Map<Command>(map =>
            //    {
            //        map.ToTable("Commands");
            //        map.MapInheritedProperties();
            //    })
            //    .Map<Event>(map =>
            //    {
            //        map.ToTable("Events");
            //        map.MapInheritedProperties();
            //    });

            modelBuilder.Entity<UnSentMessage>()
                .Map<UnSentCommand>(map =>
                {
                    map.ToTable("UnSentCommands");
                    map.MapInheritedProperties();
                })
                .Map<UnPublishedEvent>(map =>
                {
                    map.ToTable("UnPublishedEvents");
                    map.MapInheritedProperties();
                });
        }

        protected virtual Command BuildCommand(IMessageContext commandContext, Exception ex = null)
        {
            return new Command(commandContext, ex);
        }

        protected virtual Event BuildEvent(IMessageContext eventContext)
        {
            return new Event(eventContext);
        }
<commit_msg>add index in command and event table<commit_after>﻿using IFramework.Message;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using IFramework.Infrastructure;
using IFramework.Infrastructure.Logging;
using IFramework.Event;
using IFramework.EntityFramework;
using IFramework.IoC;
using IFramework.Message.Impl;
using System.Data.Entity.Infrastructure.Annotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace IFramework.MessageStoring
{
    public class MessageStore : MSDbContext, IMessageStore
    {
        protected readonly ILogger _logger;

        public DbSet<Command> Commands { get; set; }
        public DbSet<Event> Events { get; set; }

        public DbSet<HandledEvent> HandledEvents { get; set; }
        public DbSet<FailHandledEvent> FailHandledEvents { get; set; }
        public DbSet<UnSentCommand> UnSentCommands { get; set; }
        public DbSet<UnPublishedEvent> UnPublishedEvents { get; set; }

        static object EventLock = new object();

        public MessageStore(string connectionString = null)
            : base(connectionString ?? "MessageStore")
        {
            if (IoCFactory.IsInit())
            {
                _logger = IoCFactory.Resolve<ILoggerFactory>().Create(this.GetType());
            }
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<HandledEvent>().HasKey(e => new { e.Id, e.SubscriptionName });

            //modelBuilder.Entity<Message>()
            //    .Map<Command>(map =>
            //    {
            //        map.ToTable("Commands");
            //        map.MapInheritedProperties();
            //    })
            //    .Map<Event>(map =>
            //    {
            //        map.ToTable("Events");
            //        map.MapInheritedProperties();
            //    });

            modelBuilder.Entity<Command>()
                        .Property(c => c.CorrelationID)
                        .HasMaxLength(200)
                        .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("CorrelationIdIndex")));
            modelBuilder.Entity<Command>()
                        .Property(c => c.Name)
                        .HasMaxLength(200)
                        .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("NameIndex")));

            modelBuilder.Entity<Event>()
                        .Property(e => e.CorrelationID)
                        .HasMaxLength(200)
                        .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("CorrelationIdIndex")));
            modelBuilder.Entity<Event>()
                        .Property(e => e.Name)
                        .HasMaxLength(200)
                        .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("NameIndex")));

            modelBuilder.Entity<Event>()
                        .Property(e => e.AggregateRootID)
                        .HasMaxLength(200)
                        .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("AGRootIdIndex")));

            modelBuilder.Entity<UnSentMessage>()
                .Map<UnSentCommand>(map =>
                {
                    map.ToTable("UnSentCommands");
                    map.MapInheritedProperties();
                })
                .Map<UnPublishedEvent>(map =>
                {
                    map.ToTable("UnPublishedEvents");
                    map.MapInheritedProperties();
                });
        }

        protected virtual Command BuildCommand(IMessageContext commandContext, Exception ex = null)
        {
            return new Command(commandContext, ex);
        }

        protected virtual Event BuildEvent(IMessageContext eventContext)
        {
            return new Event(eventContext);
        }

{'fraction_non_alphanumeric': 0.09887471867966992, 'fraction_numerical': 0.002250562640660165, 'mean_word_length': 1.8857142857142857, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1466739', 'n_tokens_mistral': 1672, 'n_tokens_neox': 1605, 'n_words': 298}