| OLD | NEW |
| 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 19 matching lines...) Expand all Loading... |
| 30 versioned entity, use Create(key) with optional key to differentiate between | 30 versioned entity, use Create(key) with optional key to differentiate between |
| 31 multiple unique entities of the same subclass. Use GetVersion() to read and | 31 multiple unique entities of the same subclass. Use GetVersion() to read and |
| 32 Save() to write. | 32 Save() to write. |
| 33 """ | 33 """ |
| 34 | 34 |
| 35 @property | 35 @property |
| 36 def _root_id(self): | 36 def _root_id(self): |
| 37 return self.key.pairs()[0][1] if self.key else None | 37 return self.key.pairs()[0][1] if self.key else None |
| 38 | 38 |
| 39 @property | 39 @property |
| 40 def version(self): | 40 def version_number(self): |
| 41 # Ndb treats key.integer_id() of 0 as None, so default to 0. | 41 # Ndb treats key.integer_id() of 0 as None, so default to 0. |
| 42 return self.key.integer_id() or 0 if self.key else 0 | 42 return self.key.integer_id() or 0 if self.key else 0 |
| 43 | 43 |
| 44 @classmethod | 44 @classmethod |
| 45 def Create(cls, key=None): | 45 def Create(cls, key=None): |
| 46 """Creates an instance of cls that is to become the first version. | 46 """Creates an instance of cls that is to become the first version. |
| 47 | 47 |
| 48 The calling function of Create() should be responsible first for checking | 48 The calling function of Create() should be responsible first for checking |
| 49 no previous version of the proposed entity already exists. | 49 no previous version of the proposed entity already exists. |
| 50 | 50 |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 | 155 |
| 156 @classmethod | 156 @classmethod |
| 157 def _get_kind(cls): | 157 def _get_kind(cls): |
| 158 return root_model_name | 158 return root_model_name |
| 159 | 159 |
| 160 return _RootModel | 160 return _RootModel |
| 161 | 161 |
| 162 @classmethod | 162 @classmethod |
| 163 def _GetRootKey(cls, key=None): | 163 def _GetRootKey(cls, key=None): |
| 164 return ndb.Key(cls._GetRootModel(), key if key is not None else 1) | 164 return ndb.Key(cls._GetRootModel(), key if key is not None else 1) |
| OLD | NEW |