Chromium Code Reviews
|
| OLD | NEW |
|---|---|
| (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_or_paths, version): | |
| 16 if isinstance(path_or_paths, list) or isinstance(path_or_paths, set): | |
|
not at google - send to devlin
2013/05/11 20:39:53
isinstance(path_or_paths, (tuple, list, set))
tha
方觉(Fang Jue)
2013/05/12 03:01:47
Done.
| |
| 17 return ['%s@%s' % (p, version) for p in path_or_paths] | |
| 18 return _MakeKey([path_or_paths], version)[0] | |
| 19 | |
| 20 def _ToObjectStoreValue(raw_value, version): | |
| 21 return {_MakeKey(key, version): raw_value[key] for key in raw_value} | |
| 22 | |
| 23 def _FromObjectStoreValue(raw_value, binary): | |
| 24 return {key[0:key.find('@')]: _HandleBinary(raw_value[key], binary) | |
|
not at google - send to devlin
2013/05/11 20:39:53
What happens if a patchset contains both binary an
方觉(Fang Jue)
2013/05/12 03:01:47
For this reason, CachingRietveldPatcher always rea
| |
| 25 for key in raw_value} | |
|
not at google - send to devlin
2013/05/11 20:39:53
btw @ is a legitimate character to appear in a fil
方觉(Fang Jue)
2013/05/12 03:01:47
Maybe use a character that's not legitimate in fil
| |
| 26 | |
| 27 def _HandleBinary(data, binary): | |
| 28 return data if binary else ToUnicode(data) | |
| 29 | |
| 30 class _AsyncUncachedFuture(object): | |
| 31 def __init__(self, | |
| 32 version, | |
| 33 paths, | |
| 34 binary, | |
| 35 cached_value, | |
| 36 missing_paths, | |
| 37 fetch_delegate, | |
| 38 object_store): | |
| 39 self._version = version | |
| 40 self._paths = paths | |
| 41 self._binary = binary | |
| 42 self._cached_value = cached_value | |
| 43 self._missing_paths = missing_paths | |
| 44 self._fetch_delegate = fetch_delegate | |
| 45 self._object_store = object_store | |
| 46 | |
| 47 def Get(self): | |
| 48 uncached_raw_value = self._fetch_delegate.Get() | |
| 49 self._object_store.SetMulti(_ToObjectStoreValue(uncached_raw_value, | |
| 50 self._version)) | |
| 51 | |
| 52 for path in self._missing_paths: | |
| 53 if uncached_raw_value.get(path) is None: | |
| 54 raise FileNotFoundError('File %s was not found in the patch.' % path) | |
| 55 self._cached_value[path] = _HandleBinary(uncached_raw_value[path], | |
| 56 self._binary) | |
| 57 | |
| 58 return self._cached_value | |
| 59 | |
| 60 class CachingRietveldPatcher(Patcher): | |
| 61 ''' CachingRietveldPatcher implements a caching layer on top of |patcher|. | |
| 62 In theory, it can be used with any class that implements Patcher. But this | |
| 63 class assumes that applying to all patched files one at a time is more | |
|
not at google - send to devlin
2013/05/11 20:39:53
s/one at a time/at once/
方觉(Fang Jue)
2013/05/12 03:01:47
Done.
| |
| 64 efficient than applying to individual files. | |
| 65 ''' | |
| 66 def __init__(self, | |
| 67 rietveld_patcher, | |
| 68 object_store_creator, | |
| 69 test_datetime=datetime): | |
| 70 self._patcher = rietveld_patcher | |
| 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._list_object_store.Get(key).Get() | |
|
not at google - send to devlin
2013/05/11 20:39:53
Re-using list_object_store here and in GetPatchedF
方觉(Fang Jue)
2013/05/12 03:01:47
Done.
| |
| 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._list_object_store.Set(key, | |
| 87 (version, self._datetime.now())) | |
| 88 return version | |
| 89 | |
| 90 def GetPatchedFiles(self, version = None): | |
|
not at google - send to devlin
2013/05/11 20:39:53
version=None
方觉(Fang Jue)
2013/05/12 03:01:47
Done.
方觉(Fang Jue)
2013/05/12 03:01:47
Done.
| |
| 91 if version is None: | |
| 92 version = self.GetVersion() | |
| 93 value = self._list_object_store.Get(version).Get() | |
| 94 if value is not None: | |
| 95 return value | |
| 96 | |
| 97 value = self._patcher.GetPatchedFiles(version) | |
|
not at google - send to devlin
2013/05/11 20:39:53
more descriptive name than "value" pls
方觉(Fang Jue)
2013/05/12 03:01:47
Done.
| |
| 98 self._list_object_store.Set(version, value) | |
| 99 return value | |
| 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(paths, version)).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 return _AsyncUncachedFuture(version, | |
| 112 paths, | |
| 113 binary, | |
| 114 cached_value, | |
| 115 missing_paths, | |
| 116 self._patcher.Apply(set(added) | set(modified), | |
| 117 None, | |
| 118 True, | |
| 119 version), | |
|
not at google - send to devlin
2013/05/11 20:39:53
why is binary explicitly True?
方觉(Fang Jue)
2013/05/12 03:01:47
See the comment above. Maybe I should add a commen
| |
| 120 self._file_object_store) | |
| OLD | NEW |