Exemple #1
0
func TopUpPost(postId string) {
	if dbcon == nil {
		Error("Database not connected.\n")
		return
	}
	RmPostStream(postId)

	post, err := GetPost(postId)
	if err != nil {
		Error("TopUpPost: post doesn't exists for id `%v`\n", postId)
		return
	}

	Info2("TopUpPost: got post with id `%v`\n", strid(post.Id_))

	qfind, err := mongo.Marshal(oidSearch{"_id": mongo.ObjectId{postId}}, atreps)
	if err != nil {
		Error("TopUpPost: Cannot marshal. %s\n", err)
		return
	}

	doc, err := mongo.Marshal(map[string]map[string]bool{"$set": {"_popular_post": true}}, atreps)
	if err != nil {
		Error("TopUpPost: Cannot marshal. %s\n", err)
		return
	}

	err = ColPost.Update(qfind, doc)
	if err != nil {
		Error("TopUpPost: Cannot update.\n")
		return
	}

	BroadcastAll(postId)
}
Exemple #2
0
func TestOtherStuff(t *testing.T) {
	doc, _ := mongo.Marshal(map[string]string{"_id": "doc1", "title": "A Mongo document", "content": "Testing, 1. 2. 3."})
	conn, _ := mongo.Connect("127.0.0.1", 27017)
	collection := conn.GetDB("test").GetCollection("test_collection")
	collection.Insert(doc)

	query, _ := mongo.Marshal(map[string]string{"_id": "doc1"})
	got, _ := collection.FindOne(query)
	assertTrue(mongo.Equal(doc, got), "equal", t)

}
Exemple #3
0
func InsertPostStream(userId string, postId string) {
	if dbcon == nil {
		Error("Database not connected.\n")
		return
	}
	Info2("Inserting Post stream with id `%v`, writer id: `%v`\n", postId, userId)

	if len(userId) == 0 || len(postId) == 0 {
		return
	}
	if PostStreamExists(userId, postId) {
		Warn("InsertPostStream: Cannot insert for userId: %v, postId: %v. Already exists.\n", userId, postId)
		return
	}

	doc, err := mongo.Marshal(&PostStream{
		Metaname_: "NdayakStream",
		UserId:    userId,
		PostId:    postId,
		Timepos:   time.Nanoseconds() / 1e6,
	}, atreps)
	if err != nil {
		Error("Cannot insertPostStream for userId: %v, postId: %v. %v\n", userId, postId, err)
		return
	}
	ColStream.Insert(doc)
}
Exemple #4
0
func GetOrigin(originId string) (doc mongo.BSON, err os.Error) {
	if dbcon == nil {
		Error("Database not connected.\n")
		return
	}
	qfind, err := mongo.Marshal(OidSearch{"_id": mongo.ObjectId{originId}}, atreps)
	if err != nil {
		err = os.NewError("Cannot marshal")
		return
	}

	doc, err = ColUser.FindOne(qfind)
	if err != nil {
		doc, err = ColChan.FindOne(qfind)
		if err != nil {
			doc, err = ColTun.FindOne(qfind)
			if err != nil {
				err = os.NewError(fmt.Sprintf("Cannot find origin for id `%s`", originId))
				return
			}
		}
	}

	return doc, nil
}
Exemple #5
0
func TestStuff(t *testing.T) {
	obj, err := mongo.BytesToBSON([]byte{92, 0, 0, 0, 1, 115, 101, 99, 111, 110, 100, 0, 0, 0, 0, 0, 0, 0, 0, 64, 3, 102, 105, 102, 116, 104, 0, 23, 0, 0, 0, 2, 118, 0, 2, 0, 0, 0, 101, 0, 2, 102, 0, 2, 0, 0, 0, 105, 0, 0, 3, 102, 111, 117, 114, 116, 104, 0, 5, 0, 0, 0, 0, 2, 116, 104, 105, 114, 100, 0, 6, 0, 0, 0, 116, 104, 114, 101, 101, 0, 16, 102, 105, 114, 115, 116, 0, 1, 0, 0, 0, 0})
	assertTrue(err == nil, "failed parsing BSON obj", t)

	conn, err := mongo.Connect("127.0.0.1", 27017)
	assertTrue(err == nil && conn != nil, fmt.Sprintf("failed connecting to mongo: %v", err), t)

	db := conn.GetDB("go_driver_tests")
	coll := db.GetCollection("coll")
	coll.Drop()

	coll.Insert(obj)

	q, _ := mongo.Marshal(map[string]string{})
	ret, err := coll.FindAll(q)
	assertTrue(err == nil && ret != nil, "query succeeded", t)

	doc, _ := ret.GetNext()
	assertTrue(doc.Kind() == mongo.ObjectKind, "query returned document", t)
	assertTrue(doc.Get("first").Int() == 1, "returned doc has proper 'first' element", t)
	assertTrue(doc.Get("second").Number() == 2, "returned doc has proper 'second' element", t)
	assertTrue(doc.Get("third").String() == "three", "returned doc has proper 'third' element", t)
	assertTrue(doc.Get("fourth").Kind() == mongo.ObjectKind, "returned doc has proper 'fourth' element", t)
	assertTrue(doc.Get("fifth").Get("f").String() == "i" && doc.Get("fifth").Get("v").String() == "e", "returned doc has proper 'fifth' element", t)

	count, err := coll.Count(q)
	assertTrue(count == 1, "count", t)

	newDoc, _ := mongo.Marshal(map[string]string{"first": "one", "second": "two", "third": "three"})
	coll.Update(q, newDoc)
	doc, _ = coll.FindOne(q)
	assertTrue(doc.Get("first").String() == "one", "update", t)

	rem, _ := mongo.Marshal(map[string]string{"third": "three"})
	coll.Remove(rem)
	doc, err = coll.FindOne(rem)
	assertTrue(err != nil, "remove", t)

	coll.Drop()

	statusCmd, _ := mongo.Marshal(map[string]float64{"serverStatus": 1})
	status, _ := db.Command(statusCmd)
	assertTrue(status.Get("uptime").Number() != 0, "valid serverStatus", t)

	db.Drop()
}
Exemple #6
0
func findOne(coll *mongo.Collection, t *testing.T) {
	ss := &smallStruct{PER_TRIAL / 2}
	obj, err := mongo.Marshal(ss)
	if err != nil {
		t.Errorf("findOne Marshal: %v\n", err)
	}

	for i := 0; i < PER_TRIAL; i++ {
		_, err = coll.FindOne(obj)
		if err != nil {
			t.Errorf("findOne FindOne: %v\n", err)
		}
	}
}
Exemple #7
0
func TestMarshal(t *testing.T) {
	var es1 ExampleStruct
	mongo.Unmarshal(b, &es1)
	bs1, _ := mongo.Marshal(&es1)
	bs2, _ := mongo.BytesToBSON(b)
	assertTrue(mongo.Equal(bs1, bs2), "unmarshal->marshal", t)

	m := map[string]string{"f": "i", "v": "e"}
	bs3, _ := mongo.Marshal(&m)
	assertTrue(mongo.Equal(bs3, bs2.Get("fifth")), "marshal map", t)

	arr, _ := mongo.Marshal([]int{1, 2, 3})
	assertTrue(arr.Elem(0).Long() == 1, "array marshal (0)", t)
	assertTrue(arr.Elem(1).Long() == 2, "array marshal (1)", t)
	assertTrue(arr.Elem(2).Long() == 3, "array marshal (2)", t)

	d := time.UTC()
	es2 := &ExampleStruct2{d}
	bs2, _ = mongo.Marshal(es2)
	assertTrue(bs2.Get("date").Date().Seconds() == d.Seconds(), "date marshal", t)
	es2 = new(ExampleStruct2)
	mongo.Unmarshal(bs2.Bytes(), es2)
	assertTrue(es2.Date.Seconds() == d.Seconds(), "date unmarshal", t)
}
Exemple #8
0
func singleInsertMedium(coll *mongo.Collection, t *testing.T) {
	ms := &mediumStruct{5, 5.05, false, []string{"test", "benchmark"}, 0}
	for i := 0; i < PER_TRIAL; i++ {
		ms.X = i
		obj, err := mongo.Marshal(ms)
		if err != nil {
			t.Errorf("singleInsertMedium Marshal: %v\n", err)
		}

		err = coll.Insert(obj)
		if err != nil {
			t.Errorf("singleInsertMedium Insert: %v\n", err)
		}
	}
}
Exemple #9
0
func singleInsertSmall(coll *mongo.Collection, t *testing.T) {
	ss := &smallStruct{0}
	for i := 0; i < PER_TRIAL; i++ {
		ss.X = i
		obj, err := mongo.Marshal(ss)
		if err != nil {
			t.Errorf("singleInsertSmall Marshal: %v\n", err)
		}

		err = coll.Insert(obj)
		if err != nil {
			t.Errorf("singleInsertSmall Insert: %v\n", err)
		}
	}
}
Exemple #10
0
func RmPostStream(postId string) {
	if dbcon == nil {
		Error("Database not connected.\n")
		return
	}
	qfind, err := mongo.Marshal(map[string]string{"postid": postId}, atreps)
	if err != nil {
		err = os.NewError("Cannot marshal")
		return
	}

	err = ColStream.Remove(qfind)
	if err != nil {
		Error("rmPost: Cannot remove post with id `%s`\n", postId)
	}
}
Exemple #11
0
func PostStreamExists(userId string, postId string) bool {
	if dbcon == nil {
		Error("Database not connected.\n")
		return false
	}
	if len(userId) == 24 && len(postId) == 24 {
		qfind, err := mongo.Marshal(map[string]string{"userid": userId, "postid": postId}, atreps)
		if err != nil {
			Error("Cannot marshal. %s\n", err)
			return false
		}
		doc, err := ColStream.FindOne(qfind)
		if err == nil || doc != nil {
			return true
		}
	}
	return false
}
Exemple #12
0
func GetPost(postId string) (user *UserPost, err os.Error) {
	if dbcon == nil {
		Error("Database not connected.\n")
		return
	}
	qfind, err := mongo.Marshal(OidSearch{"_id": mongo.ObjectId{postId}}, atreps)
	if err != nil {
		return nil, os.NewError(fmt.Sprintf("getUser: Cannot marshal. %s", err))
	}

	doc, err := ColPost.FindOne(qfind)
	if err != nil {
		return nil, os.NewError(fmt.Sprintf("getUser: Cannot find user by id `%s`. %s.", postId, err))
	}

	var post *UserPost = new(UserPost)

	mongo.Unmarshal(doc.Bytes(), post, atreps)

	return post, nil
}
Exemple #13
0
func GetUserSettings(userId string) (user_settings *UserSettings, err os.Error) {
	if dbcon == nil {
		Error("Database not connected.\n")
		return
	}
	qfind, err := mongo.Marshal(map[string]string{"_user_id": userId}, atreps)
	if err != nil {
		return nil, os.NewError(fmt.Sprintf("getUserSettings: Cannot marshal. %s", err))
	}

	doc, err := ColUserSettings.FindOne(qfind)
	if err != nil {
		return nil, os.NewError(fmt.Sprintf("getUserSettings: Cannot find user settings by user id `%s`. %s.", userId, err))
	}

	user_settings = new(UserSettings)

	mongo.Unmarshal(doc.Bytes(), user_settings, atreps)

	return user_settings, nil
}
Exemple #14
0
func singleInsertLarge(coll *mongo.Collection, t *testing.T) {
	base_words := []string{"10gen", "web", "open", "source", "application", "paas",
		"platform-as-a-service", "technology", "helps",
		"developers", "focus", "building", "mongodb", "mongo",
	}

	words := make([]string, 280)
	for i := 0; i < 20; i++ {
		for k, word := range base_words {
			words[i+k] = word
		}
	}

	ls := &largeStruct{"http://www.example.com/test-me",
		6743, time.UTC(),
		map[string]string{"description": "i am a long description string",
			"author":                       "Holly Man",
			"dynamically_created_meta_tag": "who know\n what",
		},
		map[string]int{"counted_tags": 3450,
			"no_of_js_attached": 10,
			"no_of_images":      6,
		},
		words, 0,
	}

	for i := 0; i < PER_TRIAL; i++ {
		ls.X = i
		obj, err := mongo.Marshal(ls)
		if err != nil {
			t.Errorf("singleInsertLarge Marshal: %v", err)
		}

		err = coll.Insert(obj)
		if err != nil {
			t.Errorf("singleInsertLarge Insert: %v", err)
		}
	}
}
Exemple #15
0
func ProcessPost(post_id string) {

	if dbcon == nil {
		Error("Database not connected.\n")
		return
	}

	// get post

	qfind, err := mongo.Marshal(oidSearch{"_id": mongo.ObjectId{post_id}}, atreps)
	if err != nil {
		Error("Cannot marshal. %s\n", err)
		return
	}

	doc, err := ColPost.FindOne(qfind)
	if err != nil {
		Warn("Cannot find post by id `%s`. %s.\n", post_id, err)
		return
	}

	var post UserPost

	mongo.Unmarshal(doc.Bytes(), &post, atreps)

	Info2("Got post id: %v, writer: %s, origin: %s\n", strid(post.Id_), post.WriterId, post.Origin_id_)

	// get writer
	writer, err := GetUser(post.WriterId)
	if err != nil {
		Warn("Cannot get writer with id `%s` for post id `%s`. err: %v\n", post.WriterId, strid(post.Id_), err)
		return
	}

	//fmt.Printf("writer: %v\n", writer.Name)
	InsertPostStream(strid(writer.Id_), post_id)

	var writer_id string = strid(writer.Id_)
	//fmt.Printf("writer_id: %s\n", writer_id)

	// get all followers
	qfind, err = mongo.Marshal(map[string]string{"_followed_user_ids": writer_id}, atreps)
	if err != nil {
		Error("Cannot marshal. %s\n", err)
		return
	}

	// broadcast to all followers
	cursor, err := ColUser.FindAll(qfind)
	if err == nil {
		for cursor.HasMore() {
			doc, err = cursor.GetNext()
			if err != nil {
				Error("Cannot get next. e: %v\n", err)
				break
			}

			var follower User
			mongo.Unmarshal(doc.Bytes(), &follower, atreps)

			if strid(follower.Id_) == post.Origin_id_ {
				continue
			}

			Info2("broadcast to follower: id: %v, name: %v\n", strid(follower.Id_), follower.Name)

			// insert to follower streams
			InsertPostStream(strid(follower.Id_), post_id)
		}
	} else {
		Warn("Cannot find post by id `%s`. %s.\n", post_id, err)
	}

	// get origin
	if len(strings.Trim(post.Origin_id_, " \n\t\r")) == 0 {
		Warn("post with id `%s` has no origin id\n", strid(post.Id_))
		return
	}

	doc, err = GetOrigin(post.Origin_id_)
	if err == nil {

		var spdoc SuperDoc

		mongo.Unmarshal(doc.Bytes(), &spdoc, atreps)

		switch spdoc.Metaname_ {
		case "Channel":
			var ch Channel
			mongo.Unmarshal(doc.Bytes(), &ch, atreps)

			//var ch_id string = strid(ch.Id_)
			// fmt.Printf("ch_id: %s\n", ch_id)

			// get all members
			qfind, err := mongo.Marshal(map[string]string{"_channel_ids": strid(ch.Id_)}, atreps)
			if err != nil {
				Error("Cannot marshal. %s\n", err)
				return
			}

			// broadcast to all members
			cursor, err := ColUser.FindAll(qfind)
			if err == nil {
				for cursor.HasMore() {
					doc, err = cursor.GetNext()
					if err != nil {
						Error("Cannot get next. e: %v\n", err)
						break
					}

					var follower User
					mongo.Unmarshal(doc.Bytes(), &follower, atreps)
					Info2("broadcast to member: id: %v, name: %v\n", strid(follower.Id_), follower.Name)

					// insert to follower streams
					InsertPostStream(strid(follower.Id_), post_id)
				}
			} else {
				Warn("Cannot find post by id `%s`. %s.\n", post_id, err)
			}
		case "User":
			Info2("Inserting into origin=User. origin_id: %v\n", post.Origin_id_)
			InsertPostStream(post.Origin_id_, post_id)
		}

	} else {
		Warn("Cannot get origin id `%s`\n", post.Origin_id_)
	}

}
Exemple #16
0
func BroadcastAll(postId string) {

	if dbcon == nil {
		Error("Database not connected.\n")
		return
	}

	// get post

	qfind, err := mongo.Marshal(oidSearch{"_id": mongo.ObjectId{postId}}, atreps)
	if err != nil {
		Error("Cannot marshal. %s\n", err)
		return
	}

	doc, err := ColPost.FindOne(qfind)
	if err != nil {
		Warn("Cannot find post by id `%s`. %s.\n", postId, err)
		return
	}

	var post UserPost

	mongo.Unmarshal(doc.Bytes(), &post, atreps)

	Info2("Got post id: %v, writer: %s, origin: %s\n", strid(post.Id_), post.WriterId, post.Origin_id_)

	// rm old refs
	RmPostStream(postId)

	// broadcast to all users
	qfind, err = mongo.Marshal(map[string]string{}, atreps)
	if err != nil {
		Error("Cannot marshal. %s\n", err)
		return
	}

	cursor, err := ColUser.FindAll(qfind)
	if err == nil {
		for cursor.HasMore() {
			doc, err = cursor.GetNext()
			if err != nil {
				Error("Cannot get next. e: %v\n", err)
				break
			}

			var user User
			mongo.Unmarshal(doc.Bytes(), &user, atreps)
			user_id := strid(user.Id_)
			Info2("broadcast to all: id: %v, name: %v\n", user_id, user.Name)

			// get user settings, is accept feed pop article
			st, err := GetUserSettings(user_id)
			if err != nil {
				Error("BroadcastAll: Cannot get user settings for user id `%v`\n", user_id)
				continue
			}
			if st.Feed_pop_article == false {
				Info2("BroadcastAll: Ignore feed article for user id `%v`\n", user_id)
				continue
			}

			// insert to follower streams
			InsertPostStream(user_id, postId)
		}
	} else {
		Warn("Cannot find post by id `%s`. %s.\n", postId, err)
	}

}