Closed Alex1304 closed 3 years ago
Given the direction it's taking, it looks like the new stores api is going to reside either in d4j-common or in a brand new module, leaving this one for the old stores only. I'm going to close this PR and work exclusively on https://github.com/Discord4J/Discord4J/pull/788 going forward, I'll add any new classes in d4j-common and will let you decide later what should belong to a new module.
As another attempt to design a Store API that would allow implementors to make proper optimizations on store access operations, I tried to make one that is based on actions rather than generic CRUD operations. Now I fully understand the need to have something action-specific rather than trying to be too generic, I hope we can come to a viable solution this time.
For that there were basically two options, either have a huge interface with dozens of methods for every possible action on the store, or have a common interface for individual actions, reducing the Store interface to just one method that accepts an action as parameter. I went for the second option, because it would allow more things like making user-defined actions, have Stores that only handle a subset of actions, and most importantly not break backward-compatibility when adding a new action to the API.
Here are a few things to understand about the content of this PR:
discord4j.store.api.wip
package for the time beingSwitchingStore
is basically the equivalent of the currentMappingStoreService
. Except that is is capable of filtering on actions instead of entities, and it can also multi-cast an action onto several stores.<R>
onStoreAction
corresponds to the expected return type of the action. It is only used to infer the return type ofStore#execute
.StoreActionHandlingHelper
exists because the implementations ofStore
would otherwise have to use tons ofinstanceof
and also make tons of unsafe casts because of the<R>
generic parameter. It is not mandatory for implementors to use it, hence why I've put it in anutil
package, but I'm sure that could help a lot.action
contains empty intermediate interfaces such asReadAction
,GatewayWriteAction
, etc. They don't provide extra methods, but will be very useful to write conditions for aSwitchingStore
, and also give the ability to make read-only stores and whatnot.GatewayWriteAction
), and on the other hand write actions that could be performed manually (ExternalWriteAction
), which can open up the path for REST caching. The Store implementation is free to support either only Gateway or External, or both.Some example usage of the new API:
How to use a store:
How to implement a store using the helper class: