Exemple #1
0
func incrCount(tde *kv.TiedotEngine) int64 {
	mu.Lock()
	defer mu.Unlock()
	counter := new(Counter)
	id, err := tde.Query(counterCollection).Has(kv.Path{"Count"}).OneInto(counter)
	if err == kv.ErrNotFound {
		log.Warning("Counter not found, saving new one.")
		_, err = tde.Insert(counterCollection, Counter{Count: 1})
		if err != nil {
			log.Errorf("Error saving new counter err=%s", err.Error())
		}
		r, err := tde.All(counterCollection)
		log.V(3).Infof("total of %d results=%v, err=%v", len(r), r, err)
		return 1
	}
	if err != nil {
		log.Error("Failure getting counter err:" + err.Error())
		panic(err)
	}
	counter.Count++
	err = tde.Update(counterCollection, id, counter)
	if err != nil {
		log.Error("Failure updating counter err:" + err.Error())
		panic(err)
	}
	return counter.Count
}
Exemple #2
0
func (q Query) JSON() string {
	j, err := json.Marshal(q.q)
	if err != nil {
		log.Error("Failure JSONifying query err=%s query=%v", err.Error(), q.q)
	}
	return string(j)
}
Exemple #3
0
func (t *TiedotEngine) All(collectionName string) (map[uint64]struct{}, error) {
	r := make(map[uint64]struct{})
	if err := tiedot.EvalQuery("all", t.tiedot.Use(collectionName), &r); err != nil {
		log.Error("Error executing TiedotEngine.All() err=%s", err.Error())
		return nil, err
	}
	return r, nil
}
Exemple #4
0
func remove(w http.ResponseWriter, r *http.Request, tde *kv.TiedotEngine, params martini.Params) (int, []byte) {
	short := params["short"]
	_, err := tde.Query(urlCollection).Equals(kv.Path{"Short"}, base62.DecodeString(short)).Delete()
	if err != nil {
		log.Error("Failure deleting URL /" + short + " err:" + err.Error())
		return 500, M{
			"message": "Could not delete URL /" + short,
			"error":   err.Error(),
		}.JSON()
	}
	log.V(1).Info("Deleted URL /" + short)
	return 200, M{
		"deleted": M{"short": short},
	}.JSON()
}
Exemple #5
0
func retrieve(w http.ResponseWriter, r *http.Request, tde *kv.TiedotEngine, params martini.Params) {
	short := params["short"]
	domain, err := LongURL(short, tde)
	if err == kv.ErrNotFound {
		log.V(1).Info("Path /" + short + " not found")
		w.WriteHeader(http.StatusNotFound)
		return
	}
	if len(domain.Original) > 0 {
		log.V(3).Info("[INFO]: Served /" + short + " redirect to " + domain.Original)
		http.Redirect(w, r, domain.Original, http.StatusFound)
		hits <- short
		return
	}
	log.Error("retrieving long URL err:" + err.Error())
	w.WriteHeader(500)
	if err != nil {
		w.Write(M{"error": err.Error()}.JSON())
	}
}
Exemple #6
0
func incrHits(tde *kv.TiedotEngine, key string) uint64 {
	mu.Lock()
	defer mu.Unlock()
	short := new(Shortened)
	id, err := tde.Query(urlCollection).Equals(
		kv.Path{"Short"},
		base62.DecodeString(key),
	).OneInto(short)
	if err == kv.ErrNotFound {
		log.Warningf("Short URL %s not found", key)
		return 0
	} else if err != nil {
		log.Errorf("Failure getting shortened URL key=%s err:%v", key, err)
		return 0
	}

	short.HitCount++
	err = tde.Update(urlCollection, id, short)
	if err != nil {
		log.Error("Failure updating hitcount key=%s err=%s", key, err.Error())
		return 0
	}
	return short.HitCount
}