Refactored take_snapshot().
@@ -103,27 +103,22 @@ class EventPlayer(object):
# taken before or at the entity version of the last event. Please
# note, the snapshot might have a smaller version number than
# the last event if events occurred since the last snapshot was taken.
- last_snapshot = self.get_snapshot(entity_id, lte=last_event.entity_version)
+ last_version = last_event.entity_version
+ last_snapshot = self.get_snapshot(entity_id, lte=last_version)
- if last_snapshot and last_snapshot.entity_version == last_event.entity_version:
+ if last_snapshot and last_snapshot.entity_version == last_version:
# If up-to-date snapshot exists, there's nothing to do.
snapshot = last_snapshot
else:
# Otherwise recover entity and take snapshot.
if last_snapshot:
- # Recover entity by applying to snapshotted state the
- # events that occurred since snapshot was taken.
- snapshotted_entity = entity_from_snapshot(last_snapshot)
- entity = self.replay_entity(entity_id,
- gt=last_snapshot.entity_version,
- lte=last_event.entity_version,
- initial_state=snapshotted_entity
- )
+ initial_state = entity_from_snapshot(last_snapshot)
+ gt = last_snapshot.entity_version
else:
- # Recover entity from scratch.
- entity = self.replay_entity(entity_id, lte=last_event.entity_version)
- # Take snapshot of recovered entity.
- snapshot = self.snapshot_strategy.take_snapshot(entity_id, entity, last_event.entity_version)
+ initial_state = None
+ gt = None
+ entity = self.replay_entity(entity_id, gt=gt, lte=last_version, initial_state=initial_state)
+ snapshot = self.snapshot_strategy.take_snapshot(entity_id, entity, last_version)
return snapshot
