forked from couchbase/gocb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
87 lines (71 loc) · 2.08 KB
/
token.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
package gocb
import (
"encoding/json"
"fmt"
"gopkg.in/couchbase/gocbcore.v7"
)
// MutationToken holds the mutation state information from an operation.
type MutationToken struct {
token gocbcore.MutationToken
bucket *Bucket
}
type bucketToken struct {
SeqNo uint64 `json:"seqno"`
VbUuid string `json:"vbuuid"`
}
func (mt bucketToken) MarshalJSON() ([]byte, error) {
info := []interface{}{mt.SeqNo, mt.VbUuid}
return json.Marshal(info)
}
func (mt *bucketToken) UnmarshalJSON(data []byte) error {
info := []interface{}{&mt.SeqNo, &mt.VbUuid}
return json.Unmarshal(data, &info)
}
type bucketTokens map[string]*bucketToken
type mutationStateData map[string]*bucketTokens
// MutationState holds and aggregates MutationToken's across multiple operations.
type MutationState struct {
data *mutationStateData
}
// NewMutationState creates a new MutationState for tracking mutation state.
func NewMutationState(tokens ...MutationToken) *MutationState {
mt := &MutationState{}
mt.Add(tokens...)
return mt
}
func (mt *MutationState) addSingle(token MutationToken) {
if token.bucket == nil {
return
}
if mt.data == nil {
data := make(mutationStateData)
mt.data = &data
}
bucketName := token.bucket.name
if (*mt.data)[bucketName] == nil {
tokens := make(bucketTokens)
(*mt.data)[bucketName] = &tokens
}
vbId := fmt.Sprintf("%d", token.token.VbId)
stateToken := (*(*mt.data)[bucketName])[vbId]
if stateToken == nil {
stateToken = &bucketToken{}
(*(*mt.data)[bucketName])[vbId] = stateToken
}
stateToken.SeqNo = uint64(token.token.SeqNo)
stateToken.VbUuid = fmt.Sprintf("%d", token.token.VbUuid)
}
// Add includes an operation's mutation information in this mutation state.
func (mt *MutationState) Add(tokens ...MutationToken) {
for _, v := range tokens {
mt.addSingle(v)
}
}
// MarshalJSON marshal's this mutation state to JSON.
func (mt *MutationState) MarshalJSON() ([]byte, error) {
return json.Marshal(mt.data)
}
// UnmarshalJSON unmarshal's a mutation state from JSON.
func (mt *MutationState) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &mt.data)
}