Skip to content

Commit

Permalink
Mercury can BatchGet keys
Browse files Browse the repository at this point in the history
  • Loading branch information
zc authored and CMGS committed Jul 18, 2019
1 parent 8cfb874 commit d2229cd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
18 changes: 13 additions & 5 deletions store/etcdv3/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,24 @@ func (m *Mercury) GetContainer(ctx context.Context, ID string) (*types.Container
}

// GetContainers get many containers
// TODO merge etcd ops
func (m *Mercury) GetContainers(ctx context.Context, IDs []string) (containers []*types.Container, err error) {
keys := []string{}
for _, ID := range IDs {
container, err := m.GetContainer(ctx, ID)
if err != nil {
return containers, err
keys = append(keys, fmt.Sprintf(containerInfoKey, ID))
}
tnxResponse, err := m.BatchGet(ctx, IDs)
for _, responseOp := range tnxResponse.Responses {
val := []byte{}
if val, err = responseOp.Marshal(); err != nil {
return
}
container := &types.Container{}
if err = json.Unmarshal(val, container); err != nil {
return
}
containers = append(containers, container)
}
return containers, err
return
}

// ContainerDeployed store deployed container info
Expand Down
11 changes: 11 additions & 0 deletions store/etcdv3/mercury.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,17 @@ func (m *Mercury) GetOne(ctx context.Context, key string, opts ...clientv3.OpOpt
return resp.Kvs[0], nil
}

// BatchGet gets keys
func (m *Mercury) BatchGet(ctx context.Context, keys []string, opt ...clientv3.OpOption) (txnResponse *clientv3.TxnResponse, err error) {
txn := m.cliv3.Txn(ctx)
ops := []clientv3.Op{}
for _, key := range keys {
op := clientv3.OpGet(m.parseKey(key), opt...)
ops = append(ops, op)
}
return txn.Then(ops...).Commit()
}

// Delete delete key
func (m *Mercury) Delete(ctx context.Context, key string, opts ...clientv3.OpOption) (*clientv3.DeleteResponse, error) {
return m.cliv3.Delete(ctx, m.parseKey(key), opts...)
Expand Down

0 comments on commit d2229cd

Please sign in to comment.