-
-
Notifications
You must be signed in to change notification settings - Fork 129
/
internal_document.go
214 lines (179 loc) · 5.85 KB
/
internal_document.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* 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 document
import (
"github.com/yorkie-team/yorkie/api/converter"
"github.com/yorkie-team/yorkie/pkg/document/change"
"github.com/yorkie-team/yorkie/pkg/document/crdt"
"github.com/yorkie-team/yorkie/pkg/document/key"
"github.com/yorkie-team/yorkie/pkg/document/time"
)
type statusType int
const (
// Detached means that the document is not attached to the client.
// The actor of the ticket is created without being assigned.
Detached statusType = iota
// Attached means that this document is attached to the client.
// The actor of the ticket is created with being assigned by the client.
Attached
)
// InternalDocument represents a document in MongoDB and contains logical clocks.
type InternalDocument struct {
key key.Key
status statusType
root *crdt.Root
checkpoint change.Checkpoint
changeID change.ID
localChanges []*change.Change
}
// NewInternalDocument creates a new instance of InternalDocument.
func NewInternalDocument(k key.Key) *InternalDocument {
root := crdt.NewObject(crdt.NewElementRHT(), time.InitialTicket)
return &InternalDocument{
key: k,
status: Detached,
root: crdt.NewRoot(root),
checkpoint: change.InitialCheckpoint,
changeID: change.InitialID,
}
}
// NewInternalDocumentFromSnapshot creates a new instance of InternalDocument with the snapshot.
func NewInternalDocumentFromSnapshot(
k key.Key,
serverSeq int64,
lamport int64,
snapshot []byte,
) (*InternalDocument, error) {
obj, err := converter.BytesToObject(snapshot)
if err != nil {
return nil, err
}
return &InternalDocument{
key: k,
status: Detached,
root: crdt.NewRoot(obj),
checkpoint: change.InitialCheckpoint.NextServerSeq(serverSeq),
changeID: change.InitialID.SyncLamport(lamport),
}, nil
}
// Key returns the key of this document.
func (d *InternalDocument) Key() key.Key {
return d.key
}
// Checkpoint returns the checkpoint of this document.
func (d *InternalDocument) Checkpoint() change.Checkpoint {
return d.checkpoint
}
// HasLocalChanges returns whether this document has local changes or not.
func (d *InternalDocument) HasLocalChanges() bool {
return len(d.localChanges) > 0
}
// ApplyChangePack applies the given change pack into this document.
func (d *InternalDocument) ApplyChangePack(pack *change.Pack) error {
// 01. Apply remote changes to both the clone and the document.
if len(pack.Snapshot) > 0 {
if err := d.applySnapshot(pack.Snapshot, pack.Checkpoint.ServerSeq); err != nil {
return err
}
} else {
if err := d.ApplyChanges(pack.Changes...); err != nil {
return err
}
}
// 02. Remove local changes applied to server.
for d.HasLocalChanges() {
c := d.localChanges[0]
if c.ClientSeq() > pack.Checkpoint.ClientSeq {
break
}
d.localChanges = d.localChanges[1:]
}
// 03. Update the checkpoint.
d.checkpoint = d.checkpoint.Forward(pack.Checkpoint)
if pack.MinSyncedTicket != nil {
d.GarbageCollect(pack.MinSyncedTicket)
}
return nil
}
// GarbageCollect purge elements that were removed before the given time.
func (d *InternalDocument) GarbageCollect(ticket *time.Ticket) int {
return d.root.GarbageCollect(ticket)
}
// GarbageLen returns the count of removed elements.
func (d *InternalDocument) GarbageLen() int {
return d.root.GarbageLen()
}
// Marshal returns the JSON encoding of this document.
func (d *InternalDocument) Marshal() string {
return d.root.Object().Marshal()
}
// CreateChangePack creates pack of the local changes to send to the server.
func (d *InternalDocument) CreateChangePack() *change.Pack {
changes := d.localChanges
cp := d.checkpoint.IncreaseClientSeq(uint32(len(changes)))
return change.NewPack(d.key, cp, changes, nil)
}
// SetActor sets actor into this document. This is also applied in the local
// changes the document has.
func (d *InternalDocument) SetActor(actor *time.ActorID) {
for _, c := range d.localChanges {
c.SetActor(actor)
}
d.changeID = d.changeID.SetActor(actor)
}
// Lamport returns the Lamport clock of this document.
func (d *InternalDocument) Lamport() int64 {
return d.changeID.Lamport()
}
// ActorID returns ID of the actor currently editing the document.
func (d *InternalDocument) ActorID() *time.ActorID {
return d.changeID.ActorID()
}
// SetStatus sets the status of this document.
func (d *InternalDocument) SetStatus(status statusType) {
d.status = status
}
// IsAttached returns the whether this document is attached or not.
func (d *InternalDocument) IsAttached() bool {
return d.status == Attached
}
// Root returns the root of this document.
func (d *InternalDocument) Root() *crdt.Root {
return d.root
}
// RootObject returns the root object.
func (d *InternalDocument) RootObject() *crdt.Object {
return d.root.Object()
}
func (d *InternalDocument) applySnapshot(snapshot []byte, serverSeq int64) error {
rootObj, err := converter.BytesToObject(snapshot)
if err != nil {
return err
}
d.root = crdt.NewRoot(rootObj)
d.changeID = d.changeID.SyncLamport(serverSeq)
return nil
}
// ApplyChanges applies remote changes to the document.
func (d *InternalDocument) ApplyChanges(changes ...*change.Change) error {
for _, c := range changes {
if err := c.Execute(d.root); err != nil {
return err
}
d.changeID = d.changeID.SyncLamport(c.ID().Lamport())
}
return nil
}