Beispiel #1
0
// getwords reads the words from the database into a local variable stored on the heap.
// This cuts down on database accesses, and encapsulates the database interactions,
// making the random word selection more portable.
func getwords(localwords []string, words *mgo.Collection) error {
	// Within the words collection, Find documents with the word field, and then Select only
	// the contents of that field, and return an Iterator.
	iter := words.Find(bson.M{wordField: bson.M{"$exists": true}}).Select(bson.M{wordField: 1}).Limit(10000).Iter()

	// Iterate through the results pulling out the strings.
	tempresult := Word{}
	for i := 0; iter.Next(&tempresult); i++ {
		localwords[i] = tempresult.Word
		//Debug: fmt.Print(tempresult.Word, ",")
	}

	/* // Debug
	   //fmt.Println("First: ", localwords[0])
	   //fmt.Println("Last: ", localwords[10000-1])

	   //Debug: fmt.Println(len(localwords))
	*/

	// We should check len(localwords) to catch errors
	if len(localwords) < 1000 {
		// some error - we need to close the iterator also.
		// composite errors may occur.
	}
	// Close the iterator, return an error where applicable.
	if err := iter.Close(); err != nil {
		return err
	}
	return nil
}
Beispiel #2
0
func GetNumberKnown(user_id bson.ObjectId, c *mgo.Collection) int {
	n, err := c.Find(bson.M{"user_id": user_id, "level": 9}).Count()
	if err != nil {
		return 0
	}
	return n
}
Beispiel #3
0
func idempotentRecordChapter(bid interface{}, chapter Chapter, col *mgo.Collection) interface{} {
	them := col.Find(bson.M{
		"position": chapter.Position,
		"book":     bid,
	}).Limit(1)
	cpt, e := them.Count()
	if e != nil {
		panic(e)
	}
	if cpt > 0 {
		ans := make(map[string]interface{})
		e = them.One(ans)
		if e != nil {
			panic(e)
		}
		return ans["_id"]
	}
	e = col.Insert(bson.M{
		"position": chapter.Position,
		"book":     bid,
	})
	if e != nil {
		panic(e)
	}
	return idempotentRecordChapter(bid, chapter, col)
}
Beispiel #4
0
// 查询某些字段, q是查询条件, fields是字段名列表
func ListByQWithFields(collection *mgo.Collection, q bson.M, fields []string, i interface{}) {
	selector := make(bson.M, len(fields))
	for _, field := range fields {
		selector[field] = true
	}
	collection.Find(q).Select(selector).All(i)
}
Beispiel #5
0
func Count(collection *mgo.Collection, q interface{}) int {
	cnt, err := collection.Find(q).Count()
	if err != nil {
		Err(err)
	}
	return cnt
}
Beispiel #6
0
func addPost(database *mgo.Database, collection *mgo.Collection, title, subtitle, slug, category, body, image string) (post models.Post) {
	// Index
	index := mgo.Index{
		Key:        []string{"shortid", "timestamp", "title", "tags"},
		Unique:     true,
		DropDups:   true,
		Background: true,
		Sparse:     true,
	}

	err := collection.EnsureIndex(index)
	if err != nil {
		panic(err)
	}

	// Insert Dataz
	err = collection.Insert(&models.Post{
		ShortID:   models.GetNextSequence(database),
		Title:     title,
		Category:  category,
		Slug:      slug,
		Subtitle:  subtitle,
		Body:      body,
		Timestamp: time.Now(),
		Published: false})

	if err != nil {
		panic(err)
	}

	result := models.Post{}
	collection.Find(bson.M{"title": title}).One(&result)
	return result
}
Beispiel #7
0
func FindKotoba(user_id bson.ObjectId, search string, c *mgo.Collection) *[]MongoKotoba {
	kotoba := []MongoKotoba{}
	c.Find(bson.M{"user_id": user_id, "$or": []interface{}{bson.M{"goi": bson.M{"$regex": ".*" + search + ".*"}},
		bson.M{"imis.imi": bson.M{"$regex": ".*" + search + ".*"}},
		bson.M{"hatsuons.hatsuon": bson.M{"$regex": ".*" + search + ".*"}}}}).All(&kotoba)
	return &kotoba
}
Beispiel #8
0
func assertSequenceExists(collection *mgo.Collection) {
	c, _ := collection.Find(nil).Count()
	if c == 0 {
		collection.Insert(&Counter{
			Seq: 0})
	}
}
Beispiel #9
0
func idempotentRecordBook(vid interface{}, book Book, col *mgo.Collection) interface{} {
	them := col.Find(bson.M{
		"name":    book.Name,
		"version": vid,
	}).Limit(1)
	cpt, e := them.Count()
	if e != nil {
		panic(e)
	}
	if cpt > 0 {
		ans := make(map[string]interface{})
		e = them.One(ans)
		if e != nil {
			panic(e)
		}
		return ans["_id"]
	}
	e = col.Insert(bson.M{
		"name":     book.Name,
		"position": book.Position,
		"version":  vid,
	})
	if e != nil {
		panic(e)
	}
	return idempotentRecordBook(vid, book, col)
}
Beispiel #10
0
func checkIdConflict(w http.ResponseWriter, statementsC *mgo.Collection, statement Statement) int {

	if statement.Id != "" {
		// find any statements with this id
		var result Statement
		err := statementsC.Find(bson.M{"id": statement.Id}).One(&result)
		if err != nil {
			//fmt.Fprint(w, err)
			return 0
		}

		// kludge the result has "stored" so add field for comparison
		statement.Stored = result.Stored

		// can't compare structs with arrays/maps
		rj, _ := json.Marshal(result)
		sj, _ := json.Marshal(statement)

		if string(rj) != string(sj) {
			return http.StatusConflict
		} else {
			//same no need to insert
			return http.StatusNoContent
		}
	}
	return 0
}
Beispiel #11
0
// loadApplications is a helper function which loads applications from the
// passed mongo collection and registers them as backends with the passed proxy
// mux.
func loadApplications(c *mgo.Collection, mux *triemux.Mux) (apps map[string]http.Handler) {
	app := &Application{}
	apps = make(map[string]http.Handler)

	iter := c.Find(nil).Iter()

	for iter.Next(&app) {
		backendUrl, err := url.Parse(app.BackendURL)
		if err != nil {
			log.Printf("router: couldn't parse URL %s for backend %s "+
				"(error: %v), skipping!", app.BackendURL, app.ApplicationId, err)
			continue
		}

		proxy := httputil.NewSingleHostReverseProxy(backendUrl)
		// Allow the proxy to keep more than the default (2) keepalive connections
		// per upstream.
		proxy.Transport = &http.Transport{MaxIdleConnsPerHost: 20}

		apps[app.ApplicationId] = proxy
	}

	if err := iter.Err(); err != nil {
		panic(err)
	}

	return
}
Beispiel #12
0
// Retrieve Account into Mongo
func (a Account) retrieve(name string, s *mgo.Collection) Account {
	result := Account{}
	err := s.Find(bson.M{"name": name}).One(&result)
	if err != nil {
		panic(err)
	}
	return result
}
Beispiel #13
0
func GetNumberReviewsNow(user_id bson.ObjectId, c *mgo.Collection) int {
	n, err := c.Find(bson.M{"user_id": user_id,
		"review": bson.M{"$lte": time.Now()}}).Count()
	if err != nil {
		return 0
	}
	return n
}
Beispiel #14
0
// GetTweetsVerified obtains all the Tweets with Verified User from a collection
func GetTweetsVerified(colec mgo.Collection) []twittertypes.Tweet {
	var tweetsVerified []twittertypes.Tweet
	err := colec.Find(bson.M{"user.verified": true}).All(&tweetsVerified)
	if err != nil {
		panic(err)
	}
	return tweetsVerified
}
Beispiel #15
0
func GetNumberMaster(user_id bson.ObjectId, c *mgo.Collection) int {
	n, err := c.Find(bson.M{"user_id": user_id, "$or": []interface{}{bson.M{"level": 7},
		bson.M{"level": 8}}}).Count()
	if err != nil {
		return 0
	}
	return n
}
Beispiel #16
0
// GetTweetsRange gets from the collection the tweets în the time period determined by [start, end]
func GetTweetsRange(colec mgo.Collection, start, end time.Time) []twittertypes.Tweet {
	var tweetsDate []twittertypes.Tweet
	err := colec.Find(bson.M{"epoch": bson.M{"$gte": start.Unix(), "$lt": end.Unix()}}).All(&tweetsDate)
	if err != nil {
		panic(err)
	}
	return tweetsDate
}
Beispiel #17
0
func GetAllRelevantSessions(scoringTaskCollection *mgo.Collection) []Simulator {
	var calculators []Simulator
	err := scoringTaskCollection.Find(bson.M{}).Distinct("calculator", &calculators)
	if err != nil {
		panic(err)
	}
	fmt.Println(calculators)
	return calculators
}
Beispiel #18
0
// GetTweetsAll gets all the tweets from the collection
func GetTweetsAll(colec mgo.Collection) []twittertypes.Tweet {
	var tweets []twittertypes.Tweet
	// Lectura de los tweets de la BD
	err := colec.Find(bson.M{"text": bson.M{"$exists": true}}).All(&tweets)
	if err != nil {
		panic(err)
	}
	return tweets
}
Beispiel #19
0
func GetNumberReviewsDay(user_id bson.ObjectId, c *mgo.Collection) int {
	hours, _ := time.ParseDuration("24h")
	t := time.Now().Local().Add(hours)
	n, err := c.Find(bson.M{"user_id": user_id,
		"review": bson.M{"$lte": t}}).Count()
	if err != nil {
		return 0
	}
	return n
}
Beispiel #20
0
// Retrieves the ACL from the collection using the object's key and the user, if the user is granted privileges
func (a ACL) Has(c *mgo.Collection, service string, object string, key string, user string, privileges []string) error {
	selector := bson.M{"service": service, "object": object, "key": key, "user": user}
	for _, privilege := range privileges {
		selector["privileges."+privilege] = "allow"
	}

	log.Finest("Checking user has privilege: %s", selector)
	result := ACL{}
	return c.Find(selector).One(&result)
}
Beispiel #21
0
// findone calls an mgo Find for one doc
func findone(c *mgo.Collection) error {
	result := Mongodoc{}
	defer foendtimer(starttimer())
	err := c.Find(bson.M{"name": "CalvinandHobbes"}).One(&result)
	if err != nil {
		fmt.Println("not found")
		panic(err)
	}
	return err
}
Beispiel #22
0
func AddAutoIncrementingField(c *mgo.Collection) {
	i, e := c.Find(bson.M{"_id": "users"}).Count()
	if e != nil {
		panic(fmt.Sprintf("Could not Add Auto Incrementing Field! collection:%s err:%v\n", c.FullName, e))
	}
	if i > 0 {
		return
	}
	c.Insert(bson.M{"_id": "users", "seq": uint32(0)})
}
Beispiel #23
0
func Read(col *mgo.Collection, key int) Sample {
	// result holder
	iter := col.Find(bson.M{keyfield: key}).Select(bson.M{keyfield: 1, namefield: 1}).Iter()

	result := Sample{}
	iter.Next(&result)
	iter.Close()

	return result
}
Beispiel #24
0
func (fs *BFS) existsDocument(p string, c *mgo.Collection) (SimpleResultItem, bool) {
	q := fs.findPathQuery(p)
	var ri SimpleResultItem
	err := c.Find(q).One(&ri)
	if err != nil {
		// log error
		return ri, false
	}
	return ri, true
}
Beispiel #25
0
func GetAllRelevantSessions(levelSessionsCollection *mgo.Collection) []lib.GameSession {
	var gameSessions []lib.GameSession
	queryParameters := bson.M{"level.original": "53558b5a9914f5a90d7ccddb", "submitted": true}
	selection := bson.M{"team": 1, "creator": 1}
	err := levelSessionsCollection.Find(queryParameters).Select(selection).All(&gameSessions)
	if err != nil {
		panic(err)
	}
	fmt.Println("Retrieved", len(gameSessions), "sessions to verify!")
	return gameSessions
}
Beispiel #26
0
// Retrieves a list of the keys for a service/object/user combo that the user has "allow" privileges for
func (a ACL) Match(c *mgo.Collection, service string, object string, user string, privileges []string) ([]string, error) {
	selector := bson.M{"service": service, "object": object, "user": user}
	for _, privilege := range privileges {
		selector["privileges."+privilege] = "allow"
	}

	log.Finest("Matching user privileges to keys: %s", selector)
	result := []string{}
	err := c.Find(selector).Distinct("key", &result)
	return result, err
}
Beispiel #27
0
func GetRandomKotoba(user_id bson.ObjectId, c *mgo.Collection) *MongoKotoba {
	kotoba := MongoKotoba{}
	query := c.Find(bson.M{"user_id": user_id, "review": bson.M{"$lte": time.Now()}})
	n, _ := query.Count()
	if n > 0 {
		query.Skip(rand.Intn(n)).One(&kotoba)
		fmt.Println(kotoba)
		return &kotoba
	} else {
		return nil
	}
}
Beispiel #28
0
func ReviewUpdateKotoba(id string, review bool, c *mgo.Collection) error {
	_id := bson.ObjectIdHex(id)
	kotoba := MongoKotoba{}
	c.Find(bson.M{"_id": _id}).One(&kotoba)
	if review {
		kotoba.IncLevel()
	} else {
		kotoba.DecLevel()
	}
	fmt.Println(kotoba)
	return c.UpdateId(_id, kotoba)
}
Beispiel #29
0
func queryToMgoQuery(c *mgo.Collection, q Query) *mgo.Query {
	query := c.Find(q.Conditions)
	if q.Order != nil {
		query = query.Sort(q.Order...)
	}
	if q.Offset != nil {
		query = query.Skip(*q.Offset)
	}
	if q.Limit != nil {
		query = query.Limit(*q.Limit)
	}
	return query
}
Beispiel #30
0
func (m MongoModel) query(c *mgo.Collection) *mgo.Query {
	q := c.Find(m.F).Skip(m.Start)
	if m.Rows > 0 {
		q = q.Limit(m.Rows)
	}
	if len(m.S) > 0 {
		q = q.Sort(m.S)
	}
	if m.Select != nil {
		q = q.Select(m.Select)
	}
	return q
}