Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 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 import collections | |
| 6 import datetime | |
| 7 | |
| 5 from object_store_creator import ObjectStoreCreator | 8 from object_store_creator import ObjectStoreCreator |
| 6 from future import Future | 9 from future import Future |
| 7 | 10 |
| 8 | 11 |
| 12 # The maximum number of commit IDs to retain in a named commit's history deque. | |
| 13 _MAX_COMMIT_HISTORY_LENGTH = 50 | |
| 14 | |
| 15 | |
| 16 class CachedCommit(object): | |
| 17 '''Object type which is stored for each entry in a named commit's history.''' | |
| 18 def __init__(self, commit_id, datetime): | |
| 19 self.commit_id = commit_id | |
| 20 self.datetime = datetime | |
|
not at google - send to devlin
2014/11/19 19:32:41
Maybe mention what datetime is used for? Because I
Ken Rockot(use gerrit already)
2015/02/02 23:57:44
Done. FOR DISPLAY PURPOSES ONLY
| |
| 21 | |
| 22 | |
| 9 class CommitTracker(object): | 23 class CommitTracker(object): |
| 10 '''Utility class for managing and querying the storage of various named commit | 24 '''Utility class for managing and querying the storage of various named commit |
| 11 IDs.''' | 25 IDs.''' |
| 12 def __init__(self, object_store_creator): | 26 def __init__(self, object_store_creator): |
| 13 # The object store should never be created empty since the sole purpose of | 27 # The object stores should never be created empty since the sole purpose of |
| 14 # this tracker is to persist named commit data across requests. | 28 # this tracker is to persist named commit data across requests. |
| 15 self._store = object_store_creator.Create(CommitTracker, start_empty=False) | 29 self._store = object_store_creator.Create(CommitTracker, start_empty=False) |
| 30 self._history_store = object_store_creator.Create(CommitTracker, | |
| 31 category='history', start_empty=False) | |
| 16 | 32 |
| 17 def Get(self, key): | 33 def Get(self, key): |
| 18 return self._store.Get(key) | 34 return self._store.Get(key) |
| 19 | 35 |
| 20 def Set(self, key, commit): | 36 def Set(self, key, commit): |
| 21 return self._store.Set(key, commit) | 37 return (self._store.Set(key, commit) |
| 38 .Then(lambda _: self._UpdateHistory(key, commit))) | |
| 39 | |
| 40 def GetHistory(self, key): | |
| 41 '''Fetches the commit ID history for a named commit. If the commit has no | |
| 42 history, this will return an empty collection.''' | |
| 43 return (self._history_store.Get(key) | |
| 44 .Then(lambda history: () if history is None else history)) | |
| 45 | |
| 46 def _UpdateHistory(self, key, commit): | |
| 47 '''Appends a commit ID to a named commit's tracked history.''' | |
| 48 def create_or_amend_history(history): | |
| 49 if history is None: | |
| 50 history = collections.deque([], maxlen=50) | |
|
not at google - send to devlin
2014/11/19 19:32:41
You're sure that .deque() pickles and unpickles pr
Ken Rockot(use gerrit already)
2015/02/02 23:57:44
Yar, deques support pickling
| |
| 51 history.append(CachedCommit(commit, datetime.datetime.now())) | |
| 52 return self._history_store.Set(key, history) | |
| 53 return self._history_store.Get(key).Then(create_or_amend_history) | |
| OLD | NEW |