-
-
Notifications
You must be signed in to change notification settings - Fork 129
/
checkpoint.go
126 lines (103 loc) · 3.64 KB
/
checkpoint.go
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
/*
* Copyright 2020 The Yorkie Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package change
import (
"fmt"
"math"
)
const (
// InitialClientSeq is the initial sequence number of the client.
InitialClientSeq = 0
// InitialServerSeq is the initial sequence number of the server.
InitialServerSeq = 0
// MaxClientSeq is the maximum sequence number of the client.
MaxClientSeq = math.MaxUint32
// MaxServerSeq is the maximum sequence number of the server.
MaxServerSeq = int64(math.MaxInt64)
)
// InitialCheckpoint is the initial value of Checkpoint.
var InitialCheckpoint = NewCheckpoint(InitialServerSeq, InitialClientSeq)
// MaxCheckpoint is the maximum value of Checkpoint.
var MaxCheckpoint = NewCheckpoint(MaxServerSeq, MaxClientSeq)
// Checkpoint is used to determine the client received changes.
// It is not meant to be used to determine the logical order of changes.
type Checkpoint struct {
// serverSeq is the sequence of the change on the server. We can find the
// change with serverSeq and documentID in the server. If the change is not
// stored on the server, serverSeq is 0.
ServerSeq int64
// clientSeq is the sequence of the change within the client that made the
// change.
ClientSeq uint32
}
// NewCheckpoint creates a new instance of Checkpoint.
func NewCheckpoint(serverSeq int64, clientSeq uint32) Checkpoint {
return Checkpoint{
ServerSeq: serverSeq,
ClientSeq: clientSeq,
}
}
// NextServerSeq creates a new instance with next server sequence.
func (cp Checkpoint) NextServerSeq(serverSeq int64) Checkpoint {
if cp.ServerSeq == serverSeq {
return cp
}
return NewCheckpoint(serverSeq, cp.ClientSeq)
}
// NextClientSeq creates a new instance with next client sequence.
func (cp Checkpoint) NextClientSeq() Checkpoint {
return cp.IncreaseClientSeq(1)
}
// IncreaseClientSeq creates a new instance with increased client sequence.
func (cp Checkpoint) IncreaseClientSeq(inc uint32) Checkpoint {
if inc == 0 {
return cp
}
return NewCheckpoint(cp.ServerSeq, cp.ClientSeq+inc)
}
// SyncClientSeq updates the given clientSeq if it is greater than the internal
// value.
func (cp Checkpoint) SyncClientSeq(clientSeq uint32) Checkpoint {
if cp.ClientSeq < clientSeq {
return NewCheckpoint(cp.ServerSeq, clientSeq)
}
return cp
}
// Forward updates the given checkpoint with those values when it is greater
// than the values of internal properties.
func (cp Checkpoint) Forward(other Checkpoint) Checkpoint {
if cp.Equals(other) {
return cp
}
maxServerSeq := cp.ServerSeq
if cp.ServerSeq < other.ServerSeq {
maxServerSeq = other.ServerSeq
}
maxClientSeq := cp.ClientSeq
if cp.ClientSeq < other.ClientSeq {
maxClientSeq = other.ClientSeq
}
return NewCheckpoint(maxServerSeq, maxClientSeq)
}
// Equals returns whether the given checkpoint is equal to this checkpoint or not.
func (cp Checkpoint) Equals(other Checkpoint) bool {
return cp.ServerSeq == other.ServerSeq &&
cp.ClientSeq == other.ClientSeq
}
// String returns the string of information about this checkpoint.
func (cp Checkpoint) String() string {
return fmt.Sprintf("serverSeq=%d, clientSeq=%d", cp.ServerSeq, cp.ClientSeq)
}