MCPcopy Create free account
hub / github.com/encoder-run/operator / CheckAndSetReference

Method CheckAndSetReference

pkg/cache/redis/reference.go:52–105  ·  view source on GitHub ↗
(new, old *plumbing.Reference)

Source from the content-addressed store, hash-verified

50}
51
52func (r *ReferenceStorage) CheckAndSetReference(new, old *plumbing.Reference) error {
53 if new == nil {
54 return errors.New("new reference cannot be nil")
55 }
56
57 key := withNamespace(r.namespacePrefix, referencePrefix, new.Name().String())
58
59 // The transaction function. It will be retried if the watched keys change.
60 txnFunc := func(tx *redis.Tx) error {
61 // Get the current reference data.
62 data, err := tx.Get(ctx, key).Bytes()
63 if err != nil && err != redis.Nil {
64 return err
65 }
66
67 // If old reference is provided, ensure the current reference matches.
68 if old != nil {
69 if err == redis.Nil {
70 return storage.ErrReferenceHasChanged // Reference was expected but not found.
71 }
72
73 currentRef, err := jsonToReference(data)
74 if err != nil {
75 return err
76 }
77
78 if currentRef.Hash() != old.Hash() {
79 return storage.ErrReferenceHasChanged // Hash mismatch indicates concurrent update.
80 }
81 }
82
83 // If the reference was found or no old reference was provided, proceed to set the new reference.
84 newRefMap, err := referenceToMap(new)
85 if err != nil {
86 return err
87 }
88
89 newData, err := json.Marshal(newRefMap)
90 if err != nil {
91 return err
92 }
93
94 // Set the new reference data inside the transaction.
95 _, err = tx.Pipelined(ctx, func(pipe redis.Pipeliner) error {
96 pipe.Set(ctx, key, newData, 0)
97 return nil
98 })
99
100 return err
101 }
102
103 // Execute the transaction.
104 return r.client.Watch(ctx, txnFunc, key)
105}
106
107// PackRefs implements storage.Storer.
108func (s *ReferenceStorage) PackRefs() error {

Callers

nothing calls this directly

Calls 4

withNamespaceFunction · 0.85
jsonToReferenceFunction · 0.85
referenceToMapFunction · 0.85
StringMethod · 0.45

Tested by

no test coverage detected