Skip to content

Use a general store to handle Solid behaviour - #167

Merged
jvherweg merged 9 commits into
masterfrom
data-accessors
Oct 14, 2020
Merged

jvherweg merged 9 commits into
masterfrom
data-accessors

Conversation

@jvherweg

Copy link
Copy Markdown
Member

Resolves #63.

Draft PR since I also want to add integration tests. Specifically I want to run the same integration tests as are currently run on the file store (this actually works but I didn't have time to finish it cleanly today and I'm not here on Tuesday, just in case someone had time to look already tomorrow).

This is a pretty big one so I assume there will be discussions here (including the names 😄). Also grew a bit bigger than expected when working on it. We now sort of have a partial mocking framework for fs. This way we don't need to mock the results of the fs functions in every call and tests don't break if you add one more call somewhere in the source code.

I tried to keep as much as possible into account what might be done or required by future DataAccessors, but I'm happy with how this turned out actualy. The Solid behaviour is now (mostly) handled by a single class, which means when we add some extra features in the future (such as redirects) only this one class will need to be updated. I might also add an InMemoryDataAccessor after the integration part since that should be easy to implement now.

@jvherweg jvherweg added the ☀️ enhancement New feature or request label Sep 28, 2020
@RubenVerborgh

Copy link
Copy Markdown
Member

Just a quick question:

We now sort of have a partial mocking framework for fs

Why don't we simply put files on disk?

@jvherweg

Copy link
Copy Markdown
Member Author

Why don't we simply put files on disk?

That would also work, but I'm a bit hesitant about writing actual files during tests due to the extra overhead and potential issues it could have. But one big thing is that it is now very easy also to quickly set up a file structure for a single test such as here and also quickly see if data was written.

@RubenVerborgh

Copy link
Copy Markdown
Member

quickly set up a file structure for a single test such as here and also quickly see if data was written.

Looks nice indeed!

@jvherweg

Copy link
Copy Markdown
Member Author

Added integration. Since they should have identical functionality, the integration tests of the file store could be completely reused as seen here.

@jvherweg
jvherweg marked this pull request as ready for review September 30, 2020 10:53
@jvherweg

jvherweg commented Oct 1, 2020

Copy link
Copy Markdown
Member Author

I added an InMemoryDataAccessor as well to show how a different DataAccessor would work (going to stop working on this branch now in case the entire idea gets scrapped 😄).

Some things I did notice that would probably need issues:

  • The integration tests were a big help, but still need some work to prevent duplication when you want to run the same tests on different configs (I did some changes but none to big since it's not part of this PR).
  • If this gets merged we should also look into have a generic test that all DataAccessors should pass so you can easily see if a new DataAccessor conforms to what is expected. There currently is some overlap between the tests of the File and the InMemory DataAccessor.

@RubenVerborgh RubenVerborgh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The accessor pattern itself is good (perhaps with the exception of modifyResource).

Can you comment on why the existing file store is still in there? Performance?

Overall, I found some of the longer methods difficult to read. I like code that's partitioned in blocks, preceded by one comment explaining why what is happening. I often had to reconstruct what was happening. Perhaps consider helper methods where it makes the flow more clear.

Also, a couple of private methods can just be protected for me.

Comment thread src/storage/DataAccessorBasedStore.ts Outdated
Comment on lines +25 to +26
* This class is important because this way different stores don't all need to implement behaviour
* Examples:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* This class is important because this way different stores don't all need to implement behaviour
* Examples:
* The DataAccessor interface provides elementary store operations such as read and write.
* This DataAccessorBasedStore uses those elementary store operations
* to implement the more high-level ResourceStore contact, abstracting all common functionality
* such that new stores can be added by implementing the more simple DataAccessor contract.
* DataAccessorBasedStore thereby provides behaviours for reuse across different stores, such as:

Comment thread src/storage/DataAccessorBasedStore.ts Resolved
Comment thread src/storage/DataAccessorBasedStore.ts Outdated
Comment on lines +64 to +72
const result = {
binary: false,
get data(): Readable {
// This allows other modules to still add metadata before the output data is written
return streamifyArray(result.metadata.quads());
},
metadata,
};
return result;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const result = {
binary: false,
get data(): Readable {
// This allows other modules to still add metadata before the output data is written
return streamifyArray(result.metadata.quads());
},
metadata,
};
return result;
return {
binary: false,
get data(): Readable {
// This allows other modules to still add metadata before the output data is written
return streamifyArray(result.metadata.quads());
},
metadata,
};

Comment thread src/storage/DataAccessorBasedStore.ts Outdated
return result;
}

return { binary: metadata.contentType !== INTERNAL_QUADS, data: await this.accessor.getData(identifier), metadata };

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return { binary: metadata.contentType !== INTERNAL_QUADS, data: await this.accessor.getData(identifier), metadata };
return {
binary: metadata.contentType !== INTERNAL_QUADS,
data: await this.accessor.getData(identifier), metadata,
};

Comment thread src/storage/DataAccessorBasedStore.ts Resolved
return entry;
}

private copyData(source: DataEntry): Readable {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't there a better method? The highWaterMark thing was just a hack.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, I should drain the stream into an array.

Comment on lines +10 to +14
/**
* Abstract class that implements `getNormalizedMetadata` by calling the `getMetadata` with the incoming identifier
* and trying again by changing the trailing slash and trying again.
*/
export abstract class NormalizedDataAccessor implements DataAccessor {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming and comment can be clearer. (What is normalizing?)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very open to suggestions there since finding good names for things is way more difficult than actually implementing them

Comment on lines +10 to +14
/**
* Abstract class that implements `getNormalizedMetadata` by calling the `getMetadata` with the incoming identifier
* and trying again by changing the trailing slash and trying again.
*/
export abstract class NormalizedDataAccessor implements DataAccessor {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I would not encode this functionality as a class. Can't FileDataAccessor (or perhaps better, DataAccessorBasedStore, possibly with a flag) have that functionality?

Comment thread src/storage/accessors/DataAccessor.ts Outdated
* then the other URI MUST NOT correspond to another resource."
* @param identifier - Identifier that needs to be checked.
*/
getNormalizedMetadata: (identifier: ResourceIdentifier) => Promise<RepresentationMetadata>;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmm, why is that in the accessor contract? Seems like a more high-level thing to me.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason for this one being in the DataAccessor is performance reasons. Specifically a SPARQL backend can do this with 1 query, but would need 2 queries if it is encoded in the DataAccessorBasedStore. So it's a trade-off. Can always move it back if you don't think the trade-off is worth it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the accessor contract is there for simplicity. So would not add this. Performance can be an additional interface if needed.

Comment thread src/storage/accessors/DataAccessor.ts Outdated
* @param identifier - Resource to modify.
* @param patch - Patch specifying how the resource should be modified.
*/
modifyResource: (identifier: ResourceIdentifier, patch: Patch) => Promise<void>;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was a bit surprised to see that one here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is something we discussed in a meeting before the work on this was started. The problem is that modifyResource is completely dependend on the backend storage. E.g. a file backend can't handle patches but a SPARQL backend can send the update request to the store. There is not really a way around this (except not allowing any backend to have custom patch implementations but that's not ideal), so there needs to be a way to send the patch to the backend. The function can always be renamed to something else of course.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(except not allowing any backend to have custom patch implementations but that's not ideal)

Yeah, this was my suggestion as I was reading this. But perhaps a bit too harsh.
That seems like the kind of optimization for which you'd implement a store then instead of an accessor.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To combine with the comment about getNormalizedMetadata. When making the SPARQL store, this would mean that we would have a SparqlDataAccessorBasedStore (or a name with less than 5 words) and a SparqlDataAccessor. The store would inherit from the DataAccessorBasedStore. The store would override 2 functions: modifyResource and getNormalizedMetadata (now being a protected function of the store). For both of these, SPARQL queries are required, so the store would have to call doSparql, which is a specific function of a SparqlDataAccessor. So that would mean this specific store only works with this specific kind of data accessor.

Not saying that's a bad thing, none of the actual store "behaviour" is duplicated, that's all still stored in the original store, and that is the main important part of this PR for me. Just want to clear up what the result of this is.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Would it also work when plugging in SparqlDataAccessor into a normal DataAccessorBasedStore? So "work" as in functionally identical, just less optimal?

  2. Instead of SparqlDataAccessorBasedStore, how about SparqlDataAccessor actually implements two interfaces: DataAccessor and something like NormalizedDataAccessor? Then DataAccessorBasedStore can check for the optional presence of the second interface, and adjust accordingly?

  3. To what sense are we strongly optimizing here for SPARQL though? I.e., would the Normalized pattern also occur in other cases>

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Yes, there would only be a few more requests.
  2. That would also work. Not sure yet what my opinion is on this compared to inheriting from the store. My first gut reaction is that this might make the store messier since for several functions it might need to check if they exist. Another potential option this made me think of is having the class extend both the store and implement the data accessor interface so all sparql related stuff is contained in the same class, so it would be its own data accessor (but still use most of the generic functions of the default store). But that would be even tighter coupling of course.
  3. That I don't know and is hard to predict. This is relevant specifically for backends where doing an "access" is expensive. In the case of the file store, it's a lot cheaper to check if a file exists on the file system than the cost of sending a query to a triple store.

But I am ok with moving the getNormalizedMetadata to the store, it makes the data accessor even cleaner. But if we ever want to optimize in the future it is something to keep in mind. The good thing is that this doesn't make optimizations impossible (or even super hard).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, sounds good. Derivatives of SparqlDataAccessorBasedStore just seem to defeat the purpose (only a little though, I still see the reuse).

@jvherweg

jvherweg commented Oct 7, 2020

Copy link
Copy Markdown
Member Author

Can you comment on why the existing file store is still in there? Performance?

Because I first wanted to see the response to this PR before going into that discussion 😄 . But yes, I would suggest removing that one (and the InMemoryStore) after this PR since there already are some differences (mostly edge cases based on the spec).

@jvherweg

jvherweg commented Oct 9, 2020

Copy link
Copy Markdown
Member Author

Tried to fix all issues. Might have succeeded!

@jvherweg
jvherweg requested a review from RubenVerborgh October 9, 2020 07:26
@jvherweg
jvherweg force-pushed the data-accessors branch 3 times, most recently from 66d1e32 to 45d51f9 Compare October 9, 2020 14:57
@jvherweg

jvherweg commented Oct 9, 2020

Copy link
Copy Markdown
Member Author

I already rebased to the master branch. One big consequence of this is that the new resourcemapper is used. I did not fully support #146 yet since that would potentially create a few new discussions and I first want to close this one off. This does mean that this PR will not work well with the edge case of updating the content-type of a resource (since that requires changing the extension of the stored file).

@RubenVerborgh RubenVerborgh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beautiful work 😍

Comment thread src/storage/DataAccessorBasedStore.ts Resolved
Comment thread src/storage/DataAccessorBasedStore.ts Resolved
Comment thread src/storage/DataAccessorBasedStore.ts Resolved
Comment thread src/storage/DataAccessorBasedStore.ts Resolved
Comment thread src/storage/DataAccessorBasedStore.ts Resolved
Comment thread src/storage/accessors/DataAccessor.ts Resolved
Comment thread src/storage/accessors/DataAccessor.ts Resolved
}

// eslint-disable-next-line @typescript-eslint/naming-convention
public _read(): void {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public _read(): void {
protected _read(): void {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Has to be public to match the Readable interface.

Comment thread src/util/Util.ts Resolved
The root container wasn't being created for file based tests.
Containers should be POSTed as RDF bodies.
Containment triples of child containers had no trailing slash.
@jvherweg
jvherweg merged commit b896004 into master Oct 14, 2020
@jvherweg
jvherweg deleted the data-accessors branch October 14, 2020 09:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

☀️ enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Investigate creating new interface for file/sparql store backends

2 participants