db

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2019 License: Apache-2.0 Imports: 28 Imported by: 46

Documentation

Overview

Package db implements generic connection to MongoDB, and contains subpackages for specific methods of connection.

Index

Constants

View Source
const (
	Snapshot = 1 << iota
	LogReplay
	Prefetch
)

Query flags

View Source
const (
	Mongos     NodeType = "mongos"
	Standalone          = "standalone"
	ReplSet             = "replset"
	Unknown             = "unknown"
)
View Source
const (
	None      sessionFlag = 0
	Monotonic sessionFlag = 1 << iota
	DisableSocketTimeout
)

Session flags.

View Source
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"
	ErrCouldNotContactPrimaryPrefix = "could not contact primary for replica set"
	ErrWriteResultsUnavailable      = "write results unavailable from"
	ErrCouldNotFindPrimaryPrefix    = `could not find host matching read preference { mode: "primary"`
	ErrUnableToTargetPrefix         = "unable to target"
	ErrNotMaster                    = "not master"
	ErrConnectionRefusedSuffix      = "Connection refused"

	// ignorable errors
	ErrDuplicateKeyCode         = 11000
	ErrFailedDocumentValidation = 121
	ErrUnacknowledgedWrite      = "unacknowledged write"
)
View Source
const (
	DefaultTestPort = "33333"
)

Default port for integration tests

View Source
const (
	MaxBSONSize = 16 * 1024 * 1024 // 16MB - maximum BSON document size
)

MongoDB enforced limits.

View Source
const (
	WarningNonPrimaryMongosConnection = "Warning: using a non-primary readPreference with a " +
		"connection to mongos may produce inconsistent duplicates or miss some documents."
)

Variables

This section is empty.

Functions

func ApplyFlags

func ApplyFlags(opts *mopt.FindOneOptions, flags int)

ApplyFlags applies flags to the given query session.

func CanIgnoreError

func CanIgnoreError(err error) bool

Returns whether the tools can continue when encountering the given error. Currently, only DuplicateKeyErrors are ignorable.

func FilterError

func FilterError(stopOnError bool, err error) error

FilterError determines whether an error needs to be propagated back to the user or can be continued through. If an error cannot be ignored, a non-nil error is returned. If an error can be continued through, it is logged and nil is returned.

func GetCollections

func GetCollections(database *mongo.Database, name string) (*mongo.Cursor, error)

Assumes that mongo.Database will normalize legacy names to omit database name as required by the Enumerate Collections spec

func GetIndexes

func GetIndexes(coll *mongo.Collection) (*mongo.Cursor, 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 GetLatestOplogTimestamp

func GetLatestOplogTimestamp(client *mongo.Client, query interface{}) (primitive.Timestamp, error)

GetLatestOplogTimestamp returns the timestamp of the most recent oplog record satisfying the given `query` or a zero-value primitive.Timestamp if no oplog record matches. This method does not ensure that all prior oplog entries are visible (i.e. have been storage-committed).

func GetLatestVisibleOplogTimestamp

func GetLatestVisibleOplogTimestamp(client *mongo.Client) (primitive.Timestamp, error)

GetLatestVisibleOplogTimestamp returns the timestamp of the most recent "visible" oplog record. By "visible", we mean that all prior oplog entries have been storage-committed. See SERVER-30724 for a more detailed description.

func GetOldestActiveTransactionTimestamp

func GetOldestActiveTransactionTimestamp(client *mongo.Client) (primitive.Timestamp, error)

GetOldestActiveTransactionTimestamp returns the oldest active transaction timestamp from the config.transactions table or else a zero-value primitive.Timestamp.

func NewMongoWriteConcern

func NewMongoWriteConcern(writeConcern string, cs *connstring.ConnString) (wc *writeconcern.WriteConcern, err error)

NewMongoWriteConcern takes a string (from the command line writeConcern option) and a ConnString object (from the command line uri option) and returns a WriteConcern. If both are provided, preference is given to the command line writeConcern option. If neither is provided, the default 'majority' write concern is constructed.

func NewReadPreference

func NewReadPreference(rp string, cs *connstring.ConnString) (*readpref.ReadPref, error)

NewReadPreference takes a string (command line read preference argument) and a ConnString (from the command line URI argument) and returns a ReadPref. If both are provided, preference is given to the command line argument. If both are empty, a default read preference of primary will be returned.

func StripDBFromNamespace

func StripDBFromNamespace(namespace string, dbName string) (string, error)

Types

type ApplyOpsResponse

type ApplyOpsResponse struct {
	Ok     bool   `bson:"ok"`
	ErrMsg string `bson:"errmsg"`
}

ApplyOpsResponse represents the response from an 'applyOps' command.

type BSONSource

type BSONSource struct {
	Stream io.ReadCloser

	MaxBSONSize int32
	// 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

func (*BSONSource) SetMaxBSONSize

func (bs *BSONSource) SetMaxBSONSize(size int32)

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 NewOrderedBufferedBulkInserter

func NewOrderedBufferedBulkInserter(collection *mongo.Collection, docLimit int) *BufferedBulkInserter

NewOrderedBufferedBulkInserter returns an initialized BufferedBulkInserter for performing ordered bulk writes.

func NewUnorderedBufferedBulkInserter

func NewUnorderedBufferedBulkInserter(collection *mongo.Collection, docLimit int) *BufferedBulkInserter

NewOrderedBufferedBulkInserter returns an initialized BufferedBulkInserter for performing unordered bulk writes.

func (*BufferedBulkInserter) Delete

func (bb *BufferedBulkInserter) Delete(selector, replacement bson.D) (*mongo.BulkWriteResult, error)

Delete adds a document to the buffer for bulk removal. If the buffer becomes full, the bulk delete is performed, returning any error that occurs.

func (*BufferedBulkInserter) Flush

Flush writes all buffered documents in one bulk write and then resets the buffer.

func (*BufferedBulkInserter) Insert

func (bb *BufferedBulkInserter) Insert(doc interface{}) (*mongo.BulkWriteResult, error)

Insert adds a document to the buffer for bulk insertion. If the buffer becomes full, the bulk write is performed, returning any error that occurs.

func (*BufferedBulkInserter) InsertRaw

func (bb *BufferedBulkInserter) InsertRaw(rawBytes []byte) (*mongo.BulkWriteResult, error)

InsertRaw adds a document, represented as raw bson bytes, to the buffer for bulk insertion. If the buffer becomes full, the bulk write is performed, returning any error that occurs.

func (*BufferedBulkInserter) Replace

func (bb *BufferedBulkInserter) Replace(selector, replacement bson.D) (*mongo.BulkWriteResult, error)

Replace adds a document to the buffer for bulk replacement. If the buffer becomes full, the bulk write is performed, returning any error that occurs.

func (*BufferedBulkInserter) SetBypassDocumentValidation

func (bb *BufferedBulkInserter) SetBypassDocumentValidation(bypass bool) *BufferedBulkInserter

func (*BufferedBulkInserter) SetOrdered

func (bb *BufferedBulkInserter) SetOrdered(ordered bool) *BufferedBulkInserter

func (*BufferedBulkInserter) SetUpsert

func (bb *BufferedBulkInserter) SetUpsert(upsert bool) *BufferedBulkInserter

func (*BufferedBulkInserter) Update

func (bb *BufferedBulkInserter) Update(selector, update bson.D) (*mongo.BulkWriteResult, error)

Update adds a document to the buffer for bulk update. If the buffer becomes full, the bulk write is performed, returning any error that occurs.

type CollectionInfo

type CollectionInfo struct {
	Name    string `bson:"name"`
	Type    string `bson:"type"`
	Options bson.M `bson:"options"`
	Info    bson.M `bson:"info"`
}

func GetCollectionInfo

func GetCollectionInfo(coll *mongo.Collection) (*CollectionInfo, error)

func (*CollectionInfo) GetUUID

func (ci *CollectionInfo) GetUUID() string

func (*CollectionInfo) IsSystemCollection

func (ci *CollectionInfo) IsSystemCollection() bool

func (*CollectionInfo) IsView

func (ci *CollectionInfo) IsView() bool

type CommandRunner

type CommandRunner interface {
	Run(command interface{}, out interface{}, database string) error
	RunString(commandName string, 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 XXX Does anything rely on this?

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

NextGBSON unmarshals the next BSON document into result using the official go driver. Returns true if no errors are encountered and false otherwise. This function does NOT zero out the result before writing to it.

type DeferredQuery

type DeferredQuery struct {
	Coll      *mongo.Collection
	Filter    interface{}
	Hint      interface{}
	LogReplay bool
}

DeferredQuery represents a deferred query

func (*DeferredQuery) EstimatedDocumentCount

func (q *DeferredQuery) EstimatedDocumentCount() (int, error)

EstimatedDocumentCount issues a count command.

func (*DeferredQuery) Iter

func (q *DeferredQuery) Iter() (*mongo.Cursor, error)

Iter executes a find query and returns a cursor.

type NodeType

type NodeType string

type Oplog

type Oplog struct {
	Timestamp  primitive.Timestamp `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,omitempty"`
	UI         *primitive.Binary   `bson:"ui,omitempty"`
	LSID       bson.Raw            `bson:"lsid,omitempty"`
	TxnNumber  *int64              `bson:"txnNumber,omitempty"`
	PrevOpTime bson.Raw            `bson:"prevOpTime,omitempty"`
}

Oplog represents a MongoDB oplog document.

type OplogTailTime

type OplogTailTime struct {
	Latest  primitive.Timestamp
	Restart primitive.Timestamp
}

OplogTailTime represents two ways of describing the "end" of the oplog at a point in time. The Latest field represents the last visible (storage committed) timestamp. The Restart field represents a (possibly older) timestamp that can be used to start tailing or copying the oplog without losing parts of transactions in progress.

func GetOplogTailTime

func GetOplogTailTime(client *mongo.Client) (OplogTailTime, error)

GetOplogTailTime constructs an OplogTailTime

type RawDocSource

type RawDocSource interface {
	LoadNext() []byte
	Close() error
	Err() error
}

RawDocSource wraps basic functions for reading a BSON source file.

type SessionProvider

type SessionProvider struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Used to manage database sessions

func NewSessionProvider

func NewSessionProvider(opts options.ToolOptions) (*SessionProvider, error)

NewSessionProvider constructs a session provider, including a connected client.

func (*SessionProvider) Close

func (sp *SessionProvider) Close()

Close closes the master session in the connection pool

func (*SessionProvider) CreateCollection

func (sp *SessionProvider) CreateCollection(dbName, collName string) error

func (*SessionProvider) DB

func (sp *SessionProvider) DB(name string) *mongo.Database

DB provides a database with the default read preference

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) DropDatabase

func (sp *SessionProvider) DropDatabase(dbName string) error

func (*SessionProvider) FindOne

func (sp *SessionProvider) FindOne(db, collection string, skip int, query interface{}, sort interface{}, 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 (sp *SessionProvider) GetSession() (*mongo.Client, error)

Returns a mongo.Client 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) Run

func (sp *SessionProvider) Run(command interface{}, out interface{}, name string) error

func (*SessionProvider) RunString

func (sp *SessionProvider) RunString(commandName string, out interface{}, name string) error

func (*SessionProvider) ServerVersion

func (sp *SessionProvider) ServerVersion() (string, error)

func (*SessionProvider) ServerVersionArray

func (sp *SessionProvider) ServerVersionArray() (Version, error)

type Version

type Version [3]int

func (Version) Cmp

func (v1 Version) Cmp(v2 Version) int

func (Version) GT

func (v1 Version) GT(v2 Version) bool

func (Version) GTE

func (v1 Version) GTE(v2 Version) bool

func (Version) LT

func (v1 Version) LT(v2 Version) bool

func (Version) LTE

func (v1 Version) LTE(v2 Version) bool

Jump to

Keyboard shortcuts

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