Skip to content

Commit 3283232

Browse files
committed
add explicitDeltest to the transaction for rollback
1 parent 71fc574 commit 3283232

File tree

7 files changed

+33
-18
lines changed

7 files changed

+33
-18
lines changed

pkg/datastore/transaction_rpc.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (d *Datastore) SdcpbTransactionIntentToInternalTI(ctx context.Context, req
5353
ti.AddUpdates(Updates)
5454

5555
// add the deletes
56-
ti.AddDeletes(req.Deletes)
56+
ti.AddExplicitDeletes(req.Deletes)
5757

5858
return ti, nil
5959
}
@@ -214,11 +214,11 @@ func (d *Datastore) lowlevelTransactionSet(ctx context.Context, transaction *typ
214214
if err != nil {
215215
return nil, err
216216
}
217-
// clear the owners existing explicit delete entries
218-
root.RemoveExplicitDeletes(intent.GetName())
217+
// clear the owners existing explicit delete entries, retrieving the old entries for storing in the transaction for possible rollback
218+
oldExplicitDeletes := root.RemoveExplicitDeletes(intent.GetName())
219219

220220
// store the old intent content in the transaction as the old intent.
221-
err = transaction.AddIntentContent(intent.GetName(), types.TransactionIntentOld, oldIntentContent.GetFirstPriorityValue(), oldIntentContent)
221+
err = transaction.AddIntentContent(intent.GetName(), types.TransactionIntentOld, oldIntentContent.GetFirstPriorityValue(), oldIntentContent, oldExplicitDeletes)
222222
if err != nil {
223223
return nil, err
224224
}

pkg/datastore/types/transaction.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/sdcio/data-server/pkg/tree"
99
treetypes "github.com/sdcio/data-server/pkg/tree/types"
10+
sdcpb "github.com/sdcio/sdc-protos/sdcpb"
1011
)
1112

1213
type Transaction struct {
@@ -138,7 +139,7 @@ func (t *Transaction) AddTransactionIntent(ti *TransactionIntent, tit Transactio
138139
}
139140

140141
// AddIntentContent add the content of an intent. If the intent did not exist, add the name of the intent and content == nil.
141-
func (t *Transaction) AddIntentContent(name string, tit TransactionIntentType, priority int32, content treetypes.UpdateSlice) error {
142+
func (t *Transaction) AddIntentContent(name string, tit TransactionIntentType, priority int32, content treetypes.UpdateSlice, explicitDeletes *sdcpb.PathSet) error {
142143
dstMap := t.getTransactionIntentTypeMap(tit)
143144
_, exists := dstMap[name]
144145
if exists {
@@ -148,6 +149,7 @@ func (t *Transaction) AddIntentContent(name string, tit TransactionIntentType, p
148149
dstMap[name] = ti
149150

150151
ti.AddUpdates(content)
152+
ti.AddExplicitDeletes(explicitDeletes.ToPathSlice())
151153
return nil
152154
}
153155

pkg/datastore/types/transaction_intent.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ type TransactionIntent struct {
1717
// it will be stored and used for change calculation but will be excluded when claculating actual deviations.
1818
deviation bool
1919
deleteIgnoreNonExisting bool
20-
deletes *sdcpb.PathSet
20+
explicitDeletes *sdcpb.PathSet
2121
}
2222

2323
func NewTransactionIntent(name string, priority int32) *TransactionIntent {
2424
return &TransactionIntent{
25-
name: name,
26-
updates: make(treetypes.UpdateSlice, 0),
27-
priority: priority,
28-
deletes: sdcpb.NewPathSet(),
25+
name: name,
26+
updates: make(treetypes.UpdateSlice, 0),
27+
priority: priority,
28+
explicitDeletes: sdcpb.NewPathSet(),
2929
}
3030
}
3131

@@ -46,7 +46,7 @@ func (ti *TransactionIntent) GetUpdates() treetypes.UpdateSlice {
4646
}
4747

4848
func (ti *TransactionIntent) GetDeletes() *sdcpb.PathSet {
49-
return ti.deletes
49+
return ti.explicitDeletes
5050
}
5151

5252
func (ti *TransactionIntent) GetOnlyIntended() bool {
@@ -86,8 +86,8 @@ func (ti *TransactionIntent) AddUpdate(u *treetypes.Update) {
8686
ti.updates = append(ti.updates, u)
8787
}
8888

89-
func (ti *TransactionIntent) AddDeletes(p []*sdcpb.Path) {
89+
func (ti *TransactionIntent) AddExplicitDeletes(p []*sdcpb.Path) {
9090
for _, x := range p {
91-
ti.deletes.AddPath(x)
91+
ti.explicitDeletes.AddPath(x)
9292
}
9393
}

pkg/tree/intent_path_mapping.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,13 @@ func (dp *DeletePathSet) DeepCopy() *DeletePathSet {
2525
return result
2626
}
2727

28-
func (dp *DeletePathSet) RemoveIntentDeletes(intentName string) {
29-
delete(dp.data, intentName)
28+
func (dp *DeletePathSet) RemoveIntentDeletes(intentName string) *sdcpb.PathSet {
29+
if data, exists := dp.data[intentName]; exists {
30+
result := data.GetPathSet()
31+
delete(dp.data, intentName)
32+
return result
33+
}
34+
return sdcpb.NewPathSet()
3035
}
3136

3237
func (dp *DeletePathSet) Add(intentName string, prio int32, pathset *sdcpb.PathSet) {
@@ -79,6 +84,10 @@ func (ddp *DeletePathPrio) DeepCopy() *DeletePathPrio {
7984
return result
8085
}
8186

87+
func (dpp *DeletePathPrio) GetPathSet() *sdcpb.PathSet {
88+
return dpp.paths
89+
}
90+
8291
func (dpp *DeletePathPrio) PathItems() iter.Seq[*sdcpb.Path] {
8392
return dpp.paths.Items()
8493
}

pkg/tree/json.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func (s *sharedEntryAttributes) toJsonInternal(onlyNewOrUpdated bool, ietf bool)
118118
}
119119

120120
case *sdcpb.SchemaElem_Leaflist, *sdcpb.SchemaElem_Field:
121-
if s.leafVariants.shouldDelete() {
121+
if s.leafVariants.canDeleteBranch(false) {
122122
return nil, nil
123123
}
124124
le := s.leafVariants.GetHighestPrecedence(onlyNewOrUpdated, false, false)

pkg/tree/leaf_entry_filter.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ func FilterDeleted(l *LeafEntry) bool {
1919
return l.GetDeleteFlag()
2020
}
2121

22+
func FilterDeletedNotExplicitDelete(l *LeafEntry) bool {
23+
return l.GetDeleteFlag() && !l.GetExplicitDeleteFlag()
24+
}
25+
2226
// FilterNew Accepts New LeafEntries
2327
func FilterNew(l *LeafEntry) bool {
2428
return l.GetNewFlag()

pkg/tree/root_entry.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ func (r *RootEntry) DeepCopy(ctx context.Context) (*RootEntry, error) {
7171
return result, nil
7272
}
7373

74-
func (r *RootEntry) RemoveExplicitDeletes(intentName string) {
75-
r.explicitDeletes.RemoveIntentDeletes(intentName)
74+
func (r *RootEntry) RemoveExplicitDeletes(intentName string) *sdcpb.PathSet {
75+
return r.explicitDeletes.RemoveIntentDeletes(intentName)
7676
}
7777

7878
func (r *RootEntry) AddUpdatesRecursive(ctx context.Context, us types.UpdateSlice, flags *types.UpdateInsertFlags) error {

0 commit comments

Comments
 (0)