Check-in [0f00f13c99]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Bugfix: PopulateReferences should now be able to cope with duplicate RfcMessageIDs.
Timelines: family | ancestors | descendants | both | dotnet
Files: files | file ages | folders
SHA1: 0f00f13c99aaeccd4a509c9c4c40bdc1e1dce7cc
User & Date: tinus 2019-09-10 19:00:36
Wiki:dotnet
Context
2019-09-10
19:08
Updated TODO.md. check-in: a0bfc52b64 user: tinus tags: dotnet
19:00
Bugfix: PopulateReferences should now be able to cope with duplicate RfcMessageIDs. check-in: 0f00f13c99 user: tinus tags: dotnet
2019-09-07
06:33
Store the name of a participant each time it occurs. check-in: acd83823df user: tinus tags: dotnet
Changes

Changes to MailSynchronizer.cs.

722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738



739

740
741
742
743
744
745
746
            var sourceReferences = new List<string>(sourceMsg.References);
            if (!string.IsNullOrWhiteSpace(sourceMsg.InReplyTo) && !sourceReferences.Contains(sourceMsg.InReplyTo))
                sourceReferences.Insert(0, sourceMsg.InReplyTo);

            var dbMessageRefs = await work.DB.MessageReferences
                .Where(mr => mr.Message == targetMsg)
                .ToDictionaryAsync(mr => mr.ReferencedRfcMessageID, _cancellationToken);
            // TODO: figure out how to handle multiple messages with identical RfcMessageID
            var dbReferencedMessageIDs = await work.Messages
                .GetDictionaryAsync(m => sourceReferences.Contains(m.RfcMessageID),
                                    m => m.RfcMessageID,
                                    m => m?.ID);
            int index = 0;
            foreach (string mailRef in sourceReferences)
            {
                index++;
                MessageReference dbReference = dbMessageRefs.GetValueOrDefault(mailRef, null);



                long? dbMessageID = dbReferencedMessageIDs.GetValueOrDefault(mailRef, null);

                if (dbReference == null)
                {
                    await work.DB.MessageReferences.AddAsync(new MessageReference
                    {
                        Message = targetMsg,
                        ReferencedRfcMessageID = mailRef,
                        InReplyTo = mailRef == sourceMsg.InReplyTo,







<
|
|
|
|





>
>
>
|
>







722
723
724
725
726
727
728

729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
            var sourceReferences = new List<string>(sourceMsg.References);
            if (!string.IsNullOrWhiteSpace(sourceMsg.InReplyTo) && !sourceReferences.Contains(sourceMsg.InReplyTo))
                sourceReferences.Insert(0, sourceMsg.InReplyTo);

            var dbMessageRefs = await work.DB.MessageReferences
                .Where(mr => mr.Message == targetMsg)
                .ToDictionaryAsync(mr => mr.ReferencedRfcMessageID, _cancellationToken);

            var dbReferencedMessageIDs = work.Messages
                .GetLookup(m => sourceReferences.Contains(m.RfcMessageID),
                           m => m.RfcMessageID,
                           m => m?.ID);
            int index = 0;
            foreach (string mailRef in sourceReferences)
            {
                index++;
                MessageReference dbReference = dbMessageRefs.GetValueOrDefault(mailRef, null);
                long? dbMessageID = null;
                if (dbReferencedMessageIDs.Contains(mailRef))
                {
                    dbMessageID = dbReferencedMessageIDs[mailRef].FirstOrDefault(id => id.HasValue);
                }
                if (dbReference == null)
                {
                    await work.DB.MessageReferences.AddAsync(new MessageReference
                    {
                        Message = targetMsg,
                        ReferencedRfcMessageID = mailRef,
                        InReplyTo = mailRef == sourceMsg.InReplyTo,

Changes to Repositories/Repository.cs.

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
..
76
77
78
79
80
81
82
83
84
85
86
87


































88
89
90
91
92
93
94
...
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
        public Task<Dictionary<TKey, T>> GetDictionaryAsync<TKey>(Expression<Func<T, bool>> where,
                                                                  Func<T, TKey> keySelector,
                                                                  IEqualityComparer<TKey> keyComparer = null)
        {
            IQueryable<T> result = _set;
            if (where != null)
                result = result.Where(where);
            return result.ToDictionaryAsync(keySelector, 
                                            keyComparer ?? EqualityComparer<TKey>.Default, 
                                            _cancellationToken);
        }
        public Task<Dictionary<TKey,TValue>> GetDictionaryAsync<TKey, TValue>(Func<T, TKey> keySelector,
                                                                              Func<T, TValue> valueSelector,
                                                                              IEqualityComparer<TKey> keyComparer = null)
        {
            return GetDictionaryAsync(null, keySelector, valueSelector, keyComparer);
................................................................................
                                                                              Func<T, TKey> keySelector,
                                                                              Func<T, TValue> valueSelector,
                                                                              IEqualityComparer<TKey> keyComparer = null)
        {
            IQueryable<T> result = _set;
            if (where != null)
                result = result.Where(where);
            return result.ToDictionaryAsync<T, TKey, TValue>(keySelector, 
                                                             valueSelector, 
                                                             keyComparer ?? EqualityComparer<TKey>.Default, 
                                                             _cancellationToken);
        }



































        public IEnumerable<TResult> Select<TResult>(Expression<Func<T, bool>> where,
                                                    Expression<Func<T, TResult>> valueExpression,
                                                    Expression<Func<TResult, bool>> valueFilter = null,
                                                    IComparer<TResult> valueSorter = null)
        {
            return Select(valueExpression, where, valueFilter, valueSorter);
................................................................................
                if (keyProperties.Count == 1)
                {
                    IProperty key = keyProperties.First();
                    if (typeof(long).IsAssignableFrom(key.ClrType))
                    {
                        // Ensure we've got Int64s, and nothing else
                        var numericIDs = ids.Select(id => Convert.ToInt64(id));
                        
                        // Prepare the SQL statement
                        string tableName = entityType.Relational().TableName;
                        string keyColumn = key.Relational().ColumnName;
                        string sql = "DELETE FROM [" + tableName + "] WHERE [" + keyColumn + "] IN (" + string.Join(", ", numericIDs) + ")";
                        #pragma warning disable EF1000 // SQL command has been checked for SQL injection vectors, and found safe.
                        int numDeleted = _db.Database.ExecuteSqlCommand(sql);
                        #pragma warning restore EF1000
                        
                        // Detach all corresponding entries
                        var entries = _db.ChangeTracker.Entries()
                            .Where(e => e.Metadata == entityType)
                            .Where(e => numericIDs.Contains((long)e.Property(key.Name).CurrentValue));
                        foreach (var entry in entries)
                        {
                            entry.State = EntityState.Detached;







|
|







 







|
|
|


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|







|







58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
..
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
...
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
        public Task<Dictionary<TKey, T>> GetDictionaryAsync<TKey>(Expression<Func<T, bool>> where,
                                                                  Func<T, TKey> keySelector,
                                                                  IEqualityComparer<TKey> keyComparer = null)
        {
            IQueryable<T> result = _set;
            if (where != null)
                result = result.Where(where);
            return result.ToDictionaryAsync(keySelector,
                                            keyComparer ?? EqualityComparer<TKey>.Default,
                                            _cancellationToken);
        }
        public Task<Dictionary<TKey,TValue>> GetDictionaryAsync<TKey, TValue>(Func<T, TKey> keySelector,
                                                                              Func<T, TValue> valueSelector,
                                                                              IEqualityComparer<TKey> keyComparer = null)
        {
            return GetDictionaryAsync(null, keySelector, valueSelector, keyComparer);
................................................................................
                                                                              Func<T, TKey> keySelector,
                                                                              Func<T, TValue> valueSelector,
                                                                              IEqualityComparer<TKey> keyComparer = null)
        {
            IQueryable<T> result = _set;
            if (where != null)
                result = result.Where(where);
            return result.ToDictionaryAsync<T, TKey, TValue>(keySelector,
                                                             valueSelector,
                                                             keyComparer ?? EqualityComparer<TKey>.Default,
                                                             _cancellationToken);
        }

        public ILookup<TKey, T> GetLookup<TKey>(Func<T, TKey> keySelector,
                                                IEqualityComparer<TKey> keyComparer = null)
        {
            return GetLookup((Expression<Func<T, bool>>)null, keySelector, keyComparer);
        }
        public ILookup<TKey, T> GetLookup<TKey>(Expression<Func<T, bool>> where,
                                                Func<T, TKey> keySelector,
                                                IEqualityComparer<TKey> keyComparer = null)
        {
            IQueryable<T> result = _set;
            if (where != null)
                result = result.Where(where);
            return result.ToLookup(keySelector,
                                   keyComparer ?? EqualityComparer<TKey>.Default);
        }
        public ILookup<TKey, TValue> GetLookup<TKey, TValue>(Func<T, TKey> keySelector,
                                                             Func<T, TValue> valueSelector,
                                                             IEqualityComparer<TKey> keyComparer = null)
        {
            return GetLookup(null, keySelector, valueSelector, keyComparer);
        }
        public ILookup<TKey, TValue> GetLookup<TKey, TValue>(Expression<Func<T, bool>> where,
                                                             Func<T, TKey> keySelector,
                                                             Func<T, TValue> valueSelector,
                                                             IEqualityComparer<TKey> keyComparer = null)
        {
            IQueryable<T> result = _set;
            if (where != null)
                result = result.Where(where);
            return result.ToLookup(keySelector,
                                   valueSelector,
                                   keyComparer ?? EqualityComparer<TKey>.Default);
        }

        public IEnumerable<TResult> Select<TResult>(Expression<Func<T, bool>> where,
                                                    Expression<Func<T, TResult>> valueExpression,
                                                    Expression<Func<TResult, bool>> valueFilter = null,
                                                    IComparer<TResult> valueSorter = null)
        {
            return Select(valueExpression, where, valueFilter, valueSorter);
................................................................................
                if (keyProperties.Count == 1)
                {
                    IProperty key = keyProperties.First();
                    if (typeof(long).IsAssignableFrom(key.ClrType))
                    {
                        // Ensure we've got Int64s, and nothing else
                        var numericIDs = ids.Select(id => Convert.ToInt64(id));

                        // Prepare the SQL statement
                        string tableName = entityType.Relational().TableName;
                        string keyColumn = key.Relational().ColumnName;
                        string sql = "DELETE FROM [" + tableName + "] WHERE [" + keyColumn + "] IN (" + string.Join(", ", numericIDs) + ")";
                        #pragma warning disable EF1000 // SQL command has been checked for SQL injection vectors, and found safe.
                        int numDeleted = _db.Database.ExecuteSqlCommand(sql);
                        #pragma warning restore EF1000

                        // Detach all corresponding entries
                        var entries = _db.ChangeTracker.Entries()
                            .Where(e => e.Metadata == entityType)
                            .Where(e => numericIDs.Contains((long)e.Property(key.Name).CurrentValue));
                        foreach (var entry in entries)
                        {
                            entry.State = EntityState.Detached;