-
Notifications
You must be signed in to change notification settings - Fork 2k
/
EventSequenceToken.cs
119 lines (107 loc) · 4.49 KB
/
EventSequenceToken.cs
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
using System;
using System.Globalization;
using Orleans.Streams;
namespace Orleans.Providers.Streams.Common
{
/// <summary>
/// Stream sequence token that tracks sequence number and event index
/// </summary>
[Serializable]
public class EventSequenceToken : StreamSequenceToken
{
/// <summary>
/// Number of event batches in stream prior to this event batch
/// </summary>
public override long SequenceNumber { get; protected set; }
/// <summary>
/// Number of events in batch prior to this event
/// </summary>
public override int EventIndex { get; protected set; }
/// <summary>
/// Sequence token constructor
/// </summary>
/// <param name="seqNumber"></param>
public EventSequenceToken(long seqNumber)
{
SequenceNumber = seqNumber;
EventIndex = 0;
}
/// <summary>
/// Sequence token constructor
/// </summary>
/// <param name="seqNumber"></param>
/// <param name="eventInd"></param>
public EventSequenceToken(long seqNumber, int eventInd)
{
SequenceNumber = seqNumber;
EventIndex = eventInd;
}
/// <summary>
/// Creates a sequence token for a specific event in the current batch
/// </summary>
/// <param name="eventInd"></param>
/// <returns></returns>
public EventSequenceToken CreateSequenceTokenForEvent(int eventInd)
{
return new EventSequenceToken(SequenceNumber, eventInd);
}
/// <summary>
/// Determines whether the specified object is equal to the current object.
/// </summary>
/// <returns>
/// true if the specified object is equal to the current object; otherwise, false.
/// </returns>
/// <param name="obj">The object to compare with the current object. </param><filterpriority>2</filterpriority>
public override bool Equals(object obj)
{
return Equals(obj as EventSequenceToken);
}
/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <returns>
/// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
/// </returns>
/// <param name="other">An object to compare with this object.</param>
public override bool Equals(StreamSequenceToken other)
{
var token = other as EventSequenceToken;
return token != null && (token.SequenceNumber == SequenceNumber &&
token.EventIndex == EventIndex);
}
/// <summary>
/// Compares the current object with another object of the same type.
/// </summary>
/// <returns>
/// A value that indicates the relative order of the objects being compared. The return value has the following meanings: Value Meaning Less than zero This object is less than the <paramref name="other"/> parameter.Zero This object is equal to <paramref name="other"/>. Greater than zero This object is greater than <paramref name="other"/>.
/// </returns>
/// <param name="other">An object to compare with this object.</param>
public override int CompareTo(StreamSequenceToken other)
{
if (other == null)
return 1;
var token = other as EventSequenceToken;
if (token == null)
throw new ArgumentOutOfRangeException("other");
int difference = SequenceNumber.CompareTo(token.SequenceNumber);
return difference != 0 ? difference : EventIndex.CompareTo(token.EventIndex);
}
/// <summary>
/// GetHashCode method for current EventSequenceToken
/// </summary>
/// <returns> Hash code for current EventSequenceToken object </returns>
public override int GetHashCode()
{
// why 397?
return (EventIndex * 397) ^ SequenceNumber.GetHashCode();
}
/// <summary>
/// ToString method
/// </summary>
/// <returns> A string which represent current EventSequenceToken object </returns>
public override string ToString()
{
return string.Format(CultureInfo.InvariantCulture, "[EventSequenceToken: SeqNum={0}, EventIndex={1}]", SequenceNumber, EventIndex);
}
}
}