Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 appengine_wrappers import GetAppVersion | |
| 5 from cache_chain_object_store import CacheChainObjectStore | 6 from cache_chain_object_store import CacheChainObjectStore |
| 6 from memcache_object_store import MemcacheObjectStore | 7 from memcache_object_store import MemcacheObjectStore |
| 7 from test_object_store import TestObjectStore | 8 from test_object_store import TestObjectStore |
| 8 from persistent_object_store import PersistentObjectStore | 9 from persistent_object_store import PersistentObjectStore |
| 9 | 10 |
| 11 _unspecified = object() | |
| 12 | |
| 10 class ObjectStoreCreator(object): | 13 class ObjectStoreCreator(object): |
| 11 class Factory(object): | 14 '''Creates ObjectStores with a namespacing and behaviour configuration. |
| 12 '''Parameters: | |
| 13 - |branch| The branch to create object stores for. This becomes part of the | |
| 14 namespace for the object stores that are created. | |
| 15 - |start_empty| Whether the caching object store that gets created should | |
| 16 start empty, or start with the content of its delegate object stores. | |
| 17 ''' | |
| 18 def __init__(self, app_version, branch): | |
| 19 self._app_version = app_version | |
| 20 self._branch = branch | |
| 21 | 15 |
| 22 def Create(self, cls, store_type=None): | 16 The initial configuration is specified on object store construction. When |
| 23 return ObjectStoreCreator(cls, | 17 creating ObjectStores via Create this configuration can be overridden (or |
| 24 self._app_version, | 18 via the variants of Create which do this automatically). |
| 25 self._branch, | 19 ''' |
| 26 store_type=store_type) | 20 def __init__(self, |
| 21 channel, | |
| 22 start_empty=_unspecified, | |
| 23 # Override for testing. A custom ObjectStore type to construct | |
| 24 # on Create(). Useful with TestObjectStore, for example. | |
| 25 store_type=None, | |
| 26 # Override for testing. Whether the ObjectStore type specified | |
| 27 # with |store_type| should be wrapped e.g. with Caching. This is | |
| 28 # useful to override when specific state tests/manipulations are | |
| 29 # being done on the underlying object store. | |
| 30 disable_wrappers=False): | |
| 31 self._channel = channel | |
| 32 if start_empty is _unspecified: | |
| 33 raise ValueError('start_empty must be specified (typically False)') | |
| 34 self._start_empty = start_empty | |
| 35 self._store_type = store_type | |
| 36 if disable_wrappers and store_type is None: | |
| 37 raise ValueError('disable_wrappers much specify a store_type') | |
| 38 self._disable_wrappers = disable_wrappers | |
| 27 | 39 |
| 28 class SharedFactory(object): | 40 @staticmethod |
| 29 '''A |Factory| for creating object stores shared across branches. | 41 def ForTest(start_empty=False, |
| 30 ''' | 42 store_type=TestObjectStore, |
| 31 def __init__(self, app_version): | 43 disable_wrappers=True): |
| 32 # TODO(kalman): Pass in (app_version, None) here. | 44 return ObjectStoreCreator('test', |
| 33 self._factory = ObjectStoreCreator.Factory(app_version, 'shared') | 45 start_empty=start_empty, |
| 46 store_type=store_type, | |
| 47 disable_wrappers=disable_wrappers) | |
| 34 | 48 |
| 35 def Create(self, cls, store_type=None): | 49 def Create(self, |
| 36 return self._factory.Create(cls, store_type=store_type) | 50 cls, |
| 51 category=None, | |
| 52 # Override any of these for a custom configuration. | |
| 53 start_empty=_unspecified, | |
| 54 channel=_unspecified, | |
| 55 app_version=_unspecified): | |
| 56 # Resolve namespace components. | |
| 57 if start_empty is not _unspecified: | |
| 58 start_empty = bool(start_empty) | |
| 59 else: | |
| 60 start_empty = self._start_empty | |
| 61 if channel is _unspecified: | |
| 62 channel = self._channel | |
| 63 if app_version is _unspecified: | |
| 64 app_version = GetAppVersion() | |
| 37 | 65 |
| 38 class GlobalFactory(object): | 66 # Reserve & and = for namespace separators. |
| 39 '''A |Factory| for creating object stores shared across all branches and | 67 for component in (category, channel, app_version): |
| 40 app versions. | 68 if component is not None and (component.find('&') != -1 or |
|
cduvall
2013/05/08 03:09:14
use:
'&' in component or '=' in component
not at google - send to devlin
2013/05/08 18:26:19
Done.
| |
| 41 ''' | 69 component.find('=') != -1): |
| 42 def __init__(self): | 70 raise ValueError('%s cannot be used in a namespace') |
| 43 # TODO(kalman): Pass in (None, None) here. | |
| 44 self._factory = ObjectStoreCreator.Factory('all', 'shared') | |
| 45 | 71 |
| 46 def Create(self, cls, store_type=None): | 72 namespace = '&'.join( |
| 47 return self._factory.Create(cls, store_type=store_type) | 73 '%s=%s' % (key, value) |
| 74 for key, value in (('class', cls.__name__), | |
| 75 ('category', category), | |
| 76 ('channel', channel), | |
| 77 ('app_version', app_version)) | |
| 78 if value is not None) | |
| 48 | 79 |
| 49 class TestFactory(object): | 80 if self._disable_wrappers: |
| 50 '''A |Factory| for creating object stores for tests, with fake defaults. | 81 return self._store_type(namespace, start_empty=start_empty) |
| 51 ''' | |
| 52 def __init__(self, | |
| 53 # TODO(kalman): make these version=None and branch=None. | |
| 54 version='test-version', | |
| 55 branch='test-branch', | |
| 56 store_type=TestObjectStore): | |
| 57 self._factory = ObjectStoreCreator.Factory(version, branch) | |
| 58 self._store_type = store_type | |
| 59 | 82 |
| 60 def Create(self, cls): | |
| 61 return self._factory.Create(cls, store_type=self._store_type) | |
| 62 | |
| 63 def __init__(self, cls, app_version, branch, store_type=None): | |
| 64 '''Creates stores with a top-level namespace given by the name of |cls| | |
| 65 combined with |branch|. Set an explicit |store_type| if necessary for tests. | |
| 66 | |
| 67 By convention this should be the name of the class which owns the object | |
| 68 store. If a class needs multiple object stores it should use Create with the | |
| 69 |category| argument. | |
| 70 ''' | |
| 71 assert isinstance(cls, type) | |
| 72 assert not cls.__name__[0].islower() # guard against non-class types | |
| 73 self._name = '%s/%s@%s' % (app_version, cls.__name__, branch) | |
| 74 self._store_type = store_type | |
| 75 | |
| 76 def Create(self, category=None, start_empty=False): | |
| 77 '''Creates a new object store with the top namespace given in the | |
| 78 constructor with an optional |category| for classes that need multiple | |
| 79 object stores (e.g. one for stat and one for read). | |
| 80 ''' | |
| 81 namespace = self._name | |
| 82 if category is not None: | |
| 83 assert not any(c.isdigit() for c in category) | |
| 84 namespace = '%s/%s' % (namespace, category) | |
| 85 if self._store_type is not None: | 83 if self._store_type is not None: |
| 86 return self._store_type(namespace, start_empty=start_empty) | 84 chain = (self._store_type(namespace),) |
| 87 return CacheChainObjectStore( | 85 else: |
| 88 (MemcacheObjectStore(namespace), PersistentObjectStore(namespace)), | 86 chain = (MemcacheObjectStore(namespace), PersistentObjectStore(namespace)) |
| 89 start_empty=start_empty) | 87 return CacheChainObjectStore(chain, start_empty=start_empty) |
| OLD | NEW |