本文整理汇总了Golang中appengine/datastore.RunInTransaction函数的典型用法代码示例。如果您正苦于以下问题:Golang RunInTransaction函数的具体用法?Golang RunInTransaction怎么用?Golang RunInTransaction使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了RunInTransaction函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Increment
// Increment increments the named counter.
func Increment(c appengine.Context, name string) error {
// Get counter config.
var cfg counterConfig
ckey := datastore.NewKey(c, configKind, name, 0, nil)
err := datastore.RunInTransaction(c, func(c appengine.Context) error {
err := datastore.Get(c, ckey, &cfg)
if err == datastore.ErrNoSuchEntity {
cfg.Shards = defaultShards
_, err = datastore.Put(c, ckey, &cfg)
}
return err
}, nil)
if err != nil {
return err
}
err = datastore.RunInTransaction(c, func(c appengine.Context) error {
shardName := fmt.Sprintf("%v%d", name, rand.Intn(cfg.Shards))
key := datastore.NewKey(c, shardKind, shardName, 0, nil)
var s shard
err := datastore.Get(c, key, &s)
// A missing entity and a present entity will both work.
if err != nil && err != datastore.ErrNoSuchEntity {
return err
}
s.Count++
s.Name = name
_, err = datastore.Put(c, key, &s)
return err
}, nil)
if err != nil {
return err
}
//memcache.Increment(c, memcacheKey(name), 1, 0)
return nil
}
示例2: Increment
// Increment increments the named counter.
func Increment(c appengine.Context, valName string) error {
// Get counter config.
wNumShards := dsu.WrapInt{}
dsu.McacheGet(c, mCKNumShards(valName), &wNumShards)
if wNumShards.I < 1 {
ckey := datastore.NewKey(c, dsKindNumShards, mCKNumShards(valName), 0, nil)
errTx := datastore.RunInTransaction(c,
func(c appengine.Context) error {
err := datastore.Get(c, ckey, &wNumShards)
if err == datastore.ErrNoSuchEntity {
wNumShards.I = defaultNumShards
_, err = datastore.Put(c, ckey, &wNumShards)
}
return err
}, nil)
if errTx != nil {
return errTx
}
dsu.McacheSet(c, mCKNumShards(valName), dsu.WrapInt{wNumShards.I})
}
// pick random counter and increment it
errTx := datastore.RunInTransaction(c,
func(c appengine.Context) error {
shardId := rand.Intn(wNumShards.I)
dsKey := datastore.NewKey(c, dsKindShard, dSKSingleShard(valName, shardId), 0, nil)
var sd WrapShardData
err := datastore.Get(c, dsKey, &sd)
// A missing entity and a present entity will both work.
if err != nil && err != datastore.ErrNoSuchEntity {
return err
}
sd.Name = valName
sd.ShardId = shardId
sd.I++
_, err = datastore.Put(c, dsKey, &sd)
c.Infof("ds put %v %v", dsKey, sd)
return err
}, nil)
if errTx != nil {
return errTx
}
memcache.Increment(c, mCKValue(valName), 1, 0)
// collect number of updates
// per valName per instance in memory
// for every interval of 10 minutes
//
// a batch job checks if the number of shards should be increased or decreased
// and truncates this map
updateSamplingFrequency[valName+util.TimeMarker()[:len("2006-01-02 15:0")]] += 1
return nil
}
示例3: commitHandler
// commitHandler retrieves commit data or records a new commit.
//
// For GET requests it returns a Commit value for the specified
// packagePath and hash.
//
// For POST requests it reads a JSON-encoded Commit value from the request
// body and creates a new Commit entity. It also updates the "tip" Tag for
// each new commit at tip.
//
// This handler is used by a gobuilder process in -commit mode.
func commitHandler(r *http.Request) (interface{}, error) {
c := appengine.NewContext(r)
com := new(Commit)
if r.Method == "GET" {
com.PackagePath = r.FormValue("packagePath")
com.Hash = r.FormValue("hash")
if err := datastore.Get(c, com.Key(c), com); err != nil {
return nil, fmt.Errorf("getting Commit: %v", err)
}
return com, nil
}
if r.Method != "POST" {
return nil, errBadMethod(r.Method)
}
// POST request
defer r.Body.Close()
if err := json.NewDecoder(r.Body).Decode(com); err != nil {
return nil, fmt.Errorf("decoding Body: %v", err)
}
if len(com.Desc) > maxDatastoreStringLen {
com.Desc = com.Desc[:maxDatastoreStringLen]
}
if err := com.Valid(); err != nil {
return nil, fmt.Errorf("validating Commit: %v", err)
}
defer cache.Tick(c)
tx := func(c appengine.Context) error {
return addCommit(c, com)
}
return nil, datastore.RunInTransaction(c, tx, nil)
}
示例4: importFile
// method to import from JSON read with the file reader
func importFile(c *context, file io.Reader) {
// run in a transaction so that each datastore write doesn't redo the index,
// rather all the indecies get updated at once when we're done
datastore.RunInTransaction(c.c, func(tc appengine.Context) error {
// decode the data
decoder := json.NewDecoder(file)
data := backup{}
err := decoder.Decode(&data)
check(err)
// setup a worker to do the work
worker := &importer{
context: *c,
jsonData: data,
fixUpKeys: make(map[string]*datastore.Key),
allTags: make(map[string]map[string]bool),
newTags: make([]interface{}, 0, 100),
newTagKeys: make([]*datastore.Key, 0, 100),
dirtyCacheEntries: make([]string, 0, 1000),
}
worker.c = tc
// kick off the import
worker.doImport()
return nil
}, nil)
}
示例5: SetPlayerName
func SetPlayerName(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
seasonName := r.FormValue("SeasonName")
seasonYear := r.FormValue("SeasonYear")
playerId := r.FormValue("PlayerId")
newPlayerName := r.FormValue("NewPlayerName")
c.Infof("Replaceing '%v' with '%v' for season '%v' '%v'", playerId, newPlayerName, seasonName, seasonYear)
var season *model.Season
if seasonName == "" || seasonYear == "" {
c.Infof("Lookup season")
tmpSeason := api.GetActiveSeasonWithContext(c)
season = &tmpSeason
} else {
season = api.LoadSeasonByNameYear(c, seasonName, seasonYear)
}
players := season.GetPlayers(c)
targetIndex := playerIndex(players, playerId)
replacePlayer := players[targetIndex]
replacePlayer.Name = newPlayerName
season.Schedule = []byte(strings.Replace(string(season.Schedule), playerId, newPlayerName, -1))
season.Conferences = []byte(strings.Replace(string(season.Conferences), playerId, newPlayerName, -1))
pkey := model.PlayerKey(c, season.Name, season.Year, newPlayerName)
season.Players[targetIndex] = pkey
err := datastore.RunInTransaction(c, func(c appengine.Context) error {
model.SavePlayer(c, season, replacePlayer)
err := model.SaveSeason(c, *season)
return err
}, nil)
if err != nil {
panic(err)
}
}
示例6: RetireTransaction
// Transactions in phase FINISHED will cause the price to be credited on the seller's
// account, and the fee to be deducted.
// All other phases will lead to price and fee being reimbursed to the buyer.
// Returns ErrTransactionTooYoung if the transaction has not passed its timout at the
// time of the call.
// Returns ErrTransactionAlreadyRetired if the transaction has already been retired at
// the time of the call.
func RetireTransaction(c appengine.Context, key *datastore.Key) error {
f := func(c appengine.Context) error {
now := time.Now()
dao := NewGaeAccountingDao(c, true)
var tx Transaction
if err := datastore.Get(c, key, txCodec{&tx}); err != nil {
return err
}
if err := tx.Retire(dao, key.Encode(), now); err == ErrTooYoung {
return ErrTransactionTooYoung
} else if err == ErrAlreadyRetired {
return ErrTransactionAlreadyRetired
} else if err != nil {
return err
}
if _, err := datastore.Put(c, key, txCodec{&tx}); err != nil {
return err
}
return dao.Flush()
}
return datastore.RunInTransaction(c, f, &datastore.TransactionOptions{XG: true})
}
示例7: RetireBid
// This will reimburse the bid's price and fee to the buyer.
func RetireBid(c appengine.Context, key *datastore.Key) error {
f := func(c appengine.Context) error {
now := time.Now()
dao := NewGaeAccountingDao(c, true)
var bid Bid
if err := datastore.Get(c, key, bidCodec{&bid}); err != nil {
return err
}
if bid.State == Matched {
c.Infof("Not retiring matched bid %v", key)
return nil
}
if err := bid.Retire(dao, key.Encode(), now); err != nil {
return err
}
if _, err := datastore.Put(c, key, bidCodec{&bid}); err != nil {
return err
}
return dao.Flush()
}
if err := datastore.RunInTransaction(c, f, &datastore.TransactionOptions{XG: true}); err != nil {
return err
}
return nil
}
示例8: refresh
// RefreshFeed fetches and updates a feed from the remote source,
// stores it's info and articles in the datastore, and removes old articles
// (those not retrieved on the latest fetch).
func (f *FeedInfo) refresh(c appengine.Context) error {
fnew, articles, fetchErr := f.readSource(c)
err := datastore.RunInTransaction(c, func(c appengine.Context) error {
var stored FeedInfo
key := datastore.NewKey(c, feedKind, f.Url, 0, nil)
err := fixMissingFieldError(datastore.Get(c, key, &stored))
if err != nil && err != datastore.ErrNoSuchEntity {
return err
}
if fetchErr != nil {
*f = stored
f.LastFetch = time.Now()
} else {
*f = fnew
}
f.Refs = stored.Refs
_, err = datastore.Put(c, key, f)
return err
}, nil)
if fetchErr != nil {
return fetchErr
}
if err != nil {
return err
}
return f.updateArticles(c, articles)
}
示例9: sit
func sit(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
u := user.Current(c)
id := r.FormValue("id")
g, err := poker.LoadGame(id, r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
err = datastore.RunInTransaction(c, func(c appengine.Context) error {
name := r.FormValue("name")
if name == "" {
return errors.New("Please choose a name")
}
return g.Sit(u.Email, name)
}, nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err = g.Save(r); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = broadcastState(c, g)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
示例10: addAtom
func addAtom(c appengine.Context, url string) error {
feedRoot := datastore.NewKey(c, "feedRoot", "feedRoot", 0, nil)
fk := datastore.NewKey(c, "feed", url, 0, feedRoot)
err := datastore.RunInTransaction(c, func(c appengine.Context) error {
done, err := exists(c, fk)
if err != nil {
return err
}
if !done {
f, err := fetchAtom(c, url)
if err != nil {
return err
}
_, err = datastore.Put(c, fk, f)
if err != nil {
return err
}
err = f.update(c, fk)
if err != nil {
return err
}
return nil
}
return nil
}, nil)
if err != nil {
return err
}
return subscribe(c, fk, true)
}
示例11: ChangeLounge
// TODO(dlluncor): Merge this with ChangeGame as they are the same except for the key
// and the type of the changeEntityFunc, and the defaultLounge() thing.
// Utility function for reading from and updating a game before then
// doing further processing.
func ChangeLounge(c appengine.Context, loungeId string, clf changeLoungeFunc) *MyLounge {
// Store all tables as part of the game state and send
// a "startTimer" response.
loungeKey := loungeId
l := defaultLounge()
err := datastore.RunInTransaction(c, func(c appengine.Context) error {
k := datastore.NewKey(c, "WrLounge", loungeKey, 0, nil)
if err := datastore.Get(c, k, l); err != nil {
return err
}
// Perform special logic here to manipulate game.
shouldRunUpdate := clf(l)
if !shouldRunUpdate {
// Sometimes we don't need to update the database so don't.
return nil
}
if _, err := datastore.Put(c, k, l); err != nil {
return err
}
return nil
}, nil)
if err != nil {
c.Errorf("Err in db transaction %v", err)
}
return l
}
示例12: createLounge
func createLounge(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
queryMap := r.URL.Query()
loungeName := queryMap.Get("l")
gamesStr := queryMap.Get("g")
games := strings.Split(gamesStr, ",")
l := defaultLounge()
l.Name = loungeName
l.Games = games
addLoungeNameDb(c, loungeName)
// Create the lounges.
err := datastore.RunInTransaction(c, func(c appengine.Context) error {
k := datastore.NewKey(c, "WrLounge", loungeName, 0, nil)
if _, err := datastore.Put(c, k, l); err != nil {
return err
}
return nil
}, nil)
if err != nil {
fmt.Fprintf(w, "Error creating a lounge: %v", err)
} else {
fmt.Fprintf(w, "Success in creating lounge: %v with games: %s", loungeName, games)
}
// Create the games as well.
lang := queryMap.Get("lang")
for _, tableKey := range games {
g := defaultGame()
g.Language = lang
createGame(c, tableKey, g)
}
}
示例13: tx
func tx(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
// start tx OMIT
key := datastore.NewKey(c, "Book", "book1", 0, nil) // HL
if err := datastore.RunInTransaction(c, func(c appengine.Context) error { // HL
var book Book
if err := datastore.Get(c, key, &book); err != nil { // HL
return err
}
book.Price += 200
if _, err := datastore.Put(c, key, &book); err != nil { // HL
return err
}
return nil
}, nil); err != nil { // HL
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// end tx OMIT
w.Write([]byte("success"))
}
示例14: Claim
// Claim attempts to uniquely associate the user and email.
func (e *ClaimedEmail) Claim(c appengine.Context) error {
err := datastore.RunInTransaction(c, func(c appengine.Context) error {
key := e.key(c)
old := &ClaimedEmail{}
lookupErr := datastore.Get(c, key, old)
switch {
case lookupErr == nil:
return ErrEmailAlreadyClaimed
case lookupErr == datastore.ErrNoSuchEntity:
// Didn't find old claim: all is well.
break
default:
return lookupErr
}
if _, storeErr := datastore.Put(c, key, e); storeErr != nil {
return storeErr
}
return nil
}, nil)
if err != nil {
return err
}
return nil
}
示例15: RewriteID
// RewriteID transactionally rewrites the Account under the
// correct (i.e., obfuscated) key.
func (a *Account) RewriteID(c appengine.Context, u *user.User) error {
var err error
a.ID, err = ID(u)
if err != nil {
return fmt.Errorf("couldn't create ID for %v", u)
}
var txnErr error
for i := 0; i < 10; i++ {
txnErr = datastore.RunInTransaction(c, func(c appengine.Context) error {
if err := a.Put(c); err != nil {
return err
}
oldKey := datastore.NewKey(c, "UserAccount", u.ID, 0, nil)
if err := datastore.Delete(c, oldKey); err != nil {
return err
}
return nil
}, &datastore.TransactionOptions{XG: true})
if txnErr != datastore.ErrConcurrentTransaction {
break
}
}
if txnErr != nil {
return txnErr
}
return nil
}