cache compiled schema and implement cache invalidation strategy#16
cache compiled schema and implement cache invalidation strategy#16srivastava-diya wants to merge 7 commits into
Conversation
jdesrosiers
left a comment
There was a problem hiding this comment.
We need to handle cache invalidation when a schema changes.
0ad2b54 to
46a1d63
Compare
id to fixtureSchemaUri in schemaValidation testuse json-document model
006ca38 to
58f499e
Compare
| schemaStore.clear(change.uri); | ||
| } | ||
|
|
||
| for (const document of documents.all()) { |
There was a problem hiding this comment.
i am figuring out some way to find which open files reference the updated schema, until then i am looping through and revalidating all open documents.
There was a problem hiding this comment.
What you're doing in onDidChangeContent is the right idea for knowing which documents to revalidate. That will revalidate any document that uses that schema in $schema.
However, consider the case where schema A.shcema.json references schema B.schema.json and instance C.json uses $schema: A.schema.json. If B.schema.js changes, it wouldn't trigger revalidation of C.json. So, we need to know not just the $schema URI, but also the URIs of any referenced schemas.
We can calculate that list of dependent schemas for the compiled schema AST. The AST is an object whose keys are the URIs of all schema locations. If you strip the fragment off of each of those URIs and remove the duplicates, you will end up with a list of schema URIs that the instance is dependent on.
Earlier this week, I updated @hyperjump/json-schema-errors to expose evaluateCompiledSchema, which should be helpful for this. We can compile the schema to get the AST, then use that to both determine the dependent schema and validate instances.
|
hey @jdesrosiers This PR is still a WIP, so I'm not looking for approval just yet. I'd really appreciate a review to make sure if I'm proceeding in the right direction before I continue implementing the other stuff. |
jdesrosiers
left a comment
There was a problem hiding this comment.
You're on the right track! There's some clean up and optimization we can do, but we can focus on that later after we get it working.
| schemaStore.clear(change.uri); | ||
| } | ||
|
|
||
| for (const document of documents.all()) { |
There was a problem hiding this comment.
What you're doing in onDidChangeContent is the right idea for knowing which documents to revalidate. That will revalidate any document that uses that schema in $schema.
However, consider the case where schema A.shcema.json references schema B.schema.json and instance C.json uses $schema: A.schema.json. If B.schema.js changes, it wouldn't trigger revalidation of C.json. So, we need to know not just the $schema URI, but also the URIs of any referenced schemas.
We can calculate that list of dependent schemas for the compiled schema AST. The AST is an object whose keys are the URIs of all schema locations. If you strip the fragment off of each of those URIs and remove the duplicates, you will end up with a list of schema URIs that the instance is dependent on.
Earlier this week, I updated @hyperjump/json-schema-errors to expose evaluateCompiledSchema, which should be helpful for this. We can compile the schema to get the AST, then use that to both determine the dependent schema and validate instances.
Description
The
validate(schemaUri)function is curried calling it with just the schema URI compiles the schema and returns a reusable validator function. Previously this compilation was happening on every keystroke.