Documentation ¶
Overview ¶
Package db implements generic connection to MongoDB, and contains subpackages for specific methods of connection.
Index ¶
- Constants
- Variables
- func ApplyFlags(q *mgo.Query, session *mgo.Session, flags int) *mgo.Query
- func BuildWriteConcern(writeConcern string, nodeType NodeType) (*mgo.Safe, error)
- func GetCollectionOptions(coll *mgo.Collection) (*bson.D, error)
- func GetCollections(database *mgo.Database, name string) (*mgo.Iter, bool, error)
- func GetIndexes(coll *mgo.Collection) (*mgo.Iter, error)
- func IsConnectionError(err error) bool
- func IsNoCmd(err error) bool
- type ApplyOpsResponse
- type BSONSource
- type BufferedBulkInserter
- type CommandRunner
- type DBConnector
- type DecodedBSONSource
- type GetConnectorFunc
- type NodeType
- type Oplog
- type RawDocSource
- type SessionProvider
- func (sp *SessionProvider) CollectionNames(dbName string) ([]string, error)
- func (sp *SessionProvider) DatabaseNames() ([]string, error)
- func (sp *SessionProvider) FindOne(db, collection string, skip int, query interface{}, sort []string, ...) error
- func (sp *SessionProvider) GetNodeType() (NodeType, error)
- func (self *SessionProvider) GetSession() (*mgo.Session, error)
- func (sp *SessionProvider) IsMongos() (bool, error)
- func (sp *SessionProvider) IsReplicaSet() (bool, error)
- func (sp *SessionProvider) Remove(db, c string, q interface{}) error
- func (sp *SessionProvider) Run(command interface{}, out interface{}, db string) error
- func (self *SessionProvider) SetFlags(flagBits sessionFlag)
- func (sp *SessionProvider) SupportsRepairCursor(db, collection string) (bool, error)
- func (sp *SessionProvider) SupportsWriteCommands() (bool, error)
- type VanillaDBConnector
Constants ¶
const ( Snapshot = 1 << iota LogReplay Prefetch )
Query flags
const ( Mongos NodeType = "mongos" Standalone = "standalone" ReplSet = "replset" Unknown = "unknown" )
const ( None sessionFlag = 0 Monotonic sessionFlag = 1 << iota DisableSocketTimeout )
Session flags.
const ( MaxBSONSize = 16 * 1024 * 1024 // 16MB - maximum BSON document size MaxMessageSize = 2 * 16 * 1024 * 1024 // 32MB - maximum message size in wire protocol )
MongoDB enforced limits.
const (
DefaultTestPort = "33333"
)
Default port for integration tests
Variables ¶
var ( ErrLostConnection = errors.New("lost connection to server") ErrNoReachableServers = errors.New("no reachable servers") ErrNsNotFound = errors.New("ns not found") DefaultDialTimeout = time.Second * 3 GetConnectorFuncs = []GetConnectorFunc{} )
Functions ¶
func ApplyFlags ¶
func ApplyFlags(q *mgo.Query, session *mgo.Session, flags int) *mgo.Query
ApplyFlags applies flags to the given query session.
func BuildWriteConcern ¶
BuildWriteConcern takes a string and a NodeType indicating the type of node the write concern is intended to be used against, and converts the write concern string argument into an mgo.Safe object that's usable on sessions for that node type.
func GetCollectionOptions ¶
func GetCollections ¶
func GetIndexes ¶
func GetIndexes(coll *mgo.Collection) (*mgo.Iter, error)
GetIndexes returns an iterator to thethe raw index info for a collection by using the listIndexes command if available, or by falling back to querying against system.indexes (pre-3.0 systems).
func IsConnectionError ¶
IsConnectionError returns a boolean indicating if a given error is due to an error in an underlying DB connection (as opposed to some other write failure such as a duplicate key error)
Types ¶
type ApplyOpsResponse ¶
ApplyOpsResponse represents the response from an 'applyOps' command.
type BSONSource ¶
type BSONSource struct { Stream io.ReadCloser // contains filtered or unexported fields }
BSONSource reads documents from the underlying io.ReadCloser, Stream which wraps a stream of BSON documents.
func NewBSONSource ¶
func NewBSONSource(in io.ReadCloser) *BSONSource
func (*BSONSource) Close ¶
func (bs *BSONSource) Close() error
Close closes the BSONSource, rendering it unusable for I/O. It returns an error, if any.
func (*BSONSource) Err ¶
func (bs *BSONSource) Err() error
func (*BSONSource) LoadNextInto ¶
func (bs *BSONSource) LoadNextInto(into []byte) (bool, int32)
LoadNextInto unmarshals the next BSON document into result. Returns a boolean indicating whether or not the operation was successful (true if no errors) and the size of the unmarshaled document.
type BufferedBulkInserter ¶
type BufferedBulkInserter struct {
// contains filtered or unexported fields
}
BufferedBulkInserter implements a bufio.Writer-like design for queuing up documents and inserting them in bulk when the given doc limit (or max message size) is reached. Must be flushed at the end to ensure that all documents are written.
func NewBufferedBulkInserter ¶
func NewBufferedBulkInserter(collection *mgo.Collection, docLimit int, continueOnError bool) *BufferedBulkInserter
NewBufferedBulkInserter returns an initialized BufferedBulkInserter for writing.
func (*BufferedBulkInserter) Flush ¶
func (bb *BufferedBulkInserter) Flush() error
Flush writes all buffered documents in one bulk insert then resets the buffer.
func (*BufferedBulkInserter) Insert ¶
func (bb *BufferedBulkInserter) Insert(doc interface{}) error
Insert adds a document to the buffer for bulk insertion. If the buffer is full, the bulk insert is made, returning any error that occurs.
type CommandRunner ¶
type CommandRunner interface { Run(command interface{}, out interface{}, database string) error FindOne(db, collection string, skip int, query interface{}, sort []string, into interface{}, opts int) error Remove(db, collection string, query interface{}) error DatabaseNames() ([]string, error) CollectionNames(db string) ([]string, error) }
CommandRunner exposes functions that can be run against a server
type DBConnector ¶
type DBConnector interface { // configure, based on the options passed in Configure(options.ToolOptions) error // dial the database and get a fresh new session GetNewSession() (*mgo.Session, error) }
Interface type for connecting to the database.
type DecodedBSONSource ¶
type DecodedBSONSource struct { RawDocSource // contains filtered or unexported fields }
DecodedBSONSource reads documents from the underlying io.ReadCloser, Stream which wraps a stream of BSON documents.
func NewDecodedBSONSource ¶
func NewDecodedBSONSource(ds RawDocSource) *DecodedBSONSource
func (*DecodedBSONSource) Err ¶
func (dbs *DecodedBSONSource) Err() error
Err returns any error in the DecodedBSONSource or its RawDocSource.
func (*DecodedBSONSource) Next ¶
func (dbs *DecodedBSONSource) Next(result interface{}) bool
Next unmarshals the next BSON document into result. Returns true if no errors are encountered and false otherwise.
type GetConnectorFunc ¶
type GetConnectorFunc func(opts options.ToolOptions) DBConnector
Used to get appropriate the DBConnector(s) based on opts
type Oplog ¶
type Oplog struct { Timestamp bson.MongoTimestamp `bson:"ts"` HistoryID int64 `bson:"h"` Version int `bson:"v"` Operation string `bson:"op"` Namespace string `bson:"ns"` Object bson.M `bson:"o"` Query bson.M `bson:"o2"` }
Oplog represents a MongoDB oplog document.
type RawDocSource ¶
RawDocSource wraps basic functions for reading a BSON source file.
type SessionProvider ¶
type SessionProvider struct {
// contains filtered or unexported fields
}
Used to manage database sessions
func NewSessionProvider ¶
func NewSessionProvider(opts options.ToolOptions) (*SessionProvider, error)
NewSessionProvider constructs a session provider but does not attempt to create the initial session.
func (*SessionProvider) CollectionNames ¶
func (sp *SessionProvider) CollectionNames(dbName string) ([]string, error)
CollectionNames returns the names of all the collections in the dbName database.
func (*SessionProvider) DatabaseNames ¶
func (sp *SessionProvider) DatabaseNames() ([]string, error)
DatabaseNames returns a slice containing the names of all the databases on the connected server.
func (*SessionProvider) FindOne ¶
func (sp *SessionProvider) FindOne(db, collection string, skip int, query interface{}, sort []string, into interface{}, flags int) error
FindOne retuns the first document in the collection and database that matches the query after skip, sort and query flags are applied.
func (*SessionProvider) GetNodeType ¶
func (sp *SessionProvider) GetNodeType() (NodeType, error)
GetNodeType checks if the connected SessionProvider is a mongos, standalone, or replset, by looking at the result of calling isMaster.
func (*SessionProvider) GetSession ¶
func (self *SessionProvider) GetSession() (*mgo.Session, error)
Returns a session connected to the database server for which the session provider is configured.
func (*SessionProvider) IsMongos ¶
func (sp *SessionProvider) IsMongos() (bool, error)
IsMongos returns true if the connected server is a mongos.
func (*SessionProvider) IsReplicaSet ¶
func (sp *SessionProvider) IsReplicaSet() (bool, error)
IsReplicaSet returns a boolean which is true if the connected server is part of a replica set.
func (*SessionProvider) Remove ¶
func (sp *SessionProvider) Remove(db, c string, q interface{}) error
Remove removes all documents matched by query q in the db database and c collection.
func (*SessionProvider) Run ¶
func (sp *SessionProvider) Run(command interface{}, out interface{}, db string) error
Run issues the provided command on the db database and unmarshals its result into out.
func (*SessionProvider) SetFlags ¶
func (self *SessionProvider) SetFlags(flagBits sessionFlag)
SetFlags allows certain modifications to the masterSession after initial creation.
func (*SessionProvider) SupportsRepairCursor ¶
func (sp *SessionProvider) SupportsRepairCursor(db, collection string) (bool, error)
SupportsRepairCursor takes in an example db and collection name and returns true if the connected server supports the repairCursor command. It returns false and the error that occurred if it is not supported.
func (*SessionProvider) SupportsWriteCommands ¶
func (sp *SessionProvider) SupportsWriteCommands() (bool, error)
SupportsWriteCommands returns true if the connected server supports write commands, returns false otherwise.
type VanillaDBConnector ¶
type VanillaDBConnector struct {
// contains filtered or unexported fields
}
Basic connector for dialing the database, with no authentication.
func (*VanillaDBConnector) Configure ¶
func (self *VanillaDBConnector) Configure(opts options.ToolOptions) error
Configure sets up the db connector using the options in opts. It parses the connection string and then sets up the dial information using the default dial timeout.
func (*VanillaDBConnector) GetNewSession ¶
func (self *VanillaDBConnector) GetNewSession() (*mgo.Session, error)
GetNewSession connects to the server and returns the established session and any error encountered.