Stack: Geode 9.1, SDG 2.0 Scenario: We have a scenario where temporary regions are created(snapshotted) on the fly and would like to leverage the SDG queries to use these new region instead of the defined regions. Question; Is there a hook-point to override the region name post the proxy invocation so we can superimpose the temp-region name instead of the defined region for the SDG repository?
1 Answer
Is there a hook-point to override the Region name post the proxy invocation so we can superimpose the temp Region name instead of the defined Region for the SDG Repository?
Well, yes, there are hook-points (per say) in several areas of SD's Repository infrastructure where you could accomplish this, but it (specifically, SDG) was not intentionally designed with this purpose in mind, so it is a bit involved.
As you know, you enable SDG Repositories using the SDG @EnableGemfireRepositories
annotation on your Spring application @Configuration
class, like so..
@Configuration
@EnableGemfireRepositories(..)
class ApplicationGemFireConfiguration { .. }
Effectively, the "proxy" created by the SD Repository infrastructure for your application-specific, SD Repository interface extension is backed by a default implementation provided by the data store-specific (e.g. GemFire) SD module (e.g. SDG).
1 way in which this default implementation is determined is by the repositoryBaseClass
attribute on the annotation. As you can see, it defaults to o.s.d.g.repository.support.SimpleGemfireRepository
.
Internally, SD's Repository infrastructure, and specifically SDG, creates an instance of the SDG SimpleGemfireRepository
class to back the proxy in order to implement the basic CRUD and query operations defined on SD CrudRepository
interface (so developers do not have to; a developer just need declare additional, application-specific query methods).
The SimpleGemfireRepository
class expects an instance of o.s.d.g.GemfireTemplate
along with EntityInformation
about the application domain object type that is being persisted (handled) by the application SD Repository. As you know, this is defined by the application-specific SD Repository, like so...
interface CustomerRepository extends GemfireRepository<Customer, Long> { .. }
The application domain object type (i.e. entity class; e.g. "Customer
") typically determines the Region in which the objects of that type are persisted/accessed (i.e. mapped)...
@Region("Customers")
class Customer .. { .. }
It is this mapping meta-data (i.e. @Region("Customers")
) that is used by the SD[G] Repository infrastructure/extension by default to determine the GemFire Region in which the entity is mapped. It would have been a bit easier if I have made the "resolution" of the Region 7 configurable, such as with a pluggable Strategy interface, something like "RegionResolver
" (I may consider this).
It is also possible to override the Region on the application Repository interface itself; for instance, in addition to the CustomersRepository
above, you can...
@Region("VIPS")
interface VipCustomerRepository extends CustomersRepository { .. }
Again, see "POJO mapping meta-data" for more information.
Since SimpleGemfireRepository
delegates to the GemfireTemplate
to perform all Region data access operations, it is really the GemfireTemplate
that references the Region used by the repo. As you can see, the GemfireTemplate
takes a reference to the Region on which the data access operations, including "queries", are performed. Again the Region was determine by the SD[G] Repo infrastructure in 7.
So, how can you use this knowledge to, I gather determine the Region "dynamically" at Runtime, since you creating "temporary" Regions as such.
Well, you could provide your own implementing class (in place of SDG's SimpleGemfireRepository
), like so...
@Configuration
@EnableGemfireRepositories(repositoryBaseClass = DynamicRegionGemfireRepository.class)
class ApplicationGemFireConfiguration { .. }
You must still define a constructor that accepts the GemfireTemplate
and EntityInformation
as before, in SDG's SimpleGemfireRepository
class...
class DynamicRegionGemfireRepository<T, ID> extends SimpleGemFireRepository<T, ID {
DynamicRegionGemfireRepository(GemfireTemplate gemfireTemplate, EntityInformation<T, ID> entityInformation) {
super(new DyanmicRegionGemfireTemplate(gemfireTemplate, entityInformation), entityInformation);
}
}
Once you get the reference to the GemfireTemplate
, you could wrap it or replace it with your own implementation as well, such as...
class DynamicRegionGemfireTemplate<T, ID> extends GemfireTemplate {
private final EntityInformation<T, ID> entityInformation;
private final Region<?, T> primaryRegion;
DynamicRegionGemfireTemplate(GemfireTemplate gemfireTemplate, EntityInformation entityInformation) {
this.primaryRegion = gemfireTemplate.getRegion();
}
// override Region<?,?> getRegion() here
}
Now, you override the getRegion()
method on GemfireTemplate
(technically, the o.s.d.g.GemfireAccessor
, which GemfireTemplate
extends).
@Override
public Region<K, V> getRegion() {
// logically determine the "temporary" Region to use,
// perhaps using the EntityInformation, or else just return
// the primary Region if no temporary Region exists.
return primaryRegion;
}
Something like this... make sense?
There are other ways you can accomplish this as well.
Anyway, hope this helps!
Regards, John
-
Thanks John! This helps. We have been able to override the region-name for regions using SimpleGemfireRepository (e.g findOne, findAll). But the custom repository methods defined for each of the repository are not invoking the DynamicRegionGemfireTemplate.getRegion() – @EnableGemfireRepositories(repositoryBaseClass = DynamicRegionGemfireRepository.class, basePackages = {"com.test.repo"}) Oct 5, 2017 at 6:40
-
Submitted PR - github.com/spring-projects/spring-data-gemfire/pull/93 The Travis-ci is not green - not sure of the reason,, can you help? Jan 26, 2018 at 18:36