Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distinguish between Add or Update #275

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions src/net/KNet/Specific/Replicator/KNetCompactedReplicator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ public interface IKNetCompactedReplicator<TKey, TValue> : IDictionary<TKey, TVal
{
#region Events

/// <summary>
/// Called when a [<typeparamref name="TKey"/>, <typeparamref name="TValue"/>] is added by consuming data from the others <see cref="IKNetCompactedReplicator{TKey, TValue}"/>
/// </summary>
event Action<IKNetCompactedReplicator<TKey, TValue>, KeyValuePair<TKey, TValue>> OnRemoteAdd;

/// <summary>
/// Called when a [<typeparamref name="TKey"/>, <typeparamref name="TValue"/>] is updated by consuming data from the others <see cref="IKNetCompactedReplicator{TKey, TValue}"/>
/// </summary>
Expand All @@ -112,6 +117,11 @@ public interface IKNetCompactedReplicator<TKey, TValue> : IDictionary<TKey, TVal
/// </summary>
event Action<IKNetCompactedReplicator<TKey, TValue>, KeyValuePair<TKey, TValue>> OnRemoteRemove;

/// <summary>
/// Called when a [<typeparamref name="TKey"/>, <typeparamref name="TValue"/>] is added on this <see cref="IKNetCompactedReplicator{TKey, TValue}"/>
/// </summary>
event Action<IKNetCompactedReplicator<TKey, TValue>, KeyValuePair<TKey, TValue>> OnLocalAdd;

/// <summary>
/// Called when a [<typeparamref name="TKey"/>, <typeparamref name="TValue"/>] is updated on this <see cref="IKNetCompactedReplicator{TKey, TValue}"/>
/// </summary>
Expand Down Expand Up @@ -515,12 +525,18 @@ public override void OnPartitionsLost(Java.Util.Collection<Org.Apache.Kafka.Comm

#region Events

/// <inheritdoc cref="IKNetCompactedReplicator{TKey, TValue}.OnRemoteAdd"/>
public event Action<IKNetCompactedReplicator<TKey, TValue>, KeyValuePair<TKey, TValue>> OnRemoteAdd;

/// <inheritdoc cref="IKNetCompactedReplicator{TKey, TValue}.OnRemoteUpdate"/>
public event Action<IKNetCompactedReplicator<TKey, TValue>, KeyValuePair<TKey, TValue>> OnRemoteUpdate;

/// <inheritdoc cref="IKNetCompactedReplicator{TKey, TValue}.OnRemoteRemove"/>
public event Action<IKNetCompactedReplicator<TKey, TValue>, KeyValuePair<TKey, TValue>> OnRemoteRemove;

/// <inheritdoc cref="IKNetCompactedReplicator{TKey, TValue}.OnLocalAdd"/>
public event Action<IKNetCompactedReplicator<TKey, TValue>, KeyValuePair<TKey, TValue>> OnLocalAdd;

/// <inheritdoc cref="IKNetCompactedReplicator{TKey, TValue}.OnLocalUpdate"/>
public event Action<IKNetCompactedReplicator<TKey, TValue>, KeyValuePair<TKey, TValue>> OnLocalUpdate;

Expand Down Expand Up @@ -647,9 +663,11 @@ private void OnMessage(KNetConsumerRecord<TKey, TValue> record)
}
else
{
bool containsKey = true;
ILocalDataStorage data;
if (!_dictionary.TryGetValue(record.Key, out data))
{
containsKey = false;
data = new LocalDataStorage();
_dictionary[record.Key] = data;
}
Expand All @@ -669,7 +687,14 @@ private void OnMessage(KNetConsumerRecord<TKey, TValue> record)
data.Value = record.Value;
}
}
OnRemoteUpdate?.Invoke(this, new KeyValuePair<TKey, TValue>(record.Key, record.Value));
if (containsKey)
{
OnRemoteUpdate?.Invoke(this, new KeyValuePair<TKey, TValue>(record.Key, record.Value));
}
else
{
OnRemoteAdd?.Invoke(this, new KeyValuePair<TKey, TValue>(record.Key, record.Value));
}
}

if (_OnConsumeSyncWaiter != null)
Expand Down Expand Up @@ -777,9 +802,11 @@ private void AddOrUpdate(TKey key, TValue value)
}
else
{
bool containsKey = true;
ILocalDataStorage data;
if (!_dictionary.TryGetValue(key, out data))
{
containsKey = false;
data = new LocalDataStorage();
_dictionary[key] = data;
}
Expand All @@ -799,7 +826,14 @@ private void AddOrUpdate(TKey key, TValue value)
data.Value = value;
}
}
OnLocalUpdate?.Invoke(this, new KeyValuePair<TKey, TValue>(key, value));
if (containsKey)
{
OnLocalUpdate?.Invoke(this, new KeyValuePair<TKey, TValue>(key, value));
}
else
{
OnLocalAdd?.Invoke(this, new KeyValuePair<TKey, TValue>(key, value));
}
}
}
else if (UpdateModeOnConsume || UpdateModeOnConsumeSync)
Expand Down