OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package tumble |
| 6 |
| 7 import ( |
| 8 "fmt" |
| 9 |
| 10 "github.com/luci/gae/filter/txnBuf" |
| 11 "github.com/luci/gae/service/datastore" |
| 12 "golang.org/x/net/context" |
| 13 ) |
| 14 |
| 15 // EnterTransaction is the method to use when doing transactional operations |
| 16 // in the tumble ecosystem. It allows work to be done in the entity group |
| 17 // specified by `fromRoot`, and any returned Mutation objects will be |
| 18 // transactionally queued for the tumble backend. |
| 19 // |
| 20 // Usually this is called from your application's handlers to begin a tumble |
| 21 // state machine as a result of some API interaction. |
| 22 func EnterTransaction(c context.Context, fromRoot *datastore.Key, f func(context
.Context) ([]Mutation, error)) error { |
| 23 shardSet, _, err := enterTransactionInternal(txnBuf.FilterRDS(c), fromRo
ot, f) |
| 24 if err != nil { |
| 25 return err |
| 26 } |
| 27 fireTasks(c, shardSet) |
| 28 return nil |
| 29 } |
| 30 |
| 31 func enterTransactionInternal(c context.Context, fromRoot *datastore.Key, f func
(context.Context) ([]Mutation, error)) (map[uint64]struct{}, uint, error) { |
| 32 if fromRoot == nil { |
| 33 return nil, 0, fmt.Errorf("tumble: Passing nil as fromRoot is il
legal") |
| 34 } |
| 35 |
| 36 shardSet := map[uint64]struct{}(nil) |
| 37 numNewMuts := uint(0) |
| 38 |
| 39 err := datastore.Get(c).RunInTransaction(func(c context.Context) error { |
| 40 // do a Get on the fromRoot to ensure that this transaction is a
ssociated |
| 41 // with that entity group. |
| 42 _, _ = datastore.Get(c).Exists(fromRoot) |
| 43 |
| 44 muts, err := f(c) |
| 45 if err != nil { |
| 46 return err |
| 47 } |
| 48 |
| 49 numNewMuts = uint(len(muts)) |
| 50 shardSet, err = putMutations(c, fromRoot, muts) |
| 51 |
| 52 return err |
| 53 }, nil) |
| 54 if err != nil { |
| 55 return nil, 0, err |
| 56 } |
| 57 |
| 58 return shardSet, numNewMuts, nil |
| 59 } |
OLD | NEW |