Exemple #1
0
func (this *FunServantImpl) MgFindAndModify(ctx *rpc.Context,
	pool string, table string, shardId int32,
	query []byte, change []byte, upsert bool,
	remove bool, returnNew bool) (r []byte, ex error) {
	const IDENT = "mg.findAndModify"

	if this.mg == nil {
		ex = ErrServantNotStarted
		return
	}

	svtStats.inc(IDENT)

	profiler, err := this.getSession(ctx).startProfiler()
	if err != nil {
		ex = err
		return
	}

	// get mongodb session
	sess, err := this.mongoSession(pool, shardId)
	if err != nil {
		ex = err
		return
	}
	defer sess.Recyle(&err)

	bsonQuery, err := mongo.UnmarshalIn(query)
	if err != nil {
		ex = err
		return
	}
	bsonChange, err := mongo.UnmarshalIn(change)
	if err != nil {
		ex = err
		return
	}

	doc := bson.M{}
	changeInfo, _ := sess.DB().C(table).Find(bsonQuery).
		Apply(mgo.Change{Update: bsonChange,
			Upsert: upsert, Remove: remove, ReturnNew: returnNew}, &doc)
	r = mongo.MarshalOut(doc)

	profiler.do(IDENT, ctx,
		"{pool^%s table^%s query^%v chg^%v} {err^%v updated^%d removed^%d r^%v}",
		pool, table,
		bsonQuery,
		bsonChange,
		ex,
		changeInfo.Updated, changeInfo.Removed,
		doc)
	return
}
Exemple #2
0
func (this *FunServantImpl) MgFindAll(ctx *rpc.Context,
	pool string, table string, shardId int32,
	query []byte, fields []byte, limit int32, skip int32,
	orderBy []string) (r [][]byte, ex error) {
	const IDENT = "mg.findAll"

	if this.mg == nil {
		ex = ErrServantNotStarted
		return
	}

	svtStats.inc(IDENT)

	profiler, err := this.getSession(ctx).startProfiler()
	if err != nil {
		ex = err
		return
	}

	sess, err := this.mongoSession(pool, shardId)
	if err != nil {
		ex = err
		return
	}
	defer sess.Recyle(&err)

	bsonQuery, err := mongo.UnmarshalIn(query)
	if err != nil {
		ex = err
		return
	}
	var bsonFields bson.M
	if !mongo.FieldsIsNil(fields) {
		bsonFields, err = mongo.UnmarshalIn(fields)
		if err != nil {
			ex = err
			return
		}
	}

	q := sess.DB().C(table).Find(bsonQuery)
	if !mongo.FieldsIsNil(fields) {
		q.Select(bsonFields)
	}
	if limit > 0 {
		q.Limit(int(limit))
	}
	if skip > 0 {
		q.Skip(int(skip))
	}
	if len(orderBy) > 0 {
		q.Sort(orderBy...)
	}

	var result []bson.M
	err = q.All(&result)
	if err == nil {
		r = make([][]byte, len(result))
		for i, v := range result {
			r[i] = mongo.MarshalOut(v)
		}
	} else {
		ex = err
	}

	profiler.do(IDENT, ctx,
		"{pool^%s table^%s query^%v fields^%v} {err^%v rN^%d}",
		pool, table,
		bsonQuery,
		bsonFields,
		ex,
		len(r))

	return
}
Exemple #3
0
func (this *FunServantImpl) MgFindOne(ctx *rpc.Context,
	pool string, table string, shardId int32,
	query []byte, fields []byte) (r []byte,
	miss *rpc.TMongoNotFound, ex error) {
	const IDENT = "mg.findOne"

	if this.mg == nil {
		ex = ErrServantNotStarted
		return
	}

	svtStats.inc(IDENT)

	profiler, err := this.getSession(ctx).startProfiler()
	if err != nil {
		ex = err
		return
	}

	// get mongodb session
	sess, err := this.mongoSession(pool, shardId)
	if err != nil {
		ex = err
		return
	}
	defer sess.Recyle(&err)

	bsonQuery, err := mongo.UnmarshalIn(query)
	if err != nil {
		ex = err
		return
	}
	var bsonFields bson.M
	if !mongo.FieldsIsNil(fields) {
		bsonFields, err = mongo.UnmarshalIn(fields)
		if err != nil {
			ex = err
			return
		}
	}

	var result bson.M
	q := sess.DB().C(table).Find(bsonQuery)
	if !mongo.FieldsIsNil(fields) {
		q.Select(bsonFields)
	}
	err = q.One(&result)
	if err != nil {
		if err != mgo.ErrNotFound {
			log.Error("%s: %v", IDENT, err)
		} else {
			miss = rpc.NewTMongoNotFound()
			miss.Message = thrift.StringPtr(err.Error())
			profiler.do(IDENT, ctx,
				"{pool^%s table^%s query^%v fields^%v} {miss^%v err^%v val^%v}",
				pool, table,
				bsonQuery,
				bsonFields,
				miss,
				ex,
				result)
			return
		}

		ex = err
		return
	}

	r = mongo.MarshalOut(result)

	profiler.do(IDENT, ctx,
		"{pool^%s table^%s query^%v fields^%v} {miss^%v err^%v val^%v}",
		pool, table,
		bsonQuery,
		bsonFields,
		miss,
		ex,
		result)

	return
}