Esempio n. 1
0
 def get_item_assigned(self, index):
     try:
         item_assigned = self.repo.event_store.get_event(
             originator_id=self.id, position=index)
     except IndexError as e:
         raise ArrayIndexError(e)
     else:
         return item_assigned
Esempio n. 2
0
    def __setitem__(self, index, item):
        """
        Sets item in array, at given index.

        Won't overrun the end of the array, because
        the position is fixed to be less than base_size.
        """
        size = self.repo.array_size
        if size and index >= size:
            raise ArrayIndexError("Index is {}, but size is {}".format(
                index, size))
        event = ItemAssigned(originator_id=self.id, index=index, item=item)
        publish([event])
    def __getitem__(self, item):
        """
        Returns item at index, or items in slice.
        """
        assert isinstance(item, (six.integer_types, slice))
        array_size = self.repo.array_size
        if isinstance(item, six.integer_types):
            if item < 0:
                index = array_size + item
                if index < 0:
                    raise ArrayIndexError(
                        "Array index out of range: {}".format(item))
            else:
                index = item
            event = self.get_item_assigned(index)
            assert isinstance(event, ItemAssigned)
            return event.item
        elif isinstance(item, slice):
            assert item.step in (None, 1), "Slice stepping not supported, yet"
            if item.start is None:
                start_index = 0
            elif item.start < 0:
                start_index = max(array_size + item.start, 0)
            else:
                start_index = item.start

            if not isinstance(item.stop, six.integer_types):
                stop_index = array_size
            elif item.stop < 0:
                stop_index = array_size + item.stop
            else:
                stop_index = item.stop

            if stop_index is not None:
                if stop_index <= 0:
                    return []
                # Avoid passing massive integers into the database.
                if array_size is not None:
                    stop_index = min(stop_index, self.repo.array_size)

            items_assigned = self.get_items_assigned(stop_index=stop_index,
                                                     start_index=start_index)
            items_dict = {
                str(i.originator_version): i.item
                for i in items_assigned
            }
            items = [
                items_dict.get(str(i)) for i in range(start_index, stop_index)
            ]
            return items
 def calc_required_height(self, n, size):
     if size <= 0:
         raise ValueError("Size must be greater than 0")
     capacity = size**size
     if n + 1 > capacity:
         raise ArrayIndexError("Contents can't be greater than capacity")
     if n == 0 or n == 1:
         return 1
     min_capacity = max(int(size), int(n + 1))
     required_height = log(min_capacity, int(size))
     # Quash numerical error before calling ceil.
     # - this is occasionally required, for example
     #   with base size of 1000 numerical error
     #   makes the required height one greater
     #   than the correct value, which causes
     #   an index error when assigning apex
     #   ID to the root sequence.
     required_height = round(required_height, 10)
     return int(ceil(required_height))