func prepQuery(q interface{}) (query interface{}) { j, err := json.Marshal(q) if err != nil { log.Errorf("Failure serializing query err=%v", err) } err = json.Unmarshal(j, &query) if err != nil { log.Errorf("Failure deserializing query err=%v", err) } return }
func (q *Query) All() (res ResultSet, err error) { r, err := q.eval() if err != nil { log.Errorf("Error executing kv.Query.All() query=%s err=%s", q.JSON(), err.Error()) return } res = make(ResultSet) for id, _ := range r { v, err := q.read(id) if err != nil { log.Errorf("Failure reading id=%d err=%v", id, err) } res[id] = v } return }
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 }
func (q *Query) OneInto(out interface{}) (uint64, error) { r, err := q.eval() if err != nil { log.Errorf("Error executing kv.Query.One() err=%s", err.Error()) return 0, err } for k, _ := range r { log.V(2).Infof("Found id=%d kv.Query.OneInto()", k) if _, err := q.col.Read(k, out); err != nil { log.Errorf("Failure reading id=%d err=%s", k, err.Error()) return 0, err } return k, nil } log.V(1).Infof("Nothing found for query=%s", q.JSON()) return 0, ErrNotFound }
func (q *Query) One() (uint64, interface{}, error) { r, err := q.eval() if err != nil { log.Errorf("Error executing kv.Query.One() err=%s", err.Error()) return 0, nil, err } for id, _ := range r { v, err := q.read(id) if err != nil { log.Errorf("Failure reading id=%d err=%v", id, err) return id, nil, err } log.V(2).Infof("Found id=%d val=%v for kv.Query.One()", id, v) return id, v, nil } log.V(1).Infof("Nothing found for query=%v", q.JSON()) return 0, nil, ErrNotFound }
func (t *TiedotEngine) Update(collectionName string, id uint64, item Insertable) error { err := t.tiedot.Use(collectionName).Update(id, item.ToM()) if err != nil { log.Errorf("Failure updating item=%s err=%s", item.ToM().JSON(), err.Error()) } else { log.V(3).Infof("Updating with data: %v", item.ToM()) } return err }
func (q *Query) read(id uint64) (interface{}, error) { v := new(interface{}) if q.ReadLock == NoLock { q.col.ReadNoLock(id, v) } else if q.ReadLock == MustLock { q.col.Read(id, v) } else { log.Errorf("Read preference (NoLock or MustLock) not set for query=%s", q.JSON()) return nil, ErrReadPreference } return *v, nil }
func (q *Query) Delete() (int, error) { res, err := q.eval() if err != nil { log.Errorf("Error executing kv.Query.Delete() query=%s err=%s", q.JSON(), err.Error()) return -1, err } for id, _ := range res { q.col.Delete(id) log.V(6).Info("Deleted id=%d") } log.V(5).Info("Deleted %d objects for query=%s", len(res), q.JSON()) return len(res), nil }
func (t *TiedotEngine) Insert(collectionName string, item Insertable) (uint64, error) { if len(item.ToM()) == 0 { log.Warningf("Failure: No data in item=%v", item.ToM()) return 0, nil } else { log.V(3).Infof("Insertion into collection=%s item=%v", collectionName, item.ToM()) } id, err := t.tiedot.Use(collectionName).Insert(item.ToM()) if err != nil { log.Errorf("Failure inserting item=%v err=%s", item.ToM(), err.Error()) return 0, err } else { log.V(6).Infof("Added item with ID=%d, item=%v", id, item.ToM()) return id, nil } }
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 }