Only set precedence on write path

This commit is contained in:
Paul Banks 2018-06-12 12:26:12 +01:00 committed by Jack Pearkes
parent 22b95283e9
commit 1283373a64
2 changed files with 6 additions and 11 deletions

View File

@ -106,11 +106,6 @@ func (s *Snapshot) Intentions() (structs.Intentions, error) {
var ret structs.Intentions
for wrapped := ixns.Next(); wrapped != nil; wrapped = ixns.Next() {
// Update precedence values for consistency (i.e. the object returned should
// be usable/comparable to the ones returned from Intentions() and Match()
// etc.)
ixn := wrapped.(*structs.Intention)
ixn.UpdatePrecedence()
ret = append(ret, wrapped.(*structs.Intention))
}
@ -147,9 +142,7 @@ func (s *Store) Intentions(ws memdb.WatchSet) (uint64, structs.Intentions, error
var results structs.Intentions
for ixn := iter.Next(); ixn != nil; ixn = iter.Next() {
ixnReal := ixn.(*structs.Intention)
ixnReal.UpdatePrecedence()
results = append(results, ixnReal)
results = append(results, ixn.(*structs.Intention))
}
// Sort by precedence just because that's nicer and probably what most clients
@ -180,6 +173,9 @@ func (s *Store) intentionSetTxn(tx *memdb.Txn, idx uint64, ixn *structs.Intentio
return ErrMissingIntentionID
}
// Ensure Precedence is populated correctly on "write"
ixn.UpdatePrecedence()
// Check for an existing intention
existing, err := tx.First(intentionsTableName, "id", ixn.ID)
if err != nil {
@ -324,9 +320,7 @@ func (s *Store) IntentionMatch(ws memdb.WatchSet, args *structs.IntentionQueryMa
ws.Add(iter.WatchCh())
for ixn := iter.Next(); ixn != nil; ixn = iter.Next() {
ixnReal := ixn.(*structs.Intention)
ixnReal.UpdatePrecedence()
ixns = append(ixns, ixnReal)
ixns = append(ixns, ixn.(*structs.Intention))
}
}

View File

@ -61,6 +61,7 @@ func TestStore_IntentionSetGet_basic(t *testing.T) {
ModifyIndex: 1,
},
}
expected.UpdatePrecedence()
ws = memdb.NewWatchSet()
idx, actual, err := s.IntentionGet(ws, ixn.ID)