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) { | |
iannucci
2015/10/10 17:51:53
split this up so that when we're in the process lo
| |
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).Get(datastore.PropertyMap{ | |
43 "$key": {datastore.MkPropertyNI(fromRoot)}, | |
Vadim Sh.
2015/10/12 20:05:14
is it how hypothetical Exists(key) looks?
iannucci
2015/10/13 02:39:46
Yeah, it's kinda ugly. Do you think it's worth add
Vadim Sh.
2015/10/13 02:46:20
Yes.
We actually needed this in isolate server (
| |
44 }) | |
45 | |
46 muts, err := f(c) | |
47 if err != nil { | |
48 return err | |
49 } | |
50 | |
51 numNewMuts = uint(len(muts)) | |
52 shardSet, err = putMutations(c, fromRoot, muts) | |
53 | |
54 return err | |
55 }, nil) | |
56 if err != nil { | |
57 return nil, 0, err | |
58 } | |
59 | |
60 return shardSet, numNewMuts, nil | |
61 } | |
OLD | NEW |