Example #1
0
func queryPackages(c appengine.Context, cacheKey string, query *datastore.Query) ([]*Package, error) {
	var pkgs []*Package
	item, err := cacheGet(c, cacheKey, &pkgs)
	if err == memcache.ErrCacheMiss {
		keys, err := query.GetAll(c, &pkgs)
		if err != nil {
			return nil, err
		}
		for i := range keys {
			importPath := keys[i].StringID()
			if importPath[0] == '/' {
				// Standard packages start with "/"
				importPath = importPath[1:]
			}
			pkgs[i].ImportPath = importPath
		}
		item.Expiration = time.Hour
		item.Object = pkgs
		if err := cacheSafeSet(c, item); err != nil {
			return nil, err
		}
	} else if err != nil {
		return nil, err
	}
	return pkgs, nil
}
Example #2
0
/**
 * データストアから指定された単語を削除する
 * API
 */
func delete(w http.ResponseWriter, r *http.Request) {
	var c appengine.Context
	var u *user.User
	var query *datastore.Query
	var word string
	var iterator *datastore.Iterator
	var key *datastore.Key
	var err error
	var entity Entity

	word = r.FormValue("word")
	if word == "" {
		return
	}

	c = appengine.NewContext(r)
	u = user.Current(c)
	query = datastore.NewQuery("words").Filter("UserID =", u.ID).Filter("Word =", word)
	iterator = query.Run(c)

	for {
		key, err = iterator.Next(&entity)
		if err != nil {
			break
		}
		err = datastore.Delete(c, key)
		Check(c, err)
	}
}
Example #3
0
func GetAllKeys(c appengine.Context, q *ds.Query, limit int) ([]*ds.Key, ds.Cursor, bool, error) {
	it := q.KeysOnly().Limit(limit + 1).Run(c)

	keys := make([]*ds.Key, 0, limit)
	hasMore := true
	for i := 0; i < limit; i++ {
		key, err := it.Next(nil)
		if err == ds.Done {
			hasMore = false
			break
		} else if err != nil {
			return nil, ds.Cursor{}, false, err
		}
		keys = append(keys, key)
	}

	cursor, _ := it.Cursor()
	if hasMore {
		if _, err := it.Next(nil); err != nil {
			hasMore = false
		}
	}

	return keys, cursor, hasMore, nil
}
Example #4
0
// DelQuery will delete (from datastore and memcache) all entities of type src that matches q.
// src must be a pointer to a struct type.
func DelQuery(c PersistenceContext, src interface{}, q *datastore.Query) (err error) {
	var dataIds []*datastore.Key
	results := reflect.New(reflect.SliceOf(reflect.TypeOf(src).Elem()))
	dataIds, err = q.GetAll(c, results.Interface())
	if err = FilterOkErrors(err); err != nil {
		return
	}
	memcacheKeys := []string{}
	var el reflect.Value
	resultsSlice := results.Elem()
	for index, dataId := range dataIds {
		el = resultsSlice.Index(index)
		var k key.Key
		if k, err = gaekey.FromGAE(dataId); err != nil {
			return
		}
		el.FieldByName("Id").Set(reflect.ValueOf(k))
		if _, err = MemcacheKeys(c, el.Addr().Interface(), &memcacheKeys); err != nil {
			return
		}
		if err = runProcess(c, el.Addr().Interface(), BeforeDeleteName, nil); err != nil {
			return
		}
	}
	if err = datastore.DeleteMulti(c, dataIds); err != nil {
		return
	}
	for index, _ := range dataIds {
		el = resultsSlice.Index(index)
		if err = runProcess(c, el.Addr().Interface(), AfterDeleteName, nil); err != nil {
			return
		}
	}
	return memcache.Del(c, memcacheKeys...)
}
Example #5
0
// This kind of iterator expects to last more than 60s, which is when a regular datastore
// iterator will timeout (it thinks its view of the world is too stale)
// Instead, we just grab all the keys up front, and than instantiate them one at a time.
func (db FlightDB) LongIterWith(q *datastore.Query, flightProcFunc func(*f.Flight)) error {
	keys, err := q.KeysOnly().GetAll(db.C, nil)
	if err != nil {
		return err
	}

	for _, key := range keys {
		blob := f.FrozenFlight{}

		if err := datastore.Get(db.C, key, &blob); err != nil {
			return err
		}

		if flight, err := blob.Thaw(); err != nil {
			return err
		} else {
			//flight.EnterUTC,flight.LeaveUTC = flight.Track.TimesInBox(sfo.KBoxSnarfingCatchment)
			flight.SetDatastoreKey(key.Encode())

			flightProcFunc(flight)
		}
	}

	return nil
}
Example #6
0
func getRadarsFromQuery(
	c appengine.Context,
	query *datastore.Query,
	limit int) (radars []Radar, cursor_out string, err error) {

	radars = make([]Radar, 0, limit)
	count := 0
	iterator := query.Run(c)
	for {
		var radar Radar
		_, err := iterator.Next(&radar)
		if err == datastore.Done {
			return radars, "", nil
		} else if err != nil {
			c.Errorf("Radar iterator error: %v", err)
			continue
		} else {
			radars = append(radars, radar)
		}
		count = count + 1
		if count == limit {
			break
		}
	}
	// in case there are more radars, continue using a cursor based on the current position
	if cursor, err := iterator.Cursor(); err == nil {
		cursor_out = cursor.String()
	}
	return radars, cursor_out, nil
}
Example #7
0
func ExecuteQuery(c appengine.Context, q *datastore.Query, start int, limit int, filter func(Post) bool, out chan<- Post, errout chan<- error) {

	defer close(out)
	defer close(errout)

	for t, i := q.Run(c), 0; ; i++ {
		if ((start < 0) || (limit < 0)) || (i < (start + limit)) {
			var x Post
			// key, err := t.Next(&x)
			_, err := t.Next(&x)

			if err == datastore.Done {
				return
			}
			if err != nil {
				errout <- err
				return
			}

			if !filter(x) {
				i--
				continue
			}

			if i < start {
				continue
			}

			out <- x
		} else {
			break
		}
	}
}
Example #8
0
//MultiQuery executes given query and returns slice of all the entities it returns, with their keys set.
func MultiQuery(c appengine.Context, typ reflect.Type, kind string, query *datastore.Query) (ms interface{}, err error) {
	is := reflect.MakeSlice(reflect.SliceOf(reflect.PtrTo(typ)), 0, 0)

	iter := query.Run(c)

	for {
		val := reflect.New(typ)
		var key *datastore.Key
		key, err = iter.Next(val.Interface())
		if err != nil {
			if err == datastore.Done {
				err = nil
				val.MethodByName("SetKey").Call([]reflect.Value{reflect.ValueOf(key)})
				reflect.Append(is, val)
				break
			}
			return
		}
		val.MethodByName("SetKey").Call([]reflect.Value{reflect.ValueOf(key)})
		is = reflect.Append(is, val)
	}

	ms = is.Interface()

	return
}
Example #9
0
func GreaterThanEqualQuery(query *datastore.Query, values url.Values, s string) *datastore.Query {
	if len(values[s]) != 0 {
		columnName := strings.TrimRight(strings.ToUpper(s[:1])+s[1:], "From")
		query = query.Filter(columnName+">=", ToInt(values[s][0])).Order(columnName)
	}
	return query
}
Example #10
0
func (db FlightDB) lookupByQuery(q *datastore.Query, memKey string) ([]f.Flight, error) {

	if db.Memcache && memKey != "" {
		if flights, found := db.flightsFromShardedMemcache(memKey); found {
			return flights, nil
		}
	}

	fflights := []f.FrozenFlight{}
	//db.C.Infof(" #--- actually going to datastore ...")
	keys, err := q.GetAll(db.C, &fflights)
	if err != nil {
		return nil, err
	}

	flights := []f.Flight{}
	for i, fflight := range fflights {
		if flight, err := fflight.Thaw(); err != nil {
			return nil, err
		} else {
			flight.EnterUTC, flight.LeaveUTC = flight.Track.TimesInBox(sfo.KBoxSnarfingCatchment)
			flight.SetDatastoreKey(keys[i].Encode())
			flights = append(flights, *flight)
		}
	}

	if db.Memcache && memKey != "" {
		db.flightsToShardedMemcache(memKey, flights)
	}

	return flights, nil
}
Example #11
0
func (fdb FlightDB) NewIter(q *datastore.Query) *FlightIterator {
	fi := FlightIterator{
		C:     fdb.C,
		Query: q,
		Iter:  q.Run(fdb.C),
	}
	return &fi
}
Example #12
0
File: model.go Project: snim2/howl
/* Query the datastore.
 * TODO: Use memcache.
 *
 * @param context for this particular appengine session
 * @param key datastore key for the object to be retrieved
 * @param object the object to be made persistent
 * @param error message to be printed to the log / user in case of error
 * @return a list of keys returned by the query and errors generated by the query (if there are any)
 */
func query(context appengine.Context, query *datastore.Query, objects interface{}, error string) ([]*datastore.Key, os.Error) {
	keys, err := query.GetAll(context, objects)
	if err != nil {
		log.Println(error + err.String())
		return nil, err
	}
	return keys, nil
}
Example #13
0
func (cdb ComplaintDB) NewIter(q *datastore.Query) *ComplaintIterator {
	ci := ComplaintIterator{
		C:     cdb.C,
		Query: q,
		Iter:  q.Run(cdb.C),
	}
	return &ci
}
Example #14
0
// firstMatch executes the query q and loads the first entity into v.
func firstMatch(c appengine.Context, q *datastore.Query, v interface{}) error {
	t := q.Limit(1).Run(c)
	_, err := t.Next(v)
	if err == datastore.Done {
		err = datastore.ErrNoSuchEntity
	}
	return err
}
Example #15
0
func (db FlightDB) countByQuery(q *datastore.Query) (int, error) {
	q2 := q.KeysOnly()

	keys, err := q2.GetAll(db.C, nil)
	if err != nil {
		return 0, err
	}

	return len(keys), nil
}
Example #16
0
func (mp person_mapper) MapRows(c appengine.Context, q *datastore.Query, dst interface{}) error {
	if _, ok := dst.(*[]*Person); !ok {
		return errors.New("the parameter is not slice, please check it.")
	}
	dstc := dst.(*[]*Person)
	_, err := q.GetAll(c, dstc)
	if err != nil {
		return err
	}
	return nil
}
Example #17
0
func EqualQuery(query *datastore.Query, values url.Values, s string) *datastore.Query {
	if len(values[s]) != 0 {
		if values[s][0] == "true" || values[s][0] == "false" {
			value, _ := strconv.ParseBool(values[s][0])
			query = query.Filter(strings.ToUpper(s[:1])+s[1:]+"=", value)
		} else {
			query = query.Filter(strings.ToUpper(s[:1])+s[1:]+"=", values[s][0])
		}
	}
	return query
}
Example #18
0
File: db.go Project: pimienta/appgo
// USE:  keys,ok := db.SelectKeys(qry)
func (db *Manager) SelectKeys(qry *datastore.Query) ([]string, bool) {
	recs := new(interface{})
	keys, err := qry.KeysOnly().GetAll(db.Context, recs)
	skeys := []string{}
	if err == nil {
		for k := range keys {
			skeys = append(skeys, keys[k].StringID())
		}
	}
	return skeys, err == nil
}
Example #19
0
// LoadConfList executes the given query and returns a new ConfList with the given title and
// containing the conferences obtained from the query result.
func LoadConfList(ctx appengine.Context, title string, q *datastore.Query) (*ConfList, error) {
	list := &ConfList{Title: title}

	ks, err := q.GetAll(ctx, &list.Conferences)
	if err != nil {
		return nil, fmt.Errorf("get %q: %v", list.Title, err)
	}
	for i, k := range ks {
		list.Conferences[i].key = k
	}
	return list, nil
}
Example #20
0
func applyCursor(query_in *datastore.Query, cursor_in string) (query_out *datastore.Query, err error) {
	if cursor_in != "" {
		cursor, err := datastore.DecodeCursor(cursor_in)
		if err == nil {
			return query_in.Start(cursor), nil
		} else {
			return query_in, err
		}
	} else {
		return query_in, nil
	}
}
Example #21
0
// create an index for items, by calling the keyFunc function provided for each item
//  returning a map from those keys to the items
func (self *importer) indexItems(query *datastore.Query, item interface{},
	keyFunc func(*datastore.Key, interface{}) string) map[string]*datastore.Key {
	index := make(map[string]*datastore.Key)
	iter := query.Run(self.c)
	for dataKey, err := iter.Next(item); err == nil; dataKey, err = iter.Next(item) {
		indexKey := keyFunc(dataKey, item)
		if len(indexKey) > 0 {
			index[indexKey] = dataKey
		}
	}
	return index
}
Example #22
0
// Builds a list of all of the simulations that match a query as SimulationData types
func GetSimulationDataSlice(r *http.Request, q *datastore.Query) ([]models.SimulationData, error) {
	var simulations []models.SimulationData
	var simulationObjs []models.Simulation

	ctx := appengine.NewContext(r)
	simulationKeys, err := q.GetAll(ctx, &simulationObjs)

	if err != nil {
		return simulations, err
	}

	return BuildSimulationDataSlice(ctx, simulationObjs, simulationKeys)
}
Example #23
0
func fetchScores(c appengine.Context, q *datastore.Query, limit int) (
	[]*Score, error) {

	scores := make([]*Score, 0, limit)
	keys, err := q.Limit(limit).GetAll(c, &scores)
	if err != nil {
		return nil, err
	}
	for i, score := range scores {
		score.key = keys[i]
	}
	return scores, nil
}
Example #24
0
// QueryData returns a datastore.Query.GetAll result and also caches the result
// into memcache.
func QueryData(c appengine.Context, mcKey string, q *datastore.Query, dst interface{}) error {
	err := mcGet(c, mcKey, dst)
	switch err {
	case memcache.ErrCacheMiss:
		if _, err := q.GetAll(c, dst); err != nil {
			return err
		}
		mcSet(c, mcKey, dst)
		return nil
	case nil:
		return nil
	}
	return err
}
Example #25
0
func scanData(ctxt appengine.Context, name string, period time.Duration, q *datastore.Query, f func(ctxt appengine.Context, kind, key string) error) {
	// TODO: Handle even more keys by using cursor.
	const chunk = 100000

	keys, err := q.Limit(chunk).KeysOnly().GetAll(ctxt, nil)
	if err != nil {
		ctxt.Errorf("scandata %q: %v", name, err)
		return
	}

	const maxBatch = 100
	for _, key := range keys {
		Task(ctxt, fmt.Sprintf("app.scandata.%s.%s", key.Kind(), key.StringID()), "scandata", name, key.Kind(), key.StringID())
	}
}
Example #26
0
func GetAll(c appengine.Context, q *ds.Query, dst interface{}) (ds.Cursor, bool, error) {
	v := reflect.ValueOf(dst)
	if v.Kind() != reflect.Ptr || v.IsNil() {
		return ds.Cursor{}, false, ds.ErrInvalidEntityType
	}
	v = v.Elem()

	et := v.Type().Elem()
	if et.Kind() == reflect.Ptr {
		et = et.Elem()
	}
	if et.Kind() != reflect.Struct {
		return ds.Cursor{}, false, ds.ErrInvalidEntityType
	}

	vlen := v.Cap()
	it := q.Limit(vlen + 1).Run(c)
	hasMore := true
	for i := 0; i < vlen; i++ {
		ev := reflect.New(et)
		key, err := it.Next(ev.Interface())
		if err == ds.Done {
			hasMore = false
			break
		} else if err != nil {
			return ds.Cursor{}, false, err
		}
		ev.Elem().FieldByName("Key").Set(reflect.ValueOf(key))
		v.Set(reflect.Append(v, ev))
	}

	cursor, err := it.Cursor()
	if err != nil {
		c.Errorf("Cursor failed: %v", err)
	}
	if hasMore {
		ev := reflect.New(et)
		if _, err := it.Next(ev.Interface()); err != nil {
			hasMore = false
			if err != ds.Done {
				c.Errorf("Next failed: %v", err)
			}
		}
	}

	return cursor, hasMore, nil
}
Example #27
0
func GetQuery(c PersistenceContext, src interface{}, q *datastore.Query) (err error) {
	srcTyp := reflect.TypeOf(src)
	if srcTyp.Kind() != reflect.Ptr {
		err = errors.Errorf("%+v is not a pointer", src)
		return
	}
	if srcTyp.Elem().Kind() != reflect.Slice {
		err = errors.Errorf("%+v is not a pointer to a slice", src)
		return
	}
	if srcTyp.Elem().Elem().Kind() == reflect.Ptr {
		if srcTyp.Elem().Elem().Elem().Kind() != reflect.Struct {
			err = errors.Errorf("%+v is not a pointer to a slice of struct pointers", src)
			return
		}
	} else if srcTyp.Elem().Elem().Kind() != reflect.Struct {
		err = errors.Errorf("%+v is not a pointer to a slice of structs", src)
		return
	}
	var dataIds []*datastore.Key
	dataIds, err = q.GetAll(c, src)
	if err = FilterOkErrors(err); err != nil {
		return
	}
	srcVal := reflect.ValueOf(src)
	for index, dataId := range dataIds {
		el := srcVal.Elem().Index(index)
		var k key.Key
		if k, err = gaekey.FromGAE(dataId); err != nil {
			return
		}
		if el.Kind() == reflect.Ptr {
			el.Elem().FieldByName("Id").Set(reflect.ValueOf(k))
			if err = runProcess(c, el.Interface(), AfterLoadName, nil); err != nil {
				return
			}
		} else {
			el.FieldByName("Id").Set(reflect.ValueOf(k))
			if err = runProcess(c, el.Addr().Interface(), AfterLoadName, nil); err != nil {
				return
			}
		}
	}
	return
}
Example #28
0
func returnItems(c appengine.Context, q *datastore.Query, w http.ResponseWriter) {
	var orders []Order
	type Item struct {
		Order
		Id string `json:"id"`
	}
	var items []Item
	keys, err := q.GetAll(c, &orders)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	for i, o := range orders {
		items = append(items, Item{Order: o, Id: keys[i].Encode()})
	}
	enc := json.NewEncoder(w)
	enc.Encode(items)
}
Example #29
0
// FindByQuery finds Records handled by this Model.
//
// Returns an array of records as the first argument,
// or an error as the second return argument.
//
// The queryOrFunc argument may be one of:
//
//   *datastore.Query
// The specified query will be used to find records.
//   func(*datastore.Query)
// A new query will be created and the specified function will be
// used to further configure the query.
//
// Example:
//  model := NewModel("people")
//  women, err := model.FindByQuery(func(q *datastore.Query){
//	  q.Filter("sex=", "male")
//  })
//
// Raises events for each record:
//   Model.AfterFind with Args(record)
func (m *Model) FindByQuery(queryOrFunc interface{}) ([]*Record, os.Error) {

	var query *datastore.Query

	if reflect.TypeOf(queryOrFunc).Kind() == reflect.Func {

		// create a new query
		query = m.NewQuery()

		// ask the func to configure the query
		queryOrFunc.(func(*datastore.Query))(query)

	} else {

		// just use the query
		query = queryOrFunc.(*datastore.Query)

	}

	var records []*Record
	keys, err := query.GetAll(m.AppEngineContext(), &records)

	if err == nil {

		// configure each loaded record
		for index, record := range records {

			record.configureRecord(m, keys[index])

			if m.AfterFind.HasCallbacks() {
				m.AfterFind.Trigger(record)
			}

		}

		return records, nil

	}

	return nil, err

}
Example #30
0
/**
 * データストアの単語をすべて削除する
 * ajaxから呼び出すためのAPI
 */
func clear(w http.ResponseWriter, r *http.Request) {
	var c appengine.Context
	var u *user.User
	var keys []*datastore.Key
	var err error
	var query *datastore.Query
	var count int
	var entities []Entity

	c = appengine.NewContext(r)
	u = user.Current(c)
	query = datastore.NewQuery("words").Filter("UserID =", u.ID)
	count, err = query.Count(c)
	Check(c, err)
	entities = make([]Entity, count)
	keys, err = query.GetAll(c, &entities)
	Check(c, err)
	err = datastore.DeleteMulti(c, keys)
	Check(c, err)
}