Răsfoiți Sursa

use default mongo db (#103)

bittoy 4 ani în urmă
părinte
comite
0a36031d48
3 a modificat fișierele cu 17 adăugiri și 16 ștergeri
  1. 6 5
      core/stores/mongo/model.go
  2. 10 10
      core/stores/mongoc/cachedmodel.go
  3. 1 1
      example/mongo/time.go

+ 6 - 5
core/stores/mongo/model.go

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

+ 10 - 10
core/stores/mongoc/cachedmodel.go

@@ -16,8 +16,8 @@ type Model struct {
 	generateCollection func(*mgo.Session) *cachedCollection
 }
 
-func MustNewNodeModel(url, database, collection string, rds *redis.Redis, opts ...cache.Option) *Model {
-	model, err := NewNodeModel(url, database, collection, rds, opts...)
+func MustNewNodeModel(url, collection string, rds *redis.Redis, opts ...cache.Option) *Model {
+	model, err := NewNodeModel(url, collection, rds, opts...)
 	if err != nil {
 		log.Fatal(err)
 	}
@@ -25,8 +25,8 @@ func MustNewNodeModel(url, database, collection string, rds *redis.Redis, opts .
 	return model
 }
 
-func MustNewModel(url, database, collection string, c cache.CacheConf, opts ...cache.Option) *Model {
-	model, err := NewModel(url, database, collection, c, opts...)
+func MustNewModel(url, collection string, c cache.CacheConf, opts ...cache.Option) *Model {
+	model, err := NewModel(url, collection, c, opts...)
 	if err != nil {
 		log.Fatal(err)
 	}
@@ -34,16 +34,16 @@ func MustNewModel(url, database, collection string, c cache.CacheConf, opts ...c
 	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...)
-	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)
 	})
 }
 
-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...)
-	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)
 	})
 }
@@ -224,9 +224,9 @@ func (mm *Model) pipe(fn func(c *cachedCollection) mongo.Pipe) (mongo.Pipe, erro
 	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) {
-	model, err := mongo.NewModel(url, database, collection)
+	model, err := mongo.NewModel(url, collection)
 	if err != nil {
 		return nil, err
 	}

+ 1 - 1
example/mongo/time.go

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