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
// 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 #3
0
func (a *Asset) confirmChecksum(coll *mgo.Collection, finished chan int) {
	defer func() { finished <- 1 }()

	fmt.Printf("Checking %s\n", a.Url)

	activeRequests <- 1
	resp, err := http.Get(a.Url)
	<-activeRequests

	if err != nil {
		fmt.Printf("Got HTTP Error for %s.  Error: %v\n", a.Url, err)
		return
	}

	newHasher := md5.New()
	io.Copy(newHasher, resp.Body)
	newHash := hex.EncodeToString(newHasher.Sum(nil))

	if newHash != a.Checksum {
		fmt.Printf("Hashes did not match: %s != %s\n", newHash, a.Checksum)
		a.Checksum = newHash
		err = coll.Update(bson.M{"_id": a.Id}, a)
		if err != nil {
			fmt.Printf("Error updating hash, got error %v\n", err)
		}
	} else {
		fmt.Printf("Hashes Matched!\n")
	}

}
Beispiel #4
0
func AddUser(collection *mgo.Collection, name, username, password string) {
	// Index
	index := mgo.Index{
		Key:        []string{"username", "email"},
		Unique:     true,
		DropDups:   true,
		Background: true,
		Sparse:     true,
	}

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

	bcryptPassword, _ := bcrypt.GenerateFromPassword(
		[]byte(password), bcrypt.DefaultCost)

	// Insert Dataz
	err = collection.Insert(&models.User{Name: name, Username: username, Password: bcryptPassword})

	if err != nil {
		panic(err)
	}
}
Beispiel #5
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 #6
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 #7
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 #8
0
func GetAllRelevantSessions(levelSessionsCollection *mgo.Collection) (topHumanSessions, topOgreSessions []lib.GameSession) {
	teams := [2]string{"humans", "ogres"}
	for _, teamName := range teams {
		queryParameters := bson.M{"level.original": "53558b5a9914f5a90d7ccddb", "submitted": true, "team": teamName}
		selection := bson.M{"team": 1, "totalScore": 1}
		sort := bson.M{"totalScore": -1}
		pipe := levelSessionsCollection.Pipe([]bson.M{{"$match": queryParameters}, {"$project": selection}, {"$sort": sort}, {"$limit": numberOfTopGamesToRank}})
		var err error
		var documentCount int
		if teamName == "humans" {
			err = pipe.All(&topHumanSessions)
			documentCount = len(topHumanSessions)
		} else {
			err = pipe.All(&topOgreSessions)
			documentCount = len(topOgreSessions)
		}

		if err != nil {
			panic(err)
		}
		fmt.Println("Retrieved", documentCount, teamName, "sessions!")
	}

	return topHumanSessions, topOgreSessions
}
Beispiel #9
0
func main() {
	var (
		mongoSession *mgo.Session
		database     *mgo.Database
		collection   *mgo.Collection
		changeInfo   *mgo.ChangeInfo
		err          error
	)

	if mongoSession, err = mgo.Dial("localhost"); err != nil {
		panic(err)
	}

	database = mongoSession.DB("mgo_examples_03")
	collection = database.C("todos")

	// START OMIT
	var todo = Todo{
		Id:      bson.NewObjectId(),
		Task:    "Demo mgo",
		Created: time.Now(),
	}

	// This is a shortcut to collection.Upsert(bson.M{"_id": todo.id}, &todo)
	if changeInfo, err = collection.UpsertId(todo.Id, &todo); err != nil {
		panic(err)
	}
	// END OMIT

	fmt.Printf("Todo: %# v", pretty.Formatter(todo))
	fmt.Printf("Change Info: %# v", pretty.Formatter(changeInfo))

}
Beispiel #10
0
func Count(collection *mgo.Collection, q interface{}) int {
	cnt, err := collection.Find(q).Count()
	if err != nil {
		Err(err)
	}
	return cnt
}
Beispiel #11
0
func (fs *BFS) copyDirectoryDocument(d *Directory, newprefix, oldprefix, newname string, c *mgo.Collection) error {
	// update parent path prefix with new prefix
	_parent_path := d.Header.Parent
	_parent_path = strings.Replace(_parent_path, oldprefix, newprefix, 1)

	// update header info
	uuid, err := makeUUID()
	if err != nil {
		return err
	}

	nw := time.Now()
	dt := formatDatetime(&nw)
	id := fmt.Sprintf("%s:%s", uuid, dt)
	d.Header.Parent = _parent_path
	if newname != "" {
		err = fs.validateDirName(newname)
		if err != nil {
			return err
		}
		d.Header.Name = newname
	}
	d.Header.Created = dt
	d.Id = id
	// save to mongodb
	err = c.Insert(&d)
	if err != nil {
		return err
	}

	return nil
}
Beispiel #12
0
// DeleteIndex deletes a collections index
func DeleteIndex(c *mgo.Collection) {
	defer dendtimer(starttimer())
	err := c.DropIndex("name")
	if err != nil {
		panic(err)
	}
}
Beispiel #13
0
func txPagingFunc(c *mgo.Collection, first, last string, args ...interface{}) (query bson.M, err error) {
	tx := &Tx{}

	if bson.IsObjectIdHex(first) {
		if err := c.FindId(bson.ObjectIdHex(first)).One(tx); err != nil {
			return nil, err
		}
		query = bson.M{
			"time": bson.M{
				"$gte": tx.Time,
			},
		}
	} else if bson.IsObjectIdHex(last) {
		if err := c.FindId(bson.ObjectIdHex(last)).One(tx); err != nil {
			return nil, err
		}
		query = bson.M{
			"time": bson.M{
				"$lte": tx.Time,
			},
		}
	}

	return
}
Beispiel #14
0
func searchPagingFunc(c *mgo.Collection, first, last string, args ...interface{}) (query bson.M, err error) {
	user := &Account{}

	if len(first) > 0 {
		if err := c.FindId(first).One(user); err != nil {
			return nil, err
		}
		query = bson.M{
			"lastlogin": bson.M{
				"$gte": user.LastLogin,
			},
		}
	} else if len(last) > 0 {
		if err := c.FindId(last).One(user); err != nil {
			return nil, err
		}
		query = bson.M{
			"lastlogin": bson.M{
				"$lte": user.LastLogin,
			},
		}
	}

	return
}
Beispiel #15
0
func recordPagingFunc(c *mgo.Collection, first, last string, args ...interface{}) (query bson.M, err error) {
	record := &Record{}

	if bson.IsObjectIdHex(first) {
		if err := c.FindId(bson.ObjectIdHex(first)).One(record); err != nil {
			return nil, err
		}
		query = bson.M{
			"starttime": bson.M{
				"$gte": record.PubTime,
			},
		}
	} else if bson.IsObjectIdHex(last) {
		if err := c.FindId(bson.ObjectIdHex(last)).One(record); err != nil {
			return nil, err
		}
		query = bson.M{
			"starttime": bson.M{
				"$lte": record.PubTime,
			},
		}
	}

	return
}
Beispiel #16
0
func assertSequenceExists(collection *mgo.Collection) {
	c, _ := collection.Find(nil).Count()
	if c == 0 {
		collection.Insert(&Counter{
			Seq: 0})
	}
}
Beispiel #17
0
func (w *entityWatcher) loop(coll *mgo.Collection, key string) (err error) {
	doc := &struct {
		TxnRevno int64 `bson:"txn-revno"`
	}{}
	fields := D{{"txn-revno", 1}}
	if err := coll.FindId(key).Select(fields).One(doc); err == mgo.ErrNotFound {
		doc.TxnRevno = -1
	} else if err != nil {
		return err
	}
	in := make(chan watcher.Change)
	w.st.watcher.Watch(coll.Name, key, doc.TxnRevno, in)
	defer w.st.watcher.Unwatch(coll.Name, key, in)
	out := w.out
	for {
		select {
		case <-w.tomb.Dying():
			return tomb.ErrDying
		case <-w.st.watcher.Dead():
			return watcher.MustErr(w.st.watcher)
		case ch := <-in:
			if _, ok := collect(ch, in, w.tomb.Dying()); !ok {
				return tomb.ErrDying
			}
			out = w.out
		case out <- struct{}{}:
			out = nil
		}
	}
	return nil
}
Beispiel #18
0
func insertFakeAssets(c *mgo.Collection) {
	assets := []Asset{
		{
			SlotID:       1,
			AssetType:    2,
			DivisionCode: "Truzzardi",
			Environment:  "preview",
		},
		{
			SlotID:       1,
			AssetType:    2,
			DivisionCode: "Prada",
			Environment:  "preview",
		},
		{
			SlotID:       1,
			AssetType:    2,
			DivisionCode: "Truzzardi",
			Environment:  "production",
		},
		{
			SlotID:       1,
			AssetType:    2,
			DivisionCode: "VinDiesel",
			Environment:  "preview",
		},
	}
	for _, asset := range assets {
		err := c.Insert(asset)
		if err != nil {
			panic(err)
		}
	}

}
Beispiel #19
0
func articlePagingFunc(c *mgo.Collection, first, last string, args ...interface{}) (query bson.M, err error) {
	article := &Article{}

	if bson.IsObjectIdHex(first) {
		if err := c.FindId(bson.ObjectIdHex(first)).One(article); err != nil {
			return nil, err
		}
		query = bson.M{
			"pub_time": bson.M{
				"$gte": article.PubTime,
			},
		}
	} else if bson.IsObjectIdHex(last) {
		if err := c.FindId(bson.ObjectIdHex(last)).One(article); err != nil {
			return nil, err
		}
		query = bson.M{
			"pub_time": bson.M{
				"$lte": article.PubTime,
			},
		}
	}

	return
}
Beispiel #20
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 #21
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 #22
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 #23
0
// Sets the privileges to a whole new ACL
func (a ACL) Set(c *mgo.Collection, service string, object string, key string, user string, privileges map[string]interface{}) (*mgo.ChangeInfo, error) {
	selector := bson.M{"service": service, "object": object, "key": key, "user": user}
	update := copyMap(selector)
	update["privileges"] = privileges

	log.Finest("Setting Privilege: %s", update)
	return c.Upsert(selector, update)
}
Beispiel #24
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 #25
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 #26
0
// Update Account into Mongo
func update(u Account, s *mgo.Collection) {
	colQuerier := bson.M{"name": u.Name}
	change := bson.M{"$set": bson.M{"email": u.Email, "phone": u.Phone, "password": u.Password, "type": u.Type}}
	err := s.Update(colQuerier, change)
	if err != nil {
		panic(err)
	}
}
Beispiel #27
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 #28
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 #29
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 #30
0
// Revokes the privileges from the existing ACL
func (a ACL) Revoke(c *mgo.Collection, service string, object string, key string, user string, privileges []string) (*mgo.ChangeInfo, error) {
	selector := bson.M{"service": service, "object": object, "key": key, "user": user}
	toRevoke := map[string]interface{}{}
	for _, privilege := range privileges {
		toRevoke["privileges."+privilege] = ""
	}
	log.Finest("Revoking Privilege: %s, %s", selector, toRevoke)
	return c.Upsert(selector, bson.M{"$unset": toRevoke})
}