-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Snapshotter #9
Merged
Merged
Snapshotter #9
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
/** | ||
* This file is part of the prooph/micro. | ||
* (c) 2017-2017 prooph software GmbH <contact@prooph.de> | ||
* (c) 2017-2017 Sascha-Oliver Prolic <saschaprolic@googlemail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ProophExample\Micro; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
/** | ||
* This file is part of the prooph/micro. | ||
* (c) 2017-2017 prooph software GmbH <contact@prooph.de> | ||
* (c) 2017-2017 Sascha-Oliver Prolic <saschaprolic@googlemail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Prooph\MicroExample\Script; | ||
|
||
use Prooph\Common\Messaging\Message; | ||
use Prooph\EventStore\EventStore; | ||
use Prooph\Micro\SnapshotReadModel; | ||
use Prooph\MicroExample\Infrastructure\UserAggregateDefinition; | ||
|
||
$autoloader = require __DIR__ . '/../vendor/autoload.php'; | ||
$autoloader->addPsr4('Prooph\\MicroExample\\', __DIR__); | ||
require 'Model/User.php'; | ||
|
||
//We could also use a container here, if dependencies grow | ||
$factories = include 'Infrastructure/factories.php'; | ||
|
||
$eventStore = $factories['eventStore'](); | ||
|
||
/* @var EventStore $eventStore */ | ||
|
||
$readModel = new SnapshotReadModel( | ||
$eventStore, | ||
$factories['snapshotStore'](), | ||
new UserAggregateDefinition() | ||
); | ||
|
||
$projection = $eventStore->createReadModelProjection( | ||
'user_snapshots', | ||
$readModel | ||
); | ||
|
||
$projection | ||
->fromStream('user_stream') | ||
->whenAny(function ($state, Message $event): void { | ||
$this->readModel()->stack('replay', $event); | ||
}) | ||
->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php | ||
/** | ||
* This file is part of the prooph/micro. | ||
* (c) 2017-2017 prooph software GmbH <contact@prooph.de> | ||
* (c) 2017-2017 Sascha-Oliver Prolic <saschaprolic@googlemail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Prooph\Micro; | ||
|
||
use Prooph\Common\Messaging\Message; | ||
use Prooph\EventStore\EventStore; | ||
use Prooph\EventStore\Projection\ReadModel; | ||
use Prooph\SnapshotStore\Snapshot; | ||
use Prooph\SnapshotStore\SnapshotStore; | ||
|
||
final class SnapshotReadModel implements ReadModel | ||
{ | ||
/** | ||
* @var EventStore | ||
*/ | ||
private $eventStore; | ||
|
||
/** | ||
* @var SnapshotStore | ||
*/ | ||
private $snapshotStore; | ||
|
||
/** | ||
* @var AggregateDefiniton | ||
*/ | ||
private $aggregateDefinition; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $cache = []; | ||
|
||
public function __construct( | ||
EventStore $eventStore, | ||
SnapshotStore $snapshotStore, | ||
AggregateDefiniton $aggregateDefiniton | ||
) { | ||
$this->snapshotStore = $snapshotStore; | ||
$this->aggregateDefinition = $aggregateDefiniton; | ||
} | ||
|
||
public function stack(string $operation, ...$events): void | ||
{ | ||
foreach ($events as $event) { | ||
if (! $event instanceof Message) { | ||
throw new \RuntimeException(get_class($this) . ' can only handle events of type ' . Message::class); | ||
} | ||
|
||
$aggregateId = $this->aggregateDefinition->extractAggregateId($event); | ||
|
||
if (! isset($this->cache[$aggregateId])) { | ||
$snapshot = $this->snapshotStore->get( | ||
$this->aggregateDefinition->aggregateType(), | ||
$aggregateId | ||
); | ||
|
||
if (! $snapshot) { | ||
$state = []; | ||
} else { | ||
$state = $snapshot->aggregateRoot(); | ||
} | ||
|
||
$version = $state[$this->aggregateDefinition->versionName()] ?? 0; | ||
++$version; | ||
|
||
$missingEvents = $this->eventStore->load( | ||
$this->aggregateDefinition->streamName($aggregateId), | ||
$version, | ||
null, | ||
$this->aggregateDefinition->metadataMatcher($aggregateId) | ||
); | ||
|
||
$state = $this->aggregateDefinition->apply($state, $missingEvents); | ||
} else { | ||
$state = $this->cache[$aggregateId]; | ||
} | ||
|
||
$this->cache[$aggregateId] = $this->aggregateDefinition->apply($state, $event); | ||
} | ||
} | ||
|
||
public function persist(): void | ||
{ | ||
foreach ($this->cache as $aggregateId => $state) { | ||
$this->snapshotStore->save(new Snapshot( | ||
$this->aggregateDefinition->aggregateType(), | ||
$aggregateId, | ||
$state, | ||
$state[$this->aggregateDefinition->versionName()], | ||
new \DateTimeImmutable('now', new \DateTimeZone('UTC')) | ||
)); | ||
} | ||
|
||
$this->cache = []; | ||
} | ||
|
||
public function init(): void | ||
{ | ||
throw new \BadMethodCallException('Initializing a snapshot read model is not supported'); | ||
} | ||
|
||
public function isInitialized(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function reset(): void | ||
{ | ||
throw new \BadMethodCallException('Resetting a snapshot read model is not supported'); | ||
} | ||
|
||
public function delete(): void | ||
{ | ||
throw new \BadMethodCallException('Deleting a snapshot read model is not supported'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@prolic Do you think we can rename
Aggregate::aggregateRoot()
toAggregate::state()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The snapshot-store is also used in the event-sourcing lib. I could not come up with a name that fits both perfectly.