Index: chrome/common/extensions/docs/server2/object_store_creator.py |
diff --git a/chrome/common/extensions/docs/server2/object_store_creator.py b/chrome/common/extensions/docs/server2/object_store_creator.py |
index c21627331e235acd2677c795bb77e2d6008a87f4..25f11a5a440f7f398b5c85ffdf89250fe9aad14f 100644 |
--- a/chrome/common/extensions/docs/server2/object_store_creator.py |
+++ b/chrome/common/extensions/docs/server2/object_store_creator.py |
@@ -8,6 +8,16 @@ from test_object_store import TestObjectStore |
from persistent_object_store import PersistentObjectStore |
class ObjectStoreCreator(object): |
+ # Start configurations. |
+ # |
+ # START_POPULATED should be used to continue using whatever has been |
+ # cached/persisted in the object stores. This is useful for live instances. |
+ # |
+ # START_EMPTY should be used when the data in the stores might be stale. This |
+ # is useful for cron jobs where we want to refresh the data in a generic way. |
+ START_POPULATED = (None,) |
+ START_EMPTY = (None,) |
+ |
class Factory(object): |
'''Parameters: |
- |branch| The branch to create object stores for. This becomes part of the |
@@ -15,22 +25,26 @@ class ObjectStoreCreator(object): |
- |start_empty| Whether the caching object store that gets created should |
start empty, or start with the content of its delegate object stores. |
''' |
- def __init__(self, app_version, branch): |
+ def __init__(self, app_version, branch, start_configuration): |
self._app_version = app_version |
self._branch = branch |
+ self._start_configuration = start_configuration |
def Create(self, cls, store_type=None): |
return ObjectStoreCreator(cls, |
self._app_version, |
self._branch, |
+ self._start_configuration, |
store_type=store_type) |
class SharedFactory(object): |
'''A |Factory| for creating object stores shared across branches. |
''' |
- def __init__(self, app_version): |
- # TODO(kalman): Pass in (app_version, None) here. |
- self._factory = ObjectStoreCreator.Factory(app_version, 'shared') |
+ def __init__(self, app_version, start_configuration): |
+ self._factory = ObjectStoreCreator.Factory(app_version, |
+ # TODO(kalman): Pass None here. |
+ 'shared', |
+ start_configuration) |
def Create(self, cls, store_type=None): |
return self._factory.Create(cls, store_type=store_type) |
@@ -39,12 +53,14 @@ class ObjectStoreCreator(object): |
'''A |Factory| for creating object stores shared across all branches and |
app versions. |
''' |
- def __init__(self): |
+ def __init__(self, start_configuration): |
# TODO(kalman): Pass in (None, None) here. |
- self._factory = ObjectStoreCreator.Factory('all', 'shared') |
+ self._factory = ObjectStoreCreator.Factory('all', |
+ 'shared', |
+ start_configuration) |
- def Create(self, cls, store_type=None): |
- return self._factory.Create(cls, store_type=store_type) |
+ def Create(self, cls): |
+ return self._factory.Create(cls) |
class TestFactory(object): |
'''A |Factory| for creating object stores for tests, with fake defaults. |
@@ -53,14 +69,26 @@ class ObjectStoreCreator(object): |
# TODO(kalman): make these version=None and branch=None. |
version='test-version', |
branch='test-branch', |
+ start_configuration=None, |
store_type=TestObjectStore): |
- self._factory = ObjectStoreCreator.Factory(version, branch) |
+ # It's unlikely that tests will use a store_type that cares about |
+ # starting empty. |
+ start_configuration = (start_configuration or |
+ ObjectStoreCreator.START_POPULATED) |
+ self._factory = ObjectStoreCreator.Factory(version, |
+ branch, |
+ start_configuration) |
self._store_type = store_type |
def Create(self, cls): |
return self._factory.Create(cls, store_type=self._store_type) |
- def __init__(self, cls, app_version, branch, store_type=None): |
+ def __init__(self, |
+ cls, |
+ app_version, |
+ branch, |
+ start_configuration, |
+ store_type=None): |
'''Creates stores with a top-level namespace given by the name of |cls| |
combined with |branch|. Set an explicit |store_type| if necessary for tests. |
@@ -71,17 +99,28 @@ class ObjectStoreCreator(object): |
assert isinstance(cls, type) |
assert not cls.__name__[0].islower() # guard against non-class types |
self._name = '%s/%s@%s' % (app_version, cls.__name__, branch) |
+ self._start_configuration = start_configuration |
self._store_type = store_type |
- def Create(self, category=None, start_empty=False): |
+ def Create(self, category=None, start_configuration=None): |
'''Creates a new object store with the top namespace given in the |
constructor with an optional |category| for classes that need multiple |
object stores (e.g. one for stat and one for read). |
+ Allow overriding |start_configuration| for special cases. Generally, stick |
+ with the one that was given in the constructor. |
''' |
namespace = self._name |
if category is not None: |
assert not any(c.isdigit() for c in category) |
namespace = '%s/%s' % (namespace, category) |
+ start_configuration = start_configuration or self._start_configuration |
+ if start_configuration is ObjectStoreCreator.START_POPULATED: |
+ start_empty = False |
+ elif start_configuration is ObjectStoreCreator.START_EMPTY: |
+ start_empty = True |
+ else: |
+ raise ValueError('%s is not a supported start configuration' % |
+ start_configuration) |
if self._store_type is not None: |
return self._store_type(namespace, start_empty=start_empty) |
return CacheChainObjectStore( |