-
-
Notifications
You must be signed in to change notification settings - Fork 143
/
document.go
223 lines (187 loc) · 5.71 KB
/
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
215
216
217
218
219
220
221
222
223
/*
* 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 provides JSON-like document(CRDT) implementation.
package document
import (
"fmt"
"github.com/yorkie-team/yorkie/pkg/document/change"
"github.com/yorkie-team/yorkie/pkg/document/crdt"
"github.com/yorkie-team/yorkie/pkg/document/json"
"github.com/yorkie-team/yorkie/pkg/document/key"
"github.com/yorkie-team/yorkie/pkg/document/time"
)
// Document represents a document accessible to the user.
//
// How document works:
// The operations are generated by the proxy while executing user's command on
// the clone. Then the operations will apply the changes into the base json
// root. This is to protect the base json from errors that may occur while user
// edit the document.
type Document struct {
// doc is the original data of the actual document.
doc *InternalDocument
// clone is a copy of `doc` to be exposed to the user and is used to
// protect `doc`.
clone *crdt.Root
}
// New creates a new instance of Document.
func New(key key.Key) *Document {
return &Document{
doc: NewInternalDocument(key),
}
}
// Update executes the given updater to update this document.
func (d *Document) Update(
updater func(root *json.Object) error,
msgAndArgs ...interface{},
) error {
d.ensureClone()
ctx := change.NewContext(
d.doc.changeID.Next(),
messageFromMsgAndArgs(msgAndArgs...),
d.clone,
)
if err := updater(json.NewObject(ctx, d.clone.Object())); err != nil {
// drop clone because it is contaminated.
d.clone = nil
return err
}
if ctx.HasOperations() {
c := ctx.ToChange()
if err := c.Execute(d.doc.root); err != nil {
return err
}
d.doc.localChanges = append(d.doc.localChanges, c)
d.doc.changeID = ctx.ID()
}
return nil
}
// ApplyChangePack applies the given change pack into this document.
func (d *Document) ApplyChangePack(pack *change.Pack) error {
// 01. Apply remote changes to both the clone and the document.
if len(pack.Snapshot) > 0 {
d.clone = nil
if err := d.doc.applySnapshot(pack.Snapshot, pack.Checkpoint.ServerSeq); err != nil {
return err
}
} else {
d.ensureClone()
for _, c := range pack.Changes {
if err := c.Execute(d.clone); err != nil {
return err
}
}
if err := d.doc.ApplyChanges(pack.Changes...); err != nil {
return err
}
}
// 02. Remove local changes applied to server.
for d.HasLocalChanges() {
c := d.doc.localChanges[0]
if c.ClientSeq() > pack.Checkpoint.ClientSeq {
break
}
d.doc.localChanges = d.doc.localChanges[1:]
}
// 03. Update the checkpoint.
d.doc.checkpoint = d.doc.checkpoint.Forward(pack.Checkpoint)
// 04. Do Garbage collection.
d.GarbageCollect(pack.MinSyncedTicket)
return nil
}
// InternalDocument returns the internal document.
func (d *Document) InternalDocument() *InternalDocument {
return d.doc
}
// Key returns the key of this document.
func (d *Document) Key() key.Key {
return d.doc.key
}
// Checkpoint returns the checkpoint of this document.
func (d *Document) Checkpoint() change.Checkpoint {
return d.doc.checkpoint
}
// HasLocalChanges returns whether this document has local changes or not.
func (d *Document) HasLocalChanges() bool {
return d.doc.HasLocalChanges()
}
// Marshal returns the JSON encoding of this document.
func (d *Document) Marshal() string {
return d.doc.Marshal()
}
// CreateChangePack creates pack of the local changes to send to the server.
func (d *Document) CreateChangePack() *change.Pack {
return d.doc.CreateChangePack()
}
// SetActor sets actor into this document. This is also applied in the local
// changes the document has.
func (d *Document) SetActor(actor *time.ActorID) {
d.doc.SetActor(actor)
}
// ActorID returns ID of the actor currently editing the document.
func (d *Document) ActorID() *time.ActorID {
return d.doc.ActorID()
}
// SetStatus updates the status of this document.
func (d *Document) SetStatus(status statusType) {
d.doc.SetStatus(status)
}
// IsAttached returns the whether this document is attached or not.
func (d *Document) IsAttached() bool {
return d.doc.IsAttached()
}
// RootObject returns the internal root object of this document.
func (d *Document) RootObject() *crdt.Object {
return d.doc.RootObject()
}
// Root returns the root object of this document.
func (d *Document) Root() *json.Object {
d.ensureClone()
ctx := change.NewContext(d.doc.changeID.Next(), "", d.clone)
return json.NewObject(ctx, d.clone.Object())
}
// GarbageCollect purge elements that were removed before the given time.
func (d *Document) GarbageCollect(ticket *time.Ticket) int {
if d.clone != nil {
d.clone.GarbageCollect(ticket)
}
return d.doc.GarbageCollect(ticket)
}
// GarbageLen returns the count of removed elements.
func (d *Document) GarbageLen() int {
return d.doc.GarbageLen()
}
func (d *Document) ensureClone() {
if d.clone == nil {
d.clone = d.doc.root.DeepCopy()
}
}
func messageFromMsgAndArgs(msgAndArgs ...interface{}) string {
if len(msgAndArgs) == 0 {
return ""
}
if len(msgAndArgs) == 1 {
msg := msgAndArgs[0]
if msgAsStr, ok := msg.(string); ok {
return msgAsStr
}
return fmt.Sprintf("%+v", msg)
}
if len(msgAndArgs) > 1 {
return fmt.Sprintf(msgAndArgs[0].(string), msgAndArgs[1:]...)
}
return ""
}