Skip to content
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

Implement arbitrary Queryable interface. #3

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

leehambley
Copy link
Contributor

@leehambley leehambley commented Jun 4, 2019

Status Quo

There are presently two ways to "query" a Depot/Repo.

  1. Repo, concerned with the "Repository Pattern" inspired by DDD allows looking up and rehydration of a specific named aggregate. (repo.Rehydrate("users/123") => User<id: 123.....>).
  2. Depo, a ES specific storage concern allows subscriptions to simple matchers (globs against partition names (e.g depot.Watch("users/123") or depot.Watch("users/*")).

This PR

A new Matching() API will be added to the Depot as it is more a storage related concern than a DDD concern and I would not expect it to see widespread use in the context of a Command. (this may change, and both Depot and Repo may implement if it is useful in both places)

The Matching() API will run synchronously and will take a Matcher instance. The Matcher will get a chance to explore the entire storage space. Implementations of the Matching() API will be expected to gather Checkpoints with some meta-data about what they matched and why.

The new API is exposed through a new named interface named Queryable, this reflects that maybe Repo and Depot are both queryable, depending on the implementation.

No thought has been given on whether the API should support limiting runtime (other than through respecting the given context.Context or depth in terms of numbers of Checkpoints or distance into the past.

Examples of Matchers may include:

  • SessionID("0x00") used to look up any Checkpoints which reference that SessionID in their headers.
  • Glob("partition/*") used to look up any Checkpoint referencing an Affix where a partition matching the globbed pattern is named.
  • Attribute("partition/*", "$.store.book[?(@.price < 10)]") This would likely be extremely slow in reality but may expose interesting possibilities. The syntax in this contrived example is (likely invalid) JSONPath but some annotation language for pulling values out of the serialized JSON objects would be handy in some instances I think.

Filters

The Matching() API is likely to push Checkpoint hashes onto a stack whilst walking the Merkle tree but I believe the use-cases will prefer consuming events, bounded by affixes in many cases.

I suspect Matching() will return some kind of ResultSet which gives an option of iterating over Checkpoints, or Affixes or Events.

For anti-fraud knowing the metadata about the distribution of Checkpoints where Orders were created over time may be relevant, but one may also legitimately just want to fetch a list of things created in a given session, in which case walking the events without caring about time spans, or the affix boundaries is sufficient.

Combining Matchers

It may turn out to be simple enough to create some boolean combinators for matchers which create new matchers so that one can combine SessionID and Glob() matchers for e.g but this isn't really a concern of changing the Depot/Repo API.

Lee Hambley and others added 13 commits June 4, 2019 22:14
…ary queries, copy and tweak code from Repo.Rehydrate as a basis
Demonstrates the simplest case of ranging over parents and collecting
more and more and more matches.

An alwaysMatches helper has been added to the tests to allow me to test
that in a naive case we'll always match everything ensuring we do a
proper full walk of the storage.

Test setup had a mistake too as the head ref pointed at a checkpoint
which referenced an affix which doesn't exist, easily corrected.
Modify existing codepaths (all tests pass now) to handle the new types.

Fix a few package naming issues (matcher can't import retro can't import
matcher) by making sure that retro.PartitionName isn't required in the
matcher package (the caller can cast to a string, first)
This genericise the behaviour of collecting a historical record from a
given ref backwards and linerarizing the result.

The implementation is sub optimal, with a full tree walk from ref to the
first ancestorless checkpoint. Sometimes twice, if a merge checkpoint
is present in the history.

Worse, for each checkpoint discovered the list of all hitherto
discovered checkpoints will be binary-searched to find the correct index
at which to insert the incoming checkpoint.

Aside from practical concerns about realworld performance, the
implementation itself leaves a little to be desired. Waitgroups,
blocking returns, and conditional branching seem like I may have missed
a more elegant solution in the Chronological#run() function.

One noteworthy remark: we can reach "the beginning" 1+n times given no
cyclic histories. Possibly some of this book-keeping can be moved to the
tocCPs type where the slice can maybe converted to a single-use sized,
and buffered channel. This would eliminate a lot of the book-keeping
from the Chronological index.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant