Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 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 from file_system import FileSystem, StatInfo, FileNotFoundError | 5 from file_system import FileSystem, StatInfo, FileNotFoundError |
| 6 from future import Future | 6 from future import Future |
| 7 import object_store | 7 import object_store |
| 8 | 8 |
| 9 class _AsyncUncachedFuture(object): | 9 class _AsyncUncachedFuture(object): |
| 10 def __init__(self, uncached, current_result, file_system, object_store): | 10 def __init__(self, |
| 11 uncached, | |
| 12 current_result, | |
| 13 file_system, | |
| 14 object_store, | |
| 15 namespace): | |
| 11 self._uncached = uncached | 16 self._uncached = uncached |
| 12 self._current_result = current_result | 17 self._current_result = current_result |
| 13 self._file_system = file_system | 18 self._file_system = file_system |
| 14 self._object_store = object_store | 19 self._object_store = object_store |
| 20 self._namespace = namespace | |
| 15 | 21 |
| 16 def Get(self): | 22 def Get(self): |
| 17 mapping = {} | 23 mapping = {} |
| 18 new_items = self._uncached.Get() | 24 new_items = self._uncached.Get() |
| 19 for item in new_items: | 25 for item in new_items: |
| 20 version = self._file_system.Stat(item).version | 26 version = self._file_system.Stat(item).version |
| 21 mapping[item] = (new_items[item], version) | 27 mapping[item] = (new_items[item], version) |
| 22 self._current_result[item] = new_items[item] | 28 self._current_result[item] = new_items[item] |
| 23 self._object_store.SetMulti(mapping, object_store.FILE_SYSTEM_READ, time=0) | 29 self._object_store.SetMulti(mapping, self._namespace, time=0) |
| 24 return self._current_result | 30 return self._current_result |
| 25 | 31 |
| 26 class MemcacheFileSystem(FileSystem): | 32 class MemcacheFileSystem(FileSystem): |
| 27 """FileSystem implementation which memcaches the results of Read. | 33 """FileSystem implementation which memcaches the results of Read. |
| 28 """ | 34 """ |
| 29 def __init__(self, file_system, object_store): | 35 def __init__(self, file_system, object_store): |
| 30 self._file_system = file_system | 36 self._file_system = file_system |
| 31 self._object_store = object_store | 37 self._object_store = object_store |
| 32 | 38 |
| 33 def Stat(self, path, stats=None): | 39 def Stat(self, path, stats=None): |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 60 mapping[child_path] = child_version | 66 mapping[child_path] = child_version |
| 61 self._object_store.SetMulti(mapping, object_store.FILE_SYSTEM_STAT) | 67 self._object_store.SetMulti(mapping, object_store.FILE_SYSTEM_STAT) |
| 62 return StatInfo(version) | 68 return StatInfo(version) |
| 63 | 69 |
| 64 def Read(self, paths, binary=False): | 70 def Read(self, paths, binary=False): |
| 65 """Reads a list of files. If a file is in memcache and it is not out of | 71 """Reads a list of files. If a file is in memcache and it is not out of |
| 66 date, it is returned. Otherwise, the file is retrieved from the file system. | 72 date, it is returned. Otherwise, the file is retrieved from the file system. |
| 67 """ | 73 """ |
| 68 result = {} | 74 result = {} |
| 69 uncached = [] | 75 uncached = [] |
| 70 results = self._object_store.GetMulti(paths, | 76 namespace = object_store.FILE_SYSTEM_READ |
| 71 object_store.FILE_SYSTEM_READ, | 77 if binary: |
| 72 time=0).Get() | 78 namespace = '%s.binary' % namespace |
|
not at google - send to devlin
2012/08/27 04:31:48
is this actually a problem? do we ever read the sa
cduvall
2012/08/27 18:04:34
Yeah we do, this is why I was getting an error whe
not at google - send to devlin
2012/08/28 01:39:09
Ok. I would prefer to see a solution which just ca
| |
| 79 results = self._object_store.GetMulti(paths, namespace, time=0).Get() | |
| 73 result_values = [x[1] for x in sorted(results.iteritems())] | 80 result_values = [x[1] for x in sorted(results.iteritems())] |
| 74 stats = self._object_store.GetMulti(paths, | 81 stats = self._object_store.GetMulti(paths, |
| 75 object_store.FILE_SYSTEM_STAT).Get() | 82 object_store.FILE_SYSTEM_STAT).Get() |
| 76 stat_values = [x[1] for x in sorted(stats.iteritems())] | 83 stat_values = [x[1] for x in sorted(stats.iteritems())] |
| 77 for path, cached_result, stat in zip(sorted(paths), | 84 for path, cached_result, stat in zip(sorted(paths), |
| 78 result_values, | 85 result_values, |
| 79 stat_values): | 86 stat_values): |
| 80 if cached_result is None: | 87 if cached_result is None: |
| 81 uncached.append(path) | 88 uncached.append(path) |
| 82 continue | 89 continue |
| 83 data, version = cached_result | 90 data, version = cached_result |
| 84 # TODO(cduvall): Make this use a multi stat. | 91 # TODO(cduvall): Make this use a multi stat. |
| 85 if stat is None: | 92 if stat is None: |
| 86 stat = self.Stat(path).version | 93 stat = self.Stat(path).version |
| 87 if stat != version: | 94 if stat != version: |
| 88 self._object_store.Delete(path, object_store.FILE_SYSTEM_READ) | 95 self._object_store.Delete(path, namespace) |
|
not at google - send to devlin
2012/08/27 04:31:48
Is it necessary to delete this? It will be overrid
cduvall
2012/08/27 18:04:34
Done.
| |
| 89 uncached.append(path) | 96 uncached.append(path) |
| 90 continue | 97 continue |
| 91 result[path] = data | 98 result[path] = data |
| 92 | 99 |
| 93 if not uncached: | 100 if not uncached: |
| 94 return Future(value=result) | 101 return Future(value=result) |
| 95 return Future(delegate=_AsyncUncachedFuture( | 102 return Future(delegate=_AsyncUncachedFuture( |
| 96 self._file_system.Read(uncached, binary=binary), | 103 self._file_system.Read(uncached, binary=binary), |
| 97 result, | 104 result, |
| 98 self, | 105 self, |
| 99 self._object_store)) | 106 self._object_store, |
| 107 namespace)) | |
| OLD | NEW |