mongo

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2024 License: Apache-2.0 Imports: 23 Imported by: 1

Documentation

Index

Constants

View Source
const (
	// PackageName is the package name.
	PackageName = "go-mongo"
	// DefaultHost is the default host for MongoDB servers.
	DefaultHost string = "localhost"
	// DefaultPort is the default port for mongod and mongos.
	DefaultPort int = 27017
	// DefaultTimeoutSecond is the default request timeout for MongoDB servers.
	DefaultTimeoutSecond = 5
)
View Source
const (
	Version = "v1.2.2"
)

Variables

View Source
var ErrCommand = errors.New("invalid command")
View Source
var ErrQuery = errors.New("query error")
View Source
var ErrQueryNotSupported = errors.New("query not supported")

Functions

func NewErrorCommand added in v1.2.0

func NewErrorCommand(cmd *Command) error

func NewNotSupported added in v0.9.1

func NewNotSupported(q *Query) error

func NewQueryError added in v0.9.1

func NewQueryError(q *Query) error

Types

type AuthCommandExecutor added in v1.1.0

type AuthCommandExecutor interface {
	// SASLSupportedMechs returns the supported SASL mechanisms.
	SASLSupportedMechs(*Conn, string) ([]sasl.SASLMechanism, error)
	// SASLStart handles SASLStart command.
	SASLStart(*Conn, *Command) (bson.Document, error)
	// SASLContinue handles SASLContinue command.
	SASLContinue(*Conn, *Command) (bson.Document, error)
}

AuthCommandExecutor represents an executor interface for MongoDB authentication commands.

type BaseCommandExecutor

BaseCommandExecutor is a complete hander for CommandExecutor.

func NewBaseCommandExecutor

func NewBaseCommandExecutor() *BaseCommandExecutor

NewBaseCommandExecutor returns a complete null executor for CommandExecutor.

func (*BaseCommandExecutor) BuildInfo added in v1.2.1

func (executor *BaseCommandExecutor) BuildInfo(conn *Conn, cmd *Command) (bson.Document, error)

BuildInfo returns statistics about the MongoDB build.

func (*BaseCommandExecutor) Delete

func (executor *BaseCommandExecutor) Delete(conn *Conn, q *Query) (int32, error)

Delete hadles OP_DELETE and 'delete' query of OP_MSG.

func (*BaseCommandExecutor) ExecuteCommand

func (executor *BaseCommandExecutor) ExecuteCommand(conn *Conn, cmd *Command) (bson.Document, error)

ExecuteCommand handles query commands other than those explicitly specified above.

func (*BaseCommandExecutor) Find

func (executor *BaseCommandExecutor) Find(conn *Conn, q *Query) ([]bson.Document, error)

Find hadles 'find' query of OP_MSG.

func (*BaseCommandExecutor) GetLastError added in v1.2.1

func (executor *BaseCommandExecutor) GetLastError(conn *Conn, cmd *Command) (bson.Document, error)

GetLastError returns statistics about the MongoDB build.

func (*BaseCommandExecutor) Hello added in v1.2.1

func (executor *BaseCommandExecutor) Hello(conn *Conn, cmd *Command) (bson.Document, error)

Hello returns information about this member’s role in the replica set, including whether it is the master.

func (*BaseCommandExecutor) Insert

func (executor *BaseCommandExecutor) Insert(conn *Conn, q *Query) (int32, error)

Insert hadles OP_INSERT and 'insert' query of OP_MSG.

func (*BaseCommandExecutor) SASLContinue added in v1.2.1

func (executor *BaseCommandExecutor) SASLContinue(*Conn, *Command) (bson.Document, error)

SASLContinue handles SASLContinue command.

func (*BaseCommandExecutor) SASLStart added in v1.2.1

func (executor *BaseCommandExecutor) SASLStart(conn *Conn, cmd *Command) (bson.Document, error)

SASLStart handles SASLStart command.

func (*BaseCommandExecutor) SetAuthCommandExecutor added in v1.2.0

func (executor *BaseCommandExecutor) SetAuthCommandExecutor(fn AuthCommandExecutor)

SetAuthCommandExecutor sets a command exector for auth operation commands.

func (*BaseCommandExecutor) SetDatabaseCommandExecutor

func (executor *BaseCommandExecutor) SetDatabaseCommandExecutor(fn DatabaseCommandExecutor)

SetDatabaseCommandExecutor sets a command exector for database operation commands.

func (*BaseCommandExecutor) SetUserCommandExecutor

func (executor *BaseCommandExecutor) SetUserCommandExecutor(fn UserCommandExecutor)

SetUserCommandExecutor sets a command exector for database operation commands.

func (*BaseCommandExecutor) Update

func (executor *BaseCommandExecutor) Update(conn *Conn, q *Query) (int32, error)

Update hadles OP_UPDATE and 'update' query of OP_MSG.

type BaseMessageHandler

type BaseMessageHandler struct {
	CommandExecutor
	MessageExecutor
}

BaseMessageHandler is a complete hander for MessageHandler.

func NewBaseMessageHandler

func NewBaseMessageHandler() *BaseMessageHandler

NewBaseMessageHandler returns a complete null handler for MessageHandler.

func (*BaseMessageHandler) OpDelete

func (handler *BaseMessageHandler) OpDelete(conn *Conn, msg *OpDelete) (bson.Document, error)

OpDelete handles OP_DELETE of MongoDB wire protocol.

func (*BaseMessageHandler) OpGetMore

func (handler *BaseMessageHandler) OpGetMore(conn *Conn, msg *OpGetMore) (bson.Document, error)

OpGetMore handles GET_MORE of MongoDB wire protocol.

func (*BaseMessageHandler) OpInsert

func (handler *BaseMessageHandler) OpInsert(conn *Conn, msg *OpInsert) (bson.Document, error)

OpInsert handles OP_INSERT of MongoDB wire protocol.

func (*BaseMessageHandler) OpKillCursors

func (handler *BaseMessageHandler) OpKillCursors(conn *Conn, msg *OpKillCursors) (bson.Document, error)

OpKillCursors handles OP_KILL_CURSORS of MongoDB wire protocol.

func (*BaseMessageHandler) OpMsg

func (handler *BaseMessageHandler) OpMsg(conn *Conn, msg *OpMsg) (bson.Document, error)

OpMsg handles OP_MSG of MongoDB wire protocol.

func (*BaseMessageHandler) OpQuery

func (handler *BaseMessageHandler) OpQuery(conn *Conn, msg *OpQuery) (bson.Document, error)

OpQuery handles OP_QUERY of MongoDB wire protocol.

func (*BaseMessageHandler) OpUpdate

func (handler *BaseMessageHandler) OpUpdate(conn *Conn, msg *OpUpdate) (bson.Document, error)

OpUpdate handles OP_UPDATE of MongoDB wire protocol.

func (*BaseMessageHandler) SetCommandExecutor

func (handler *BaseMessageHandler) SetCommandExecutor(fn CommandExecutor)

SetCommandExecutor sets a exector for OP_QUERY of MongoDB wire protocol.

func (*BaseMessageHandler) SetMessageExecutor

func (handler *BaseMessageHandler) SetMessageExecutor(fn MessageExecutor)

SetMessageExecutor sets a exector for OP_MSG of MongoDB wire protocol.

type CertConfig added in v1.2.2

type CertConfig interface {
	// SetClientAuthType sets a client authentication type.
	SetClientAuthType(authType tls.ClientAuthType)
	// SetServerKeyFile sets a SSL server key file.
	SetServerKeyFile(file string) error
	// SetServerCertFile sets a SSL server certificate file.
	SetServerCertFile(file string) error
	// SetRootCertFile sets a SSL root certificates.
	SetRootCertFiles(files ...string) error
	// SetServerKey sets a SSL server key.
	SetServerKey(key []byte)
	// SetServerCert sets a SSL server certificate.
	SetServerCert(cert []byte)
	// SetRootCerts sets a SSL root certificates.
	SetRootCerts(certs ...[]byte)
	// SetTLSConfig sets a TLS configuration.
	SetTLSConfig(tlsConfig *tls.Config)
	// TLSConfig returns a TLS configuration from the configuration.
	TLSConfig() (*tls.Config, error)
}

CertConfig represents a TLS configuration interface.

type Client

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

Client is an instance for Graphite protocols.

func NewClient

func NewClient() *Client

NewClient returns a client instance.

func (*Client) Close

func (client *Client) Close() error

Close closes the current connection.

func (*Client) Collection

func (client *Client) Collection() string

Collection returns a destination collection.

func (*Client) Connect

func (client *Client) Connect() error

Connect connects the specified destination MongoDB server.

func (*Client) Database

func (client *Client) Database() string

Database returns a destination database.

func (*Client) Host

func (client *Client) Host() string

Host returns a destination host.

func (*Client) InsertOne

func (client *Client) InsertOne(doc interface{}) error

InsertOne posts the specified document to the destination collection.

func (*Client) Port

func (client *Client) Port() int

Port returns a destination port.

func (*Client) SetCollection

func (client *Client) SetCollection(col string)

SetCollection sets a destination collection.

func (*Client) SetDatabase

func (client *Client) SetDatabase(db string)

SetDatabase sets a destination database.

func (*Client) SetHost

func (client *Client) SetHost(host string)

SetHost sets a destination host.

func (*Client) SetPort

func (client *Client) SetPort(port int)

SetPort sets a destination port.

func (*Client) SetTimeout

func (client *Client) SetTimeout(d time.Duration)

SetTimeout sets a timeout for the request.

func (*Client) Timeout

func (client *Client) Timeout() time.Duration

Timeout return the timeout for the request.

type Command

type Command = message.Command

Command represents a query command of MongoDB database command.

type CommandExecutor

type CommandExecutor interface {
	// ExecuteCommand handles query commands other than those explicitly specified above.
	ExecuteCommand(*Conn, *Command) (bson.Document, error)
}

CommandExecutor represents an interface for MongoDB database commands.

type Config

type Config interface {
	TLSConfig

	message.Config

	// SetAuthrizationEnabled sets the authorization flag.
	SetAuthrizationEnabled(authorized bool)
	// IsAuthrizationEnabled returns true when the authorization is enabled.
	IsAuthrizationEnabled() bool

	// SetAddress sets a listen address.
	SetAddress(addr string)
	// Address returns a listen address.
	Address() string

	// SetPort sets a listen port.
	SetPort(port int)
	// Port returns a listent port.
	Port() int
}

Config stores server configuration parammeters.

func NewDefaultConfig

func NewDefaultConfig() Config

NewDefaultConfig returns a default configuration instance.

type Conn added in v0.9.2

type Conn struct {
	net.Conn

	sync.Map

	tracer.Context
	// contains filtered or unexported fields
}

Conn represents a connection of Wire protocol.

func (*Conn) Close added in v1.1.4

func (conn *Conn) Close() error

Close closes the connection.

func (*Conn) IsAuthrized added in v1.1.0

func (conn *Conn) IsAuthrized() bool

IsAuthrized returns true if the connection is authrized.

func (*Conn) IsTLSConnection added in v1.1.3

func (conn *Conn) IsTLSConnection() bool

IsTLSConnection return true if the connection is enabled TLS.

func (*Conn) SASLContext added in v1.2.0

func (conn *Conn) SASLContext() sasl.Context

SaslContext returns the SASL context of the connection.

func (*Conn) SetAuthrized added in v1.1.0

func (conn *Conn) SetAuthrized(authrized bool)

SetAuthrized sets the authrized flag to the connection.

func (*Conn) SetSASLContext added in v1.2.0

func (conn *Conn) SetSASLContext(saslContext sasl.Context)

SetSaslContext sets the SASL context to the connection.

func (*Conn) SetSpanContext added in v1.0.1

func (conn *Conn) SetSpanContext(span tracer.Context)

SetSpanContext sets the span context to the connection.

func (*Conn) SpanContext added in v1.0.1

func (conn *Conn) SpanContext() tracer.Context

SpanContext returns the tracer span context of the connection.

func (*Conn) TLSConnectionState added in v1.1.3

func (conn *Conn) TLSConnectionState() (*tls.ConnectionState, bool)

TLSConnectionState returns the TLS connection state.

func (*Conn) Timestamp added in v0.9.2

func (conn *Conn) Timestamp() time.Time

Timestamp returns the creation time of the connection.

func (*Conn) UUID added in v1.1.4

func (conn *Conn) UUID() uuid.UUID

UUID returns the UUID of the connection.

type ConnManager added in v1.1.4

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

ConnManager represents a connection map.

func NewConnManager added in v1.1.4

func NewConnManager() *ConnManager

NewConnManager returns a connection map.

func (*ConnManager) AddConn added in v1.1.4

func (mgr *ConnManager) AddConn(c *Conn)

AddConn adds the specified connection.

func (*ConnManager) Close added in v1.1.4

func (mgr *ConnManager) Close() error

Close closes the connection manager.

func (*ConnManager) ConnByUUID added in v1.1.4

func (mgr *ConnManager) ConnByUUID(uuid uuid.UUID) (*Conn, bool)

ConnByUUID returns the connection with the specified UUID.

func (*ConnManager) Conns added in v1.1.4

func (mgr *ConnManager) Conns() []*Conn

Conns returns the included connections.

func (*ConnManager) RemoveConn added in v1.1.4

func (mgr *ConnManager) RemoveConn(conn *Conn) error

RemoveConn deletes the specified connection from the map.

func (*ConnManager) Start added in v1.1.4

func (mgr *ConnManager) Start() error

Start starts the connection manager.

func (*ConnManager) Stop added in v1.1.4

func (mgr *ConnManager) Stop() error

Stop closes all connections.

type DatabaseCommandExecutor

DatabaseCommandExecutor represents an executor interface for MongoDB operation commands.

type DiagnosticCommandExecutor

type DiagnosticCommandExecutor interface {
	BuildInfo(*Conn, *Command) (bson.Document, error)
}

DiagnosticCommandExecutor represents an executor interface for MongoDB diagnostic commands.

type Document

type Document = bsoncore.Document

Document represents a document of MongoDB wire protocol.

type MessageExecutor

type MessageExecutor interface {
	QueryCommandExecutor
}

MessageExecutor represents an executor interface for MongoDB message.

type MessageListener

type MessageListener interface {
	protocol.MessageListener
}

MessageListener represents a listener for MongoDB messages.

type MessageResponse added in v1.2.1

type MessageResponse = message.Response

MessageResponse represents a message response.

type OpDelete

type OpDelete = protocol.Delete

OpDelete (OP_DELETE:2006) deletes documents.

type OpFlag

type OpFlag = protocol.Flag

OpFlag represents a flag of MongoDB wire protocol.

type OpGetMore

type OpGetMore = protocol.GetMore

OpGetMore (GET_MORE:2005) gets more data from a query. See Cursors.

type OpInsert

type OpInsert = protocol.Insert

OpInsert (OP_INSERT:2002) inserts new document.

type OpKillCursors

type OpKillCursors = protocol.KillCursors

OpKillCursors (OP_KILL_CURSORS:2007) notifies database that the client has finished with the cursor.

type OpMessage

type OpMessage = protocol.Message

OpMessage represents a message of MongoDB wire protocol.

type OpMessageHandler

type OpMessageHandler interface {
	// Update handles OP_UPDATE of MongoDB wire protocol.
	OpUpdate(conn *Conn, q *OpUpdate) (bson.Document, error)
	// Insert handles OP_INSERT of MongoDB wire protocol.
	OpInsert(conn *Conn, q *OpInsert) (bson.Document, error)
	// Query handles OP_QUERY of MongoDB wire protocol.
	OpQuery(conn *Conn, q *OpQuery) (bson.Document, error)
	// GetMore handles GET_MORE of MongoDB wire protocol.
	OpGetMore(conn *Conn, q *OpGetMore) (bson.Document, error)
	// Delete handles OP_DELETE of MongoDB wire protocol.
	OpDelete(conn *Conn, q *OpDelete) (bson.Document, error)
	// KillCursors handles OP_KILL_CURSORS of MongoDB wire protocol.
	OpKillCursors(conn *Conn, q *OpKillCursors) (bson.Document, error)
	// Msg handles OP_MSG of MongoDB wire protocol.
	OpMsg(conn *Conn, q *OpMsg) (bson.Document, error)
}

OpMessageHandler represents an interface for MongoDB query request.

type OpMsg

type OpMsg = protocol.Msg

OpMsg (OP_MSG:2013) sends a message using the format introduced in MongoDB 3.6.

type OpQuery

type OpQuery = protocol.Query

OpQuery (OP_QUERY:2004) queries a collection.

type OpReply

type OpReply = protocol.Reply

OpReply (OP_REPLY) replies to a client request. responseTo is set.

type OpUpdate

type OpUpdate = protocol.Update

OpUpdate (OP_UPDATE:2001) updates document.

type Query

type Query = message.Query

Query represents a query of MongoDB database command.

type QueryCommandExecutor added in v0.9.2

type QueryCommandExecutor interface {
	// Insert hadles OP_INSERT and 'insert' query of OP_MSG.
	Insert(*Conn, *Query) (int32, error)
	// Update hadles OP_UPDATE and 'update' query of OP_MSG.
	Update(*Conn, *Query) (int32, error)
	// Find hadles 'find' query of OP_MSG.
	Find(*Conn, *Query) ([]bson.Document, error)
	// Delete hadles OP_DELETE and 'delete' query of OP_MSG.
	Delete(*Conn, *Query) (int32, error)
}

QueryCommandExecutor represents an executor interface for MongoDB queries.

type ReplicationCommandExecutor

type ReplicationCommandExecutor interface {
	Hello(*Conn, *Command) (bson.Document, error)
}

ReplicationCommandExecutor represents an executor interface for MongoDB replication commands.

type Server

type Server interface {
	Config
	auth.Manager

	// Config returns a server configuration.
	Config() Config
	// SetTracer sets a tracer.
	SetTracer(tracer.Tracer)

	// SetMessageListener sets a message listener.
	SetMessageListener(l MessageListener)
	// SetMessageHandler sets a message handler.
	SetMessageHandler(h OpMessageHandler)
	// SetUserCommandExecutor sets a command exector for database operation commands.
	SetUserCommandExecutor(fn UserCommandExecutor)
	// SetDatabaseCommandExecutor sets a command exector for database operation commands.
	SetDatabaseCommandExecutor(fn DatabaseCommandExecutor)
	// SetAuthCommandExecutor  sets a command exector for auth operation commands.
	SetAuthCommandExecutor(fn AuthCommandExecutor)

	// Start starts a server.
	Start() error
	// Stop stops a server.
	Stop() error
	// Restart restarts a server.
	Restart() error
}

Server represents a server interface.

func NewServer

func NewServer() Server

NewServer returns a new server instance.

type TLSConfig added in v1.2.2

type TLSConfig interface {
	CertConfig

	// SetTLSEnabled sets a TLS enabled flag.
	SetTLSEnabled(enabled bool)
	// IsEnabled returns true if the TLS is enabled.
	IsTLSEnabled() bool
}

TLSConfig represents a TLS configuration interface.

type UserCommandExecutor

type UserCommandExecutor interface {
	QueryCommandExecutor
}

UserCommandExecutor represents an executor interface for MongoDB query commands.

type WriteOperationExecutor

type WriteOperationExecutor interface {
	GetLastError(*Conn, *Command) (bson.Document, error)
}

WriteOperationExecutor represents an executor interface for MongoDB write operation commands.

Directories

Path Synopsis
tls

Jump to

Keyboard shortcuts

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