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

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

Issue 10878056: Extensions Docs Server: Fix samples page and ExampleZipper (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: test + fixes Created 8 years, 3 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 | Annotate | Revision Log
OLDNEW
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
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
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)
89 uncached.append(path) 95 uncached.append(path)
90 continue 96 continue
91 result[path] = data 97 result[path] = data
92 98
93 if not uncached: 99 if not uncached:
94 return Future(value=result) 100 return Future(value=result)
95 return Future(delegate=_AsyncUncachedFuture( 101 return Future(delegate=_AsyncUncachedFuture(
96 self._file_system.Read(uncached, binary=binary), 102 self._file_system.Read(uncached, binary=binary),
97 result, 103 result,
98 self, 104 self,
99 self._object_store)) 105 self._object_store,
106 namespace))
OLDNEW
« no previous file with comments | « chrome/common/extensions/docs/server2/handler.py ('k') | chrome/common/extensions/docs/server2/samples_data_source.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698