Esempio n. 1
0
func (c *Client) SetTags(uid string, fields map[string]interface{}) error {
	session := c.MgoSession.New()
	defer session.Close()

	store := session.DB(c.Cfg.Mongo.Db).C("Storage")

	fields["version"] = 0
	timestamp := int64(time.Now().Unix() / 1000)
	change := mgo.Change{
		Update: bson.M{
			"$set": bson.M{"last_visit": timestamp},
			"$inc": bson.M(fields),
		},
		ReturnNew: true,
		Upsert:    true,
	}
	resultUps := Storage{}
	info, err := store.Find(bson.M{"uid": uid}).Apply(change, &resultUps)

	if c.Debug {
		fmt.Print("set tags info: ")
		spew.Dump(info)
		fmt.Print("resultUps: ")
		spew.Dump(resultUps)
	}

	if err != nil {
		panic(err)
	}

	return nil
}
Esempio n. 2
0
// Document returns the changed document for Insert or Update.
func (op *EsOperation) Document() (map[string]interface{}, error) {
	if op.doc != nil {
		return op.doc, nil
	}
	op.doc = make(map[string]interface{})

	var changes bson.M

	switch op.Op {
	case Update:
		// Partial update
		sets, ok := op.Object["$set"]
		if ok {
			stats.Sets.Add(1)
		}
		if unsets, ok := op.Object["$unset"]; ok {
			// Instead of having to find the full object in MongoDB or passing a script to ES
			// we pretend there's a $set with null value which is enough in most cases.
			stats.Unsets.Add(1)
			if sets == nil {
				sets = make(bson.M)
			}
			for key, _ := range unsets.(bson.M) {
				sets.(bson.M)[key] = nil
			}
		}
		if sets != nil {
			changes = sets.(bson.M)
			break
		}
		// All other updates is a full document(?)
		fallthrough
	case Insert:
		stats.Complete.Add(1)
		changes = bson.M(op.Object)
	default:
		return nil, OperationError{"Unsupported operation", op}
	}

	// Run the document through the manipulators to make it look like we want it to before it hits ES
	for _, manip := range op.manipulators {
		if err := manip.Manipulate(&changes, op.Op); err != nil {
			return nil, err
		}
	}
	// Stored as a map so that ES doesn't have to know about bson.M which is the same.
	op.doc = map[string]interface{}(changes)

	return op.doc, nil
}
Esempio n. 3
0
func (b BsonTraverser) Next(key string) BsonTraverser {
	if next, ok := b.M[key]; ok {
		switch t := next.(type) {
		case bson.M:
			b = BsonTraverser{M: t}
		case map[string]interface{}:
			b = BsonTraverser{M: bson.M(t)}
		}
		// Keep a reference to the last item for Value()
		b.value = next
	} else {
		// Reached something else than a string map but we are trying to traverse it, set nil
		b.value = nil
	}
	return b
}
Esempio n. 4
0
func (c *Client) SetData(uid string, fields map[string]interface{}) error {
	session := c.MgoSession.New()
	defer session.Close()

	store := session.DB(c.Cfg.Mongo.Db).C("Storage")

	if c.Debug {
		fmt.Println("SetData")
		fmt.Print("fields: ")
		spew.Dump(fields)
	}

	change := mgo.Change{
		Update: bson.M{
			"$set": bson.M(fields),
			"$inc": bson.M{"version": 1},
		},
		ReturnNew: true,
		Upsert:    true,
	}

	resultUps := Storage{}
	info, err := store.Find(bson.M{"uid": uid}).Apply(change, &resultUps)

	if c.Debug {
		fmt.Print("info: ")
		spew.Dump(info)
		fmt.Print("resultUps: ")
		spew.Dump(resultUps)
	}

	if err != nil {
		panic(err)
	}

	// XXX: not sure here
	c.Uncache(uid, resultUps.Version)
	//c.Uncache(uid, resultUps.Version+1) // <-- race condition here ?

	return nil
}
Esempio n. 5
0
func (mongo *MongoDB) Update(key string, value map[string]interface{}) error {
	err := mongo.Collection.Update(bson.M{"_id": key}, bson.M(value))
	return err
}
Esempio n. 6
0
func (mongo *MongoDB) Create(key string, value map[string]interface{}) error {
	value["_id"] = key
	err := mongo.Collection.Insert(bson.M(value))
	return err
}