use default mongo db (#103)

master
bittoy 4 years ago committed by GitHub
parent e5d7c3ab04
commit 0a36031d48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -22,8 +22,8 @@ type (
} }
) )
func MustNewModel(url, database, collection string, opts ...Option) *Model { func MustNewModel(url, collection string, opts ...Option) *Model {
model, err := NewModel(url, database, collection, opts...) model, err := NewModel(url, collection, opts...)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -31,7 +31,7 @@ func MustNewModel(url, database, collection string, opts ...Option) *Model {
return model return model
} }
func NewModel(url, database, collection string, opts ...Option) (*Model, error) { func NewModel(url, collection string, opts ...Option) (*Model, error) {
session, err := getConcurrentSession(url) session, err := getConcurrentSession(url)
if err != nil { if err != nil {
return nil, err return nil, err
@ -39,7 +39,8 @@ func NewModel(url, database, collection string, opts ...Option) (*Model, error)
return &Model{ return &Model{
session: session, session: session,
db: session.DB(database), // If name is empty, the database name provided in the dialed URL is used instead
db: session.DB(""),
collection: collection, collection: collection,
opts: opts, opts: opts,
}, nil }, nil

@ -16,8 +16,8 @@ type Model struct {
generateCollection func(*mgo.Session) *cachedCollection generateCollection func(*mgo.Session) *cachedCollection
} }
func MustNewNodeModel(url, database, collection string, rds *redis.Redis, opts ...cache.Option) *Model { func MustNewNodeModel(url, collection string, rds *redis.Redis, opts ...cache.Option) *Model {
model, err := NewNodeModel(url, database, collection, rds, opts...) model, err := NewNodeModel(url, collection, rds, opts...)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -25,8 +25,8 @@ func MustNewNodeModel(url, database, collection string, rds *redis.Redis, opts .
return model return model
} }
func MustNewModel(url, database, collection string, c cache.CacheConf, opts ...cache.Option) *Model { func MustNewModel(url, collection string, c cache.CacheConf, opts ...cache.Option) *Model {
model, err := NewModel(url, database, collection, c, opts...) model, err := NewModel(url, collection, c, opts...)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -34,16 +34,16 @@ func MustNewModel(url, database, collection string, c cache.CacheConf, opts ...c
return model return model
} }
func NewNodeModel(url, database, collection string, rds *redis.Redis, opts ...cache.Option) (*Model, error) { func NewNodeModel(url, collection string, rds *redis.Redis, opts ...cache.Option) (*Model, error) {
c := internal.NewCacheNode(rds, sharedCalls, stats, mgo.ErrNotFound, opts...) c := internal.NewCacheNode(rds, sharedCalls, stats, mgo.ErrNotFound, opts...)
return createModel(url, database, collection, c, func(collection mongo.Collection) *cachedCollection { return createModel(url, collection, c, func(collection mongo.Collection) *cachedCollection {
return newCollection(collection, c) return newCollection(collection, c)
}) })
} }
func NewModel(url, database, collection string, conf cache.CacheConf, opts ...cache.Option) (*Model, error) { func NewModel(url, collection string, conf cache.CacheConf, opts ...cache.Option) (*Model, error) {
c := internal.NewCache(conf, sharedCalls, stats, mgo.ErrNotFound, opts...) c := internal.NewCache(conf, sharedCalls, stats, mgo.ErrNotFound, opts...)
return createModel(url, database, collection, c, func(collection mongo.Collection) *cachedCollection { return createModel(url, collection, c, func(collection mongo.Collection) *cachedCollection {
return newCollection(collection, c) return newCollection(collection, c)
}) })
} }
@ -224,9 +224,9 @@ func (mm *Model) pipe(fn func(c *cachedCollection) mongo.Pipe) (mongo.Pipe, erro
return fn(mm.GetCollection(session)), nil return fn(mm.GetCollection(session)), nil
} }
func createModel(url, database, collection string, c internal.Cache, func createModel(url, collection string, c internal.Cache,
create func(mongo.Collection) *cachedCollection) (*Model, error) { create func(mongo.Collection) *cachedCollection) (*Model, error) {
model, err := mongo.NewModel(url, database, collection) model, err := mongo.NewModel(url, collection)
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -19,7 +19,7 @@ type Roster struct {
} }
func main() { func main() {
model := mongo.MustNewModel("localhost:27017", "blackboard", "roster") model := mongo.MustNewModel("localhost:27017/blackboard", "roster")
for i := 0; i < 1000; i++ { for i := 0; i < 1000; i++ {
session, err := model.TakeSession() session, err := model.TakeSession()
if err != nil { if err != nil {

Loading…
Cancel
Save