Skip to content

Commit ccf2d23

Browse files
GODRIVER-2681 Replace CompareTimestamp with Before, After and Compare methods. (#1182)
Co-authored-by: Preston Vasquez <[email protected]>
1 parent f272645 commit ccf2d23

File tree

2 files changed

+100
-16
lines changed

2 files changed

+100
-16
lines changed

bson/primitive/primitive.go

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,16 @@ type Timestamp struct {
141141
I uint32
142142
}
143143

144+
// After reports whether the time instant tp is after tp2.
145+
func (tp Timestamp) After(tp2 Timestamp) bool {
146+
return tp.T > tp2.T || (tp.T == tp2.T && tp.I > tp2.I)
147+
}
148+
149+
// Before reports whether the time instant tp is before tp2.
150+
func (tp Timestamp) Before(tp2 Timestamp) bool {
151+
return tp.T < tp2.T || (tp.T == tp2.T && tp.I < tp2.I)
152+
}
153+
144154
// Equal compares tp to tp2 and returns true if they are equal.
145155
func (tp Timestamp) Equal(tp2 Timestamp) bool {
146156
return tp.T == tp2.T && tp.I == tp2.I
@@ -151,24 +161,25 @@ func (tp Timestamp) IsZero() bool {
151161
return tp.T == 0 && tp.I == 0
152162
}
153163

154-
// CompareTimestamp returns an integer comparing two Timestamps, where T is compared first, followed by I.
155-
// Returns 0 if tp = tp2, 1 if tp > tp2, -1 if tp < tp2.
156-
func CompareTimestamp(tp, tp2 Timestamp) int {
157-
if tp.Equal(tp2) {
164+
// Compare compares the time instant tp with tp2. If tp is before tp2, it returns -1; if tp is after
165+
// tp2, it returns +1; if they're the same, it returns 0.
166+
func (tp Timestamp) Compare(tp2 Timestamp) int {
167+
switch {
168+
case tp.Equal(tp2):
158169
return 0
159-
}
160-
161-
if tp.T > tp2.T {
162-
return 1
163-
}
164-
if tp.T < tp2.T {
170+
case tp.Before(tp2):
165171
return -1
172+
default:
173+
return +1
166174
}
167-
// Compare I values because T values are equal
168-
if tp.I > tp2.I {
169-
return 1
170-
}
171-
return -1
175+
}
176+
177+
// CompareTimestamp compares the time instant tp with tp2. If tp is before tp2, it returns -1; if tp is after
178+
// tp2, it returns +1; if they're the same, it returns 0.
179+
//
180+
// Deprecated: Use Timestamp.Compare instead.
181+
func CompareTimestamp(tp, tp2 Timestamp) int {
182+
return tp.Compare(tp2)
172183
}
173184

174185
// MinKey represents the BSON minkey value.

bson/primitive/primitive_test.go

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type zeroer interface {
2020
IsZero() bool
2121
}
2222

23-
func TestTimestampCompare(t *testing.T) {
23+
func TestCompareTimestamp(t *testing.T) {
2424
testcases := []struct {
2525
name string
2626
tp Timestamp
@@ -42,6 +42,79 @@ func TestTimestampCompare(t *testing.T) {
4242
}
4343
}
4444

45+
func TestTimestamp(t *testing.T) {
46+
t.Parallel()
47+
48+
testCases := []struct {
49+
description string
50+
tp Timestamp
51+
tp2 Timestamp
52+
expectedAfter bool
53+
expectedBefore bool
54+
expectedEqual bool
55+
expectedCompare int
56+
}{
57+
{
58+
description: "equal",
59+
tp: Timestamp{T: 12345, I: 67890},
60+
tp2: Timestamp{T: 12345, I: 67890},
61+
expectedBefore: false,
62+
expectedAfter: false,
63+
expectedEqual: true,
64+
expectedCompare: 0,
65+
},
66+
{
67+
description: "T greater than",
68+
tp: Timestamp{T: 12345, I: 67890},
69+
tp2: Timestamp{T: 2345, I: 67890},
70+
expectedBefore: false,
71+
expectedAfter: true,
72+
expectedEqual: false,
73+
expectedCompare: 1,
74+
},
75+
{
76+
description: "I greater than",
77+
tp: Timestamp{T: 12345, I: 67890},
78+
tp2: Timestamp{T: 12345, I: 7890},
79+
expectedBefore: false,
80+
expectedAfter: true,
81+
expectedEqual: false,
82+
expectedCompare: 1,
83+
},
84+
{
85+
description: "T less than",
86+
tp: Timestamp{T: 12345, I: 67890},
87+
tp2: Timestamp{T: 112345, I: 67890},
88+
expectedBefore: true,
89+
expectedAfter: false,
90+
expectedEqual: false,
91+
expectedCompare: -1,
92+
},
93+
{
94+
description: "I less than",
95+
tp: Timestamp{T: 12345, I: 67890},
96+
tp2: Timestamp{T: 12345, I: 167890},
97+
expectedBefore: true,
98+
expectedAfter: false,
99+
expectedEqual: false,
100+
expectedCompare: -1,
101+
},
102+
}
103+
104+
for _, tc := range testCases {
105+
tc := tc // Capture range variable.
106+
107+
t.Run(tc.description, func(t *testing.T) {
108+
t.Parallel()
109+
110+
assert.Equal(t, tc.expectedAfter, tc.tp.After(tc.tp2), "expected After results to be the same")
111+
assert.Equal(t, tc.expectedBefore, tc.tp.Before(tc.tp2), "expected Before results to be the same")
112+
assert.Equal(t, tc.expectedEqual, tc.tp.Equal(tc.tp2), "expected Equal results to be the same")
113+
assert.Equal(t, tc.expectedCompare, tc.tp.Compare(tc.tp2), "expected Compare result to be the same")
114+
})
115+
}
116+
}
117+
45118
func TestPrimitiveIsZero(t *testing.T) {
46119
testcases := []struct {
47120
name string

0 commit comments

Comments
 (0)