@@ -25,55 +25,57 @@ import (
25
25
//
26
26
// 1. If the change is CREATED it checks if any change connected to given entry
27
27
// is already in the cache. If not, it adds CREATED change. Otherwise, if
28
- // existing change is
29
- // a. CREATED: return an error because we can't add an entry that already exists.
30
- // b. UPDATED: return an error because we can't add an entry that already exists.
31
- // c. REMOVED: entry exists in the DB but was marked for removal; change the type
32
- // to UPDATED and update the new value.
33
- // d. RESTORED: return an error as the RESTORED change indicates the entry already
28
+ // existing change is:
29
+ // a. CREATED it returns error because we can't add an entry that already
34
30
// exists.
35
- //
31
+ // b. UPDATED it returns error because we can't add an entry that already
32
+ // exists.
33
+ // c. REMOVED it means that due to previous transitions we want to remove
34
+ // this from a DB what means that it already exists in a DB so we need to
35
+ // update the type of change to UPDATED.
36
+ // d. RESTORED it returns an error as the RESTORED change indicates the
37
+ // entry already exists.
36
38
// 2. If the change is UPDATE it checks if any change connected to given entry
37
39
// is already in the cache. If not, it adds UPDATE change. Otherwise, if
38
- // existing change is
39
- // a. CREATED: We want to create this in a DB which means that it doesn't exist
40
- // in a DB so we need to update the entry but stay with CREATED type.
41
- // b. UPDATED: update it with the new value.
40
+ // existing change is:
41
+ // a. CREATED it means that due to previous transitions we want to create
42
+ // this in a DB what means that it doesn't exist in a DB so we need to
43
+ // update the entry but stay with CREATED type.
44
+ // b. UPDATED we simply update it with the new value.
42
45
// c. REMOVED it means that at this point in the ledger the entry is removed
43
46
// so updating it returns an error.
44
- // d. RESTORED: update it with the new value but keep the type as RESTORED.
45
- //
46
- // 3. If the change is REMOVED, it checks if any change related to the given entry
47
- // already exists in the cache. If not, it adds the `REMOVED` change. Otherwise,
48
- // if existing change is
49
- // a. CREATED: due to previous transitions we want to create
50
- // this in a DB which means that it doesn't exist in a DB. If it was created and
51
- // removed in the same ledger it's a noop so we remove the entry from the cache.
52
- // b. UPDATED: update it to be a REMOVE change because the UPDATE change means
53
- // the entry exists in a DB.
54
- // c. REMOVED: return an error because we can't remove an entry that was already
55
- // removed.
56
- // d. RESTORED: if the item was previously restored from an archived state, it means
57
- // it already exists in the DB, so change it to REMOVED type. If the restored item
58
- // was evicted, it doesn't exist in the DB, so it's a noop so remove the entry from
59
- // the cache.
60
- //
47
+ // d. RESTORED we update it with the new value but keep the change type as RESTORED.
48
+ // 3. If the change is REMOVE it checks if any change connected to given entry
49
+ // is already in the cache. If not, it adds REMOVE change. Otherwise, if
50
+ // existing change is:
51
+ // a. CREATED it means that due to previous transitions we want to create
52
+ // this in a DB what means that it doesn't exist in a DB. If it was
53
+ // created and removed in the same ledger it's a noop so we remove entry
54
+ // from the cache.
55
+ // b. UPDATED we simply update it to be a REMOVE change because the UPDATE
56
+ // change means the entry exists in a DB.
57
+ // c. REMOVED it returns error because we can't remove an entry that was
58
+ // already removed.
59
+ // d. RESTORED it means the entry doesn't exist in the DB, so it's a noop
60
+ // so remove the entry from the cache.
61
61
// 4. If the change is RESTORED it checks if any change related to the given entry
62
62
// already exists in the cache. If not, it adds the RESTORED change. Otherwise,
63
63
// returns an error since restoration is only possible for previously archived/evicted
64
64
// entries. If the entry was created, updated or removed within the same ledger, restoration
65
65
// is not possible.
66
66
type ChangeCompactor struct {
67
67
// ledger key => Change
68
- cache map [string ]Change
69
- encodingBuffer * xdr.EncodingBuffer
68
+ cache map [string ]Change
69
+ encodingBuffer * xdr.EncodingBuffer
70
+ emitExpiredEntriesRemovedChange bool
70
71
}
71
72
72
73
// NewChangeCompactor returns a new ChangeCompactor.
73
- func NewChangeCompactor () * ChangeCompactor {
74
+ func NewChangeCompactor (emitExpiredEntriesRemovedChange bool ) * ChangeCompactor {
74
75
return & ChangeCompactor {
75
- cache : make (map [string ]Change ),
76
- encodingBuffer : xdr .NewEncodingBuffer (),
76
+ cache : make (map [string ]Change ),
77
+ encodingBuffer : xdr .NewEncodingBuffer (),
78
+ emitExpiredEntriesRemovedChange : emitExpiredEntriesRemovedChange ,
77
79
}
78
80
}
79
81
@@ -169,17 +171,21 @@ func (c *ChangeCompactor) addUpdatedChange(change Change) error {
169
171
170
172
switch existingChange .ChangeType {
171
173
case xdr .LedgerEntryChangeTypeLedgerEntryCreated :
172
- fallthrough
174
+ // If existing type is created it means that this entry does not
175
+ // exist in a DB so we update entry change.
176
+ c .cache [ledgerKeyString ] = Change {
177
+ Type : key .Type ,
178
+ Pre : existingChange .Pre , // = nil
179
+ Post : change .Post ,
180
+ }
173
181
case xdr .LedgerEntryChangeTypeLedgerEntryUpdated :
174
182
fallthrough
175
183
case xdr .LedgerEntryChangeTypeLedgerEntryRestored :
176
- // If existing type is created it means that this entry does not
177
- // exist in a DB so we update entry change.
178
184
c .cache [ledgerKeyString ] = Change {
179
185
Type : key .Type ,
180
- Pre : existingChange .Pre , // = nil for created type
186
+ Pre : existingChange .Pre ,
181
187
Post : change .Post ,
182
- ChangeType : existingChange .ChangeType ,
188
+ ChangeType : existingChange .ChangeType , //keep the existing change type
183
189
}
184
190
case xdr .LedgerEntryChangeTypeLedgerEntryRemoved :
185
191
return NewStateError (errors .Errorf (
@@ -232,17 +238,16 @@ func (c *ChangeCompactor) addRemovedChange(change Change) error {
232
238
base64 .StdEncoding .EncodeToString (ledgerKey ),
233
239
))
234
240
case xdr .LedgerEntryChangeTypeLedgerEntryRestored :
235
- if existingChange .Pre == nil {
236
- // Entry was created and removed in the same ledger; deleting it is effectively a noop.
237
- delete (c .cache , ledgerKeyString )
238
- } else {
239
- // If the entry exists, we mark it as removed by setting Post to nil.
241
+ if c .emitExpiredEntriesRemovedChange {
240
242
c .cache [ledgerKeyString ] = Change {
241
- Type : existingChange .Type ,
242
- Pre : existingChange .Pre ,
243
+ Type : key .Type ,
244
+ Pre : change .Pre ,
243
245
Post : nil ,
244
246
ChangeType : change .ChangeType ,
245
247
}
248
+ } else {
249
+ // Entry was restored and removed in the same ledger; deleting it is effectively a noop.
250
+ delete (c .cache , ledgerKeyString )
246
251
}
247
252
default :
248
253
return errors .Errorf ("Unknown LedgerEntryChangeType: %d" , existingChange .ChangeType )
0 commit comments