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
- func IsNoCollection(err error) bool
- func ParseReadPreference(rp string) (mgo.Mode, bson.D, error)
- 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) SetBypassDocumentValidation(bypassDocumentValidation bool)
- func (self *SessionProvider) SetFlags(flagBits sessionFlag)
- func (self *SessionProvider) SetReadPreference(pref mgo.Mode)
- func (self *SessionProvider) SetTags(tags bson.D)
- 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 ( ErrLostConnection = "lost connection to server" ErrNoReachableServers = "no reachable servers" ErrNsNotFound = "ns not found" // replication errors list the replset name if we are talking to a mongos, // so we can only check for this universal prefix ErrReplTimeoutPrefix = "waiting for replication timed out" )
const (
DefaultTestPort = "33333"
)
Default port for integration tests
const (
MaxBSONSize = 16 * 1024 * 1024 // 16MB - maximum BSON document size
)
MongoDB enforced limits.
const (
WarningNonPrimaryMongosConnection = "Warning: using a non-primary readPreference with a " +
"connection to mongos may produce inconsistent duplicates or miss some documents."
)
Variables ¶
var ( 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). nil is returned if the collection does not exist.
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)
func IsNoCmd ¶
IsNoCmd reeturns true if err indicates a query command is not supported, otherwise, returns false.
func IsNoCollection ¶
IsNoCollection returns true if err indicates a query resulted in a "no collection" error otherwise, returns false.
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
NewBSONSource creates a BSONSource with a reusable I/O buffer
func NewBufferlessBSONSource ¶
func NewBufferlessBSONSource(in io.ReadCloser) *BSONSource
NewBufferlessBSONSource creates a BSONSource without a reusable I/O buffer
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) LoadNext ¶
func (bs *BSONSource) LoadNext() []byte
LoadNext reads and returns the next BSON document in the stream. If the BSONSource was created with NewBSONSource then each returned []byte will be a slice of a single reused I/O buffer. If the BSONSource was created with NewBufferlessBSONSource then each returend []byte will be individually allocated
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.D `bson:"o"` Query bson.D `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) SetBypassDocumentValidation ¶
func (self *SessionProvider) SetBypassDocumentValidation(bypassDocumentValidation bool)
SetBypassDocumentValidation sets whether to bypass document validation in the SessionProvider and eventually in the masterSession
func (*SessionProvider) SetFlags ¶
func (self *SessionProvider) SetFlags(flagBits sessionFlag)
SetFlags allows certain modifications to the masterSession after initial creation.
func (*SessionProvider) SetReadPreference ¶
func (self *SessionProvider) SetReadPreference(pref mgo.Mode)
SetReadPreference sets the read preference mode in the SessionProvider and eventually in the masterSession
func (*SessionProvider) SetTags ¶
func (self *SessionProvider) SetTags(tags bson.D)
SetTags sets the server selection tags in the SessionProvider and eventually in the masterSession
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.