Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(35)

Side by Side Diff: appengine/findit/model/versioned_model.py

Issue 1866883002: [Findit] A huge refactoring and some bug fixing. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Fix nit. Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « appengine/findit/model/test/wf_try_job_test.py ('k') | appengine/findit/model/wf_analysis.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2015 The Chromium Authors. All rights reserved. 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 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 """Provides a model to support versioned entities in datastore. 5 """Provides a model to support versioned entities in datastore.
6 6
7 Idea: use a root model entity to keep track of the most recent version of a 7 Idea: use a root model entity to keep track of the most recent version of a
8 versioned entity, and make the versioned entities and the root model entity in 8 versioned entity, and make the versioned entities and the root model entity in
9 the same entity group so that they could be read and written in a transaction. 9 the same entity group so that they could be read and written in a transaction.
10 """ 10 """
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 if version is None: 47 if version is None:
48 version = root.current 48 version = root.current
49 elif version < 1: 49 elif version < 1:
50 # Return None for versions < 1, which causes exceptions in ndb.Key() 50 # Return None for versions < 1, which causes exceptions in ndb.Key()
51 return None 51 return None
52 52
53 return ndb.Key(cls, version, parent=root_key).get() 53 return ndb.Key(cls, version, parent=root_key).get()
54 54
55 @classmethod 55 @classmethod
56 def GetLatestVersionNumber(cls): 56 def GetLatestVersionNumber(cls):
57 return cls._GetRootKey().get().current 57 root_entity = cls._GetRootKey().get()
58 if not root_entity:
59 return -1
60 return root_entity.current
58 61
59 def Save(self): 62 def Save(self):
60 """Saves the current entity, but as a new version.""" 63 """Saves the current entity, but as a new version."""
61 root_key = self._GetRootKey() 64 root_key = self._GetRootKey()
62 root = root_key.get() or self._GetRootModel()(key=root_key) 65 root = root_key.get() or self._GetRootModel()(key=root_key)
63 66
64 def SaveData(): 67 def SaveData():
65 if self.key.get(): 68 if self.key.get():
66 return False # The entity exists, should retry. 69 return False # The entity exists, should retry.
67 ndb.put_multi([self, root]) 70 ndb.put_multi([self, root])
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 106
104 @classmethod 107 @classmethod
105 def _get_kind(cls): 108 def _get_kind(cls):
106 return root_model_name 109 return root_model_name
107 110
108 return _RootModel 111 return _RootModel
109 112
110 @classmethod 113 @classmethod
111 def _GetRootKey(cls): 114 def _GetRootKey(cls):
112 return ndb.Key(cls._GetRootModel(), 1) 115 return ndb.Key(cls._GetRootModel(), 1)
OLDNEW
« no previous file with comments | « appengine/findit/model/test/wf_try_job_test.py ('k') | appengine/findit/model/wf_analysis.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698