MCPcopy
hub / github.com/nats-io/nats.go / Create

Method Create

kv.go:706–726  ·  view source on GitHub ↗

Create will add the key/value pair if it does not exist.

(key string, value []byte)

Source from the content-addressed store, hash-verified

704
705// Create will add the key/value pair if it does not exist.
706func (kv *kvs) Create(key string, value []byte) (revision uint64, err error) {
707 v, err := kv.Update(key, value, 0)
708 if err == nil {
709 return v, nil
710 }
711
712 // TODO(dlc) - Since we have tombstones for DEL ops for watchers, this could be from that
713 // so we need to double check.
714 if e, err := kv.get(key, kvLatestRevision); errors.Is(err, ErrKeyDeleted) {
715 return kv.Update(key, value, e.Revision())
716 }
717
718 // Check if the expected last subject sequence is not zero which implies
719 // the key already exists.
720 if errors.Is(err, ErrKeyExists) {
721 jserr := ErrKeyExists.(*jsError)
722 return 0, fmt.Errorf("%w: %s", err, jserr.message)
723 }
724
725 return 0, err
726}
727
728// Update will update the value if the latest revision matches.
729func (kv *kvs) Update(key string, value []byte, revision uint64) (uint64, error) {

Callers

nothing calls this directly

Calls 5

UpdateMethod · 0.95
getMethod · 0.95
ErrorfMethod · 0.80
RevisionMethod · 0.65
IsMethod · 0.45

Tested by

no test coverage detected