wire

package
v1.21.0 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2024 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package wire provides wire protocol implementation.

Index

Constants

View Source
const (
	// OpCodeReply is used for the initial handshake with old clients.
	// It is not used otherwise and is deprecated.
	OpCodeReply = OpCode(1) // OP_REPLY

	// OpCodeUpdate is deprecated.
	OpCodeUpdate = OpCode(2001) // OP_UPDATE

	// OpCodeInsert is deprecated and unused.
	OpCodeInsert = OpCode(2002) // OP_INSERT

	// OpCodeGetByOID is deprecated and unused.
	OpCodeGetByOID = OpCode(2003) // OP_GET_BY_OID

	// OpCodeQuery is used for the initial handshake with old clients.
	// It is not used otherwise and is deprecated.
	OpCodeQuery = OpCode(2004) // OP_QUERY

	// OpCodeGetMore is deprecated and unused.
	OpCodeGetMore = OpCode(2005) // OP_GET_MORE

	// OpCodeDelete is deprecated and unused.
	OpCodeDelete = OpCode(2006) // OP_DELETE

	// OpCodeKillCursors is deprecated and unused.
	OpCodeKillCursors = OpCode(2007) // OP_KILL_CURSORS

	// OpCodeCompressed is not implemented yet.
	OpCodeCompressed = OpCode(2012) // OP_COMPRESSED

	// OpCodeMsg is the main operation for client-server communication.
	OpCodeMsg = OpCode(2013) // OP_MSG
)
View Source
const (
	// MsgHeaderLen is an expected len of the header.
	MsgHeaderLen = 16

	// MaxMsgLen is the maximum message length.
	MaxMsgLen = 48000000
)
View Source
const (
	// OpMsgChecksumPresent indicates that there is a CRC-32C checksum in a message.
	OpMsgChecksumPresent = OpMsgFlagBit(1 << 0) // checksumPresent

	// OpMsgMoreToCome indicates that there is another message coming, no need to do anything for it.
	OpMsgMoreToCome = OpMsgFlagBit(1 << 1) // moreToCome

	// OpMsgExhaustAllowed indicates that client can handle multiple replies.
	OpMsgExhaustAllowed = OpMsgFlagBit(1 << 16) // exhaustAllowed
)
View Source
const (
	// OpQueryTailableCursor indicates that the cursor will not be closed.
	OpQueryTailableCursor = OpQueryFlagBit(1 << 1) // TailableCursor

	// OpQuerySlaveOk indicates that query can run on a replica slave.
	OpQuerySlaveOk = OpQueryFlagBit(1 << 2) // SlaveOk

	// OpQueryOplogReplay is deprecated.
	OpQueryOplogReplay = OpQueryFlagBit(1 << 3) // OplogReplay

	// OpQueryNoCursorTimeout disables cursor timeouts.
	OpQueryNoCursorTimeout = OpQueryFlagBit(1 << 4) // NoCursorTimeout

	// OpQueryAwaitData together with OpQueryTailableCursor, waits for data instead of returning it.
	OpQueryAwaitData = OpQueryFlagBit(1 << 5) // AwaitData

	// OpQueryExhaust indicates that server can divide data into multiple streams and expect that client can handle it.
	OpQueryExhaust = OpQueryFlagBit(1 << 6) // Exhaust

	// OpQueryPartial ignore error and give partial results.
	OpQueryPartial = OpQueryFlagBit(1 << 7) // Partial
)
View Source
const (
	// OpReplyCursorNotFound indicates that the cursor is no longer exist at the server.
	OpReplyCursorNotFound = OpReplyFlagBit(1 << 0) // CursorNotFound

	// OpReplyQueryFailure reports query has failed.
	OpReplyQueryFailure = OpReplyFlagBit(1 << 1) // QueryFailure

	// OpReplyShardConfigStale indicates that shard config is outdated.
	OpReplyShardConfigStale = OpReplyFlagBit(1 << 2) // ShardConfigStale

	// OpReplyAwaitCapable indicates server supports AwaitData Query option.
	OpReplyAwaitCapable = OpReplyFlagBit(1 << 3) // AwaitCapable
)

Variables

View Source
var ErrZeroRead = errors.New("zero bytes read")

ErrZeroRead is returned when zero bytes was read from connection, indicating that connection was closed by the client.

Functions

func ReadMessage

func ReadMessage(r *bufio.Reader) (*MsgHeader, MsgBody, error)

ReadMessage reads from reader and returns wire header and body.

Error is (possibly wrapped) ErrZeroRead if zero bytes was read.

func WriteMessage

func WriteMessage(w *bufio.Writer, header *MsgHeader, msg MsgBody) error

WriteMessage validates msg and headers and writes them to the writer.

Types

type MsgBody

type MsgBody interface {

	// UnmarshalBinaryNocopy is a variant of [encoding.BinaryUnmarshaler] that does not have to copy the data.
	UnmarshalBinaryNocopy([]byte) error

	encoding.BinaryMarshaler
	fmt.Stringer

	// StringBlock returns an indented string representation for logging.
	StringBlock() string
	// contains filtered or unexported methods
}

MsgBody is a wire protocol message body.

type MsgHeader

type MsgHeader struct {
	MessageLength int32
	RequestID     int32
	ResponseTo    int32
	OpCode        OpCode
}

MsgHeader in general, each message consists of a standard message header followed by request-specific data.

func (*MsgHeader) MarshalBinary

func (msg *MsgHeader) MarshalBinary() ([]byte, error)

MarshalBinary writes a MsgHeader to a byte array.

func (*MsgHeader) String added in v0.0.6

func (msg *MsgHeader) String() string

String returns a string representation for logging.

type OpCode

type OpCode int32

OpCode represents wire operation code.

func (OpCode) String

func (i OpCode) String() string

type OpMsg

type OpMsg struct {
	Flags OpMsgFlags
	// contains filtered or unexported fields
}

OpMsg is the main wire protocol message type.

func NewOpMsg added in v1.21.0

func NewOpMsg(raw bson.RawDocument) (*OpMsg, error)

NewOpMsg creates a message with a single section of kind 0 with a single raw document.

func (*OpMsg) Document

func (msg *OpMsg) Document() (*types.Document, error)

Document returns the value of msg as a types.Document.

All sections are merged together.

func (*OpMsg) MarshalBinary

func (msg *OpMsg) MarshalBinary() ([]byte, error)

MarshalBinary writes an OpMsg to a byte array.

func (*OpMsg) RawDocument added in v1.19.0

func (msg *OpMsg) RawDocument() (bson.RawDocument, error)

RawDocument returns the value of msg as a bson.RawDocument.

The error is returned if msg contains anything other than a single section of kind 0 with a single document.

func (*OpMsg) RawSections added in v1.21.0

func (msg *OpMsg) RawSections() (bson.RawDocument, []byte)

RawSections returns the value of section with kind 0 and the value of all sections with kind 1.

func (*OpMsg) Sections added in v1.21.0

func (msg *OpMsg) Sections() []OpMsgSection

Sections returns the sections of the OpMsg.

func (*OpMsg) SetSections

func (msg *OpMsg) SetSections(sections ...OpMsgSection) error

SetSections sets sections of the OpMsg.

func (*OpMsg) String added in v0.0.6

func (msg *OpMsg) String() string

String returns a string representation for logging.

func (*OpMsg) StringBlock added in v1.21.0

func (msg *OpMsg) StringBlock() string

StringBlock returns an indented string representation for logging.

func (*OpMsg) UnmarshalBinaryNocopy added in v1.21.0

func (msg *OpMsg) UnmarshalBinaryNocopy(b []byte) error

UnmarshalBinaryNocopy implements MsgBody interface.

type OpMsgFlagBit

type OpMsgFlagBit flagBit

OpMsgFlagBit integer is a bitmask encoding flags that modify the format and behavior of OpMsg.

func (OpMsgFlagBit) String

func (i OpMsgFlagBit) String() string

type OpMsgFlags

type OpMsgFlags flags

OpMsgFlags type unint32.

func (OpMsgFlags) FlagSet

func (f OpMsgFlags) FlagSet(bit OpMsgFlagBit) bool

FlagSet check if flag is set.

func (OpMsgFlags) String

func (f OpMsgFlags) String() string

String returns OpMsgFlags as a string.

type OpMsgSection

type OpMsgSection struct {
	Identifier string
	Documents  []bson.RawDocument
	Kind       byte
}

OpMsgSection is one or more sections contained in an OpMsg.

func MakeOpMsgSection added in v1.19.0

func MakeOpMsgSection(doc *types.Document) OpMsgSection

MakeOpMsgSection creates OpMsgSection with a single document.

type OpQuery

type OpQuery struct {
	FullCollectionName string

	Flags          OpQueryFlags
	NumberToSkip   int32
	NumberToReturn int32
	// contains filtered or unexported fields
}

OpQuery is a deprecated request message type.

func (*OpQuery) MarshalBinary

func (query *OpQuery) MarshalBinary() ([]byte, error)

MarshalBinary implements MsgBody interface.

func (*OpQuery) Query

func (query *OpQuery) Query() *types.Document

Query returns the query document.

func (*OpQuery) ReturnFieldsSelector

func (query *OpQuery) ReturnFieldsSelector() *types.Document

ReturnFieldsSelector returns the fields selector document (that may be nil).

func (*OpQuery) String added in v0.0.6

func (query *OpQuery) String() string

String returns a string representation for logging.

func (*OpQuery) StringBlock added in v1.21.0

func (query *OpQuery) StringBlock() string

StringBlock returns an indented string representation for logging.

func (*OpQuery) UnmarshalBinaryNocopy added in v1.21.0

func (query *OpQuery) UnmarshalBinaryNocopy(b []byte) error

UnmarshalBinaryNocopy implements MsgBody interface.

type OpQueryFlagBit

type OpQueryFlagBit flagBit

OpQueryFlagBit an integer bitmask for the operation.

func (OpQueryFlagBit) String

func (i OpQueryFlagBit) String() string

type OpQueryFlags

type OpQueryFlags flags

OpQueryFlags enables String() and FlagSet methods for flags.

func (OpQueryFlags) FlagSet

func (f OpQueryFlags) FlagSet(bit OpQueryFlagBit) bool

FlagSet return true if flag set.

func (OpQueryFlags) String

func (f OpQueryFlags) String() string

String interface implementation for query flags.

type OpReply

type OpReply struct {
	CursorID     int64
	Flags        OpReplyFlags
	StartingFrom int32
	// contains filtered or unexported fields
}

OpReply is a deprecated response message type.

Only up to one returned document is supported.

func (*OpReply) Document added in v1.21.0

func (reply *OpReply) Document() (*types.Document, error)

Document returns reply document.

func (*OpReply) MarshalBinary

func (reply *OpReply) MarshalBinary() ([]byte, error)

MarshalBinary implements MsgBody interface.

func (*OpReply) SetDocument added in v1.19.0

func (reply *OpReply) SetDocument(doc *types.Document)

SetDocument sets reply document.

func (*OpReply) String added in v0.0.6

func (reply *OpReply) String() string

String returns a string representation for logging.

func (*OpReply) StringBlock added in v1.21.0

func (reply *OpReply) StringBlock() string

StringBlock returns an indented string representation for logging.

func (*OpReply) UnmarshalBinaryNocopy added in v1.21.0

func (reply *OpReply) UnmarshalBinaryNocopy(b []byte) error

UnmarshalBinaryNocopy implements MsgBody interface.

type OpReplyFlagBit

type OpReplyFlagBit flagBit

OpReplyFlagBit is a bit vector to specify OP_REPLY flags.

func (OpReplyFlagBit) String

func (i OpReplyFlagBit) String() string

type OpReplyFlags

type OpReplyFlags flags

OpReplyFlags are OP_REPLY flags.

func (OpReplyFlags) FlagSet

func (f OpReplyFlags) FlagSet(bit OpReplyFlagBit) bool

FlagSet returns true if the flag is set.

func (OpReplyFlags) String

func (f OpReplyFlags) String() string

String returns string value for OP_REPLY.

type Record added in v1.5.0

type Record struct {
	// those may be unset if message is invalid
	Header *MsgHeader
	Body   MsgBody

	// those are always set
	HeaderB []byte
	BodyB   []byte
}

Record represents a single recorded wire protocol message, loaded from a .bin file.

func LoadRecords added in v1.5.0

func LoadRecords(dir string, limit int) ([]Record, error)

LoadRecords finds all .bin files recursively, selects up to the limit at random (or all if limit <= 0), and parses them.

type ValidationError added in v0.7.0

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

ValidationError is used for reporting validation errors.

func (*ValidationError) Error added in v0.7.0

func (v *ValidationError) Error() string

Error implements error interface.

Jump to

Keyboard shortcuts

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