def _find_place(self, start, key): """ Find a place to where put the key. It will iterate using `next` field in record, until empty `next` found :param start: position to start from """ # Fix types if isinstance(key, str): key = key.encode() location = start while True: self.buckets.seek(location) data = self.buckets.read(self.entry_line_size) # todo, maybe partial read there... l_key, rev, start, size, status, _next = self.entry_struct.unpack( data) if l_key == key: raise IndexException("The '%s' key already exists" % key) if not _next or status == 'd': return self.buckets.tell( ) - self.entry_line_size, l_key, rev, start, size, status, _next else: location = _next # go to next record
def open_index(self): if not os.path.isfile(os.path.join(self.db_path, self.name + '_buck')): raise IndexException("Doesn't exists") self.buckets = io.open( os.path.join(self.db_path, self.name + "_buck"), 'r+b', buffering=0) self._fix_params() self._open_storage()
def create_index(self): if os.path.isfile(os.path.join(self.db_path, self.name + '_buck')): raise IndexException('Already exists') with io.open(os.path.join(self.db_path, self.name + "_buck"), 'w+b') as f: props = dict(name=self.name, bucket_line_format=self.bucket_line_format, entry_line_format=self.entry_line_format, hash_lim=self.hash_lim, version=self.__version__, storage_class=self.storage_class) f.write(marshal.dumps(props)) self.buckets = io.open( os.path.join(self.db_path, self.name + "_buck"), 'r+b', buffering=0) self._create_storage()
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)