mongoproto

package
v0.0.0-...-4f8e3ed Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2020 License: MIT, ISC Imports: 10 Imported by: 0

README

mongoproto

implements the mongodb wire protocol

GoDoc CI Status

features:

  • reading ops
  • writing ops

Documentation

Overview

Package mongoproto implements the MongoDB wire protocol

See http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/

Index

Constants

View Source
const (
	OpCodeReply       = OpCode(1)
	OpCodeMessage     = OpCode(1000)
	OpCodeUpdate      = OpCode(2001)
	OpCodeInsert      = OpCode(2002)
	OpCodeReserved    = OpCode(2003)
	OpCodeQuery       = OpCode(2004)
	OpCodeGetMore     = OpCode(2005)
	OpCodeDelete      = OpCode(2006)
	OpCodeKillCursors = OpCode(2007)
)

The full set of known request op codes: http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/#request-opcodes

View Source
const MsgHeaderLen = 16 // mongo MsgHeader length in bytes

Variables

View Source
var (
	ErrInvalidSize = errors.New("mongoproto: got invalid document size")
)

Functions

func CopyMessage

func CopyMessage(w io.Writer, r io.Reader) error

CopyMessage copies reads & writes an entire message.

func ReadDocument

func ReadDocument(r io.Reader) ([]byte, error)

ReadDocument read an entire BSON document. This document can be used with bson.Unmarshal.

Types

type MsgHeader

type MsgHeader struct {
	// MessageLength is the total message size, including this header
	MessageLength int32
	// RequestID is the identifier for this miessage
	RequestID int32
	// ResponseTo is the RequestID of the message being responded to. used in DB responses
	ResponseTo int32
	// OpCode is the request type, see consts above.
	OpCode OpCode
}

MsgHeader is the mongo MessageHeader

func ReadHeader

func ReadHeader(r io.Reader) (*MsgHeader, error)

func (*MsgHeader) String

func (m *MsgHeader) String() string

String returns a string representation of the message header. Useful for debugging.

func (*MsgHeader) WriteTo

func (m *MsgHeader) WriteTo(w io.Writer) (int64, error)

type Op

type Op interface {
	OpCode() OpCode
	FromReader(io.Reader) error
}

Op is a Mongo operation

func OpFromReader

func OpFromReader(r io.Reader) (Op, error)

OpFromReader reads an Op from an io.Reader

type OpCode

type OpCode int32

OpCode allow identifying the type of operation:

http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/#request-opcodes

func (OpCode) HasResponse

func (c OpCode) HasResponse() bool

HasResponse tells us if the operation will have a response from the server.

func (OpCode) IsMutation

func (c OpCode) IsMutation() bool

IsMutation tells us if the operation will mutate data. These operations can be followed up by a getLastErr operation.

func (OpCode) String

func (c OpCode) String() string

String returns a human readable representation of the OpCode.

type OpDelete

type OpDelete struct {
	Header             MsgHeader
	FullCollectionName string // "dbname.collectionname"
	Flags              OpDeleteFlags
	Selector           []byte // the query to select the document(s)
}

OpDelete is used to remove one or more documents from a collection. http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/#op-delete

func (*OpDelete) FromReader

func (op *OpDelete) FromReader(r io.Reader) error

func (*OpDelete) OpCode

func (op *OpDelete) OpCode() OpCode

func (*OpDelete) String

func (op *OpDelete) String() string

type OpDeleteFlags

type OpDeleteFlags int32
const (
	OpDeleteSingleRemove OpDeleteFlags = 1 << iota
)

type OpGetMore

type OpGetMore struct {
	Header             MsgHeader
	FullCollectionName string // "dbname.collectionname"
	NumberToReturn     int32  // number of documents to return
	CursorID           int64  // cursorID from the OpReply
}

OpGetMore is used to query the database for documents in a collection. http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/#op-get-more

func (*OpGetMore) FromReader

func (op *OpGetMore) FromReader(r io.Reader) error

func (*OpGetMore) OpCode

func (op *OpGetMore) OpCode() OpCode

type OpInsert

type OpInsert struct {
	Header             MsgHeader
	Flags              OpInsertFlags
	FullCollectionName string   // "dbname.collectionname"
	Documents          [][]byte // one or more documents to insert into the collection
}

OpInsert is used to insert one or more documents into a collection. http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/#op-insert

func (*OpInsert) FromReader

func (op *OpInsert) FromReader(r io.Reader) error

func (*OpInsert) OpCode

func (op *OpInsert) OpCode() OpCode

func (*OpInsert) String

func (op *OpInsert) String() string

type OpInsertFlags

type OpInsertFlags int32
const (
	OpInsertContinueOnError OpInsertFlags = 1 << iota
)

type OpKillCursors

type OpKillCursors struct {
	Header    MsgHeader
	CursorIDs []int64
}

OpKillCursors is used to close an active cursor in the database. This is necessary to ensure that database resources are reclaimed at the end of the query. http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/#op-kill-cursors

func (*OpKillCursors) FromReader

func (op *OpKillCursors) FromReader(r io.Reader) error

func (*OpKillCursors) OpCode

func (op *OpKillCursors) OpCode() OpCode

func (*OpKillCursors) String

func (op *OpKillCursors) String() string

type OpMsg

type OpMsg struct {
	Header  MsgHeader
	Message string
}

OpMsg sends a diagnostic message to the database. The database sends back a fixed response. OpMsg is Deprecated http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/#op-msg

type OpQuery

type OpQuery struct {
	Header               MsgHeader
	Flags                OpQueryFlags
	FullCollectionName   string // "dbname.collectionname"
	NumberToSkip         int32  // number of documents to skip
	NumberToReturn       int32  // number of documents to return
	Query                []byte // query object
	ReturnFieldsSelector []byte // Optional. Selector indicating the fields to return
}

OpQuery is used to query the database for documents in a collection. http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/#op-query

func (*OpQuery) FromReader

func (op *OpQuery) FromReader(r io.Reader) error

func (*OpQuery) OpCode

func (op *OpQuery) OpCode() OpCode

func (*OpQuery) String

func (op *OpQuery) String() string

type OpQueryFlags

type OpQueryFlags int32
const (
	OpQueryTailableCursor  OpQueryFlags // Tailable means cursor is not closed when the last data is retrieved. Rather, the cursor marks the final object’s position. You can resume using the cursor later, from where it was located, if more data were received. Like any “latent cursor”, the cursor may become invalid at some point (CursorNotFound) – for example if the final object it references were deleted.
	OpQuerySlaveOk                      // Allow query of replica slave. Normally these return an error except for namespace “local”.
	OpQueryOplogReplay                  // Internal replication use only - driver should not set
	OpQueryNoCursorTimeout              // The server normally times out idle cursors after an inactivity period (10 minutes) to prevent excess memory use. Set this option to prevent that.
	OpQueryAwaitData                    // Use with TailableCursor. If we are at the end of the data, block for a while rather than returning no data. After a timeout period, we do return as normal.
	OpQueryExhaust                      // Stream the data down full blast in multiple “more” packages, on the assumption that the client will fully read all data queried. Faster when you are pulling a lot of data and know you want to pull it all down. Note: the client is not allowed to not read all the data unless it closes the connection.
	OpQueryPartial                      // Get partial results from a mongos if some shards are down (instead of throwing an error)
)

type OpReply

type OpReply struct {
	Header         MsgHeader
	Message        string
	Flags          OpReplyFlags
	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      [][]byte // documents
}

OpReply is sent by the database in response to an OpQuery or OpGetMore message. http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/#op-reply

func (*OpReply) FromReader

func (op *OpReply) FromReader(r io.Reader) error

func (*OpReply) OpCode

func (op *OpReply) OpCode() OpCode

func (*OpReply) String

func (op *OpReply) String() string

func (*OpReply) WriteTo

func (op *OpReply) WriteTo(w io.Writer) (int64, error)

type OpReplyFlags

type OpReplyFlags int32
const (
	OpReplyCursorNotFound   OpReplyFlags = 1 << iota // Set when getMore is called but the cursor id is not valid at the server. Returned with zero results.
	OpReplyQueryFailure                              // Set when query failed. Results consist of one document containing an “$err” field describing the failure.
	OpReplyShardConfigStale                          //Drivers should ignore this. Only mongos will ever see this set, in which case, it needs to update config from the server.
	OpReplyAwaitCapable                              //Set 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.
)

type OpUnknown

type OpUnknown struct {
	Header MsgHeader
	Body   []byte
}

OpUnknown is not a real mongo Op but represents an unrecognized or corrupted op

func (*OpUnknown) FromReader

func (op *OpUnknown) FromReader(r io.Reader) error

func (*OpUnknown) OpCode

func (op *OpUnknown) OpCode() OpCode

func (*OpUnknown) String

func (op *OpUnknown) String() string

type OpUpdate

type OpUpdate struct {
	Header             MsgHeader
	FullCollectionName string // "dbname.collectionname"
	Flags              OpUpdateFlags
	Selector           []byte // the query to select the document
	Update             []byte // specification of the update to perform
}

OpUpdate is used to update a document in a collection. http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/#op-update

func (*OpUpdate) FromReader

func (op *OpUpdate) FromReader(r io.Reader) error

func (*OpUpdate) OpCode

func (op *OpUpdate) OpCode() OpCode

func (*OpUpdate) String

func (op *OpUpdate) String() string

type OpUpdateFlags

type OpUpdateFlags int32
const (
	OpUpdateUpsert OpUpdateFlags = 1 << iota
	OpUpdateMuli
)

Jump to

Keyboard shortcuts

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