Create will add the key/value pair if it does not exist.
(key string, value []byte)
| 704 | |
| 705 | // Create will add the key/value pair if it does not exist. |
| 706 | func (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. |
| 729 | func (kv *kvs) Update(key string, value []byte, revision uint64) (uint64, error) { |