| 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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 Loading... |
| 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) |
| OLD | NEW |