wire

package module
v0.0.23 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2025 License: Apache-2.0 Imports: 17 Imported by: 1

README

wire

Go Reference Go coveralls codecov

MongoDB wire protocol implementation in Go.

This module is in the process of extraction from FerretDB. Please do not use it yet.

Documentation

Overview

Package wire provides MongoDB wire protocol implementation.

Index

Constants

View Source
const (
	OpCodeReply       = OpCode(1)    // OP_REPLY
	OpCodeUpdate      = OpCode(2001) // OP_UPDATE
	OpCodeInsert      = OpCode(2002) // OP_INSERT
	OpCodeGetByOID    = OpCode(2003) // OP_GET_BY_OID
	OpCodeQuery       = OpCode(2004) // OP_QUERY
	OpCodeGetMore     = OpCode(2005) // OP_GET_MORE
	OpCodeDelete      = OpCode(2006) // OP_DELETE
	OpCodeKillCursors = OpCode(2007) // OP_KILL_CURSORS
	OpCodeCompressed  = OpCode(2012) // OP_COMPRESSED
	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 = 48_000_000
)
View Source
const (
	OpMsgChecksumPresent = OpMsgFlagBit(1 << 0)  // checksumPresent
	OpMsgMoreToCome      = OpMsgFlagBit(1 << 1)  // moreToCome
	OpMsgExhaustAllowed  = OpMsgFlagBit(1 << 16) // exhaustAllowed
)
View Source
const (
	OpQueryTailableCursor  = OpQueryFlagBit(1 << 1) // TailableCursor
	OpQuerySlaveOk         = OpQueryFlagBit(1 << 2) // SlaveOk
	OpQueryOplogReplay     = OpQueryFlagBit(1 << 3) // OplogReplay
	OpQueryNoCursorTimeout = OpQueryFlagBit(1 << 4) // NoCursorTimeout
	OpQueryAwaitData       = OpQueryFlagBit(1 << 5) // AwaitData
	OpQueryExhaust         = OpQueryFlagBit(1 << 6) // Exhaust
	OpQueryPartial         = OpQueryFlagBit(1 << 7) // Partial
)
View Source
const (
	OpReplyCursorNotFound   = OpReplyFlagBit(1 << 0) // CursorNotFound
	OpReplyQueryFailure     = OpReplyFlagBit(1 << 1) // QueryFailure
	OpReplyShardConfigStale = OpReplyFlagBit(1 << 2) // ShardConfigStale
	OpReplyAwaitCapable     = OpReplyFlagBit(1 << 3) // AwaitCapable
)

Variables

View Source
var Debug bool

Debug set to true performs additional slow checks during encoding/decoding that are not normally required. It is exposed mainly to simplify testing.

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 {
	encoding.BinaryMarshaler

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

	fmt.Stringer

	// StringIndent returns an indented string representation for logging.
	StringIndent() 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 represents common message header.

func (*MsgHeader) MarshalBinary

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

MarshalBinary writes a MsgHeader to a byte array.

func (*MsgHeader) String

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.

Message is checked during construction by NewOpMsg, MustOpMsg, or OpMsg.UnmarshalBinaryNocopy.

func MustOpMsg added in v0.0.3

func MustOpMsg(pairs ...any) *OpMsg

MustOpMsg creates a message with a single section of kind 0 with a single document constructed from the given pairs of field names and values. It panics on error.

func NewOpMsg

func NewOpMsg(doc wirebson.AnyDocument) (*OpMsg, error)

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

func (*OpMsg) Document added in v0.0.19

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

Document returns the value of msg as decoded frozen *wirebson.Document. It may be shallowly or deeply decoded.

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

func (*OpMsg) DocumentDeep added in v0.0.19

func (msg *OpMsg) DocumentDeep() (*wirebson.Document, error)

DocumentDeep returns the value of msg as deeply decoded frozen *wirebson.Document.

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

func (*OpMsg) DocumentRaw added in v0.0.19

func (msg *OpMsg) DocumentRaw() (wirebson.RawDocument, error)

DocumentRaw returns the value of msg as a wirebson.DocumentRaw.

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

func (*OpMsg) MarshalBinary

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

MarshalBinary writes an OpMsg to a byte array.

func (*OpMsg) RawDocument deprecated

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

Deprecated: use DocumentRaw instead.

func (*OpMsg) Section0 added in v0.0.21

func (msg *OpMsg) Section0() (*wirebson.Document, error)

Section0 returns the frozen *wirebson.Document decoded from the section of kind 0. It may be shallowly or deeply decoded.

Most callers should use OpMsg.Document instead.

func (*OpMsg) Sections

func (msg *OpMsg) Sections() (*wirebson.Document, wirebson.RawDocument, []byte, error)

Sections returns the frozen *wirebson.Document decoded from the section of kind 0 (it may be shallowly or deeply decoded), the raw value of that document, and the concatenation of raw values of all sections with kind 1.

Most callers should use OpMsg.Document instead.

func (*OpMsg) String

func (msg *OpMsg) String() string

String returns an string representation for logging.

func (*OpMsg) StringIndent added in v0.0.10

func (msg *OpMsg) StringIndent() string

StringIndent returns an indented string representation for logging.

func (*OpMsg) UnmarshalBinaryNocopy

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

UnmarshalBinaryNocopy implements MsgBody.

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 OpQuery

type OpQuery struct {
	FullCollectionName string

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

OpQuery is a deprecated request message type.

Message is checked during construction by NewOpQuery, MustOpQuery, or OpQuery.UnmarshalBinaryNocopy.

func MustOpQuery added in v0.0.19

func MustOpQuery(pairs ...any) *OpQuery

MustOpQuery creates a new OpQuery message constructed from the given pairs of field names and values. It panics on error.

func NewOpQuery

func NewOpQuery(doc wirebson.AnyDocument) (*OpQuery, error)

NewOpQuery creates a new OpQuery message.

func (*OpQuery) MarshalBinary

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

MarshalBinary implements MsgBody.

func (*OpQuery) Query

func (query *OpQuery) Query() (*wirebson.Document, error)

Query returns decoded frozen query document. It may be shallowly or deeply decoded.

func (*OpQuery) QueryDeep added in v0.0.19

func (query *OpQuery) QueryDeep() (*wirebson.Document, error)

QueryDeep returns deeply decoded frozen query document.

func (*OpQuery) QueryRaw added in v0.0.19

func (query *OpQuery) QueryRaw() wirebson.RawDocument

QueryRaw returns raw query (that might be nil).

func (*OpQuery) ReturnFieldsSelector added in v0.0.19

func (query *OpQuery) ReturnFieldsSelector() (*wirebson.Document, error)

ReturnFieldsSelector returns decoded frozen returnFieldsSelector document, or nil. It may be shallowly or deeply decoded.

func (*OpQuery) ReturnFieldsSelectorDeep added in v0.0.19

func (query *OpQuery) ReturnFieldsSelectorDeep() (*wirebson.Document, error)

ReturnFieldsSelectorDeep returns deeply decoded frozen returnFieldsSelector document, or nil.

func (*OpQuery) ReturnFieldsSelectorRaw added in v0.0.19

func (query *OpQuery) ReturnFieldsSelectorRaw() wirebson.RawDocument

ReturnFieldsSelectorRaw returns raw returnFieldsSelector (that might be nil).

func (*OpQuery) String

func (query *OpQuery) String() string

String returns an string representation for logging.

func (*OpQuery) StringIndent added in v0.0.10

func (query *OpQuery) StringIndent() string

StringIndent returns an indented string representation for logging.

func (*OpQuery) UnmarshalBinaryNocopy

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

UnmarshalBinaryNocopy implements MsgBody.

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.

Message is checked during construction by NewOpReply, MustOpReply, or OpReply.UnmarshalBinaryNocopy.

Only up to one returned document is supported.

func MustOpReply added in v0.0.19

func MustOpReply(pairs ...any) *OpReply

MustOpReply creates a new OpReply message constructed from the given pairs of field names and values. It panics on error.

func NewOpReply

func NewOpReply(doc wirebson.AnyDocument) (*OpReply, error)

NewOpReply creates a new OpReply message.

func (*OpReply) Document

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

Document returns decoded frozen document, or nil. It may be shallowly or deeply decoded.

func (*OpReply) DocumentDeep added in v0.0.19

func (reply *OpReply) DocumentDeep() (*wirebson.Document, error)

DocumentDeep returns deeply decoded frozen document, or nil.

func (*OpReply) DocumentRaw added in v0.0.19

func (reply *OpReply) DocumentRaw() wirebson.RawDocument

DocumentRaw returns raw document (that might be nil).

func (*OpReply) MarshalBinary

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

MarshalBinary implements MsgBody.

func (*OpReply) RawDocument deprecated

func (reply *OpReply) RawDocument() wirebson.RawDocument

Deprecated: use DocumentRaw instead.

func (*OpReply) String

func (reply *OpReply) String() string

String returns an string representation for logging.

func (*OpReply) StringIndent added in v0.0.10

func (reply *OpReply) StringIndent() string

StringIndent returns an indented string representation for logging.

func (*OpReply) UnmarshalBinaryNocopy

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

UnmarshalBinaryNocopy implements MsgBody.

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

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

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.

Directories

Path Synopsis
internal
util/lazyerrors
Package lazyerrors provides error wrapping with file location.
Package lazyerrors provides error wrapping with file location.
util/must
Package must provides helper functions that panic on error.
Package must provides helper functions that panic on error.
util/testutil
Package testutil provides testing utilities.
Package testutil provides testing utilities.
tools module
Package wirebson implements encoding and decoding of BSON as defined by https://bsonspec.org/spec.html.
Package wirebson implements encoding and decoding of BSON as defined by https://bsonspec.org/spec.html.
Package wireclient provides low-level wire protocol client.
Package wireclient provides low-level wire protocol client.
Package wiretest provides testing helpers.
Package wiretest provides testing helpers.

Jump to

Keyboard shortcuts

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