Skip to content

Updates to MalloyTranslator to enable model caching#2091

Merged
christopherswenson merged 14 commits intomainfrom
crs-model-caching-2
Jan 13, 2025
Merged

Updates to MalloyTranslator to enable model caching#2091
christopherswenson merged 14 commits intomainfrom
crs-model-caching-2

Conversation

@christopherswenson
Copy link
Contributor

@christopherswenson christopherswenson commented Jan 13, 2025

Changes

  • Move queryList into ModelDef
  • Add dependencies to ModelDef, which is a nested object map (url to dependency tree)
  • Translator's translate() method returns an object with a modelDef: ModelDef, rather than a translated: { modelDef: ModelDef }
  • Add a utility method to a translator translatorForDependency which returns the translator for a dependent URL.
  • Add a utility method newlyTranslatedDependencies, which returns all dependencies that were newly translated
  • Allow a user of a translator to pass { translations: { [url]: ModelDef } } to the update method to provide pre-translated models. It us up to the user to only do so when those models are known to be up-to-date. The user may respond to { urls: ... } requests in this way.

Note, this preserves the translate().fromSources property, but translate().modelDef.dependencies contains the same information plus the tree structure. VSCode seems to prefer the flat version, so leaving it for now.

Next Steps

  • Add an interface Cache for the Runtime API, which has get() and set(key: string) methods
  • Allow a Cache to be passed into a Runtime on construction
  • Modify URLReader.readURL to return both the file contents and an "invalidation key"
    • An "invalidation key" represents the freshness of the file; it must be true that if two invalidation keys for a file are the same, then the file has not changed. Valid invalidation keys include
      • File hashes
      • Sequence numbers
      • File contents (though this is pretty silly)
      • Last updated times (if this is used, it is up to the user to be sure that last updated times cannot be modified manually)
  • Add method getInvalidationKey to URLReader, which should return the current invalidation key of a file.
  • Implement a ModelCache class in malloy.ts which manages invalidation keys and decides whether a file can be retrieved from cache or should be recompiled
  • Maybe remove translate().fromSources

Example Dependency Traversal

const translator = new MalloyTranslator('a.malloy');
translator.translate(); // { urls: 'a.malloy' }
translator.update({urls: {'a.malloy': fileContentsA}});
translator.translate(); // { urls: 'b.malloy' }
translator.update({translations: {'b.malloy': modelDefB}});
const result = translator.translate();
if (result.modelDef) {
  cache.set('a.malloy', {
    modelDef: result.modelDef,
    invalidationKey: invalidationKeys['a.malloy'];
  });
  const dependencies = translator.newlyTranslatedDependencies();
  for (const dependency of dependencies) {
    cache.set(dependency.url, {
      modelDef: dependency.modelDef,
      invalidationKey: invalidationKeys[dependency.url]
    }
  }
}

@christopherswenson christopherswenson merged commit f7fdbdb into main Jan 13, 2025
13 checks passed
@christopherswenson christopherswenson deleted the crs-model-caching-2 branch January 13, 2025 20:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant