OLD | NEW |
(Empty) | |
| 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 |
| 3 # found in the LICENSE file. |
| 4 |
| 5 from in_memory_object_store import InMemoryObjectStore |
| 6 from memcache_object_store import MemcacheObjectStore |
| 7 |
| 8 class ObjectStoreCreator(object): |
| 9 def __init__(self, cls, store_type=None): |
| 10 '''Creates stores with a top-level namespace given by the name of |cls|. |
| 11 Set an explicit |store_type| if necessary for tests. |
| 12 |
| 13 By convention this should be the name of the class which owns the object |
| 14 store. If a class needs multiple object store it should use Create with the |
| 15 |category| argument. |
| 16 ''' |
| 17 assert isinstance(cls, type) |
| 18 assert not cls.__name__[0].islower() # guard against non-class types |
| 19 self._name = cls.__name__ |
| 20 self._store_type = store_type |
| 21 |
| 22 def Create(self, version=None, category=None): |
| 23 '''Creates a new object store with the top namespace given in the |
| 24 constructor, at version |version|, with an optional |category| for classes |
| 25 that need multiple object stores (e.g. one for stat and one for read). |
| 26 ''' |
| 27 namespace = self._name |
| 28 if category is not None: |
| 29 assert not any(c.isdigit() for c in category) |
| 30 namespace = '%s/%s' % (namespace, category) |
| 31 if version is not None: |
| 32 assert isinstance(version, int) |
| 33 namespace = '%s/%s' % (namespace, version) |
| 34 if self._store_type is not None: |
| 35 return self._store_type(namespace) |
| 36 return InMemoryObjectStore(MemcacheObjectStore(namespace)) |
OLD | NEW |