示例#1
0
    def take_snapshot(self, entity_id, until=None):
        """Takes a snapshot of the entity as it existed after
        the most recent event, optionally until a given time.
        """
        if self.snapshot_strategy is None:
            raise ValueError(
                "Can't take snapshots without a snapshot strategy.")

        # Get the last event (optionally until a particular time).
        last_event = self.get_most_recent_event(entity_id, until=until)

        # If there aren't any events, there can't be a snapshot, so return None.
        if last_event is None:
            return None

        # If there is something to snapshot, then look for
        # the last snapshot before the last event.
        last_event_id = last_event.domain_event_id
        last_snapshot = self.get_snapshot(entity_id, until=last_event_id)

        # If there's a snapshot...
        if last_snapshot:
            assert isinstance(last_snapshot,
                              AbstractSnapshop), type(last_snapshot)

            # and it is coincident with the last event...
            if last_snapshot.at_event_id == last_event_id:
                # then there's nothing to do.
                return last_snapshot
            else:
                # Otherwise there must be events after the snapshot, so
                # remember to get events after the last event that was
                # applied to the entity, and obtain the initial entity
                # state so those event can be applied.
                after_event_id = last_snapshot.at_event_id
                initial_state = entity_from_snapshot(last_snapshot)
        else:
            # If there isn't a snapshot, start from scratch.
            after_event_id = None
            initial_state = None

        # Get entity in the state after this event was applied.
        entity = self.replay_events(entity_id,
                                    after=after_event_id,
                                    until=last_event_id,
                                    initial_state=initial_state)

        # Take a snapshot of the entity.
        return self.snapshot_strategy.take_snapshot(entity,
                                                    at_event_id=last_event_id)
    def get_entity(self, entity_id, until=None):
        """
        Returns entity with given ID, optionally as it was until the given domain event ID.
        """

        # Get a snapshot (None if none exist).
        snapshot = self.event_player.get_snapshot(entity_id, until)

        # Decide the initial state, and after when we need to get the events.
        if snapshot is None:
            after = None
            initial_state = None
        else:
            after = snapshot.at_event_id
            initial_state = entity_from_snapshot(snapshot)

        # Replay domain events.
        return self.event_player.replay_events(entity_id, after=after, until=until, initial_state=initial_state)
示例#3
0
    def take_snapshot(self, entity_id, until=None):
        """Takes a snapshot of the entity as it existed after
        the most recent event, optionally until a given time.
        """
        if self.snapshot_strategy is None:
            raise ValueError("Can't take snapshots without a snapshot strategy.")

        # Get the last event (optionally until a particular time).
        last_event = self.get_most_recent_event(entity_id, until=until)

        # If there aren't any events, there can't be a snapshot, so return None.
        if last_event is None:
            return None

        # If there is something to snapshot, then look for
        # the last snapshot before the last event.
        last_event_id = last_event.domain_event_id
        last_snapshot = self.get_snapshot(entity_id, until=last_event_id)

        # If there's a snapshot...
        if last_snapshot:
            assert isinstance(last_snapshot, AbstractSnapshop), type(last_snapshot)

            # and it is coincident with the last event...
            if last_snapshot.at_event_id == last_event_id:
                # then there's nothing to do.
                return last_snapshot
            else:
                # Otherwise there must be events after the snapshot, so
                # remember to get events after the last event that was
                # applied to the entity, and obtain the initial entity
                # state so those event can be applied.
                after_event_id = last_snapshot.at_event_id
                initial_state = entity_from_snapshot(last_snapshot)
        else:
            # If there isn't a snapshot, start from scratch.
            after_event_id = None
            initial_state = None

        # Get entity in the state after this event was applied.
        entity = self.replay_events(entity_id, after=after_event_id, until=last_event_id, initial_state=initial_state)

        # Take a snapshot of the entity.
        return self.snapshot_strategy.take_snapshot(entity, at_event_id=last_event_id)
    def get_entity(self, entity_id, until=None):
        """
        Returns entity with given ID, optionally as it was until the given domain event ID.
        """

        # Get a snapshot (None if none exist).
        snapshot = self.event_player.get_snapshot(entity_id, until)

        # Decide the initial state, and after when we need to get the events.
        if snapshot is None:
            after = None
            initial_state = None
        else:
            after = snapshot.at_event_id
            initial_state = entity_from_snapshot(snapshot)

        # Replay domain events.
        return self.event_player.replay_events(entity_id,
                                               after=after,
                                               until=until,
                                               initial_state=initial_state)
    def test_snapshots(self):
        stored_event_repo = PythonObjectsStoredEventRepository()
        event_store = EventStore(stored_event_repo)
        self.ps = PersistenceSubscriber(event_store)
        event_player = EventPlayer(event_store=event_store,
                                   id_prefix='Example',
                                   mutate_func=Example.mutate)

        # Create a new entity.
        registered_example = register_new_example(a=123, b=234)

        # Take a snapshot.
        snapshot = take_snapshot(registered_example, uuid1().hex)

        # Replay from this snapshot.
        after = snapshot.at_event_id
        initial_state = entity_from_snapshot(snapshot)
        retrieved_example = event_player.replay_events(
            registered_example.id, initial_state=initial_state, after=after)

        # Check the attributes are correct.
        self.assertEqual(retrieved_example.a, 123)

        # Remember the time now.
        timecheck1 = uuid1().hex

        # Change attribute value.
        retrieved_example.a = 999

        # Check the initial state doesn't move.
        self.assertEqual(initial_state.a, 123)

        # Remember the time now.
        timecheck2 = uuid1().hex

        # Change attribute value.
        retrieved_example.a = 9999

        # Remember the time now.
        timecheck3 = uuid1().hex

        # Check the event sourced entities are correct.
        assert initial_state.a == 123
        retrieved_example = event_player.replay_events(registered_example.id)
        self.assertEqual(retrieved_example.a, 9999)

        # Take another snapshot.
        snapshot2 = take_snapshot(retrieved_example, uuid1().hex)

        # Check we can replay from this snapshot.
        initial_state2 = entity_from_snapshot(snapshot2)
        after2 = snapshot2.domain_event_id
        retrieved_example = event_player.replay_events(
            registered_example.id, initial_state=initial_state2, after=after2)
        # Check the attributes are correct.
        self.assertEqual(retrieved_example.a, 9999)

        # Check we can get historical state at timecheck1.
        retrieved_example = event_player.replay_events(registered_example.id,
                                                       until=timecheck1)
        self.assertEqual(retrieved_example.a, 123)

        # Check we can get historical state at timecheck2.
        retrieved_example = event_player.replay_events(registered_example.id,
                                                       until=timecheck2)
        self.assertEqual(retrieved_example.a, 999)

        # Check we can get historical state at timecheck3.
        retrieved_example = event_player.replay_events(registered_example.id,
                                                       until=timecheck3)
        self.assertEqual(retrieved_example.a, 9999)

        # Similarly, check we can get historical state using a snapshot
        retrieved_example = event_player.replay_events(
            registered_example.id,
            initial_state=initial_state,
            after=after,
            until=timecheck2)
        self.assertEqual(retrieved_example.a, 999)
    def test_snapshots(self):
        stored_event_repo = PythonObjectsStoredEventRepository()
        event_store = EventStore(stored_event_repo)
        self.ps = PersistenceSubscriber(event_store)
        event_player = EventPlayer(event_store=event_store, id_prefix='Example', mutate_func=Example.mutate)

        # Create a new entity.
        registered_example = register_new_example(a=123, b=234)

        # Take a snapshot.
        snapshot = take_snapshot(registered_example, uuid1().hex)

        # Replay from this snapshot.
        after = snapshot.at_event_id
        initial_state = entity_from_snapshot(snapshot)
        retrieved_example = event_player.replay_events(registered_example.id, initial_state=initial_state, after=after)

        # Check the attributes are correct.
        self.assertEqual(retrieved_example.a, 123)

        # Remember the time now.
        timecheck1 = uuid1().hex

        # Change attribute value.
        retrieved_example.a = 999

        # Check the initial state doesn't move.
        self.assertEqual(initial_state.a, 123)

        # Remember the time now.
        timecheck2 = uuid1().hex

        # Change attribute value.
        retrieved_example.a = 9999

        # Remember the time now.
        timecheck3 = uuid1().hex

        # Check the event sourced entities are correct.
        assert initial_state.a == 123
        retrieved_example = event_player.replay_events(registered_example.id)
        self.assertEqual(retrieved_example.a, 9999)

        # Take another snapshot.
        snapshot2 = take_snapshot(retrieved_example, uuid1().hex)

        # Check we can replay from this snapshot.
        initial_state2 = entity_from_snapshot(snapshot2)
        after2 = snapshot2.domain_event_id
        retrieved_example = event_player.replay_events(registered_example.id, initial_state=initial_state2, after=after2)
        # Check the attributes are correct.
        self.assertEqual(retrieved_example.a, 9999)

        # Check we can get historical state at timecheck1.
        retrieved_example = event_player.replay_events(registered_example.id, until=timecheck1)
        self.assertEqual(retrieved_example.a, 123)

        # Check we can get historical state at timecheck2.
        retrieved_example = event_player.replay_events(registered_example.id, until=timecheck2)
        self.assertEqual(retrieved_example.a, 999)

        # Check we can get historical state at timecheck3.
        retrieved_example = event_player.replay_events(registered_example.id, until=timecheck3)
        self.assertEqual(retrieved_example.a, 9999)

        # Similarly, check we can get historical state using a snapshot
        retrieved_example = event_player.replay_events(registered_example.id, initial_state=initial_state, after=after, until=timecheck2)
        self.assertEqual(retrieved_example.a, 999)