示例#1
0
    def _locate_key(self, key, start):
        """
        Locate position of the key, it will iterate using `next` field in record
        until required key will be find.

        :param key: the key to locate
        :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...
            try:
                l_key, rev, start, size, status, _next = self.entry_struct.unpack(
                    data)
            except struct.error:
                raise ElemNotFound("Location '%s' not found" % key)
            if l_key == key:
                break
            else:
                if not _next:
                    # not found
                    raise ElemNotFound("Location '%s' not found" % key)
                else:
                    location = _next  # go to next record
        return self.buckets.tell(
        ) - self.entry_line_size, l_key, rev, start, size, status, _next
示例#2
0
    def _locate_key(self, key, start):
        """
        Locate position of the key, it will iterate using `next` field in record
        until required key will be find.

        :param key: the key to locate
        :param start: position to start from
        """
        location = start
        while True:
            self.buckets.seek(location)
            data = self.buckets.read(self.entry_line_size)
            # todo, maybe partial read there...
            try:
                doc_id, l_key, start, size, status, _next = self.entry_struct.unpack(
                    data)
            except struct.error:
                # not found but might be also broken
                raise ElemNotFound("Not found or Error")

            if l_key == key:
                break
            else:
                if not _next:
                    # not found
                    raise ElemNotFound("Not found")
                else:
                    location = _next  # go to next record
        return location, doc_id, l_key, start, size, status, _next
示例#3
0
    def _locate_key(self, key, start):
        """
        Locate position of the key, it will iterate using `next` field in record
        until required key will be find.

        :param key: the key to locate
        :param start: position to start from
        """
        # Fix types
        if isinstance(key, six.text_type):
            key = key.encode()

        location = start
        while True:
            self.buckets.seek(location)
            data = self.buckets.read(self.entry_line_size)
            # todo, maybe partial read there...
            try:
                l_key, rev, start, size, status, _next = self.entry_struct.unpack(data)
                # print('IU_UniqueHashIndex._locate_key %r key=%r start=%r location=%r l_key=%r rev=%r start=%r size=%r status=%r _next=%r' % (
                #     self.name, key, start, location, l_key, rev, start, size, status, _next))
            except struct.error:
                # print("Location %r not found 1" % key)
                raise ElemNotFound("Location %r not found" % key)
            if l_key == key:
                break
            else:
                if not _next:
                    # not found
                    # print("Location %r not found 2" % key)
                    raise ElemNotFound("Location %r not found" % key)
                else:
                    location = _next  # go to next record
        return self.buckets.tell() - self.entry_line_size, l_key, rev, start, size, status, _next
示例#4
0
    def _locate_key(self, key, start):
        """
        Locate position of the key, it will iterate using `next` field in record
        until required key will be find.

        :param key: the key to locate
        :param start: position to start from
        """
        # Fix types
        if isinstance(key, six.text_type):
            key = key.encode()

        location = start
        while True:
            self.buckets.seek(location)
            data = self.buckets.read(self.entry_line_size)
            # todo, maybe partial read there...
            try:
                doc_id, l_key, start, size, status, _next = self.entry_struct.unpack(data)
                # print('IU_HashIndex._locate_key key=%r %r doc_id=%r l_key=%r start=%r size=%r status=%r _next=%r' % (key, self.name, doc_id, l_key, start, size, status, _next))
            except struct.error:
                # print('ElemNotFound 1')
                raise ElemNotFound(
                    "Not found")  # not found but might be also broken
            if l_key == key:
                break
            else:
                if not _next:
                    # print('ElemNotFound 2')
                    # not found
                    raise ElemNotFound("Not found")
                else:
                    location = _next  # go to next record
        return location, doc_id, l_key, start, size, status, _next
示例#5
0
    def update(self, key, rev, u_start=0, u_size=0, u_status='o'):
        # Fix types
        if isinstance(key, str):
            key = key.encode()
        if isinstance(rev, str):
            rev = rev.encode()
        if isinstance(u_status, str):
            u_status = u_status.encode()

        start_position = self._calculate_position(key)
        self.buckets.seek(start_position)
        curr_data = self.buckets.read(self.bucket_line_size)
        # test if it's unique or not really unique hash

        if curr_data:
            location = self.bucket_struct.unpack(curr_data)[0]
        else:
            raise ElemNotFound("Location '%s' not found" % key)
        found_at, _key, _rev, start, size, status, _next = self._locate_key(
            key, location)
        if u_start == 0:
            u_start = start
        if u_size == 0:
            u_size = size
        self.buckets.seek(found_at)
        self.buckets.write(
            self.entry_struct.pack(key, rev, u_start, u_size, u_status, _next))
        self.flush()
        self._find_key.delete(key)
        return True
示例#6
0
    def update(self, doc_id, key, u_start=0, u_size=0, u_status='o'):
        # Fix types
        if isinstance(doc_id, six.text_type):
            doc_id = doc_id.encode()
        if isinstance(key, six.text_type):
            key = key.encode()
        if isinstance(u_status, six.text_type):
            u_status = u_status.encode()

        start_position = self._calculate_position(key)
        self.buckets.seek(start_position)
        curr_data = self.buckets.read(self.bucket_line_size)
        # test if it's unique or not really unique hash
        if curr_data:
            location = self.bucket_struct.unpack(curr_data)[0]
        else:
            raise ElemNotFound("Location '%s' not found" % doc_id)
        found_at, _doc_id, _key, start, size, status, _next = self._locate_doc_id(
            doc_id, key, location)
        self.buckets.seek(found_at)
        self.buckets.write(
            self.entry_struct.pack(doc_id, key, u_start, u_size, u_status,
                                   _next))
        self.flush()
        self._find_key.delete(key)
        self._locate_doc_id.delete(doc_id)
        return True