Repository navigation
Expand file tree
/
Copy pathLeftJoin.cs
More file actions
127 lines (105 loc) · 5.9 KB
/
Copy pathLeftJoin.cs
File metadata and controls
127 lines (105 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
// Copyright (c) 2011-2023 Roland Pheasant. All rights reserved.
// Roland Pheasant licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.
using System.Reactive.Disposables;
using System.Reactive.Linq;
using DynamicData.Kernel;
namespace DynamicData.Cache.Internal;
internal sealed class LeftJoin<TLeft, TLeftKey, TRight, TRightKey, TDestination>(IObservable<IChangeSet<TLeft, TLeftKey>> left, IObservable<IChangeSet<TRight, TRightKey>> right, Func<TRight, TLeftKey> rightKeySelector, Func<TLeftKey, TLeft, Optional<TRight>, TDestination> resultSelector)
where TLeft : notnull
where TLeftKey : notnull
where TRight : notnull
where TRightKey : notnull
where TDestination : notnull
{
private readonly IObservable<IChangeSet<TLeft, TLeftKey>> _left = left ?? throw new ArgumentNullException(nameof(left));
private readonly Func<TLeftKey, TLeft, Optional<TRight>, TDestination> _resultSelector = resultSelector ?? throw new ArgumentNullException(nameof(resultSelector));
private readonly IObservable<IChangeSet<TRight, TRightKey>> _right = right ?? throw new ArgumentNullException(nameof(right));
private readonly Func<TRight, TLeftKey> _rightKeySelector = rightKeySelector ?? throw new ArgumentNullException(nameof(rightKeySelector));
public IObservable<IChangeSet<TDestination, TLeftKey>> Run() => Observable.Create<IChangeSet<TDestination, TLeftKey>>(
observer =>
{
var locker = new object();
// create local backing stores
var leftCache = _left.Synchronize(locker).AsObservableCache(false);
var rightCache = _right.Synchronize(locker).ChangeKey(_rightKeySelector).AsObservableCache(false);
// joined is the final cache
var joined = new ChangeAwareCache<TDestination, TLeftKey>();
var leftLoader = leftCache.Connect().Select(
changes =>
{
foreach (var change in changes.ToConcreteType())
{
switch (change.Reason)
{
case ChangeReason.Add:
case ChangeReason.Update:
// Update with left (and right if it is presents)
var leftCurrent = change.Current;
var rightLookup = rightCache.Lookup(change.Key);
joined.AddOrUpdate(_resultSelector(change.Key, leftCurrent, rightLookup), change.Key);
break;
case ChangeReason.Remove:
// remove from result because a left value is expected
joined.Remove(change.Key);
break;
case ChangeReason.Refresh:
// propagate upstream
joined.Refresh(change.Key);
break;
}
}
return joined.CaptureChanges();
});
var rightLoader = rightCache.Connect().Select(
changes =>
{
foreach (var change in changes.ToConcreteType())
{
var right = change.Current;
var left = leftCache.Lookup(change.Key);
switch (change.Reason)
{
case ChangeReason.Add:
case ChangeReason.Update:
{
if (left.HasValue)
{
// Update with left and right value
joined.AddOrUpdate(_resultSelector(change.Key, left.Value, right), change.Key);
}
else
{
// remove if it is already in the cache
joined.Remove(change.Key);
}
}
break;
case ChangeReason.Remove:
{
if (left.HasValue)
{
// Update with no right value
joined.AddOrUpdate(_resultSelector(change.Key, left.Value, Optional<TRight>.None), change.Key);
}
else
{
// remove if it is already in the cache
joined.Remove(change.Key);
}
}
break;
case ChangeReason.Refresh:
// propagate upstream
joined.Refresh(change.Key);
break;
}
}
return joined.CaptureChanges();
});
lock (locker)
{
return new CompositeDisposable(leftLoader.Merge(rightLoader).SubscribeSafe(observer), leftCache, rightCache);
}
});
}