forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchange_compactor.go
244 lines (220 loc) · 8.6 KB
/
change_compactor.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package ingest
import (
"encoding/base64"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/xdr"
)
// ChangeCompactor is a cache of ledger entry changes that squashes all
// changes within a single ledger. By doing this, it decreases number of DB
// queries sent to a DB to update the current state of the ledger.
// It has integrity checks built in so ex. removing an account that was
// previously removed returns an error. In such case verify.StateError is
// returned.
//
// The ChangeCompactor should not be used when ingesting from history archives
// because the history archive snapshots only contain CREATED changes.
// The ChangeCompactor is suited for compacting ledger entry changes derived
// from LedgerCloseMeta payloads because they typically contain a mix of
// CREATED, UPDATED, and REMOVED ledger entry changes and therefore may benefit
// from compaction.
//
// It applies changes to the cache using the following algorithm:
//
// 1. If the change is CREATED it checks if any change connected to given entry
// is already in the cache. If not, it adds CREATED change. Otherwise, if
// existing change is:
// a. CREATED it returns error because we can't add an entry that already
// exists.
// b. UPDATED it returns error because we can't add an entry that already
// exists.
// c. REMOVED it means that due to previous transitions we want to remove
// this from a DB what means that it already exists in a DB so we need to
// update the type of change to UPDATED.
// 2. If the change is UPDATE it checks if any change connected to given entry
// is already in the cache. If not, it adds UPDATE change. Otherwise, if
// existing change is:
// a. CREATED it means that due to previous transitions we want to create
// this in a DB what means that it doesn't exist in a DB so we need to
// update the entry but stay with CREATED type.
// b. UPDATED we simply update it with the new value.
// c. REMOVED it means that at this point in the ledger the entry is removed
// so updating it returns an error.
// 3. If the change is REMOVE it checks if any change connected to given entry
// is already in the cache. If not, it adds REMOVE change. Otherwise, if
// existing change is:
// a. CREATED it means that due to previous transitions we want to create
// this in a DB what means that it doesn't exist in a DB. If it was
// created and removed in the same ledger it's a noop so we remove entry
// from the cache.
// b. UPDATED we simply update it to be a REMOVE change because the UPDATE
// change means the entry exists in a DB.
// c. REMOVED it returns error because we can't remove an entry that was
// already removed.
type ChangeCompactor struct {
// ledger key => Change
cache map[string]Change
encodingBuffer *xdr.EncodingBuffer
}
// NewChangeCompactor returns a new ChangeCompactor.
func NewChangeCompactor() *ChangeCompactor {
return &ChangeCompactor{
cache: make(map[string]Change),
encodingBuffer: xdr.NewEncodingBuffer(),
}
}
// AddChange adds a change to ChangeCompactor. All changes are stored
// in memory. To get the final, squashed changes call GetChanges.
//
// Please note that the current ledger capacity in pubnet (max 1000 ops/ledger)
// makes ChangeCompactor safe to use in terms of memory usage. If the
// cache takes too much memory, you apply changes returned by GetChanges and
// create a new ChangeCompactor object to continue ingestion.
func (c *ChangeCompactor) AddChange(change Change) error {
switch {
case change.Pre == nil && change.Post != nil:
return c.addCreatedChange(change)
case change.Pre != nil && change.Post != nil:
return c.addUpdatedChange(change)
case change.Pre != nil && change.Post == nil:
return c.addRemovedChange(change)
default:
return errors.New("Unknown entry change state")
}
}
// addCreatedChange adds a change to the cache, but returns an error if create
// change is unexpected.
func (c *ChangeCompactor) addCreatedChange(change Change) error {
// safe, since we later cast to string (causing a copy)
key, err := change.Post.LedgerKey()
if err != nil {
return errors.Wrap(err, "error getting ledger key for new entry")
}
ledgerKey, err := c.encodingBuffer.UnsafeMarshalBinary(key)
if err != nil {
return errors.Wrap(err, "error marshaling ledger key for new entry")
}
ledgerKeyString := string(ledgerKey)
existingChange, exist := c.cache[ledgerKeyString]
if !exist {
c.cache[ledgerKeyString] = change
return nil
}
switch existingChange.LedgerEntryChangeType() {
case xdr.LedgerEntryChangeTypeLedgerEntryCreated:
return NewStateError(errors.Errorf(
"can't create an entry that already exists (ledger key = %s)",
base64.StdEncoding.EncodeToString(ledgerKey),
))
case xdr.LedgerEntryChangeTypeLedgerEntryUpdated:
return NewStateError(errors.Errorf(
"can't create an entry that already exists (ledger key = %s)",
base64.StdEncoding.EncodeToString(ledgerKey),
))
case xdr.LedgerEntryChangeTypeLedgerEntryRemoved:
// If existing type is removed it means that this entry does exist
// in a DB so we update entry change.
c.cache[ledgerKeyString] = Change{
Type: key.Type,
Pre: existingChange.Pre,
Post: change.Post,
}
default:
return errors.Errorf("Unknown LedgerEntryChangeType: %d", existingChange.LedgerEntryChangeType())
}
return nil
}
// addUpdatedChange adds a change to the cache, but returns an error if update
// change is unexpected.
func (c *ChangeCompactor) addUpdatedChange(change Change) error {
// safe, since we later cast to string (causing a copy)
key, err := change.Post.LedgerKey()
if err != nil {
return errors.Wrap(err, "error getting ledger key for updated entry")
}
ledgerKey, err := c.encodingBuffer.UnsafeMarshalBinary(key)
if err != nil {
return errors.Wrap(err, "error marshaling ledger key for updated entry")
}
ledgerKeyString := string(ledgerKey)
existingChange, exist := c.cache[ledgerKeyString]
if !exist {
c.cache[ledgerKeyString] = change
return nil
}
switch existingChange.LedgerEntryChangeType() {
case xdr.LedgerEntryChangeTypeLedgerEntryCreated:
// If existing type is created it means that this entry does not
// exist in a DB so we update entry change.
c.cache[ledgerKeyString] = Change{
Type: key.Type,
Pre: existingChange.Pre, // = nil
Post: change.Post,
}
case xdr.LedgerEntryChangeTypeLedgerEntryUpdated:
c.cache[ledgerKeyString] = Change{
Type: key.Type,
Pre: existingChange.Pre,
Post: change.Post,
}
case xdr.LedgerEntryChangeTypeLedgerEntryRemoved:
return NewStateError(errors.Errorf(
"can't update an entry that was previously removed (ledger key = %s)",
base64.StdEncoding.EncodeToString(ledgerKey),
))
default:
return errors.Errorf("Unknown LedgerEntryChangeType: %d", existingChange.Type)
}
return nil
}
// addRemovedChange adds a change to the cache, but returns an error if remove
// change is unexpected.
func (c *ChangeCompactor) addRemovedChange(change Change) error {
// safe, since we later cast to string (causing a copy)
key, err := change.Pre.LedgerKey()
if err != nil {
return errors.Wrap(err, "error getting ledger key for removed entry")
}
ledgerKey, err := c.encodingBuffer.UnsafeMarshalBinary(key)
if err != nil {
return errors.Wrap(err, "error marshaling ledger key for removed entry")
}
ledgerKeyString := string(ledgerKey)
existingChange, exist := c.cache[ledgerKeyString]
if !exist {
c.cache[ledgerKeyString] = change
return nil
}
switch existingChange.LedgerEntryChangeType() {
case xdr.LedgerEntryChangeTypeLedgerEntryCreated:
// If existing type is created it means that this will be no op.
// Entry was created and is now removed in a single ledger.
delete(c.cache, ledgerKeyString)
case xdr.LedgerEntryChangeTypeLedgerEntryUpdated:
c.cache[ledgerKeyString] = Change{
Type: key.Type,
Pre: existingChange.Pre,
Post: nil,
}
case xdr.LedgerEntryChangeTypeLedgerEntryRemoved:
return NewStateError(errors.Errorf(
"can't remove an entry that was previously removed (ledger key = %s)",
base64.StdEncoding.EncodeToString(ledgerKey),
))
default:
return errors.Errorf("Unknown LedgerEntryChangeType: %d", existingChange.Type)
}
return nil
}
// GetChanges returns a slice of Changes in the cache. The order of changes is
// random but each change is connected to a separate entry.
func (c *ChangeCompactor) GetChanges() []Change {
changes := make([]Change, 0, len(c.cache))
for _, entryChange := range c.cache {
changes = append(changes, entryChange)
}
return changes
}
// Size returns number of ledger entries in the cache.
func (c *ChangeCompactor) Size() int {
return len(c.cache)
}