Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
G
gostnops
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wei.xuan
gostnops
Commits
ebe29f96
Commit
ebe29f96
authored
Apr 15, 2021
by
Randy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bulk insertion
parent
4bf08b8b
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
33 additions
and
2 deletions
+33
-2
client.go
database/kv/etcd/v3/client.go
+33
-2
No files found.
database/kv/etcd/v3/client.go
View file @
ebe29f96
...
...
@@ -36,6 +36,7 @@ var (
ErrNilETCDV3Client
=
perrors
.
New
(
"etcd raw client is nil"
)
// full describe the ERR
// ErrKVPairNotFound not found key
ErrKVPairNotFound
=
perrors
.
New
(
"k/v pair not found"
)
ErrKVListSizeIllegal
=
perrors
.
New
(
"k/v List is empty or kList size not equal to vList size"
)
)
// NewConfigClient create new Client
...
...
@@ -211,7 +212,7 @@ func (c *Client) GetEndPoints() []string {
return
c
.
endpoints
}
// if k not exist will put k/v in etcd, otherwise return
nil
// if k not exist will put k/v in etcd, otherwise return
error
func
(
c
*
Client
)
put
(
k
string
,
v
string
,
opts
...
clientv3
.
OpOption
)
error
{
rawClient
:=
c
.
GetRawClient
()
...
...
@@ -226,6 +227,36 @@ func (c *Client) put(k string, v string, opts ...clientv3.OpOption) error {
return
err
}
// if k in bulk insertion not exist all, then put all k/v in etcd, otherwise return error
func
(
c
*
Client
)
batchPut
(
kList
[]
string
,
vList
[]
string
,
opts
...
clientv3
.
OpOption
)
error
{
rawClient
:=
c
.
GetRawClient
()
if
rawClient
==
nil
{
return
ErrNilETCDV3Client
}
kLen
:=
len
(
kList
)
vLen
:=
len
(
vList
)
if
kLen
==
0
||
vLen
==
0
||
kLen
!=
vLen
{
return
ErrKVListSizeIllegal
}
var
cs
[]
clientv3
.
Cmp
var
ops
[]
clientv3
.
Op
for
i
,
k
:=
range
kList
{
v
:=
vList
[
i
]
cs
=
append
(
cs
,
clientv3
.
Compare
(
clientv3
.
Version
(
k
),
"<"
,
1
))
ops
=
append
(
ops
,
clientv3
.
OpPut
(
k
,
v
,
opts
...
))
}
_
,
err
:=
rawClient
.
Txn
(
c
.
ctx
)
.
If
(
cs
...
)
.
Then
(
ops
...
)
.
Commit
()
return
err
}
// if k not exist will put k/v in etcd
// if k is already exist in etcd, replace it
func
(
c
*
Client
)
update
(
k
string
,
v
string
,
opts
...
clientv3
.
OpOption
)
error
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment