command

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 18, 2018 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package command contains abstractions for operations that can be performed against a MongoDB deployment. The types in this package are meant to provide a general set of commands that a user can run against a MongoDB database without knowing the version of the database.

Each type consists of two levels of interaction. The lowest level are the Encode and Decode methods. These are meant to be symmetric eventually, but currently only support the driver side of commands. The higher level is the RoundTrip method. This only makes sense from the driver side of commands and this method handles the encoding of the request and decoding of the response using the given wiremessage.ReadWriter.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnknownCommandFailure occurs when a command fails for an unknown reason.
	ErrUnknownCommandFailure = errors.New("unknown command failure")
	// ErrNoCommandResponse occurs when the server sent no response document to a command.
	ErrNoCommandResponse = errors.New("no command response document")
	// ErrMultiDocCommandResponse occurs when the server sent multiple documents in response to a command.
	ErrMultiDocCommandResponse = errors.New("command returned multiple documents")
	// ErrNoDocCommandResponse occurs when the server indicated a response existed, but none was found.
	ErrNoDocCommandResponse = errors.New("command returned no documents")
)

Functions

func ClientDoc

func ClientDoc(app string) *bson.Document

ClientDoc creates a client information document for use in an isMaster command.

func IsNotFound

func IsNotFound(err error) bool

IsNotFound indicates if the error is from a namespace not being found.

Types

type Aggregate

type Aggregate struct {
	NS       Namespace
	Pipeline *bson.Array
	Opts     []options.AggregateOptioner
	ReadPref *readpref.ReadPref
	// contains filtered or unexported fields
}

Aggregate represents the aggregate command.

The aggregate command performs an aggregation.

func (*Aggregate) Decode

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 (*Aggregate) Encode

Encode will encode this command into a wire message for the given server description.

func (*Aggregate) Err

func (a *Aggregate) Err() error

Err returns the error set on this command.

func (*Aggregate) HasDollarOut

func (a *Aggregate) HasDollarOut() bool

HasDollarOut returns true if the Pipeline field contains a $out stage.

func (*Aggregate) Result

func (a *Aggregate) Result() (Cursor, error)

Result returns the result of a decoded wire message and server description.

func (*Aggregate) RoundTrip

RoundTrip handles the execution of this command using the provided wiremessage.ReadWriter.

type BuildInfo

type BuildInfo struct {
	// contains filtered or unexported fields
}

BuildInfo represents the buildInfo command.

The buildInfo command is used for getting the build information for a MongoDB server.

func (*BuildInfo) Decode

func (bi *BuildInfo) Decode(wm wiremessage.WireMessage) *BuildInfo

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 (*BuildInfo) Encode

func (bi *BuildInfo) Encode() (wiremessage.WireMessage, error)

Encode will encode this command into a wire message for the given server description.

func (*BuildInfo) Err

func (bi *BuildInfo) Err() error

Err returns the error set on this command.

func (*BuildInfo) Result

func (bi *BuildInfo) Result() (result.BuildInfo, error)

Result returns the result of a decoded wire message and server description.

func (*BuildInfo) RoundTrip

RoundTrip handles the execution of this command using the provided wiremessage.ReadWriter.

type Command

type Command struct {
	DB       string
	Command  interface{}
	ReadPref *readpref.ReadPref
	// contains filtered or unexported fields
}

Command represents a generic database command.

This can be used to send arbitrary commands to the database, e.g. runCommand.

func (*Command) Decode

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 (*Command) Encode

Encode will encode this command into a wire message for the given server description.

func (*Command) Err

func (c *Command) Err() error

Err returns the error set on this command.

func (*Command) Result

func (c *Command) Result() (bson.Reader, error)

Result returns the result of a decoded wire message and server description.

func (*Command) RoundTrip

RoundTrip handles the execution of this command using the provided wiremessage.ReadWriter.

type Count

type Count struct {
	NS       Namespace
	Query    *bson.Document
	Opts     []options.CountOptioner
	ReadPref *readpref.ReadPref
	// contains filtered or unexported fields
}

Count represents the count command.

The count command counts how many documents in a collection match the given query.

func (*Count) Decode

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 (*Count) Encode

Encode will encode this command into a wire message for the given server description.

func (*Count) Err

func (c *Count) Err() error

Err returns the error set on this command.

func (*Count) Result

func (c *Count) Result() (int64, error)

Result returns the result of a decoded wire message and server description.

func (*Count) RoundTrip

RoundTrip handles the execution of this command using the provided wiremessage.ReadWriter.

type CreateIndexes

type CreateIndexes struct {
	NS      Namespace
	Indexes *bson.Array
	// contains filtered or unexported fields
}

CreateIndexes represents the createIndexes command.

The createIndexes command creates indexes for a namespace.

func (*CreateIndexes) Decode

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 (*CreateIndexes) Encode

Encode will encode this command into a wire message for the given server description.

func (*CreateIndexes) Err

func (ci *CreateIndexes) Err() error

Err returns the error set on this command.

func (*CreateIndexes) Result

func (ci *CreateIndexes) Result() (result.CreateIndexes, error)

Result returns the result of a decoded wire message and server description.

func (*CreateIndexes) RoundTrip

RoundTrip handles the execution of this command using the provided wiremessage.ReadWriter.

type Cursor

type Cursor interface {
	// Get the ID of the cursor.
	ID() int64

	// Get the next result from the cursor.
	// Returns true if there were no errors and there is a next result.
	Next(context.Context) bool

	// Decode the next document into the provided object according to the
	// rules of the bson package.
	Decode(interface{}) error

	// Returns the next document as a bson.Reader. The user must copy the
	// bytes to retain them.
	DecodeBytes() (bson.Reader, error)

	// Returns the error status of the cursor
	Err() error

	// Close the cursor.
	Close(context.Context) error
}

Cursor instances iterate a stream of documents. Each document is decoded into the result according to the rules of the bson package.

A typical usage of the Cursor interface would be:

var cur Cursor
ctx := context.Background()
defer cur.Close(ctx)

for cur.Next(ctx) {
	elem := bson.NewDocument()
	if err := cur.Decode(elem); err != nil {
		log.Fatal(err)
	}

	// do something with elem....
}

if err := cur.Err(); err != nil {
	log.Fatal(err)
}

type CursorBuilder

type CursorBuilder interface {
	BuildCursor(bson.Reader, ...options.CursorOptioner) (Cursor, error)
}

CursorBuilder is a type that can build a Cursor.

type Delete

type Delete struct {
	NS      Namespace
	Deletes []*bson.Document
	Opts    []options.DeleteOptioner
	// contains filtered or unexported fields
}

Delete represents the delete command.

The delete command executes a delete with a given set of delete documents and options.

func (*Delete) Decode

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 (*Delete) Encode

Encode will encode this command into a wire message for the given server description.

func (*Delete) Err

func (d *Delete) Err() error

Err returns the error set on this command.

func (*Delete) Result

func (d *Delete) Result() (result.Delete, error)

Result returns the result of a decoded wire message and server description.

func (*Delete) RoundTrip

RoundTrip handles the execution of this command using the provided wiremessage.ReadWriter.

type Distinct

type Distinct struct {
	NS       Namespace
	Field    string
	Query    *bson.Document
	Opts     []options.DistinctOptioner
	ReadPref *readpref.ReadPref
	// contains filtered or unexported fields
}

Distinct represents the disctinct command.

The distinct command returns the distinct values for a specified field across a single collection.

func (*Distinct) Decode

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 (*Distinct) Encode

Encode will encode this command into a wire message for the given server description.

func (*Distinct) Err

func (d *Distinct) Err() error

Err returns the error set on this command.

func (*Distinct) Result

func (d *Distinct) Result() (result.Distinct, error)

Result returns the result of a decoded wire message and server description.

func (*Distinct) RoundTrip

RoundTrip handles the execution of this command using the provided wiremessage.ReadWriter.

type DropIndexes

type DropIndexes struct {
	NS    Namespace
	Index string
	// contains filtered or unexported fields
}

DropIndexes represents the dropIndexes command.

The dropIndexes command drops indexes for a namespace.

func (*DropIndexes) Decode

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 (*DropIndexes) Encode

Encode will encode this command into a wire message for the given server description.

func (*DropIndexes) Err

func (di *DropIndexes) Err() error

Err returns the error set on this command.

func (*DropIndexes) Result

func (di *DropIndexes) Result() (bson.Reader, error)

Result returns the result of a decoded wire message and server description.

func (*DropIndexes) RoundTrip

RoundTrip handles the execution of this command using the provided wiremessage.ReadWriter.

type Error

type Error struct {
	Code    int32
	Message string
	Name    string
}

Error is a command execution error from the database.

func (Error) Error

func (e Error) Error() string

Error implements the error interface.

type Find

type Find struct {
	NS       Namespace
	Filter   *bson.Document
	Opts     []options.FindOptioner
	ReadPref *readpref.ReadPref
	// contains filtered or unexported fields
}

Find represents the find command.

The find command finds documents within a collection that match a filter.

func (*Find) Decode

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 (*Find) Encode

Encode will encode this command into a wire message for the given server description.

func (*Find) Err

func (f *Find) Err() error

Err returns the error set on this command.

func (*Find) Result

func (f *Find) Result() (Cursor, error)

Result returns the result of a decoded wire message and server description.

func (*Find) RoundTrip

RoundTrip handles the execution of this command using the provided wiremessage.ReadWriter.

type FindOneAndDelete

type FindOneAndDelete struct {
	NS    Namespace
	Query *bson.Document
	Opts  []options.FindOneAndDeleteOptioner
	// contains filtered or unexported fields
}

FindOneAndDelete represents the findOneAndDelete operation.

The findOneAndDelete command deletes a single document that matches a query and returns it.

func (*FindOneAndDelete) Decode

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 (*FindOneAndDelete) Encode

Encode will encode this command into a wire message for the given server description.

func (*FindOneAndDelete) Err

func (f *FindOneAndDelete) Err() error

Err returns the error set on this command.

func (*FindOneAndDelete) Result

func (f *FindOneAndDelete) Result() (result.FindAndModify, error)

Result returns the result of a decoded wire message and server description.

func (*FindOneAndDelete) RoundTrip

RoundTrip handles the execution of this command using the provided wiremessage.ReadWriter.

type FindOneAndReplace

type FindOneAndReplace struct {
	NS          Namespace
	Query       *bson.Document
	Replacement *bson.Document
	Opts        []options.FindOneAndReplaceOptioner
	// contains filtered or unexported fields
}

FindOneAndReplace represents the findOneAndReplace operation.

The findOneAndReplace command modifies and returns a single document.

func (*FindOneAndReplace) Decode

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 (*FindOneAndReplace) Encode

Encode will encode this command into a wire message for the given server description.

func (*FindOneAndReplace) Err

func (f *FindOneAndReplace) Err() error

Err returns the error set on this command.

func (*FindOneAndReplace) Result

Result returns the result of a decoded wire message and server description.

func (*FindOneAndReplace) RoundTrip

RoundTrip handles the execution of this command using the provided wiremessage.ReadWriter.

type FindOneAndUpdate

type FindOneAndUpdate struct {
	NS     Namespace
	Query  *bson.Document
	Update *bson.Document
	Opts   []options.FindOneAndUpdateOptioner
	// contains filtered or unexported fields
}

FindOneAndUpdate represents the findOneAndUpdate operation.

The findOneAndUpdate command modifies and returns a single document.

func (*FindOneAndUpdate) Decode

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 (*FindOneAndUpdate) Encode

Encode will encode this command into a wire message for the given server description.

func (*FindOneAndUpdate) Err

func (f *FindOneAndUpdate) Err() error

Err returns the error set on this command.

func (*FindOneAndUpdate) Result

func (f *FindOneAndUpdate) Result() (result.FindAndModify, error)

Result returns the result of a decoded wire message and server description.

func (*FindOneAndUpdate) RoundTrip

RoundTrip handles the execution of this command using the provided wiremessage.ReadWriter.

type GetLastError

type GetLastError struct {
	// contains filtered or unexported fields
}

GetLastError represents the getLastError command.

The getLastError command is used for getting the last error from the last command on a connection.

Since GetLastError only makes sense in the context of a single connection, there is no Dispatch method.

func (*GetLastError) Decode

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 (*GetLastError) Encode

func (gle *GetLastError) Encode() (wiremessage.WireMessage, error)

Encode will encode this command into a wire message for the given server description.

func (*GetLastError) Err

func (gle *GetLastError) Err() error

Err returns the error set on this command.

func (*GetLastError) Result

func (gle *GetLastError) Result() (result.GetLastError, error)

Result returns the result of a decoded wire message and server description.

func (*GetLastError) RoundTrip

RoundTrip handles the execution of this command using the provided wiremessage.ReadWriter.

type GetMore

type GetMore struct {
	ID   int64
	NS   Namespace
	Opts []options.CursorOptioner
	// contains filtered or unexported fields
}

GetMore represents the getMore command.

The getMore command retrieves additional documents from a cursor.

func (*GetMore) Decode

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 (*GetMore) Encode

Encode will encode this command into a wire message for the given server description.

func (*GetMore) Err

func (gm *GetMore) Err() error

Err returns the error set on this command.

func (*GetMore) Result

func (gm *GetMore) Result() (bson.Reader, error)

Result returns the result of a decoded wire message and server description.

func (*GetMore) RoundTrip

RoundTrip handles the execution of this command using the provided wiremessage.ReadWriter.

type Handshake

type Handshake struct {
	Client *bson.Document
	// contains filtered or unexported fields
}

Handshake represents a generic MongoDB Handshake. It calls isMaster and buildInfo.

The isMaster and buildInfo commands are used to build a server description.

func (*Handshake) Decode

func (h *Handshake) Decode(wms [2]wiremessage.WireMessage) *Handshake

Decode will decode the wire messages. The order of the wire messages are expected to be an isMaster response first and a buildInfo response second. Errors during decoding are deferred until either the Result or Err methods are called.

func (*Handshake) Encode

func (h *Handshake) Encode() ([2]wiremessage.WireMessage, error)

Encode will encode the handshake commands into two wire messages. The wire messages are ordered with the isMaster command first and the buildInfo command second.

func (*Handshake) Err

func (h *Handshake) Err() error

Err returns the error set on this Handshake.

func (*Handshake) Handshake

func (h *Handshake) Handshake(ctx context.Context, address addr.Addr, rw wiremessage.ReadWriter) (description.Server, error)

Handshake implements the connection.Handshaker interface. It is identical to the RoundTrip methods on other types in this package. It will execute the isMaster and buildInfo commands, using pipelining to enable a single roundtrip.

func (*Handshake) Result

func (h *Handshake) Result(address addr.Addr) (description.Server, error)

Result returns the result of decoded wire messages.

type Insert

type Insert struct {
	NS   Namespace
	Docs []*bson.Document
	Opts []options.InsertOptioner
	// contains filtered or unexported fields
}

Insert represents the insert command.

The insert command inserts a set of documents into the database.

Since the Insert command does not return any value other than ok or an error, this type has no Err method.

func (*Insert) Decode

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 (*Insert) Encode

Encode will encode this command into a wire message for the given server description.

func (*Insert) Err

func (i *Insert) Err() error

Err returns the error set on this command.

func (*Insert) Result

func (i *Insert) Result() (result.Insert, error)

Result returns the result of a decoded wire message and server description.

func (*Insert) RoundTrip

RoundTrip handles the execution of this command using the provided wiremessage.ReadWriter.

type IsMaster

type IsMaster struct {
	Client *bson.Document
	// contains filtered or unexported fields
}

IsMaster represents the isMaster command.

The isMaster command is used for setting up a connection to MongoDB and for monitoring a MongoDB server.

Since IsMaster can only be run on a connection, there is no Dispatch method.

func (*IsMaster) Decode

func (im *IsMaster) Decode(wm wiremessage.WireMessage) *IsMaster

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 (*IsMaster) Encode

func (im *IsMaster) Encode() (wiremessage.WireMessage, error)

Encode will encode this command into a wire message for the given server description.

func (*IsMaster) Err

func (im *IsMaster) Err() error

Err returns the error set on this command.

func (*IsMaster) Result

func (im *IsMaster) Result() (result.IsMaster, error)

Result returns the result of a decoded wire message and server description.

func (*IsMaster) RoundTrip

func (im *IsMaster) RoundTrip(ctx context.Context, rw wiremessage.ReadWriter) (result.IsMaster, error)

RoundTrip handles the execution of this command using the provided wiremessage.ReadWriter.

type KillCursors

type KillCursors struct {
	NS  Namespace
	IDs []int64
	// contains filtered or unexported fields
}

KillCursors represents the killCursors command.

The killCursors command kills a set of cursors.

func (*KillCursors) Decode

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 (*KillCursors) Encode

Encode will encode this command into a wire message for the given server description.

func (*KillCursors) Err

func (kc *KillCursors) Err() error

Err returns the error set on this command.

func (*KillCursors) Result

func (kc *KillCursors) Result() (result.KillCursors, error)

Result returns the result of a decoded wire message and server description.

func (*KillCursors) RoundTrip

RoundTrip handles the execution of this command using the provided wiremessage.ReadWriter.

type ListCollections

type ListCollections struct {
	DB     string
	Filter *bson.Document
	Opts   []options.ListCollectionsOptioner
	// contains filtered or unexported fields
}

ListCollections represents the listCollections command.

The listCollections command lists the collections in a database.

func (*ListCollections) Decode

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 (*ListCollections) Encode

Encode will encode this command into a wire message for the given server description.

func (*ListCollections) Err

func (lc *ListCollections) Err() error

Err returns the error set on this command.

func (*ListCollections) Result

func (lc *ListCollections) Result() (Cursor, error)

Result returns the result of a decoded wire message and server description.

func (*ListCollections) RoundTrip

RoundTrip handles the execution of this command using the provided wiremessage.ReadWriter.

type ListDatabases

type ListDatabases struct {
	Filter *bson.Document
	Opts   []options.ListDatabasesOptioner
	// contains filtered or unexported fields
}

ListDatabases represents the listDatabases command.

The listDatabases command lists the databases in a MongoDB deployment.

func (*ListDatabases) Decode

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 (*ListDatabases) Encode

Encode will encode this command into a wire message for the given server description.

func (*ListDatabases) Err

func (ld *ListDatabases) Err() error

Err returns the error set on this command.

func (*ListDatabases) Result

func (ld *ListDatabases) Result() (result.ListDatabases, error)

Result returns the result of a decoded wire message and server description.

func (*ListDatabases) RoundTrip

RoundTrip handles the execution of this command using the provided wiremessage.ReadWriter.

type ListIndexes

type ListIndexes struct {
	NS   Namespace
	Opts []options.ListIndexesOptioner
	// contains filtered or unexported fields
}

ListIndexes represents the listIndexes command.

The listIndexes command lists the indexes for a namespace.

func (*ListIndexes) Decode

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 (*ListIndexes) Encode

Encode will encode this command into a wire message for the given server description.

func (*ListIndexes) Err

func (li *ListIndexes) Err() error

Err returns the error set on this command.

func (*ListIndexes) Result

func (li *ListIndexes) Result() (Cursor, error)

Result returns the result of a decoded wire message and server description.

func (*ListIndexes) RoundTrip

RoundTrip handles the execution of this command using the provided wiremessage.ReadWriter.

type Namespace

type Namespace struct {
	DB         string
	Collection string
}

Namespace encapsulates a database and collection name, which together uniquely identifies a collection within a MongoDB cluster.

func NewNamespace

func NewNamespace(db, collection string) Namespace

NewNamespace returns a new Namespace for the given database and collection.

func ParseNamespace

func ParseNamespace(name string) Namespace

ParseNamespace parses a namespace string into a Namespace.

The namespace string must contain at least one ".", the first of which is the separator between the database and collection names. If not, the default (invalid) Namespace is returned.

func (*Namespace) FullName

func (ns *Namespace) FullName() string

FullName returns the full namespace string, which is the result of joining the database name and the collection name with a "." character.

func (*Namespace) Validate

func (ns *Namespace) Validate() error

Validate validates the namespace.

type QueryFailureError

type QueryFailureError struct {
	Message  string
	Response bson.Reader
}

QueryFailureError is an error representing a command failure as a document.

func (QueryFailureError) Error

func (e QueryFailureError) Error() string

Error implements the error interface.

type ResponseError

type ResponseError struct {
	Message string
	Wrapped error
}

ResponseError is an error parsing the response to a command.

func NewCommandResponseError

func NewCommandResponseError(msg string, err error) ResponseError

NewCommandResponseError creates a CommandResponseError.

func (ResponseError) Error

func (e ResponseError) Error() string

Error implements the error interface.

type Update

type Update struct {
	NS   Namespace
	Docs []*bson.Document
	Opts []options.UpdateOptioner
	// contains filtered or unexported fields
}

Update represents the update command.

The update command updates a set of documents with the database.

func (*Update) Decode

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 (*Update) Encode

Encode will encode this command into a wire message for the given server description.

func (*Update) Err

func (u *Update) Err() error

Err returns the error set on this command.

func (*Update) Result

func (u *Update) Result() (result.Update, error)

Result returns the result of a decoded wire message and server description.

func (*Update) RoundTrip

RoundTrip handles the execution of this command using the provided wiremessage.ReadWriter.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL