Exemple #1
0
func (db *MongoDB) SetDebug(on bool) {
	mgo.SetDebug(on)
	if on {
		mgo.SetLogger(log.New(os.Stderr, "[db] ", log.LstdFlags))
	} else {
		mgo.SetLogger(nil)
	}
}
Exemple #2
0
func (s *S) SetUpTest(c *C) {
	err := run("mongo --nodb testdb/dropall.js")
	if err != nil {
		panic(err.Error())
	}
	mgo.SetLogger((*cLogger)(c))
	mgo.ResetStats()
}
Exemple #3
0
func (s *MgoSuite) SetUpTest(c *C) {
	err := DropAll("localhost:50017")
	c.Assert(err, IsNil)
	mgo.SetLogger(c)
	mgo.ResetStats()
	s.Addr = "127.0.0.1:50017"
	s.Session, err = mgo.Dial(s.Addr)
	c.Assert(err, IsNil)
}
Exemple #4
0
func (s *MgoSuite) SetUpTest(c *C) {
	err := DropAll(mgoaddr)
	if err != nil {
		panic(err)
	}
	mgo.SetLogger(c)
	mgo.ResetStats()

	s.session, err = mgo.Dial(mgoaddr)
	c.Assert(err, IsNil)
}
Exemple #5
0
func main() {
	logout := log.New(os.Stdout, "MGO: ", log.Lshortfile)
	mgo.SetLogger(logout)
	mgo.SetDebug(false)
	session, err := mgo.Dial("localhost")
	if err != nil {
		panic(err)
	}
	defer session.Close()
	db := session.DB("bookdb")
	cbooks := db.C("bookcoll")
	cauthors := db.C("authorcoll")

	aids := []bson.ObjectId{bson.NewObjectId(), bson.NewObjectId()}
	authors := []Author{{
		aids[0],
		"Author 1",
	}, {
		aids[1],
		"Author 2",
	}}
	cauthors.Insert(authors[0])
	cauthors.Insert(authors[1])
	// Insert some books
	mine := Book{
		bson.NewObjectId(),
		"Gang of four thingy",
		aids,
	}
	cbooks.Insert(&mine)

	var assembl []AssembledBooks
	cauthors.Find(bson.M{}).All(&assembl)
	str1, _ := json.MarshalIndent(assembl, "", " ")
	fmt.Printf("%s\n", str1)

	var allauthors []Author
	cauthors.Find(bson.M{}).All(&allauthors)
	str, _ := json.MarshalIndent(allauthors, "", " ")
	fmt.Printf("%s\n", str)
	var allbooks []Book
	cbooks.Find(bson.M{}).All(&allbooks)
	str, _ = json.MarshalIndent(allbooks, "", " ")
	fmt.Printf("%s\n", str)

	fmt.Println("Dropping all collections...")
	cauthors.DropCollection()
	cbooks.DropCollection()
	fmt.Println("Done")
}
Exemple #6
0
func main() {
	logout := log.New(os.Stdout, "MGO: ", log.Lshortfile)
	mgo.SetLogger(logout)
	mgo.SetDebug(false)
	session, err := mgo.Dial("localhost")
	if err != nil {
		panic(err)
	}
	defer session.Close()
	db := session.DB("bookdb")
	coll := db.C("bookcoll")

	// Insert some books
	mine := Book{
		bson.NewObjectId(),
		"I never wrote a book",
		bson.Now(),
	}
	coll.Insert(&mine)
	hpott := Book{
		bson.NewObjectId(),
		"Harry Potter",
		time.Now(),
	}
	coll.Insert(&hpott)

	var alldocs Books
	coll.Find(bson.M{}).All(&alldocs)
	str, _ := json.MarshalIndent(alldocs, "", " ")
	fmt.Printf("%s\n", str)

	c, err := coll.Count()
	fmt.Printf("Total documents: %d\n", c)

	fmt.Println("Dropping entire collection...")
	coll.DropCollection()
	fmt.Println("Done")
}
Exemple #7
0
func (this *FunServantImpl) createServants() {
	log.Info("creating servants...")

	// proxy can dynamically auto discover peers
	if this.conf.Proxy.Enabled() {
		log.Debug("creating servant: proxy")
		this.proxy = proxy.New(this.conf.Proxy)
	} else {
		panic("peers proxy required")
	}

	log.Debug("creating servant: idgen")
	var err error
	this.idgen, err = idgen.NewIdGenerator(this.conf.IdgenWorkerId)
	if err != nil {
		panic(err)
	}

	if this.conf.Lcache.Enabled() {
		log.Debug("creating servant: lcache")
		this.lc = cache.NewLruCache(this.conf.Lcache.MaxItems)
		this.lc.OnEvicted = this.onLcLruEvicted
	}

	if this.conf.Memcache.Enabled() {
		log.Debug("creating servant: memcache")
		this.mc = memcache.New(this.conf.Memcache)
	}

	if this.conf.Redis.Enabled() {
		log.Debug("creating servant: redis")
		this.rd = redis.New(this.conf.Redis)
	}

	if this.conf.Lock.Enabled() {
		log.Debug("creating servant: lock")
		this.lk = lock.New(this.conf.Lock)
	}

	if this.conf.Mysql.Enabled() {
		log.Debug("creating servant: mysql")
		this.my = mysql.New(this.conf.Mysql)

		switch this.conf.Mysql.CacheStore {
		case "mem":
			this.dbCacheStore = store.NewMemStore(this.conf.Mysql.CacheStoreMemMaxItems)

		case "redis":
			this.dbCacheStore = store.NewRedisStore(this.conf.Mysql.CacheStoreRedisPool,
				this.conf.Redis)

		default:
			panic("unknown mysql cache store")
		}
	}

	if this.conf.Mongodb.Enabled() {
		log.Debug("creating servant: mongodb")
		this.mg = mongo.New(this.conf.Mongodb)
		if this.conf.Mongodb.DebugProtocol ||
			this.conf.Mongodb.DebugHeartbeat {
			mgo.SetLogger(&mongoProtocolLogger{})
			mgo.SetDebug(this.conf.Mongodb.DebugProtocol)
		}
	}

	if this.conf.Couchbase.Enabled() {
		log.Debug("creating servant: couchbase")

		var err error
		// pool is always 'default'
		this.cb, err = couch.New(this.conf.Couchbase.Servers, "default")
		if err != nil {
			log.Error("couchbase: %s", err)
		}
	}

	log.Info("servants created")
}