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 cache_chain_object_store import CacheChainObjectStore | 5 from cache_chain_object_store import CacheChainObjectStore |
6 from memcache_object_store import MemcacheObjectStore | 6 from memcache_object_store import MemcacheObjectStore |
7 from test_object_store import TestObjectStore | 7 from test_object_store import TestObjectStore |
8 from persistent_object_store import PersistentObjectStore | 8 from persistent_object_store import PersistentObjectStore |
9 | 9 |
10 class ObjectStoreCreator(object): | 10 class ObjectStoreCreator(object): |
| 11 # Start configurations. |
| 12 # |
| 13 # START_POPULATED should be used to continue using whatever has been |
| 14 # cached/persisted in the object stores. This is useful for live instances. |
| 15 # |
| 16 # START_EMPTY should be used when the data in the stores might be stale. This |
| 17 # is useful for cron jobs where we want to refresh the data in a generic way. |
| 18 START_POPULATED = (None,) |
| 19 START_EMPTY = (None,) |
| 20 |
11 class Factory(object): | 21 class Factory(object): |
12 '''Parameters: | 22 '''Parameters: |
13 - |branch| The branch to create object stores for. This becomes part of the | 23 - |branch| The branch to create object stores for. This becomes part of the |
14 namespace for the object stores that are created. | 24 namespace for the object stores that are created. |
15 - |start_empty| Whether the caching object store that gets created should | 25 - |start_empty| Whether the caching object store that gets created should |
16 start empty, or start with the content of its delegate object stores. | 26 start empty, or start with the content of its delegate object stores. |
17 ''' | 27 ''' |
18 def __init__(self, app_version, branch): | 28 def __init__(self, app_version, branch, start_configuration): |
19 self._app_version = app_version | 29 self._app_version = app_version |
20 self._branch = branch | 30 self._branch = branch |
| 31 self._start_configuration = start_configuration |
21 | 32 |
22 def Create(self, cls, store_type=None): | 33 def Create(self, cls, store_type=None): |
23 return ObjectStoreCreator(cls, | 34 return ObjectStoreCreator(cls, |
24 self._app_version, | 35 self._app_version, |
25 self._branch, | 36 self._branch, |
| 37 self._start_configuration, |
26 store_type=store_type) | 38 store_type=store_type) |
27 | 39 |
28 class SharedFactory(object): | 40 class SharedFactory(object): |
29 '''A |Factory| for creating object stores shared across branches. | 41 '''A |Factory| for creating object stores shared across branches. |
30 ''' | 42 ''' |
31 def __init__(self, app_version): | 43 def __init__(self, app_version, start_configuration): |
32 # TODO(kalman): Pass in (app_version, None) here. | 44 self._factory = ObjectStoreCreator.Factory(app_version, |
33 self._factory = ObjectStoreCreator.Factory(app_version, 'shared') | 45 # TODO(kalman): Pass None here. |
| 46 'shared', |
| 47 start_configuration) |
34 | 48 |
35 def Create(self, cls, store_type=None): | 49 def Create(self, cls, store_type=None): |
36 return self._factory.Create(cls, store_type=store_type) | 50 return self._factory.Create(cls, store_type=store_type) |
37 | 51 |
38 class GlobalFactory(object): | 52 class GlobalFactory(object): |
39 '''A |Factory| for creating object stores shared across all branches and | 53 '''A |Factory| for creating object stores shared across all branches and |
40 app versions. | 54 app versions. |
41 ''' | 55 ''' |
42 def __init__(self): | 56 def __init__(self, start_configuration): |
43 # TODO(kalman): Pass in (None, None) here. | 57 # TODO(kalman): Pass in (None, None) here. |
44 self._factory = ObjectStoreCreator.Factory('all', 'shared') | 58 self._factory = ObjectStoreCreator.Factory('all', |
| 59 'shared', |
| 60 start_configuration) |
45 | 61 |
46 def Create(self, cls, store_type=None): | 62 def Create(self, cls): |
47 return self._factory.Create(cls, store_type=store_type) | 63 return self._factory.Create(cls) |
48 | 64 |
49 class TestFactory(object): | 65 class TestFactory(object): |
50 '''A |Factory| for creating object stores for tests, with fake defaults. | 66 '''A |Factory| for creating object stores for tests, with fake defaults. |
51 ''' | 67 ''' |
52 def __init__(self, | 68 def __init__(self, |
53 # TODO(kalman): make these version=None and branch=None. | 69 # TODO(kalman): make these version=None and branch=None. |
54 version='test-version', | 70 version='test-version', |
55 branch='test-branch', | 71 branch='test-branch', |
| 72 start_configuration=None, |
56 store_type=TestObjectStore): | 73 store_type=TestObjectStore): |
57 self._factory = ObjectStoreCreator.Factory(version, branch) | 74 # It's unlikely that tests will use a store_type that cares about |
| 75 # starting empty. |
| 76 start_configuration = (start_configuration or |
| 77 ObjectStoreCreator.START_POPULATED) |
| 78 self._factory = ObjectStoreCreator.Factory(version, |
| 79 branch, |
| 80 start_configuration) |
58 self._store_type = store_type | 81 self._store_type = store_type |
59 | 82 |
60 def Create(self, cls): | 83 def Create(self, cls): |
61 return self._factory.Create(cls, store_type=self._store_type) | 84 return self._factory.Create(cls, store_type=self._store_type) |
62 | 85 |
63 def __init__(self, cls, app_version, branch, store_type=None): | 86 def __init__(self, |
| 87 cls, |
| 88 app_version, |
| 89 branch, |
| 90 start_configuration, |
| 91 store_type=None): |
64 '''Creates stores with a top-level namespace given by the name of |cls| | 92 '''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. | 93 combined with |branch|. Set an explicit |store_type| if necessary for tests. |
66 | 94 |
67 By convention this should be the name of the class which owns the object | 95 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 | 96 store. If a class needs multiple object stores it should use Create with the |
69 |category| argument. | 97 |category| argument. |
70 ''' | 98 ''' |
71 assert isinstance(cls, type) | 99 assert isinstance(cls, type) |
72 assert not cls.__name__[0].islower() # guard against non-class types | 100 assert not cls.__name__[0].islower() # guard against non-class types |
73 self._name = '%s/%s@%s' % (app_version, cls.__name__, branch) | 101 self._name = '%s/%s@%s' % (app_version, cls.__name__, branch) |
| 102 self._start_configuration = start_configuration |
74 self._store_type = store_type | 103 self._store_type = store_type |
75 | 104 |
76 def Create(self, category=None, start_empty=False): | 105 def Create(self, category=None, start_configuration=None): |
77 '''Creates a new object store with the top namespace given in the | 106 '''Creates a new object store with the top namespace given in the |
78 constructor with an optional |category| for classes that need multiple | 107 constructor with an optional |category| for classes that need multiple |
79 object stores (e.g. one for stat and one for read). | 108 object stores (e.g. one for stat and one for read). |
| 109 Allow overriding |start_configuration| for special cases. Generally, stick |
| 110 with the one that was given in the constructor. |
80 ''' | 111 ''' |
81 namespace = self._name | 112 namespace = self._name |
82 if category is not None: | 113 if category is not None: |
83 assert not any(c.isdigit() for c in category) | 114 assert not any(c.isdigit() for c in category) |
84 namespace = '%s/%s' % (namespace, category) | 115 namespace = '%s/%s' % (namespace, category) |
| 116 start_configuration = start_configuration or self._start_configuration |
| 117 if start_configuration is ObjectStoreCreator.START_POPULATED: |
| 118 start_empty = False |
| 119 elif start_configuration is ObjectStoreCreator.START_EMPTY: |
| 120 start_empty = True |
| 121 else: |
| 122 raise ValueError('%s is not a supported start configuration' % |
| 123 start_configuration) |
85 if self._store_type is not None: | 124 if self._store_type is not None: |
86 return self._store_type(namespace, start_empty=start_empty) | 125 return self._store_type(namespace, start_empty=start_empty) |
87 return CacheChainObjectStore( | 126 return CacheChainObjectStore( |
88 (MemcacheObjectStore(namespace), PersistentObjectStore(namespace)), | 127 (MemcacheObjectStore(namespace), PersistentObjectStore(namespace)), |
89 start_empty=start_empty) | 128 start_empty=start_empty) |
OLD | NEW |