Пример #1
0
func GetSong(ctx *middleware.Context, w http.ResponseWriter, r *http.Request) (interface{}, error) {
	var s song.Song
	s.ID = bson.ObjectIdHex(ctx.Params.ByName("id"))
	err := s.Get()
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return s, err
	}
	return s, nil
}
Пример #2
0
func DeleteSong(ctx *middleware.Context, w http.ResponseWriter, r *http.Request) (interface{}, error) {
	var s song.Song
	s.ID = bson.ObjectIdHex(r.URL.Query().Get("id"))
	err := s.Delete()
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return s, err
	}
	return s, nil
}
Пример #3
0
func DeleteUser(ctx *middleware.Context, w http.ResponseWriter, r *http.Request) (interface{}, error) {
	var u user.User
	var err error
	u.ID = bson.ObjectIdHex(r.URL.Query().Get("id"))
	err = u.Delete()
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return u, err
	}
	return u, nil
}
Пример #4
0
// PurgeMissing removes from collections any state that refers to transaction
// documents that for whatever reason have been lost from the system (removed
// by accident or lost in a hard crash, for example).
//
// This method should very rarely be needed, if at all, and should never be
// used during the normal operation of an application. Its purpose is to put
// a system that has seen unavoidable corruption back in a working state.
func (r *Runner) PurgeMissing(collections ...string) error {
	type M map[string]interface{}
	type S []interface{}
	pipeline := []M{
		{"$project": M{"_id": 1, "txn-queue": 1}},
		{"$unwind": "$txn-queue"},
		{"$sort": M{"_id": 1, "txn-queue": 1}},
		//{"$group": M{"_id": M{"$substr": S{"$txn-queue", 0, 24}}, "docids": M{"$push": "$_id"}}},
	}

	type TRef struct {
		DocId interface{} "_id"
		TxnId string      "txn-queue"
	}

	found := make(map[bson.ObjectId]bool)
	colls := make(map[string]bool)

	sort.Strings(collections)
	for _, collection := range collections {
		c := r.tc.Database.C(collection)
		iter := c.Pipe(pipeline).Iter()
		var tref TRef
		for iter.Next(&tref) {
			txnId := bson.ObjectIdHex(tref.TxnId[:24])
			if found[txnId] {
				continue
			}
			if r.tc.FindId(txnId).One(nil) == nil {
				found[txnId] = true
				continue
			}
			logf("WARNING: purging from document %s/%v the missing transaction id %s", collection, tref.DocId, txnId)
			err := c.UpdateId(tref.DocId, M{"$pull": M{"txn-queue": M{"$regex": "^" + txnId.Hex() + "_*"}}})
			if err != nil {
				return fmt.Errorf("error purging missing transaction %s: %v", txnId.Hex(), err)
			}
		}
		colls[collection] = true
	}

	type StashTRef struct {
		Id    docKey "_id"
		TxnId string "txn-queue"
	}

	iter := r.sc.Pipe(pipeline).Iter()
	var stref StashTRef
	for iter.Next(&stref) {
		txnId := bson.ObjectIdHex(stref.TxnId[:24])
		if found[txnId] {
			continue
		}
		if r.tc.FindId(txnId).One(nil) == nil {
			found[txnId] = true
			continue
		}
		logf("WARNING: purging from stash document %s/%v the missing transaction id %s", stref.Id.C, stref.Id.Id, txnId)
		err := r.sc.UpdateId(stref.Id, M{"$pull": M{"txn-queue": M{"$regex": "^" + txnId.Hex() + "_*"}}})
		if err != nil {
			return fmt.Errorf("error purging missing transaction %s: %v", txnId.Hex(), err)
		}
	}

	return nil
}
Пример #5
0
func (tt token) id() bson.ObjectId { return bson.ObjectIdHex(string(tt[:24])) }