func (s *S) TestPrefetching(c *C) { session, err := mgo.Mongo("localhost:40001") c.Assert(err, IsNil) defer session.Close() coll := session.DB("mydb").C("mycoll") docs := make([]interface{}, 200) for i := 0; i != 200; i++ { docs[i] = M{"n": i} } coll.Insert(docs...) // Same test three times. Once with prefetching via query, then with the // default prefetching, and a third time tweaking the default settings in // the session. for testi := 0; testi != 3; testi++ { mgo.ResetStats() var iter *mgo.Iter var nextn int switch testi { case 0: // First, using query methods. iter, err = coll.Find(M{}).Prefetch(0.27).Batch(100).Iter() c.Assert(err, IsNil) nextn = 73 case 1: // Then, the default session value. session.SetBatch(100) iter, err = coll.Find(M{}).Iter() c.Assert(err, IsNil) nextn = 75 case 2: // Then, tweaking the session value. session.SetBatch(100) session.SetPrefetch(0.27) iter, err = coll.Find(M{}).Iter() c.Assert(err, IsNil) nextn = 73 } result := struct{ N int }{} for i := 0; i != nextn; i++ { iter.Next(&result) } stats := mgo.GetStats() c.Assert(stats.ReceivedDocs, Equals, 100) iter.Next(&result) // Ping the database just to wait for the fetch above // to get delivered. session.Run("ping", M{}) // XXX Should support nil here. stats = mgo.GetStats() c.Assert(stats.ReceivedDocs, Equals, 201) // 200 + the ping result } }
func (t *Task) returnArray(it *mgo.Iter, proj *entity.Project) (ret []*entity.Task, err error) { var dt *data.Task ret = make([]*entity.Task, 0) dt = &data.Task{} for it.Next(&dt) { ret = append(ret, &entity.Task{dt.Id, dt.Title, dt.Description, dt.Done, proj}) } // in case of any error while fetching the results // set this to the return err = it.Err() return }