Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / common / extensions / docs / server2 / commit_tracker.py
blob874c4b7bdfe8990ce311d8b297b72f51ed2c3dc7
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
3 # found in the LICENSE file.
5 import collections
6 import datetime
8 from object_store_creator import ObjectStoreCreator
9 from future import Future
12 # The maximum number of commit IDs to retain in a named commit's history deque.
13 _MAX_COMMIT_HISTORY_LENGTH = 50
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,
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
26 class CommitTracker(object):
27 '''Utility class for managing and querying the storage of various named commit
28 IDs.'''
29 def __init__(self, object_store_creator):
30 # The object stores should never be created empty since the sole purpose of
31 # this tracker is to persist named commit data across requests.
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)
36 def Get(self, key):
37 return self._store.Get(key)
39 def Set(self, key, commit):
40 return (self._store.Set(key, commit)
41 .Then(lambda _: self._UpdateHistory(key, commit)))
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))
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)