You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Implement a Switching and SPARQL-based ResourceStore #59
As a followup to the file-based ResourceStore of #46, we also want to be able to support SPARQL-based backends.
Data would be sent to and retrieved from a SPARQL endpoint by converting incoming requests to SPARQL queries.
We also want some sort of SwitchingResourceStore class (potentially abstract with separate specific implementation) that can write data to different stores based on specific parameters of the incoming requests.
Specifically in this case we would want to send triple data to the SPARQL store,
and binary data to a different store (such as a file based store).
But this implementation needs to be generic enough so any type of requirement can be used for any number of stores.
I'm not sure yet how much of this can be generalized, so that part might be quite small compared to the actual implementations.
Practical notes:
It is important to make sure no functionality of the file store is duplicated. All functionality that is required by both stores should be provided by a separate external class.
The SPARQL store should only support incoming internal/quads data and can reject all other types.
Don't write SPARQL queries as strings, use a library like SPARQL.js.
One way the switching store could work would be to try to convert incoming data to internal/quads using the currently existing converters. If that fails the data should be written to the other store.
The SPARQL store should only support patches of the sparql-update kind.
The graph value of the quads can be used to identify to which resource the triples in the store belong.
Container triples will need to be instantiated in the store to keep track of container contents.
We also want some sort of SwitchingResourceStore class (potentially abstract with separate specific implementation)
I would suggest a single RoutingResourceStore with an injectable (abstract) ResourceRouter. This router takes a set of rules as contructor argument, and has a method findResourceStore taking a resource identifier (and potentially also representation preferences) as an argument, returning the applicable store. (So the strategy pattern.)
One router implementation could be path-based, so the rules take the form of a path/store map, e.g., /binary/* goes to a filesystem, and the rest to a SPARQL endpoint. Another could be content-type based.
The original idea is to have 1 specific store that can be injected with a "rule". We also want to support multiple rules, but if we make the rules a subclass of AsyncHandler we can use a CompositeAsyncHandler for that. These rules would take as input the identifier and the representation if present) and output the store that needs to be checked.
Relevant for this issue is the content-type rule that writes all RDF data to a SPARQL store and all other data to an other store (file store for example). This is very easy for incoming data (POST/PUT) since we can check if the content-type can be converted to Quads. The problem is when doing a GET (or PATCH/DELETE) since we have no way of knowing in what store the resource is stored. The only way would be to check in both stores. Following the above, this store would then be returned and the RoutingResourceStore would then call the returned store to get the data. So we would request the data twice. (This is another case for getMetadata but not relevant here and would not help that much actually).
My idea to not have to request the data twice in case of a GET (which is probably going to be the majority of the calls). Would be to have a specific store implementation for every rule. Note that this was under the assumption that only 1 rule would be used at a time (since the content-type one above covers all cases anyway). In that situation RoutingResourceStore would be an abstract store extending the passthrough store and having the abstract function getStore. Standard rules would then extend that and only implement the getStore rule (so implementation-wise would still be simple). But rules that could handle certain verbs (GET/PUT/etc.) more efficiently with more specific function could do so if they wanted. Specifically the content-type rule could override the getRepresentation function to immediately return the data it had to request to find the correct store, thereby not needing to do the same call twice.
The path-based routing store would on the other hand only override the getStore function and check if one of its stored substrings is present in the path for example. (Reason I would use substrings there instead of prefixes is in case we want to have multiple pods in a single store so the path would be /alice/binary/data instead of just /binary/data).
This becomes more difficult if multiple rules would have to be supported at the same time though, besides having some ugly interfaces for the rules which are pretty much going to be ResourceStore & AsyncHandler. Or we could have the content-type routing store be a separate store that can be configured instead of the default routing store. But none of the are ideal and would look messy so we might have to accept the inefficiency if multiple rules need to be able to be supported.
Are we sure we're not doing premature optimization here?
A common case is "SPARQL backend for triples + file store for binary". There are multiple ways to implement this, such as:
if the resource /abc/ does not exist in the file store, it's triples
if the resource /abc/ does not exist as triples, it's binary
get /abc/ in parallel from both and return whichever one does not 404
write metadata about /abc/ to the triple store if it's a binary file
…
So the specific problem of two representation retrievals would only arise in cases 1 and 2.
I think it's alright for now to just have a simple router. This architecture has modularity first and performance second, in general, unless there would be serious issues. I think it will be fine here; if performance is a bottleneck, we can still switch. But for all we know, deployment might use option 3 or 4 or even 5.
Option 3 would also require extra retrievals since it still does a retrieval to check if the resource exists, returns the store, and then the RoutingResourceStore does a retrieval on that store (again).
Option 4 would not be an exact retrieval but for the triple store there is not that much difference between retrieving data or metadata since both require a SPARQL query.
The most efficient solution (if we want a class that only returns a store) would be to always first check the file store since checking if a file exists is cheaper than doing a SPARQL request. That is what I was doing first before I thought of looking into doing it more efficiently.
Are we sure we're not doing premature optimization here?
That is always the difficult question. I don't always know how many extra operations are acceptable :D . But I'll just do it like was originally planned, with routers returning a store based on the input and we can think about changing it if there are issues with it later.
My idea with the options was to sketch that there are multiple routes, so optimizing for one of them could be premature. But not necessarily advocating for any of them in particular 🙂
Option 3 would also require extra retrievals since it still does a retrieval to check if the resource exists, returns the store, and then the RoutingResourceStore does a retrieval on that store (again).
Yeah I was discussing it as a general option, not as a getStore implementation.
Option 4 would not be an exact retrieval but for the triple store there is not that much difference between retrieving data or metadata since both require a SPARQL query.
Agree, but just 1 check then even if you have 5 backends, as opposed to a cascade.
The bigger point being: there are different reasons for choosing different options, so I wouldn't optimize for one just yet.
The most efficient solution (if we want a class that only returns a store) would be to always first check the file store since checking if a file exists is cheaper than doing a SPARQL request. That is what I was doing first before I thought of looking into doing it more efficiently.
I think we're fine for now.
But I'll just do it like was originally planned, with routers returning a store based on the input and we can think about changing it if there are issues with it later.
Activity
RubenVerborgh commentedon Aug 18, 2020
I would suggest a single
RoutingResourceStorewith an injectable (abstract)ResourceRouter. This router takes a set of rules as contructor argument, and has a methodfindResourceStoretaking a resource identifier (and potentially also representation preferences) as an argument, returning the applicable store. (So the strategy pattern.)One router implementation could be path-based, so the rules take the form of a path/store map, e.g.,
/binary/*goes to a filesystem, and the rest to a SPARQL endpoint. Another could be content-type based.jvherweg commentedon Oct 21, 2020
To clear up after separate discussion:
The original idea is to have 1 specific store that can be injected with a "rule". We also want to support multiple rules, but if we make the rules a subclass of
AsyncHandlerwe can use aCompositeAsyncHandlerfor that. These rules would take as input the identifier and the representation if present) and output the store that needs to be checked.Relevant for this issue is the content-type rule that writes all RDF data to a SPARQL store and all other data to an other store (file store for example). This is very easy for incoming data (POST/PUT) since we can check if the content-type can be converted to Quads. The problem is when doing a GET (or PATCH/DELETE) since we have no way of knowing in what store the resource is stored. The only way would be to check in both stores. Following the above, this store would then be returned and the
RoutingResourceStorewould then call the returned store to get the data. So we would request the data twice. (This is another case forgetMetadatabut not relevant here and would not help that much actually).My idea to not have to request the data twice in case of a GET (which is probably going to be the majority of the calls). Would be to have a specific store implementation for every rule. Note that this was under the assumption that only 1 rule would be used at a time (since the content-type one above covers all cases anyway). In that situation
RoutingResourceStorewould be an abstract store extending the passthrough store and having the abstract functiongetStore. Standard rules would then extend that and only implement thegetStorerule (so implementation-wise would still be simple). But rules that could handle certain verbs (GET/PUT/etc.) more efficiently with more specific function could do so if they wanted. Specifically the content-type rule could override thegetRepresentationfunction to immediately return the data it had to request to find the correct store, thereby not needing to do the same call twice.The path-based routing store would on the other hand only override the
getStorefunction and check if one of its stored substrings is present in the path for example. (Reason I would use substrings there instead of prefixes is in case we want to have multiple pods in a single store so the path would be/alice/binary/datainstead of just/binary/data).This becomes more difficult if multiple rules would have to be supported at the same time though, besides having some ugly interfaces for the rules which are pretty much going to be
ResourceStore & AsyncHandler. Or we could have the content-type routing store be a separate store that can be configured instead of the default routing store. But none of the are ideal and would look messy so we might have to accept the inefficiency if multiple rules need to be able to be supported.RubenVerborgh commentedon Oct 21, 2020
Are we sure we're not doing premature optimization here?
A common case is "SPARQL backend for triples + file store for binary". There are multiple ways to implement this, such as:
So the specific problem of two representation retrievals would only arise in cases 1 and 2.
I think it's alright for now to just have a simple router. This architecture has modularity first and performance second, in general, unless there would be serious issues. I think it will be fine here; if performance is a bottleneck, we can still switch. But for all we know, deployment might use option 3 or 4 or even 5.
jvherweg commentedon Oct 23, 2020
Option 3 would also require extra retrievals since it still does a retrieval to check if the resource exists, returns the store, and then the
RoutingResourceStoredoes a retrieval on that store (again).Option 4 would not be an exact retrieval but for the triple store there is not that much difference between retrieving data or metadata since both require a SPARQL query.
The most efficient solution (if we want a class that only returns a store) would be to always first check the file store since checking if a file exists is cheaper than doing a SPARQL request. That is what I was doing first before I thought of looking into doing it more efficiently.
That is always the difficult question. I don't always know how many extra operations are acceptable :D . But I'll just do it like was originally planned, with routers returning a store based on the input and we can think about changing it if there are issues with it later.
RubenVerborgh commentedon Oct 23, 2020
My idea with the options was to sketch that there are multiple routes, so optimizing for one of them could be premature. But not necessarily advocating for any of them in particular 🙂
Yeah I was discussing it as a general option, not as a
getStoreimplementation.Agree, but just 1 check then even if you have 5 backends, as opposed to a cascade.
The bigger point being: there are different reasons for choosing different options, so I wouldn't optimize for one just yet.
I think we're fine for now.
❤️
jvherweg commentedon Nov 2, 2020
Closed by #279 and #283