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
| package document
import (
protocol "wellquite.org/edist/protocol/go"
)
type WordManager struct {
root *protocol.Word
orderedWords []*protocol.Word
wordsById map[protocol.WordId]*protocol.Word
}
func NewWordManager() *WordManager {
root := &protocol.Word{
WordId: protocol.WordId{
ClientId: 0,
Counter: 1,
},
}
return &WordManager{
root: root,
orderedWords: []*protocol.Word{root},
wordsById: map[protocol.WordId]*protocol.Word{root.WordId: root},
}
}
type UpdatePair struct {
original *protocol.Word
updated *protocol.Word
isNewWord bool
}
func (self *WordManager) FilterUpdatesForNewer(updates []protocol.Word) []*UpdatePair {
filteredUpdates := make([]*UpdatePair, 0, len(updates))
for idx, word := range updates {
existing, found := self.wordsById[word.WordId]
if found {
if word.Version.GreaterThan(existing.Version) {
filteredUpdates = append(filteredUpdates, &UpdatePair{
original: existing,
updated: &updates[idx],
isNewWord: false,
})
}
} else {
filteredUpdates = append(filteredUpdates, &UpdatePair{
original: self.NewWord(word.WordId),
updated: &updates[idx],
isNewWord: true,
})
}
}
return filteredUpdates
}
func (self *WordManager) FilterUpdatesForKnown(updates []*UpdatePair) []protocol.Word {
words := make([]protocol.Word, 0, len(updates))
for _, update := range updates {
if _, found := self.wordsById[update.original.WordId]; found {
words = append(words, *update.original)
}
}
return words
}
func (self *WordManager) ApplyUpdates(updates []*UpdatePair) {
for _, update := range updates {
original, updated := update.original, update.updated
if updated.Version.GreaterThan(original.Version) {
original.Version = updated.Version
}
original.Letters = updated.Letters
original.Links = updated.Links
}
}
func (self *WordManager) NewWord(wordId protocol.WordId) *protocol.Word {
word := &protocol.Word{
WordId: wordId,
}
self.wordsById[wordId] = word
return word
}
func (self *WordManager) GC() {
if len(self.wordsById) < 2 {
return
}
wordsById := make(map[protocol.WordId]*protocol.Word, len(self.wordsById))
orderedWords := make([]*protocol.Word, 0, len(self.orderedWords))
for cur := self.root; cur != nil; {
orderedWords = append(orderedWords, cur)
wordsById[cur.WordId] = cur
if cur.Links.Next == nil {
break
}
cur = self.wordsById[*cur.Links.Next]
}
self.wordsById = wordsById
self.orderedWords = orderedWords
}
func (self *WordManager) CreateCheckpoint() *protocol.Message {
words := make([]protocol.Word, len(self.orderedWords))
for idx, word := range self.orderedWords {
words[idx] = *word
}
return &protocol.Message{
Updates: &words,
}
}
// How do we get from b to a.
func (a *WordManager) Delta(b *WordManager) *protocol.Message {
words := make([]protocol.Word, 0, len(a.orderedWords))
for _, wordA := range a.orderedWords {
wordB, foundB := b.wordsById[wordA.WordId]
if !foundB || wordA.Version.GreaterThan(wordB.Version) {
words = append(words, *wordA)
}
}
return &protocol.Message{
Updates: &words,
}
}
func (self *WordManager) String() string {
str := ""
for _, word := range self.orderedWords {
str += " " + word.Letters
}
if len(str) > 0 {
return str[1:]
}
return str
}
|