def get_many(self, index_name, key=None, limit=-1, offset=0, with_doc=False, with_storage=True, start=None, end=None, **kwargs): """ Allows to get **multiple** data for given ``key`` for *Hash based indexes*. Also allows get **range** queries for *Tree based indexes* with ``start`` and ``end`` arguments. :param index_name: Index to perform the operation :param key: key to look for (has to be ``None`` to use range queries) :param limit: defines limit for query :param offset: defines offset (how many records from start it will ignore) :param with_doc: if ``True`` data from **id** index will be included in output :param with_storage: if ``True`` data from index storage will be included, otherwise just metadata. :param start: ``start`` parameter for range queries :param end: ``end`` parameter for range queries :returns: iterator over records """ if index_name == 'id': self.__not_opened() raise PreconditionsException("Can't get many from `id`") try: ind = self.indexes_names[index_name] except KeyError: self.__not_opened() raise IndexNotFoundException( "Index `%s` doesn't exists" % index_name) storage = ind.storage if start is None and end is None: gen = ind.get_many(key, limit, offset) else: gen = ind.get_between(start, end, limit, offset, **kwargs) while True: try: # l_key, start, size, status = gen.next() ind_data = next(gen) except StopIteration: break else: if with_storage and ind_data[-2]: data = storage.get(*ind_data[-3:]) else: data = {} doc_id = ind_data[0] if with_doc: doc = self.get('id', doc_id, False) if data: data['doc'] = doc else: data = {'doc': doc} data['_id'] = doc_id if key is None: data['key'] = ind_data[1] yield data
def all(self, index_name, limit=-1, offset=0, with_doc=False, with_storage=True): """ Alows to get all records for given index :param index_name: Index to perform the operation :param limit: defines limit for query :param offset: defines offset (how many records from start it will ignore) :param with_doc: if ``True`` data from **id** index will be included in output :param with_storage: if ``True`` data from index storage will be included, otherwise just metadata """ try: ind = self.indexes_names[index_name] except KeyError: self.__not_opened() raise IndexNotFoundException("Index `%s` doesn't exists" % index_name) storage = ind.storage gen = ind.all(limit, offset) while True: try: doc_id, unk, start, size, status = gen.next() except StopIteration: break else: if index_name == 'id': if with_storage and size: data = storage.get(start, size, status) else: data = {} data['_id'] = doc_id data['_rev'] = unk else: data = {} if with_storage and size: data['value'] = storage.get(start, size, status) data['key'] = unk data['_id'] = doc_id if with_doc: doc = self.get('id', doc_id, False) data['doc'] = doc yield data
def get(self, index_name, key, with_doc=False, with_storage=True): """ Get single data from Database by ``key``. :param index_name: index to get data from :param key: key to get :param with_doc: if ``True`` data from **id** index will be included in output :param with_storage: if ``True`` data from index storage will be included, otherwise just metadata. """ # if not self.indexes_names.has_key(index_name): # raise DatabaseException, "Invalid index name" try: ind = self.indexes_names[index_name] except KeyError: self.__not_opened() raise IndexNotFoundException("Index `%s` doesn't exists" % index_name) try: l_key, _unk, start, size, status = ind.get(key) except ElemNotFound as ex: raise RecordNotFound(ex) if not start and not size: raise RecordNotFound("Not found") elif status == 'd': raise RecordDeleted("Deleted") if with_storage and size: storage = ind.storage data = storage.get(start, size, status) else: data = {} if with_doc and index_name != 'id': storage = ind.storage doc = self.get('id', l_key, False) if data: data['doc'] = doc else: data = {'doc': doc} data['_id'] = l_key if index_name == 'id': data['_rev'] = _unk else: data['key'] = _unk return data
def get_index_details(self, name): """ Will return index properties. :returns: index details """ self.__not_opened() try: db_index = self.indexes_names[name] except KeyError: self.__not_opened() raise IndexNotFoundException("Index doesn't exist") props = {} for key, value in db_index.__dict__.items(): if not callable(value): # not using inspect etc... props[key] = value return props
def get_index_code(self, index_name, code_switch='All'): """ It will return full index code from index file. :param index_name: the name of index to look for code """ if not index_name in self.indexes_names: self.__not_opened() raise IndexNotFoundException("Index `%s` doesn't exists" % index_name) ind = self.indexes_names[index_name] name = "%.2d%s" % (ind._order, index_name) name += '.py' with io.FileIO(os.path.join(self.path, '_indexes', name), 'r') as f: co = f.read() if code_switch == 'All': return co if code_switch == 'S': try: ind = co.index('#SIMPLIFIED CODE') except ValueError: return " " else: s = co[ind:] l = s.splitlines()[1:-2] ll = map(lambda x: x[1:], l) return '\n'.join(ll) if code_switch == 'P': try: ind = co.index('#SIMPLIFIED CODE') except ValueError: return co else: return co[:ind] return "" # shouldn't happen
def run(self, index_name, target_funct, *args, **kwargs): """ Allows to execute given function on Database side (important for server mode) If ``target_funct==sum`` then given index must have ``run_sum`` method. :param index_name: index name to perform action. :param target_funct: target function name (without *run* prefix) :param \*args: ``*args`` for function :param \*\*kwargs: ``**kwargs`` for function """ try: ind = self.indexes_names[index_name] except KeyError: self.__not_opened() raise IndexNotFoundException("Index `%s` doesn't exists" % index_name) try: funct = getattr(ind, "run_" + target_funct) except AttributeError: raise IndexException("Invalid function to run") return funct(self, *args, **kwargs)