Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1095)

Side by Side Diff: chrome/common/extensions/docs/server2/caching_rietveld_patcher.py

Issue 14125010: Docserver: Add support for viewing docs with a codereview patch applied (Closed) Base URL: https://src.chromium.org/svn/trunk/src/
Patch Set: add executable bits for tests Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright 2013 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.
4
5 from datetime import datetime, timedelta
6 from file_system import FileNotFoundError, ToUnicode
7 from future import Future
8 from patcher import Patcher
9
10 _VERSION_CACHE_MAXAGE = timedelta(seconds=5)
11
12 ''' Append @version for keys to distinguish between different patchsets of
13 an issue.
14 '''
15 def _MakeKey(path, version):
16 return '%s@%s' % (path, version)
17
18 def _ToObjectStoreValue(raw_value, version):
19 return {_MakeKey(key, version): raw_value[key] for key in raw_value}
20
21 def _FromObjectStoreValue(raw_value, binary):
22 return {key[0:key.rfind('@')]: _HandleBinary(raw_value[key], binary)
23 for key in raw_value}
24
25 def _HandleBinary(data, binary):
26 return data if binary else ToUnicode(data)
27
28 class _AsyncUncachedFuture(object):
29 def __init__(self,
30 version,
31 paths,
32 binary,
33 cached_value,
34 missing_paths,
35 fetch_delegate,
36 object_store):
37 self._version = version
38 self._paths = paths
39 self._binary = binary
40 self._cached_value = cached_value
41 self._missing_paths = missing_paths
42 self._fetch_delegate = fetch_delegate
43 self._object_store = object_store
44
45 def Get(self):
46 uncached_raw_value = self._fetch_delegate.Get()
47 self._object_store.SetMulti(_ToObjectStoreValue(uncached_raw_value,
48 self._version))
49
50 for path in self._missing_paths:
51 if uncached_raw_value.get(path) is None:
52 raise FileNotFoundError('File %s was not found in the patch.' % path)
53 self._cached_value[path] = _HandleBinary(uncached_raw_value[path],
54 self._binary)
55
56 return self._cached_value
57
58 class CachingRietveldPatcher(Patcher):
59 ''' CachingRietveldPatcher implements a caching layer on top of |patcher|.
60 In theory, it can be used with any class that implements Patcher. But this
61 class assumes that applying to all patched files at once is more efficient
62 than applying to individual files.
63 '''
64 def __init__(self,
65 rietveld_patcher,
66 object_store_creator,
67 test_datetime=datetime):
68 self._patcher = rietveld_patcher
69 self._version_object_store = object_store_creator.Create(
70 CachingRietveldPatcher, category='version')
71 self._list_object_store = object_store_creator.Create(
72 CachingRietveldPatcher, category='list')
73 self._file_object_store = object_store_creator.Create(
74 CachingRietveldPatcher, category='file')
75 self._datetime = test_datetime
76
77 def GetVersion(self):
78 key = 'version'
79 value = self._version_object_store.Get(key).Get()
80 if value is not None:
81 version, time = value
82 if self._datetime.now() - time < _VERSION_CACHE_MAXAGE:
83 return version
84
85 version = self._patcher.GetVersion()
86 self._version_object_store.Set(key,
87 (version, self._datetime.now()))
88 return version
89
90 def GetPatchedFiles(self, version=None):
91 if version is None:
92 version = self.GetVersion()
93 patched_files = self._list_object_store.Get(version).Get()
94 if patched_files is not None:
95 return patched_files
96
97 patched_files = self._patcher.GetPatchedFiles(version)
98 self._list_object_store.Set(version, patched_files)
99 return patched_files
100
101 def Apply(self, paths, file_system, binary=False, version=None):
102 if version is None:
103 version = self.GetVersion()
104 added, deleted, modified = self.GetPatchedFiles(version)
105 cached_value = _FromObjectStoreValue(self._file_object_store.
106 GetMulti([_MakeKey(path, version) for path in paths]).Get(), binary)
107 missing_paths = list(set(paths) - set(cached_value.keys()))
108 if len(missing_paths) == 0:
109 return Future(value=cached_value)
110
111 # binary is explicitly set to True. Here we are applying the patch to
112 # ALL patched files without a way to know whether individual files are
113 # binary or not. Therefore all data cached must be binary. When reading
114 # from the cache with binary=False, it will be converted to Unicode by
115 # _HandleBinary.
116 return _AsyncUncachedFuture(version,
117 paths,
118 binary,
119 cached_value,
120 missing_paths,
121 self._patcher.Apply(set(added) | set(modified),
122 None,
123 True,
124 version),
125 self._file_object_store)
OLDNEW
« no previous file with comments | « chrome/common/extensions/docs/server2/app.yaml ('k') | chrome/common/extensions/docs/server2/caching_rietveld_patcher_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698