protocol

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: May 4, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// CursorNotFound sets when getMore is called but the cursor id is not valid at the server. Returned with zero results.
	CursorNotFound = 0x01
	// QueryFailure sets when query failed. Results consist of one document containing an “$err” field describing the failure.
	QueryFailure = 0x02
	// ShardConfigStale needs to update config from the server, and so drivers should ignore this.
	ShardConfigStale = 0x04
	// AwaitCapable sets when the server supports the AwaitData Query option. If it doesn’t, a client should sleep a little between getMore’s of a Tailable cursor. Mongod version 1.6 supports AwaitData and thus always sets AwaitCapable.
	AwaitCapable = 0x08
)
View Source
const (
	// HeaderSize is the static header size of MongoDB wire protocol.
	HeaderSize = (4 + 4 + 4 + 4)
)

Variables

This section is empty.

Functions

func AppendByte

func AppendByte(dst []byte, val byte) []byte

AppendByte appends the byte to the buffer.

func AppendCString

func AppendCString(dst []byte, val string) []byte

AppendCString appends the string value to the buffer.

func AppendDocument

func AppendDocument(dst []byte, doc bson.Document) []byte

AppendDocument appends the document to the buffer.

func AppendHeader

func AppendHeader(dst []byte, length, reqid, respto int32, opcode OpCode) []byte

AppendHeader appends a header to dst.

func AppendInt32

func AppendInt32(dst []byte, val int32) []byte

AppendInt32 appends the int32 value to the buffer.

func AppendInt64

func AppendInt64(dst []byte, val int64) []byte

AppendInt64 appends the int64 value to the buffer.

func ReadCString

func ReadCString(src []byte) (string, []byte, bool)

ReadCString reads the cstring from src.

func ReadCursorIDs

func ReadCursorIDs(src []byte, numIDs int32) (cursorIDs []int64, rem []byte, ok bool)

ReadCursorIDs reads numIDs cursor IDs from src.

func ReadDocument

func ReadDocument(src []byte) (bson.Document, []byte, bool)

ReadDocument reads a single document from src.

func ReadDocumentSequence

func ReadDocumentSequence(src []byte) (identifier string, docs []bsoncore.Document, rem []byte, ok bool)

ReadDocumentSequence reads an identifier and document sequence from src.

func ReadDocuments

func ReadDocuments(src []byte) (docs []bson.Document, rem []byte, ok bool)

ReadDocuments reads as many documents as possible from src.

func ReadInt32

func ReadInt32(src []byte) (int32, []byte, bool)

ReadInt32 reads a int32 value from src.

func ReadInt64

func ReadInt64(src []byte) (int64, []byte, bool)

ReadInt64 reads a int64 value from src.

func ReadUint32

func ReadUint32(src []byte) (uint32, []byte, bool)

ReadUint32 reads an uint32 value from src.

Types

type Delete

type Delete struct {
	*Header                          // A standard wire protocol header
	ZERO               int32         // 0 - reserved for future use
	FullCollectionName string        // "dbname.collectionname"
	Flags              Flag          // bit vector. see below
	Selector           bson.Document // the query to select the document
}

Delete represents a OP_DELETE of MongoDB wire protocol. See : MongoDB Wire Protocol https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/

func NewDeleteWithHeaderAndBody

func NewDeleteWithHeaderAndBody(header *Header, body []byte) (*Delete, error)

NewDeleteWithHeaderAndBody returns a new update instance with the specified bytes.

func (*Delete) Size

func (op *Delete) Size() int32

Size returns the message size including the header.

func (*Delete) String

func (op *Delete) String() string

String returns the string description.

type Flag

type Flag = wiremessage.MsgFlag

Flag represents a message flag of MongoDB wire protocol.

type GetMore

type GetMore struct {
	*Header                   // A standard wire protocol header
	ZERO               int32  // 0 - reserved for future use
	FullCollectionName string // "dbname.collectionname"
	NumberToReturn     int32  // number of documents to return
	CursorID           int64  // cursorID from the OP_REPLY
}

GetMore represents a OP_GET_MORE of MongoDB wire protocol. See : MongoDB Wire Protocol https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/

func NewGetMoreWithHeaderAndBody

func NewGetMoreWithHeaderAndBody(header *Header, body []byte) (*GetMore, error)

NewGetMoreWithHeaderAndBody returns a new get more instance with the specified bytes.

func (*GetMore) Size

func (op *GetMore) Size() int32

Size returns the message size including the header.

func (*GetMore) String

func (op *GetMore) String() string

String returns the string description.

type Header struct {
	MessageLength int32  // total message size, including this
	RequestID     int32  // identifier for this message
	ResponseTo    int32  // requestID from the original request
	OpCode        OpCode // request type
}

Header represents a standard header of MongoDB wire protocol.

func NewHeader

func NewHeader() *Header

NewHeader returns a new header instance.

func NewHeaderWithBytes

func NewHeaderWithBytes(msg []byte) (*Header, error)

NewHeaderWithBytes returns a new header instance of the specified bytes.

func NewHeaderWithOpCode

func NewHeaderWithOpCode(opcode OpCode) *Header

NewHeaderWithOpCode returns a new header instance with the specified opcode.

func (*Header) Bytes

func (header *Header) Bytes() []byte

Bytes returns the binary description.

func (*Header) GetBodySize

func (header *Header) GetBodySize() int32

GetBodySize returns body size excluding header.

func (*Header) GetMessageLength

func (header *Header) GetMessageLength() int32

GetMessageLength gets the message length.

func (*Header) GetOpCode

func (header *Header) GetOpCode() OpCode

GetOpCode returns a response identifier.

func (*Header) GetRequestID

func (header *Header) GetRequestID() int32

GetRequestID gets the message identifier.

func (*Header) GetResponseTo

func (header *Header) GetResponseTo() int32

GetResponseTo gets the response message identifier.

func (*Header) ParseBytes

func (header *Header) ParseBytes(msg []byte) error

ParseBytes parses the specified bytes.

func (*Header) SetMessageLength

func (header *Header) SetMessageLength(l int32)

SetMessageLength sets a message length.

func (*Header) SetRequestID

func (header *Header) SetRequestID(id int32)

SetRequestID sets a message identifier.

func (*Header) SetResponseTo

func (header *Header) SetResponseTo(id int32)

SetResponseTo sets a response message identifier.

func (*Header) String

func (header *Header) String() string

String returns the string description.

type Insert

type Insert struct {
	*Header                          // A standard wire protocol header
	Flags              Flag          // bit vector. see below
	FullCollectionName string        // "dbname.collectionname"
	Document           bson.Document // one or more documents to insert into the collection
}

Insert represents a OP_INSERT of MongoDB wire protocol. See : MongoDB Wire Protocol https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/

func NewInsertWithHeaderAndBody

func NewInsertWithHeaderAndBody(header *Header, body []byte) (*Insert, error)

NewInsertWithHeaderAndBody returns a new insert instance with the specified bytes.

func (*Insert) Size

func (op *Insert) Size() int32

Size returns the message size including the header.

func (*Insert) String

func (op *Insert) String() string

String returns the string description.

type KillCursors

type KillCursors struct {
	*Header                   // A standard wire protocol header
	ZERO              int32   // 0 - reserved for future use
	NumberOfCursorIDs int32   // number of documents to return
	CursorIDs         []int64 // cursorID from the OP_REPLY
}

KillCursors represents a OP_KILL_CURSORS of MongoDB wire protocol. See : MongoDB Wire Protocol https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/

func NewKillCursorsWithHeaderAndBody

func NewKillCursorsWithHeaderAndBody(header *Header, body []byte) (*KillCursors, error)

NewKillCursorsWithHeaderAndBody returns a new get more instance with the specified bytes.

func (*KillCursors) Size

func (op *KillCursors) Size() int32

Size returns the message size including the header.

func (*KillCursors) String

func (op *KillCursors) String() string

String returns the string description.

type Message

type Message interface {
	// SetRequestID sets a message identifier.
	SetRequestID(id int32)
	// SetResponseTo sets a response message identifier.
	SetResponseTo(id int32)
	// GetMessageLength returns the message length.
	GetMessageLength() int32
	// GetRequestID returns the message identifier.
	GetRequestID() int32
	// GetResponseTo returns the response message identifier.
	GetResponseTo() int32
	// GetOpCode returns the operation code.
	GetOpCode() OpCode
	// Size returns the message size including the header.
	Size() int32
	// Bytes returns the binary description of BSON format.
	Bytes() []byte
	// String returns the string description.
	String() string
}

Message represents an operation message of MongoDB wire protocol.

func NewMessageWithBytes

func NewMessageWithBytes(msg []byte) (Message, error)

NewMessageWithBytes returns a parsed message of the specified bytes.

func NewMessageWithHeaderAndBytes

func NewMessageWithHeaderAndBytes(header *Header, body []byte) (Message, error)

NewMessageWithHeaderAndBytes returns a parsed message of the specified header and body bytes.

type MessageListener

type MessageListener interface {
	MessageReceived(Message)
	MessageRespond(Message)
}

MessageListener represents a listener for MongoDB Wire Protocol.

type Msg

type Msg struct {
	*Header                   // A standard wire protocol header
	FlagBits    MsgFlag       // message flags
	Body        bson.Document // Body
	DocumentIDs []string
	Documents   []bson.Document // Document Sequence
	Checksum    uint32          // optional CRC-32C checksum
}

Msg represents a OP_MSG of MongoDB wire protocol. See : MongoDB Wire Protocol https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/

func NewMsg

func NewMsg() *Msg

NewMsg returns a new msg instance.

func NewMsgWithBody

func NewMsgWithBody(body bson.Document) *Msg

NewMsgWithBody returns a new msg instance with the specified body document.

func NewMsgWithHeaderAndBody

func NewMsgWithHeaderAndBody(header *Header, body []byte) (*Msg, error)

NewMsgWithHeaderAndBody returns a new update instance with the specified bytes.

func (*Msg) AddDocument

func (op *Msg) AddDocument(docID string, doc bson.Document)

AddDocument adds a document with the ID.

func (*Msg) AddDocuments

func (op *Msg) AddDocuments(docIDs []string, docs []bson.Document)

AddDocuments adds documents with the IDs.

func (*Msg) Bytes

func (op *Msg) Bytes() []byte

Bytes returns the binary description of BSON format.

func (*Msg) GetBody

func (op *Msg) GetBody() bson.Document

GetBody returns the body document.

func (*Msg) GetDocuments

func (op *Msg) GetDocuments() []bson.Document

GetDocuments returns the sequence documents.

func (*Msg) SetBody

func (op *Msg) SetBody(doc bson.Document)

SetBody sets the specified body.

func (*Msg) Size

func (op *Msg) Size() int32

Size returns the message size including the header.

func (*Msg) String

func (op *Msg) String() string

String returns the string description.

type MsgFlag

type MsgFlag = wiremessage.MsgFlag

MsgFlag represents a MsgFlag of MongoDB wire protocol.

type OpCode

type OpCode = wiremessage.OpCode

OpCode represents a MongoDB wire protocol opcode.

const (
	// OpReply (OP_REPLY) replies to a client request. responseTo is set.
	OpReply OpCode = wiremessage.OpReply
	// OpUpdate (OP_UPDATE:2001) updates document.
	OpUpdate OpCode = wiremessage.OpUpdate
	// OpInsert (OP_INSERT:2002) inserts new document.
	OpInsert OpCode = wiremessage.OpInsert
	// OpQuery (OP_QUERY:2004) queries a collection.
	OpQuery OpCode = wiremessage.OpQuery
	// OpGetMore (GET_MORE:2005) gets more data from a query. See Cursors.
	OpGetMore OpCode = wiremessage.OpGetMore
	// OpDelete (OP_DELETE:2006) Deletes documents.
	OpDelete OpCode = wiremessage.OpDelete
	// OpKillCursors (OP_KILL_CURSORS:2007)Notifies database that the client has finished with the cursor.
	OpKillCursors OpCode = wiremessage.OpKillCursors
	// OpCommand (OP_COMMAND:2011) clusters internal protocol representing a command request.
	OpCommand OpCode = wiremessage.OpCommand
	// OpCommandReply (OP_COMMANDREPLY:2011) clusters internal protocol representing a reply to an OP_COMMAND.
	OpCommandReply OpCode = wiremessage.OpCommandReply
	// OpMsg (OP_MSG:2013) sends a message using the format introduced in MongoDB 3.6.
	OpMsg OpCode = wiremessage.OpMsg
)

func ReadHeader

func ReadHeader(src []byte) (length, requestID, responseTo int32, opcode OpCode, rem []byte, ok bool)

ReadHeader reads a wire message header from src.

type Query

type Query struct {
	*Header                          // A standard wire protocol header
	Flags              Flag          // bit vector. see below
	FullCollectionName string        // "dbname.collectionname"
	NumberToSkip       int32         // number of documents to skip
	NumberToReturn     int32         // number of documents to return in the first OP_REPLY batch
	Query              bson.Document // query object.  See below for details.
}

Query represents a OP_QUERY of MongoDB wire protocol. See : MongoDB Wire Protocol https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/

func NewQueryWithHeaderAndBody

func NewQueryWithHeaderAndBody(header *Header, body []byte) (*Query, error)

NewQueryWithHeaderAndBody returns a new insert instance with the specified bytes.

func (*Query) GetCollectionName

func (op *Query) GetCollectionName() string

GetCollectionName returns the query collection name.

func (*Query) GetQuery

func (op *Query) GetQuery() bson.Document

GetQuery returns the query document.

func (*Query) IsCollection

func (op *Query) IsCollection(name string) bool

IsCollection returns true when the specified name equals the full collection name, otherwise false.

func (*Query) Size

func (op *Query) Size() int32

Size returns the message size including the header.

func (*Query) String

func (op *Query) String() string

String returns the string description.

type Reply

type Reply struct {
	*Header                        // A standard wire protocol header
	ReplyFlags     ReplyFlag       // bit vector - see details below
	CursorID       int64           // cursor id if client needs to do get more's
	StartingFrom   int32           // where in the cursor this reply is starting
	NumberReturned int32           // number of documents in the reply
	Documents      []bson.Document // documents
}

Reply represents a OP_REPLY of MongoDB wire protocol. See : MongoDB Wire Protocol https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/

func NewReply

func NewReply() *Reply

NewReply returns a new reply instance.

func NewReplyWithDocument

func NewReplyWithDocument(doc bson.Document) *Reply

NewReplyWithDocument returns a new reply instance with ths specified document.

func NewReplyWithDocuments

func NewReplyWithDocuments(docs []bson.Document) *Reply

NewReplyWithDocuments returns a new reply instance with ths specified documents.

func NewReplyWithHeaderAndBody

func NewReplyWithHeaderAndBody(header *Header, body []byte) (*Reply, error)

NewReplyWithHeaderAndBody returns a new reply instance with the specified bytes.

func (*Reply) Bytes

func (op *Reply) Bytes() []byte

Bytes returns the binary description of BSON format.

func (*Reply) SetResponseFlags

func (op *Reply) SetResponseFlags(flag ReplyFlag)

SetResponseFlags sets a response flag.

func (*Reply) Size

func (op *Reply) Size() int32

Size returns the message size including the header.

func (*Reply) String

func (op *Reply) String() string

String returns the string description.

type ReplyFlag

type ReplyFlag = wiremessage.ReplyFlag

ReplyFlag represents a MsgFReplyFlaglag of MongoDB wire protocol.

type Request

type Request interface {
	GetCode() OpCode
}

Request represents a request operation of MongoDB wire protocol.

type Response

type Response interface {
	GetCode() OpCode
}

Response represents a response operation of MongoDB wire protocol.

type Section

type Section = bsoncore.Document

Section represents a section of MongoDB wire protocol.

type SectionType

type SectionType = wiremessage.SectionType

SectionType represents a SectionType of MongoDB wire protocol.

func ReadSectionType

func ReadSectionType(src []byte) (stype SectionType, rem []byte, ok bool)

ReadSectionType reads the section type from src.

type Update

type Update struct {
	*Header                          // A standard wire protocol header
	ZERO               int32         // 0 - reserved for future use
	FullCollectionName string        // "dbname.collectionname"
	Flags              Flag          // bit vector. see below
	Selector           bson.Document // the query to select the document
	Update             bson.Document // specification of the update to perform
}

Update represents a OP_UPDATE of MongoDB wire protocol. See : MongoDB Wire Protocol https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/

func NewUpdateWithHeaderAndBody

func NewUpdateWithHeaderAndBody(header *Header, body []byte) (*Update, error)

NewUpdateWithHeaderAndBody returns a new update instance with the specified bytes.

func (*Update) Size

func (op *Update) Size() int32

Size returns the message size including the header.

func (*Update) String

func (op *Update) String() string

String returns the string description.

Jump to

Keyboard shortcuts

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