mgoplus

package module
v0.0.0-...-00eb309 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2015 License: MIT Imports: 3 Imported by: 0

README

mgoplus

Admin functions for the labix.org mongodb driver.

GoDoc

Example
package main

import(
	"github.com/gabstv/go-mgoplus"
	"labix.org/v2/mgo"
	"log"
	"sort"
)

func main() {
	// connect to mongodb
	sess, err := mgo.Dial("localhost")
	if err != nil {
		log.Fatalln(err)
	}

	db := sess.DB("my_collection")

	// retrieve all collections
	colls, err := mgoplus.GetCollectionNames(db)
	if err != nil {
		log.Fatalln(err)
	}

	for _, v := range colls {
		log.Println(v)
	}

	// retrieve information of a collection
	info, err := mgoplus.GetCollectionStats(db, "users")
	if err != nil {
		log.Fatalln(err)
	}

	log.Printf("Collection '%s' size is %v bytes.\n", "users", info.Size)

	// retrieve all collection infos and sort by size (in bytes)
	allstars, err := mgoplus.GetAllCollectionStats(db)
	if err != nil {
		log.Fatalln(err)
	}
	sort.Sort(mgoplus.SortCollectionStatsBySizeDesc(allstars))
	for _, v := range allstars {
		log.Println(v.NS, v.Size)
	}
}

Documentation

Overview

Package mgoplus implements admin functions to use with labix.org mongodb driver.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetCollectionNames

func GetCollectionNames(db *mgo.Database) ([]string, error)

Returns a slice containing the names of all collections in the current database.

Based on http://docs.mongodb.org/manual/reference/method/db.getCollectionNames/

Types

type CollectionStats

type CollectionStats struct {
	TotalIndexSize int64            `bson:"totalIndexSize"`
	LastExtentSize int64            `bson:"lastExtentSize"`
	AvgObjSize     int64            `bson:"avgObjSize"`
	StorageSize    int64            `bson:"storageSize"`
	UserFlags      int              `bson:"userFlags"`
	Count          int              `bson:"count"`
	Size           int64            `bson:"size"`
	NS             string           `bson:"ns"`
	Capped         bool             `bson:"capped"`
	NIndexes       int              `bson:"nindexes"`
	IndexSizes     map[string]int64 `bson:"indexSizes"`
}

CollectionStats contains mostly size measurements (in bytes) of a collection. Use GetCollectionStats to retrieve this struct.

Members:

TotalIndexSize
    The size of all indexes, combined.
LastExtentSize
    The size of the last extent allocated. The scale argument affects this value.
    Only present when using the mmapv1 storage engine.
AvgObjSize
    Average size of a document/object.
StorageSize
    The total amount of storage allocated to this collection for document storage.
UserFlags
    A number that indicates the user-set flags on the collection.
    userFlags only appears when using the mmapv1 storage engine.
Count
    The number of objects or documents in this collection.
Size
    The total size in memory of all records in a collection. This value does not
    include the record header, which is 16 bytes per record, but does include the
    record’s padding. Additionally size does not include the size of any indexes
    associated with the collection, which the TotalIndexSize field reports.
NS
    The namespace of the current collection, which follows the format
    [database].[collection].
Capped
    Capped is a fixed-sized collection that automatically overwrites
    its oldest entries when it reaches its maximum size.
NIndexes
    The amount of indexes (including _id_).
IndexSizes
    This field specifies the key and size of every existing index on the collection.

func GetAllCollectionStats

func GetAllCollectionStats(db *mgo.Database) ([]CollectionStats, error)

Returns stats of all collections in the database.

func GetCollectionStats

func GetCollectionStats(db *mgo.Database, collectionName string) (CollectionStats, error)

Returns a variety of storage statistics for a given collection.

Based on http://docs.mongodb.org/manual/reference/method/db.collection.stats/

type DbStats

type DbStats struct {
	DB              string                 `bson:"db"`
	NCollections    int                    `bson:"collections"`
	NObjects        int                    `bson:"objects"`
	AvgObjSize      int64                  `bson:"avgObjSize"`
	DataSize        int64                  `bson:"dataSize"`
	StorageSize     int64                  `bson:"storageSize"`
	NumExtents      int                    `bson:"numExtents"`
	NIndexes        int                    `bson:"indexes"`
	IndexSize       int64                  `bson:"indexSize"`
	FileSize        int64                  `bson:"fileSize"`
	NsSizeMB        int64                  `bson:"nsSizeMB"`
	DataFileVersion DbStatsDataFileVersion `bson:"dataFileVersion"`
	ExtentFreeList  DbStatsExtentFreeList  `bson:"extentFreeList"`
}

The DbStats struct contains storage statistics for a given database. All size values are in bytes.

Members:

DB
    Contains the name of the database.
NCollections
    Contains a count of the number of collections in that database.
NObjects
    Contains a count of the number of objects (i.e. documents) in
    the database across all collections.
AvgObjSize
    The average size of each document in bytes. This is the dataSize
    divided by the number of documents.
DataSize
    The total size in bytes of the data held in this database including
    the padding factor. The dataSize will not decrease when documents
    shrink, but will decrease when you remove documents.
StorageSize
    The total amount of space in bytes allocated to collections in this
    database for document storage. The storageSize does not decrease as
    you remove or shrink documents.
NumExtents
    Contains a count of the number of extents in the database across all collections.
NIndexes
    Contains a count of the total number of indexes across all collections
    in the database.
IndexSize
    The total size in bytes of all indexes created on this database.
FileSize
    The total size in bytes of the data files that hold the database.
    This value includes preallocated space and the padding factor.
    The value of fileSize only reflects the size of the data files for
    the database and not the namespace file.
    Only present when using the mmapv1 storage engine.
NsSizeMB
    The total size of the namespace files (i.e. that end with .ns) for
    this database. You cannot change the size of the namespace file
    after creating a database, but you can change the default size for
    all new namespace files with the nsSize runtime option.
    Only present when using the mmapv1 storage engine.
DataFileVersion
    See DbStatsDataFileVersion
ExtentFreeList
    See DbStatsExtentFreeList

func GetDbStats

func GetDbStats(db *mgo.Database) (DbStats, error)

Returns a variety of database statistics.

Based on http://docs.mongodb.org/manual/reference/command/dbStats/#dbcmd.dbStats

type DbStatsDataFileVersion

type DbStatsDataFileVersion struct {
	Major int `bson:"major"`
	Minor int `bson:"minor"`
}

Document that contains information about the on-disk format of the data files for the database. Only present when using the mmapv1 storage engine.

type DbStatsExtentFreeList

type DbStatsExtentFreeList struct {
	Num  int   `bson:"num"`
	Size int64 `bson:"size"`
}

Only present when using the mmapv1 storage engine.

type SortCollectionStatsByIndexSizeAsc

type SortCollectionStatsByIndexSizeAsc []CollectionStats

func (SortCollectionStatsByIndexSizeAsc) Len

func (SortCollectionStatsByIndexSizeAsc) Less

func (SortCollectionStatsByIndexSizeAsc) Swap

type SortCollectionStatsByIndexSizeDesc

type SortCollectionStatsByIndexSizeDesc []CollectionStats

func (SortCollectionStatsByIndexSizeDesc) Len

func (SortCollectionStatsByIndexSizeDesc) Less

func (SortCollectionStatsByIndexSizeDesc) Swap

type SortCollectionStatsBySizeAsc

type SortCollectionStatsBySizeAsc []CollectionStats

func (SortCollectionStatsBySizeAsc) Len

func (SortCollectionStatsBySizeAsc) Less

func (c SortCollectionStatsBySizeAsc) Less(i, j int) bool

func (SortCollectionStatsBySizeAsc) Swap

func (c SortCollectionStatsBySizeAsc) Swap(i, j int)

type SortCollectionStatsBySizeDesc

type SortCollectionStatsBySizeDesc []CollectionStats

func (SortCollectionStatsBySizeDesc) Len

func (SortCollectionStatsBySizeDesc) Less

func (c SortCollectionStatsBySizeDesc) Less(i, j int) bool

func (SortCollectionStatsBySizeDesc) Swap

func (c SortCollectionStatsBySizeDesc) Swap(i, j int)

Jump to

Keyboard shortcuts

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