// IsTokuMX determines if the server connected to is TokuMX. func IsTokuMX(db *mgo.Database) bool { var result bson.M if err := db.Run("buildInfo", &result); err != nil { log.Fatal(err) } return result["tokumxVersion"] != nil }
// creates a collection in MongoDB func createMongoCollection(collname string, db *mgo.Database) { fmt.Println("creating collection: ", collname) var result bson.M createCmd := createCollOptions{Coll: collname} err := db.Run(createCmd, &result) if err != nil { log.Fatal(err) } }
func applyTokuIndexOptions(db *mgo.Database, collname string, options tokuMXCreateOptions) { var result bson.M var optBson bson.M optBson = bson.M{"compression": options.CompressionType, "pageSize": options.NodeSize, "readPageSize": options.BasementSize} err := db.Run(bson.D{{"reIndex", collname}, {"index", "*"}, {"options", optBson}}, &result) if err != nil { log.Fatal("Failed to set options on indexes, received ", err) } }
// Returns a variety of database statistics. // // Based on // http://docs.mongodb.org/manual/reference/command/dbStats/#dbcmd.dbStats func GetDbStats(db *mgo.Database) (DbStats, error) { var output DbStats err := db.Run(bson.D{ {"dbStats", 1}, {"scale", 1}, }, &output) if err != nil { return output, err } return output, nil }
// Returns a variety of storage statistics for a given collection. // // Based on // http://docs.mongodb.org/manual/reference/method/db.collection.stats/ func GetCollectionStats(db *mgo.Database, collectionName string) (CollectionStats, error) { var output CollectionStats err := db.Run(bson.D{ {"collStats", collectionName}, {"scale", 1}, {"verbose", true}, }, &output) if err != nil { return output, err } return output, nil }
// creates a collection in TokuMX func createTokuCollection(collname string, db *mgo.Database, options tokuMXCreateOptions) { fmt.Println("creating collection ", collname) var result bson.M createCmd := createCollOptions{ Coll: collname, Compression: options.CompressionType, NodeSize: options.NodeSize, BasementSize: options.BasementSize, Partitioned: options.Partitioned} err := db.Run(createCmd, &result) if err != nil { log.Fatal(err) } }
// Returns a slice containing the names of all collections in the current database. // // Based on // http://docs.mongodb.org/manual/reference/method/db.getCollectionNames/ func GetCollectionNames(db *mgo.Database) ([]string, error) { raw := make(map[string]interface{}) err := db.Run(bson.D{{"listCollections", 1}}, &raw) if err != nil { return nil, err } jq := jsonq.NewQuery(raw) items, err := jq.ArrayOfObjects("cursor", "firstBatch") if err != nil { return nil, err } output := make([]string, len(items)) for k, v := range items { output[k], _ = v["name"].(string) } return output, nil }