2019-08-22 05:12:46 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2019-12-12 01:27:03 +00:00
|
|
|
"go.etcd.io/etcd/etcdserver/etcdserverpb"
|
2019-08-22 05:12:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func isCreate(txn *etcdserverpb.TxnRequest) *etcdserverpb.PutRequest {
|
|
|
|
if len(txn.Compare) == 1 &&
|
|
|
|
txn.Compare[0].Target == etcdserverpb.Compare_MOD &&
|
|
|
|
txn.Compare[0].Result == etcdserverpb.Compare_EQUAL &&
|
|
|
|
txn.Compare[0].GetModRevision() == 0 &&
|
|
|
|
len(txn.Failure) == 0 &&
|
|
|
|
len(txn.Success) == 1 &&
|
|
|
|
txn.Success[0].GetRequestPut() != nil {
|
|
|
|
return txn.Success[0].GetRequestPut()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LimitedServer) create(ctx context.Context, put *etcdserverpb.PutRequest, txn *etcdserverpb.TxnRequest) (*etcdserverpb.TxnResponse, error) {
|
|
|
|
if put.IgnoreLease {
|
|
|
|
return nil, unsupported("ignoreLease")
|
|
|
|
} else if put.IgnoreValue {
|
|
|
|
return nil, unsupported("ignoreValue")
|
|
|
|
} else if put.PrevKv {
|
|
|
|
return nil, unsupported("prevKv")
|
|
|
|
}
|
|
|
|
|
|
|
|
rev, err := l.backend.Create(ctx, string(put.Key), put.Value, put.Lease)
|
|
|
|
if err == ErrKeyExists {
|
|
|
|
return &etcdserverpb.TxnResponse{
|
|
|
|
Header: txnHeader(rev),
|
|
|
|
Succeeded: false,
|
|
|
|
}, nil
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &etcdserverpb.TxnResponse{
|
|
|
|
Header: txnHeader(rev),
|
|
|
|
Responses: []*etcdserverpb.ResponseOp{
|
|
|
|
{
|
|
|
|
Response: &etcdserverpb.ResponseOp_ResponsePut{
|
|
|
|
ResponsePut: &etcdserverpb.PutResponse{
|
|
|
|
Header: txnHeader(rev),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Succeeded: true,
|
|
|
|
}, nil
|
|
|
|
}
|