Index: chrome/common/extensions/docs/server2/commit_tracker.py |
diff --git a/chrome/common/extensions/docs/server2/commit_tracker.py b/chrome/common/extensions/docs/server2/commit_tracker.py |
index 53bb90c9cd6306cb043dfc417797af951ae42f28..874c4b7bdfe8990ce311d8b297b72f51ed2c3dc7 100644 |
--- a/chrome/common/extensions/docs/server2/commit_tracker.py |
+++ b/chrome/common/extensions/docs/server2/commit_tracker.py |
@@ -2,20 +2,55 @@ |
# Use of this source code is governed by a BSD-style license that can be |
# found in the LICENSE file. |
+import collections |
+import datetime |
+ |
from object_store_creator import ObjectStoreCreator |
from future import Future |
+# The maximum number of commit IDs to retain in a named commit's history deque. |
+_MAX_COMMIT_HISTORY_LENGTH = 50 |
+ |
+ |
+class CachedCommit(object): |
+ '''Object type which is stored for each entry in a named commit's history. |
+ |datetime| is used as a timestamp for when the commit cache was completed, |
not at google - send to devlin
2015/02/03 18:17:20
TBH I'd just construct this here rather than passi
Ken Rockot(use gerrit already)
2015/02/03 20:12:10
We want it to be this way so we can store the date
|
+ and is only meant to provide a loose ordering of commits for administrative |
+ servlets to display.''' |
+ def __init__(self, commit_id, datetime): |
+ self.commit_id = commit_id |
+ self.datetime = datetime |
+ |
+ |
class CommitTracker(object): |
'''Utility class for managing and querying the storage of various named commit |
IDs.''' |
def __init__(self, object_store_creator): |
- # The object store should never be created empty since the sole purpose of |
+ # The object stores should never be created empty since the sole purpose of |
# this tracker is to persist named commit data across requests. |
self._store = object_store_creator.Create(CommitTracker, start_empty=False) |
+ self._history_store = object_store_creator.Create(CommitTracker, |
+ category='history', start_empty=False) |
def Get(self, key): |
return self._store.Get(key) |
def Set(self, key, commit): |
- return self._store.Set(key, commit) |
+ return (self._store.Set(key, commit) |
+ .Then(lambda _: self._UpdateHistory(key, commit))) |
+ |
+ def GetHistory(self, key): |
+ '''Fetches the commit ID history for a named commit. If the commit has no |
+ history, this will return an empty collection.''' |
+ return (self._history_store.Get(key) |
+ .Then(lambda history: () if history is None else history)) |
+ |
+ def _UpdateHistory(self, key, commit): |
+ '''Appends a commit ID to a named commit's tracked history.''' |
+ def create_or_amend_history(history): |
+ if history is None: |
+ history = collections.deque([], maxlen=50) |
+ history.append(CachedCommit(commit, datetime.datetime.now())) |
+ return self._history_store.Set(key, history) |
+ return self._history_store.Get(key).Then(create_or_amend_history) |