Add kv txn get-not-exists operation.
This commit is contained in:
parent
9780aba54a
commit
d771725a14
3
.changelog/14474.txt
Normal file
3
.changelog/14474.txt
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
```release-note:feature
|
||||||
|
http: Add new `get-or-empty` operation to the txn api. Refer to the [API docs](https://www.consul.io/api-docs/txn#kv-operations) for more information.
|
||||||
|
```
|
|
@ -49,7 +49,7 @@ func kvsPreApply(logger hclog.Logger, srv *Server, authz resolver.Result, op api
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
case api.KVGet, api.KVGetTree:
|
case api.KVGet, api.KVGetTree, api.KVGetOrEmpty:
|
||||||
// Filtering for GETs is done on the output side.
|
// Filtering for GETs is done on the output side.
|
||||||
|
|
||||||
case api.KVCheckSession, api.KVCheckIndex:
|
case api.KVCheckSession, api.KVCheckIndex:
|
||||||
|
|
|
@ -60,6 +60,13 @@ func (s *Store) txnKVS(tx WriteTxn, idx uint64, op *structs.TxnKVOp) (structs.Tx
|
||||||
err = fmt.Errorf("key %q doesn't exist", op.DirEnt.Key)
|
err = fmt.Errorf("key %q doesn't exist", op.DirEnt.Key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case api.KVGetOrEmpty:
|
||||||
|
_, entry, err = kvsGetTxn(tx, nil, op.DirEnt.Key, op.DirEnt.EnterpriseMeta)
|
||||||
|
if entry == nil && err == nil {
|
||||||
|
entry = &op.DirEnt
|
||||||
|
entry.Value = nil
|
||||||
|
}
|
||||||
|
|
||||||
case api.KVGetTree:
|
case api.KVGetTree:
|
||||||
var entries structs.DirEntries
|
var entries structs.DirEntries
|
||||||
_, entries, err = s.kvsListTxn(tx, nil, op.DirEnt.Key, op.DirEnt.EnterpriseMeta)
|
_, entries, err = s.kvsListTxn(tx, nil, op.DirEnt.Key, op.DirEnt.EnterpriseMeta)
|
||||||
|
@ -95,7 +102,7 @@ func (s *Store) txnKVS(tx WriteTxn, idx uint64, op *structs.TxnKVOp) (structs.Tx
|
||||||
// value (we have to clone so we don't modify the entry being used by
|
// value (we have to clone so we don't modify the entry being used by
|
||||||
// the state store).
|
// the state store).
|
||||||
if entry != nil {
|
if entry != nil {
|
||||||
if op.Verb == api.KVGet {
|
if op.Verb == api.KVGet || op.Verb == api.KVGetOrEmpty {
|
||||||
result := structs.TxnResult{KV: entry}
|
result := structs.TxnResult{KV: entry}
|
||||||
return structs.TxnResults{&result}, nil
|
return structs.TxnResults{&result}, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -577,6 +577,22 @@ func TestStateStore_Txn_KVS(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
&structs.TxnOp{
|
||||||
|
KV: &structs.TxnKVOp{
|
||||||
|
Verb: api.KVGetOrEmpty,
|
||||||
|
DirEnt: structs.DirEntry{
|
||||||
|
Key: "foo/update",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
&structs.TxnOp{
|
||||||
|
KV: &structs.TxnKVOp{
|
||||||
|
Verb: api.KVGetOrEmpty,
|
||||||
|
DirEnt: structs.DirEntry{
|
||||||
|
Key: "foo/not-exists",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
&structs.TxnOp{
|
&structs.TxnOp{
|
||||||
KV: &structs.TxnKVOp{
|
KV: &structs.TxnKVOp{
|
||||||
Verb: api.KVCheckIndex,
|
Verb: api.KVCheckIndex,
|
||||||
|
@ -702,6 +718,22 @@ func TestStateStore_Txn_KVS(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
&structs.TxnResult{
|
||||||
|
KV: &structs.DirEntry{
|
||||||
|
Key: "foo/update",
|
||||||
|
Value: []byte("stale"),
|
||||||
|
RaftIndex: structs.RaftIndex{
|
||||||
|
CreateIndex: 5,
|
||||||
|
ModifyIndex: 5,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
&structs.TxnResult{
|
||||||
|
KV: &structs.DirEntry{
|
||||||
|
Key: "foo/not-exists",
|
||||||
|
Value: nil,
|
||||||
|
},
|
||||||
|
},
|
||||||
&structs.TxnResult{
|
&structs.TxnResult{
|
||||||
KV: &structs.DirEntry{
|
KV: &structs.DirEntry{
|
||||||
|
|
||||||
|
|
|
@ -67,6 +67,7 @@ const (
|
||||||
KVLock KVOp = "lock"
|
KVLock KVOp = "lock"
|
||||||
KVUnlock KVOp = "unlock"
|
KVUnlock KVOp = "unlock"
|
||||||
KVGet KVOp = "get"
|
KVGet KVOp = "get"
|
||||||
|
KVGetOrEmpty KVOp = "get-or-empty"
|
||||||
KVGetTree KVOp = "get-tree"
|
KVGetTree KVOp = "get-tree"
|
||||||
KVCheckSession KVOp = "check-session"
|
KVCheckSession KVOp = "check-session"
|
||||||
KVCheckIndex KVOp = "check-index"
|
KVCheckIndex KVOp = "check-index"
|
||||||
|
|
|
@ -266,20 +266,21 @@ look like this:
|
||||||
The following tables summarize the available verbs and the fields that apply to
|
The following tables summarize the available verbs and the fields that apply to
|
||||||
those operations ("X" means a field is required and "O" means it is optional):
|
those operations ("X" means a field is required and "O" means it is optional):
|
||||||
|
|
||||||
| Verb | Operation | Key | Value | Flags | Index | Session |
|
| Verb | Operation | Key | Value | Flags | Index | Session |
|
||||||
| ------------------ | --------------------------------------- | :-: | :---: | :---: | :---: | :-----: |
|
| ------------------ | ----------------------------------------- | :-: | :---: | :---: | :---: | :-----: |
|
||||||
| `set` | Sets the `Key` to the given `Value` | `x` | `x` | `o` | | |
|
| `set` | Sets the `Key` to the given `Value` | `x` | `x` | `o` | | |
|
||||||
| `cas` | Sets, but with CAS semantics | `x` | `x` | `o` | `x` | |
|
| `cas` | Sets, but with CAS semantics | `x` | `x` | `o` | `x` | |
|
||||||
| `lock` | Lock with the given `Session` | `x` | `x` | `o` | | `x` |
|
| `lock` | Lock with the given `Session` | `x` | `x` | `o` | | `x` |
|
||||||
| `unlock` | Unlock with the given `Session` | `x` | `x` | `o` | | `x` |
|
| `unlock` | Unlock with the given `Session` | `x` | `x` | `o` | | `x` |
|
||||||
| `get` | Get the key, fails if it does not exist | `x` | | | | |
|
| `get` | Get the key, fails if it does not exist | `x` | | | | |
|
||||||
| `get-tree` | Gets all keys with the prefix | `x` | | | | |
|
| `get-or-empty` | Get the key, or null if it does not exist | `x` | | | | |
|
||||||
| `check-index` | Fail if modify index != index | `x` | | | `x` | |
|
| `get-tree` | Gets all keys with the prefix | `x` | | | | |
|
||||||
| `check-session` | Fail if not locked by session | `x` | | | | `x` |
|
| `check-index` | Fail if modify index != index | `x` | | | `x` | |
|
||||||
| `check-not-exists` | Fail if key exists | `x` | | | | |
|
| `check-session` | Fail if not locked by session | `x` | | | | `x` |
|
||||||
| `delete` | Delete the key | `x` | | | | |
|
| `check-not-exists` | Fail if key exists | `x` | | | | |
|
||||||
| `delete-tree` | Delete all keys with a prefix | `x` | | | | |
|
| `delete` | Delete the key | `x` | | | | |
|
||||||
| `delete-cas` | Delete, but with CAS semantics | `x` | | | `x` | |
|
| `delete-tree` | Delete all keys with a prefix | `x` | | | | |
|
||||||
|
| `delete-cas` | Delete, but with CAS semantics | `x` | | | `x` | |
|
||||||
|
|
||||||
#### Node Operations
|
#### Node Operations
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue