本文整理汇总了Golang中google/golang.org/appengine/datastore.Key类的典型用法代码示例。如果您正苦于以下问题:Golang Key类的具体用法?Golang Key怎么用?Golang Key使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Key类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Set
func (c *Cache) Set(key *datastore.Key, value interface{}) {
if c == nil {
return
}
c.objs[key.String()] = value
}
示例2: createFinalResult
func createFinalResult(ctx context.Context, w http.ResponseWriter, resultKey *datastore.Key, result model.Result) (int, error) {
go computeFinalScore(ctx, result)
var challenge model.Challenge
if err := datastore.Get(ctx, result.Challenge, &challenge); err != nil {
return http.StatusInternalServerError, nil
}
var taskKey *datastore.Key
for _, taskKey = range challenge.Tasks {
switch taskKey.Kind() {
case model.CodeTaskKind:
var submissions model.CodeSubmissions
keys, err := doQuery(ctx, model.NewQueryForCodeSubmission(), resultKey, taskKey, submissions)
if err != nil {
return http.StatusInternalServerError, nil
}
if len(keys) == 0 {
// Most likely the authenticated user called this endpoint
// before finishing the challenge
return http.StatusUnauthorized, nil
}
result.FinalSubmissions = append(result.FinalSubmissions, keys[0])
//TODO(pbochis, vbalan, flowlo): Add more cases when more task kinds are added.
default:
return http.StatusBadRequest, errors.New("Unknown submission kind.")
}
}
key, err := result.Save(ctx, resultKey)
if err != nil {
return http.StatusInternalServerError, err
}
json.NewEncoder(w).Encode(result.Key(key))
return http.StatusOK, nil
}
示例3: endTask
func endTask(c context.Context, ds appwrap.Datastore, taskIntf startTopIntf, jobKey *datastore.Key, taskKey *datastore.Key, resultErr error, result interface{}) error {
if resultErr == nil {
if task, err := updateTask(ds, taskKey, TaskStatusDone, 0, "", result); err != nil {
return fmt.Errorf("Could not update task: %s", err)
} else {
taskIntf.Status(jobKey.IntID(), task)
}
} else {
if _, ok := resultErr.(tryAgainError); ok {
// wasn't fatal, go for it
if retryErr := retryTask(c, ds, taskIntf, jobKey, taskKey); retryErr != nil {
return fmt.Errorf("error retrying: %s (task failed due to: %s)", retryErr, resultErr)
} else {
logInfo(c, "retrying task due to %s", resultErr)
return nil
}
}
// fatal error
if _, err := updateTask(ds, taskKey, TaskStatusFailed, 0, resultErr.Error(), nil); err != nil {
return fmt.Errorf("Could not update task with failure: %s", err)
}
}
return nil
}
示例4: Get
func (c *Cache) Get(key *datastore.Key) interface{} {
if c == nil {
return nil
}
return c.objs[key.String()]
}
示例5: FixupComplaint
func FixupComplaint(c *types.Complaint, key *datastore.Key) {
// 0. Snag the key, so we can refer to this object later
c.DatastoreKey = key.Encode()
// 1. GAE datastore helpfully converts timezones to UTC upon storage; fix that
c.Timestamp = date.InPdt(c.Timestamp)
// 2. Compute the flight details URL, if within 24 days
age := date.NowInPdt().Sub(c.Timestamp)
if age < time.Hour*24 {
// c.AircraftOverhead.Fr24Url = c.AircraftOverhead.PlaybackUrl()
c.AircraftOverhead.Fr24Url = "http://flightaware.com/live/flight/" +
c.AircraftOverhead.FlightNumber
// Or: http://flightaware.com/live/flight/UAL337/history/20151215/ [0655Z/KLAX/KSFO]
// date is UTC of departure time; might be tricky to guess :/
}
// 3. Compute distances, if we have an aircraft
if c.AircraftOverhead.FlightNumber != "" {
a := c.AircraftOverhead
aircraftPos := geo.Latlong{a.Lat, a.Long}
observerPos := geo.Latlong{c.Profile.Lat, c.Profile.Long}
c.Dist2KM = observerPos.Dist(aircraftPos)
c.Dist3KM = observerPos.Dist3(aircraftPos, a.Altitude)
}
}
示例6: Next
// Next processes the next item
func (x *example6) Next(c context.Context, counters mapper.Counters, key *datastore.Key) error {
// we need to load the entity ourselves
photo := new(Photo)
if err := nds.Get(c, key, photo); err != nil {
return err
}
photo.ID = key.IntID()
suffix := photo.Taken.Format("20060102")
_, err := x.bq.Tabledata.InsertAll(x.appID, "datasetName", "tableName", &bigquery.TableDataInsertAllRequest{
TemplateSuffix: suffix,
Rows: []*bigquery.TableDataInsertAllRequestRows{
{
Json: map[string]bigquery.JsonValue{
"id": photo.ID,
"taken": photo.Taken,
"photographer": map[string]bigquery.JsonValue{
"id": photo.Photographer.ID,
"name": photo.Photographer.Name,
},
},
},
},
}).Context(c).Do()
return err
}
示例7: createMemcacheKey
func createMemcacheKey(key *datastore.Key) string {
memcacheKey := memcachePrefix + key.Encode()
if len(memcacheKey) > memcacheMaxKeySize {
hash := sha1.Sum([]byte(memcacheKey))
memcacheKey = hex.EncodeToString(hash[:])
}
return memcacheKey
}
示例8: mapTask
func mapTask(c context.Context, ds appwrap.Datastore, baseUrl string, mr MapReducePipeline, taskKey *datastore.Key, w http.ResponseWriter, r *http.Request) {
var finalErr error
var shardNames map[string]int
var task JobTask
start := time.Now()
// we do this before starting the task below so that the parameters are set before
// the task status callback is invoked
jsonParameters := r.FormValue("json")
mr.SetMapParameters(jsonParameters)
mr.SetShardParameters(jsonParameters)
if t, err, retry := startTask(c, ds, mr, taskKey); err != nil && retry {
logCritical(c, "failed updating task to running: %s", err)
http.Error(w, err.Error(), 500) // this will run us again
return
} else if err != nil {
logCritical(c, "(fatal) failed updating task to running: %s", err)
http.Error(w, err.Error(), 200) // this will run us again
return
} else {
task = t
}
defer func() {
if r := recover(); r != nil {
stack := make([]byte, 16384)
bytes := runtime.Stack(stack, false)
logCritical(c, "panic inside of map task %s: %s\n%s\n", taskKey.Encode(), r, stack[0:bytes])
if err := retryTask(c, ds, mr, task.Job, taskKey); err != nil {
panic(fmt.Errorf("failed to retry task after panic: %s", err))
}
}
}()
if readerName := r.FormValue("reader"); readerName == "" {
finalErr = fmt.Errorf("reader parameter required")
} else if shardStr := r.FormValue("shards"); shardStr == "" {
finalErr = fmt.Errorf("shards parameter required")
} else if shardCount, err := strconv.ParseInt(shardStr, 10, 32); err != nil {
finalErr = fmt.Errorf("error parsing shard count: %s", err.Error())
} else if reader, err := mr.ReaderFromName(c, readerName); err != nil {
finalErr = fmt.Errorf("error making reader: %s", err)
} else {
shardNames, finalErr = mapperFunc(c, mr, reader, int(shardCount),
makeStatusUpdateFunc(c, ds, mr, fmt.Sprintf("%s/mapstatus", baseUrl), taskKey.Encode()))
}
if err := endTask(c, ds, mr, task.Job, taskKey, finalErr, shardNames); err != nil {
logCritical(c, "Could not finish task: %s", err)
http.Error(w, err.Error(), 500)
return
}
logInfo(c, "mapper done after %s", time.Now().Sub(start))
}
示例9: ktoi
func ktoi(key *datastore.Key) id {
return id{
kind: key.Kind(),
stringID: key.StringID(),
intID: key.IntID(),
appID: key.AppID(),
namespace: key.Namespace(),
}
}
示例10: fromCache
func fromCache(ctx context.Context, key *datastore.Key) (p *Passenger, err error) {
item, err := memcache.Get(ctx, key.Encode())
if err != nil {
return nil, err
}
p = new(Passenger)
err = gob.NewDecoder(bytes.NewReader(item.Value)).Decode(&p)
return
}
示例11: HasParent
// HasParent returns true if key or any of its parents equals
// parent (recursively), otherwise false.
func HasParent(parent, key *datastore.Key) bool {
for key != nil {
if key.Equal(parent) {
return true
}
key = key.Parent()
}
return false
}
示例12:
// Write takes a key and the corresponding writes it out to w after marshaling to JSON.
func (ƨ Submission) Write(w http.ResponseWriter, key *datastore.Key) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Write([]byte(`{"`))
//w.Write([]byte(strconv.FormatInt(key.IntID(), 10)))
w.Write([]byte(key.Encode()))
w.Write([]byte(`":`))
e := json.NewEncoder(w)
e.Encode(ƨ)
w.Write([]byte(`}`))
}
示例13: fromDatastore
func fromDatastore(ctx context.Context, key *datastore.Key) (p *Passenger, err error) {
p = new(Passenger)
if err = datastore.Get(ctx, key, p.AccessToken); err != nil {
return
}
if p.UserKey = key.Parent(); p.UserKey == nil {
return nil, ErrTokenNotAssociated
}
return
}
示例14: lookupCache
func lookupCache(ctx context.Context, key datastore.Key) *MaybeError {
maybeItem, err := memcache.Get(ctx, key.String())
var result = new(MaybeError)
if err != nil { // treat all errors as "cache miss"
*result = CacheMiss{}
} else {
result = jsonToTodoItem(maybeItem.Value)
}
return result
}
示例15: alertAdmins
func alertAdmins(c context.Context, key *datastore.Key, entity Lockable, reason string) error {
sender := "[email protected]" + appengine.AppID(c) + ".appspot.com"
msg := &mail.Message{
Sender: sender,
Subject: reason,
Body: fmt.Sprintf("key: %s, entity: %#v", key.String(), entity),
}
return mail.SendToAdmins(c, msg)
}