Exemple #1
0
func Init(_con net.PacketConn, st *Settings, verbosity int) {
	con = _con
	settings = st

	SetVerbosity(verbosity)

	var err os.Error

	dbcon, err = mongo.Connect(st.DbServer, st.DbPort)
	if err != nil {
		fmt.Println("DB connection error.", err)
		return
	}

	db = dbcon.GetDB(st.DbName)
	ColStream = db.GetCollection("ndayak_streams")
	ColPost = db.GetCollection("user_post")
	ColChan = db.GetCollection("channel")
	ColTun = db.GetCollection("tunnel")
	ColUser = db.GetCollection("user")
	ColUserSettings = db.GetCollection("user_settings")

	ColStream.EnsureIndex("ndayax_1", map[string]int{"userid": 1, "postid": 1})

	atreps = map[string]string{
		"_origin_id":         "origin_id_",
		"_metaname_":         "metaname_",
		"_followed_user_ids": "followed_user_ids_",
		"_writer_id":         "writerid",
		"_popular_post":      "popular_post_",
		"_user_id":           "user_id_",
	}
}
Exemple #2
0
func TestBenchmark(t *testing.T) {
	conn, err := mongo.Connect("127.0.0.1", 27017)
	if err != nil {
		t.Error("failed connecting")
	}

	db := conn.GetDB("perf_test")
	db.Drop()
	db.GetCollection("creation").Insert(mongo.EmptyObject)
	db.GetCollection("creation").Count(mongo.EmptyObject)

	timeIt("single.small Insert", singleInsertSmall, db.GetCollection("single.small"), t)
	timeIt("single.medium Insert", singleInsertMedium, db.GetCollection("single.medium"), t)
	timeIt("single.large Insert", singleInsertLarge, db.GetCollection("single.large"), t)
	timeIt("single.small FindOne", findOneSmall, db.GetCollection("single.small"), t)
	timeIt("single.medium FindOne", findOneMedium, db.GetCollection("single.medium"), t)
	timeIt("single.large FindOne", findOne, db.GetCollection("single.large"), t)

	t.Log("---")
	db.GetCollection("single.small.indexed").EnsureIndex("my_index1", map[string]int{"x": 1})
	timeIt("single.small.indexed Insert", singleInsertSmall, db.GetCollection("single.small.indexed"), t)

	db.GetCollection("single.medium.indexed").EnsureIndex("my_index2", map[string]int{"x": 1})
	timeIt("single.medium.indexed Insert", singleInsertMedium, db.GetCollection("single.medium.indexed"), t)

	db.GetCollection("single.large.indexed").EnsureIndex("my_index3", map[string]int{"x": 1})
	timeIt("single.large.indexed Insert", singleInsertLarge, db.GetCollection("single.large.indexed"), t)

	timeIt("single.small.indexed FindOne", findOneSmall, db.GetCollection("single.small.indexed"), t)
	timeIt("single.medium.indexed FindOne", findOneMedium, db.GetCollection("single.medium.indexed"), t)
	timeIt("single.large.indexed FindOne", findOne, db.GetCollection("single.large.indexed"), t)
}
Exemple #3
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 #4
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()
}