1 Star 0 Fork 0

青文杰 / mongo-go-driver

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
kill_cursors.go 2.87 KB
一键复制 编辑 原始数据 按行查看 历史
Kris Brandow 提交于 2018-11-16 17:28 . Restructure repository
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package command
import (
"context"
"github.com/mongodb/mongo-go-driver/bson"
"github.com/mongodb/mongo-go-driver/x/bsonx"
"github.com/mongodb/mongo-go-driver/x/mongo/driver/session"
"github.com/mongodb/mongo-go-driver/x/network/description"
"github.com/mongodb/mongo-go-driver/x/network/result"
"github.com/mongodb/mongo-go-driver/x/network/wiremessage"
)
// KillCursors represents the killCursors command.
//
// The killCursors command kills a set of cursors.
type KillCursors struct {
Clock *session.ClusterClock
NS Namespace
IDs []int64
result result.KillCursors
err error
}
// Encode will encode this command into a wire message for the given server description.
func (kc *KillCursors) Encode(desc description.SelectedServer) (wiremessage.WireMessage, error) {
encoded, err := kc.encode(desc)
if err != nil {
return nil, err
}
return encoded.Encode(desc)
}
func (kc *KillCursors) encode(desc description.SelectedServer) (*Read, error) {
idVals := make([]bsonx.Val, 0, len(kc.IDs))
for _, id := range kc.IDs {
idVals = append(idVals, bsonx.Int64(id))
}
cmd := bsonx.Doc{
{"killCursors", bsonx.String(kc.NS.Collection)},
{"cursors", bsonx.Array(idVals)},
}
return &Read{
Clock: kc.Clock,
DB: kc.NS.DB,
Command: cmd,
}, nil
}
// Decode will decode the wire message using the provided server description. Errors during decoding
// are deferred until either the Result or Err methods are called.
func (kc *KillCursors) Decode(desc description.SelectedServer, wm wiremessage.WireMessage) *KillCursors {
rdr, err := (&Read{}).Decode(desc, wm).Result()
if err != nil {
kc.err = err
return kc
}
return kc.decode(desc, rdr)
}
func (kc *KillCursors) decode(desc description.SelectedServer, rdr bson.Raw) *KillCursors {
err := bson.Unmarshal(rdr, &kc.result)
if err != nil {
kc.err = err
return kc
}
return kc
}
// Result returns the result of a decoded wire message and server description.
func (kc *KillCursors) Result() (result.KillCursors, error) {
if kc.err != nil {
return result.KillCursors{}, kc.err
}
return kc.result, nil
}
// Err returns the error set on this command.
func (kc *KillCursors) Err() error { return kc.err }
// RoundTrip handles the execution of this command using the provided wiremessage.ReadWriter.
func (kc *KillCursors) RoundTrip(ctx context.Context, desc description.SelectedServer, rw wiremessage.ReadWriter) (result.KillCursors, error) {
cmd, err := kc.encode(desc)
if err != nil {
return result.KillCursors{}, err
}
rdr, err := cmd.RoundTrip(ctx, desc, rw)
if err != nil {
return result.KillCursors{}, err
}
return kc.decode(desc, rdr).Result()
}
1
https://gitee.com/qingwenjie/mongo-go-driver.git
git@gitee.com:qingwenjie/mongo-go-driver.git
qingwenjie
mongo-go-driver
mongo-go-driver
v0.3.0

搜索帮助

53164aa7 5694891 3bd8fe86 5694891