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 |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
| |
19 and is only meant to provide a loose ordering of commits for administrative | |
20 servlets to display.''' | |
21 def __init__(self, commit_id, datetime): | |
22 self.commit_id = commit_id | |
23 self.datetime = datetime | |
24 | |
25 | |
9 class CommitTracker(object): | 26 class CommitTracker(object): |
10 '''Utility class for managing and querying the storage of various named commit | 27 '''Utility class for managing and querying the storage of various named commit |
11 IDs.''' | 28 IDs.''' |
12 def __init__(self, object_store_creator): | 29 def __init__(self, object_store_creator): |
13 # The object store should never be created empty since the sole purpose of | 30 # The object stores should never be created empty since the sole purpose of |
14 # this tracker is to persist named commit data across requests. | 31 # this tracker is to persist named commit data across requests. |
15 self._store = object_store_creator.Create(CommitTracker, start_empty=False) | 32 self._store = object_store_creator.Create(CommitTracker, start_empty=False) |
33 self._history_store = object_store_creator.Create(CommitTracker, | |
34 category='history', start_empty=False) | |
16 | 35 |
17 def Get(self, key): | 36 def Get(self, key): |
18 return self._store.Get(key) | 37 return self._store.Get(key) |
19 | 38 |
20 def Set(self, key, commit): | 39 def Set(self, key, commit): |
21 return self._store.Set(key, commit) | 40 return (self._store.Set(key, commit) |
41 .Then(lambda _: self._UpdateHistory(key, commit))) | |
42 | |
43 def GetHistory(self, key): | |
44 '''Fetches the commit ID history for a named commit. If the commit has no | |
45 history, this will return an empty collection.''' | |
46 return (self._history_store.Get(key) | |
47 .Then(lambda history: () if history is None else history)) | |
48 | |
49 def _UpdateHistory(self, key, commit): | |
50 '''Appends a commit ID to a named commit's tracked history.''' | |
51 def create_or_amend_history(history): | |
52 if history is None: | |
53 history = collections.deque([], maxlen=50) | |
54 history.append(CachedCommit(commit, datetime.datetime.now())) | |
55 return self._history_store.Set(key, history) | |
56 return self._history_store.Get(key).Then(create_or_amend_history) | |
OLD | NEW |