Ejemplo n.º 1
0
    def select_notifications(self, start: int,
                             limit: int) -> List[Notification]:
        """
        Returns a list of event notifications
        from 'start', limited by 'limit'.
        """
        statement_name = self.select_notifications_statement_name
        self._prepare(statement_name, self.select_notifications_statement)

        notifications = []
        with self.datastore.transaction(commit=False) as conn:
            with conn.cursor() as c:
                c.execute(
                    f"EXECUTE {statement_name}(%s, %s)",
                    (start, limit),
                )
                for row in c.fetchall():
                    notifications.append(
                        Notification(
                            id=row["notification_id"],
                            originator_id=row["originator_id"],
                            originator_version=row["originator_version"],
                            topic=row["topic"],
                            state=bytes(row["state"]),
                        ))
                pass  # for Coverage 5.5 bug with CPython 3.10.0rc1
        return notifications
Ejemplo n.º 2
0
    def test_filter_received_notifications(self):
        class MyFollower(Follower):
            follow_topics = []

            def policy(self, *args, **kwargs):
                pass

        follower = MyFollower()
        notifications = [
            Notification(
                id=1,
                originator_id=uuid4(),
                originator_version=1,
                state=b"",
                topic="topic1",
            )
        ]
        self.assertEqual(
            len(follower.filter_received_notifications(notifications)), 1)
        follower.follow_topics = ["topic1"]
        self.assertEqual(
            len(follower.filter_received_notifications(notifications)), 1)
        follower.follow_topics = ["topic2"]
        self.assertEqual(
            len(follower.filter_received_notifications(notifications)), 0)
Ejemplo n.º 3
0
 def select_notifications(
         self,
         start: int,
         limit: int,
         stop: Optional[int] = None,
         topics: Sequence[str] = (),
 ) -> List[Notification]:
     with self._database_lock:
         results = []
         i = start - 1
         while True:
             if stop is not None and i > stop - 1:
                 break
             try:
                 s = self._stored_events[i]
             except IndexError:
                 break
             i += 1
             if topics and s.topic not in topics:
                 continue
             n = Notification(
                 id=i,
                 originator_id=s.originator_id,
                 originator_version=s.originator_version,
                 topic=s.topic,
                 state=s.state,
             )
             results.append(n)
             if len(results) == limit:
                 break
         return results
Ejemplo n.º 4
0
    def select_notifications(
            self,
            start: int,
            limit: int,
            stop: Optional[int] = None,
            topics: Sequence[str] = (),
    ) -> List[Notification]:
        """
        Returns a list of event notifications
        from 'start', limited by 'limit'.
        """

        params: List[Union[int, str, Sequence[str]]] = [start]
        statement = ("SELECT * "
                     f"FROM {self.events_table_name} "
                     "WHERE notification_id>=$1 ")
        statement_name = f"select_notifications_{self.events_table_name}".replace(
            ".", "_")

        if stop is not None:
            params.append(stop)
            statement += f"AND notification_id <= ${len(params)} "
            statement_name += "_stop"

        if topics:
            params.append(topics)
            statement += f"AND topic = ANY(${len(params)}) "
            statement_name += "_topics"

        params.append(limit)
        statement += "ORDER BY notification_id " f"LIMIT ${len(params)}"

        notifications = []
        with self.datastore.get_connection() as conn:
            alias = self._prepare(
                conn,
                statement_name,
                statement,
            )
            with conn.transaction(commit=False) as curs:
                curs.execute(
                    f"EXECUTE {alias}({', '.join(['%s' for _ in params])})",
                    params,
                )
                for row in curs.fetchall():
                    notifications.append(
                        Notification(
                            id=row["notification_id"],
                            originator_id=row["originator_id"],
                            originator_version=row["originator_version"],
                            topic=row["topic"],
                            state=bytes(row["state"]),
                        ))
                pass  # for Coverage 5.5 bug with CPython 3.10.0rc1
        return notifications
Ejemplo n.º 5
0
 def select(self, start: int, limit: int) -> List[Notification]:
     return [
         Notification(
             id=item["id"],
             originator_id=UUID(item["originator_id"]),
             originator_version=item["originator_version"],
             topic=item["topic"],
             state=b64decode(item["state"].encode("utf8")),
         )
         for item in json.loads(self.interface.get_notifications(start, limit))
     ]
Ejemplo n.º 6
0
 def __getitem__(self, section_id: str) -> Section:
     body = self.interface.get_log_section(section_id)
     section = json.loads(body)
     return Section(
         id=section["id"],
         next_id=section["next_id"],
         items=[
             Notification(
                 id=item["id"],
                 originator_id=UUID(item["originator_id"]),
                 originator_version=item["originator_version"],
                 topic=item["topic"],
                 state=b64decode(item["state"].encode("utf8")),
             ) for item in section["items"]
         ],
     )
Ejemplo n.º 7
0
 def select_notifications(self, start: int,
                          limit: int) -> List[Notification]:
     with self.database_lock:
         results = []
         i = start - 1
         j = i + limit
         for notification_id, s in enumerate(self.stored_events[i:j],
                                             start):
             n = Notification(
                 id=notification_id,
                 originator_id=s.originator_id,
                 originator_version=s.originator_version,
                 topic=s.topic,
                 state=s.state,
             )
             results.append(n)
         return results
Ejemplo n.º 8
0
 def select_notifications(self, start: int,
                          limit: int) -> List[Notification]:
     """
     Returns a list of event notifications
     from 'start', limited by 'limit'.
     """
     notifications = []
     with self.datastore.transaction() as c:
         c.execute(self.select_notifications_statement, [start, limit])
         for row in c.fetchall():
             notifications.append(
                 Notification(
                     id=row["rowid"],
                     originator_id=UUID(row["originator_id"]),
                     originator_version=row["originator_version"],
                     topic=row["topic"],
                     state=row["state"],
                 ))
     return notifications
Ejemplo n.º 9
0
    def select_notifications(
            self,
            start: int,
            limit: int,
            stop: Optional[int] = None,
            topics: Sequence[str] = (),
    ) -> List[Notification]:
        """
        Returns a list of event notifications
        from 'start', limited by 'limit'.
        """
        notifications = []

        params: List[Union[int, str]] = [start]
        statement = f"SELECT rowid, * FROM {self.events_table_name} " "WHERE rowid>=? "

        if stop is not None:
            params.append(stop)
            statement += "AND rowid<=? "

        if topics:
            params += list(topics)
            statement += "AND topic IN (%s) " % ",".join("?" * len(topics))

        params.append(limit)
        statement += "ORDER BY rowid LIMIT ?"

        with self.datastore.transaction(commit=False) as c:
            c.execute(statement, params)

            for row in c.fetchall():
                notifications.append(
                    Notification(
                        id=row["rowid"],
                        originator_id=UUID(row["originator_id"]),
                        originator_version=row["originator_version"],
                        topic=row["topic"],
                        state=row["state"],
                    ))
            pass  # for Coverage 5.5 bug with CPython 3.10.0rc1
        return notifications
Ejemplo n.º 10
0
 def __getitem__(self, section_id: str) -> Section:
     """
     Returns a :class:`Section` of
     :class:`~eventsourcing.persistence.Notification` objects
     from the notification log.
     """
     body = self.interface.get_log_section(section_id)
     section = json.loads(body)
     return Section(
         id=section["id"],
         next_id=section["next_id"],
         items=[
             Notification(
                 id=item["id"],
                 originator_id=UUID(item["originator_id"]),
                 originator_version=item["originator_version"],
                 topic=item["topic"],
                 state=b64decode(item["state"].encode("utf8")),
             ) for item in section["items"]
         ],
     )
Ejemplo n.º 11
0
 def select(
         self,
         start: int,
         limit: int,
         stop: Optional[int] = None,
         topics: Sequence[str] = (),
 ) -> List[Notification]:
     """
     Returns a selection
     :class:`~eventsourcing.persistence.Notification` objects
     from the notification log.
     """
     return [
         Notification(
             id=item["id"],
             originator_id=UUID(item["originator_id"]),
             originator_version=item["originator_version"],
             topic=item["topic"],
             state=b64decode(item["state"].encode("utf8")),
         ) for item in json.loads(
             self.interface.get_notifications(
                 start=start, limit=limit, topics=topics))
     ]
Ejemplo n.º 12
0
 def select_notifications(self, start: int,
                          limit: int) -> List[Notification]:
     """
     Returns a list of event notifications
     from 'start', limited by 'limit'.
     """
     params = [start, limit]
     try:
         with self.datastore.transaction() as c:
             c.execute(self.statement_notifications_statement, params)
             notifications = []
             for row in c.fetchall():
                 notifications.append(
                     Notification(
                         id=row["notification_id"],
                         originator_id=row["originator_id"],
                         originator_version=row["originator_version"],
                         topic=row["topic"],
                         state=bytes(row["state"]),
                     ))
     except psycopg2.Error as e:
         raise OperationalError(e)
     return notifications
Ejemplo n.º 13
0
 def select_notifications(self, start: int,
                          limit: int) -> List[Notification]:
     """
     Returns a list of event notifications
     from 'start', limited by 'limit'.
     """
     params = [start, limit]
     try:
         c = self.datastore.get_connection().cursor()
         c.execute(self.select_notifications_statement, params)
         notifications = []
         for row in c.fetchall():
             notifications.append(
                 Notification(
                     id=row["rowid"],
                     originator_id=UUID(row["originator_id"]),
                     originator_version=row["originator_version"],
                     topic=row["topic"],
                     state=row["state"],
                 ))
     except sqlite3.OperationalError as e:
         raise OperationalError(e)
     return notifications