def get_elements(self, ids=None, query=None, limit=0, skip=0, sort=None, with_count=False, hint=None, projection=None, tags=None, *args, **kwargs): _query = {} if query is None else query.copy() one_element = isinstance(ids, basestring) if ids is not None: if one_element: _query[MongoStorage.ID] = ids else: _query[MongoStorage.ID] = {'$in': ids} if tags: _query[MongoStorage.TAGS] = tags cursor = self._find(_query, projection) # set limit, skip and sort properties if limit: cursor.limit(limit) if skip: cursor.skip(skip) if sort is not None: sort = Storage._resolve_sort(sort) if sort: cursor.sort(sort) if hint is None: hint = self._get_hint(query=_query, cursor=cursor) if hint is not None: cursor.hint(hint) result = MongoCursor(cursor) if one_element: result = result[0] if result else None # if with_count, add count to the result if with_count: # calculate count count = cursor.count() result = result, count return result
def get_elements( self, ids=None, query=None, limit=0, skip=0, sort=None, with_count=False, hint=None, projection=None, *args, **kwargs ): _query = {} if query is None else query.copy() one_element = isinstance(ids, basestring) if ids is not None: if one_element: _query[MongoStorage.ID] = ids else: _query[MongoStorage.ID] = {"$in": ids} cursor = self._find(_query, projection) # set limit, skip and sort properties if limit: cursor.limit(limit) if skip: cursor.skip(skip) if sort is not None: sort = Storage._resolve_sort(sort) if sort: cursor.sort(sort) if hint is None: hint = self._get_hint(query=_query, cursor=cursor) if hint is not None: cursor.hint(hint) result = MongoCursor(cursor) if one_element: result = result[0] if result else None # if with_count, add count to the result if with_count: # calculate count count = cursor.count() result = result, count return result
def _resolve_sort(sort): """Resolve input sort in transforming it to a list of tuple of (name, direction). :param sort: sort configuration. Can be a string, or :type sort: list of {tuple(str, int), str} :return: depending on type of sort: - str: [(sort, Storage.ASC)] - dict: [(sort['property'], sort.get('direction', Storage.ASC))] - tuple: [(sort[0], sort[1])] - list: - str :rtype: str, dict, tuple or list """ result = [] if isinstance(sort, basestring): result.append((sort, Storage.ASC)) elif isinstance(sort, dict): sort_tuple = None field = sort.get('property', None) if field is not None: direction = sort.get('direction', Storage.ASC) if isinstance(direction, basestring): direction = getattr(Storage, direction.upper()) # Need field property filled in the sort document sort_tuple = (field, direction) if sort_tuple is not None: result.append(sort_tuple) elif isinstance(sort, tuple): direction = sort[1] if isinstance(direction, basestring): direction = getattr(Storage, direction.upper()) result.append((sort[0], direction)) elif isinstance(sort, Iterable): for item in sort: result += Storage._resolve_sort(item) return result
def copy(self, target): """Copy self content into target storage. target type must implement the same class in cstorage packege as self. If self implements directly cstorage.Storage, we don't care about target type :param target: target storage where copy content :type target: same as self or any storage if type(self) is Storage """ result = 0 from canopsis.storage.core import Storage from canopsis.storage.periodic import PeriodicStorage from canopsis.storage.timed import TimedStorage from canopsis.storage.timedtyped import TimedTypedStorage from canopsis.storage.typed import TypedStorage storage_types = [ Storage, PeriodicStorage, TimedStorage, TimedTypedStorage, TypedStorage ] if isinstance(self, storage_types): for _ in storage_types: if isinstance(self, storage_types): if not isinstance(target, storage_types): raise Storage.StorageError( 'Impossible to copy {0} content into {1}. \ Storage types must be of the same type.'.format(self, target)) else: self._copy(target) result = -1 return result
def setUp(self): """initialize storages""" self.storages = [] testconf = self._testconfcls() if testconf.storages: for storage in testconf.storages: storagecls = lookup(storage) storage = storagecls( data_scope=data_scope, conf_paths=testconf.conf_paths, **testconf.params ) self.storages.append(storage) else: for protocol in testconf.protocols: for data_type in testconf.data_types: for data_scope in testconf.data_scopes: storage = Storage.get_middleware( protocol=protocol, data_type=data_type, data_scope=data_scope, conf_paths=testconf.conf_paths, **testconf.params ) self.storages.append(storage)