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

Method Create

jetstream/kv.go:1059–1086  ·  view source on GitHub ↗

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

(ctx context.Context, key string, value []byte, opts ...KVCreateOpt)

Source from the content-addressed store, hash-verified

1057
1058// Create will add the key/value pair if it does not exist.
1059func (kv *kvs) Create(ctx context.Context, key string, value []byte, opts ...KVCreateOpt) (revision uint64, err error) {
1060 var o createOpts
1061 for _, opt := range opts {
1062 if opt != nil {
1063 if err := opt.configureCreate(&o); err != nil {
1064 return 0, err
1065 }
1066 }
1067 }
1068
1069 v, err := kv.updateRevision(ctx, key, value, 0, o.ttl)
1070 if err == nil {
1071 return v, nil
1072 }
1073
1074 if e, err := kv.get(ctx, key, kvLatestRevision); errors.Is(err, ErrKeyDeleted) {
1075 return kv.updateRevision(ctx, key, value, e.Revision(), o.ttl)
1076 }
1077
1078 // Check if the expected last subject sequence is not zero which implies
1079 // the key already exists.
1080 if errors.Is(err, ErrKeyExists) {
1081 jserr := ErrKeyExists.(*jsError)
1082 return 0, fmt.Errorf("%w: %s", err, jserr.message)
1083 }
1084
1085 return 0, err
1086}
1087
1088// Update will update the value if the latest revision matches.
1089func (kv *kvs) Update(ctx context.Context, key string, value []byte, revision uint64) (uint64, error) {

Callers

nothing calls this directly

Calls 6

updateRevisionMethod · 0.95
getMethod · 0.95
ErrorfMethod · 0.80
configureCreateMethod · 0.65
RevisionMethod · 0.65
IsMethod · 0.45

Tested by

no test coverage detected