New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement script level cache #403
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,8 @@ package ammonite.repl | |
|
||
import acyclic.file | ||
import ammonite.ops._ | ||
import ammonite.repl.Util.{IvyMap, CompileCache, ClassFiles} | ||
import Parsers.ImportTree | ||
import ammonite.repl.Util.{IvyMap, CompileCache, ClassFiles, CacheOutput} | ||
import org.apache.ivy.plugins.resolver.RepositoryResolver | ||
|
||
import scala.util.Try | ||
|
@@ -22,6 +23,15 @@ trait Storage{ | |
val ivyCache: StableRef[IvyMap] | ||
def compileCacheSave(path: String, tag: String, data: CompileCache): Unit | ||
def compileCacheLoad(path: String, tag: String): Option[CompileCache] | ||
def classFilesListSave(pkg: String, | ||
wrapper: String, | ||
dataList: Seq[(String, String)], | ||
imports: Imports, | ||
tag: String, | ||
importTreesList: Seq[ImportTree]): Unit | ||
def classFilesListLoad(pkg: String, | ||
wrapper: String, | ||
cacheTag: String): Option[CacheOutput] | ||
} | ||
|
||
object Storage{ | ||
|
@@ -42,6 +52,9 @@ object Storage{ | |
} | ||
|
||
var compileCache: mutable.Map[String, (String, CompileCache)] = mutable.Map.empty | ||
val classFilesListcache = { | ||
mutable.Map.empty[String, (String, Seq[(String, String)], Imports, Seq[ImportTree])] | ||
} | ||
def compileCacheSave(path: String, tag: String, data: CompileCache): Unit = { | ||
compileCache(path) = (tag, data) | ||
} | ||
|
@@ -51,6 +64,35 @@ object Storage{ | |
if loadedTag == tag | ||
} yield data | ||
} | ||
|
||
def classFilesListSave(pkg: String, | ||
wrapper: String, | ||
dataList: Seq[(String, String)], | ||
imports: Imports, | ||
tag: String, | ||
importTreesList: Seq[ImportTree]): Unit = { | ||
val dir = pkg + "." + wrapper | ||
classFilesListcache(dir) = (tag, dataList.reverse, imports, importTreesList) | ||
} | ||
|
||
def classFilesListLoad(pkg: String, | ||
wrapper: String, | ||
cacheTag: String): Option[CacheOutput] = { | ||
val dir = pkg + "." + wrapper | ||
classFilesListcache.get(dir) match{ | ||
case None => None | ||
case Some((loadedTag, classFilesList, imports, importTreesList)) => | ||
if (loadedTag == cacheTag) { | ||
val res = { | ||
for((path, tag) <- classFilesList) yield { | ||
compileCacheLoad(path, tag) | ||
} | ||
}.flatten | ||
Some((classFilesList.unzip._1, res.unzip._1, imports, importTreesList)) | ||
} | ||
else None | ||
} | ||
} | ||
} | ||
|
||
|
||
|
@@ -62,6 +104,7 @@ object Storage{ | |
// someone upgrades Ammonite. | ||
val cacheDir = dir/'cache/ammonite.Constants.version | ||
val compileCacheDir = cacheDir/'compile | ||
val classFilesOrder = "classFilesOrder.json" | ||
val ivyCacheFile = cacheDir/"ivycache.json" | ||
val metadataFile = "metadata.json" | ||
val historyFile = dir/'history | ||
|
@@ -90,6 +133,62 @@ object Storage{ | |
} | ||
} | ||
|
||
def classFilesListSave(pkg: String, | ||
wrapper: String, | ||
dataList: Seq[(String, String)], | ||
imports: Imports, | ||
tag: String, | ||
importTreesList: Seq[ImportTree]): Unit = { | ||
val dir = pkg.replace("/", "$div") + "." + wrapper.replace("/", "$div") | ||
val codeCacheDir = compileCacheDir/'scriptCaches/dir | ||
if(!exists(codeCacheDir)) { | ||
mkdir(codeCacheDir) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indentation |
||
write( | ||
codeCacheDir/classFilesOrder, | ||
upickle.default.write((tag, dataList.reverse), indent = 4) | ||
) | ||
write(codeCacheDir/"imports.list", upickle.default.write(imports)) | ||
write(codeCacheDir/"importTrees.json", upickle.default.write(importTreesList, indent = 4)) | ||
} | ||
} | ||
|
||
def classFilesListLoad(pkg: String, | ||
wrapper: String, | ||
cacheTag: String): Option[CacheOutput] = { | ||
|
||
val dir = pkg.replace("/", "$div") + "." + wrapper.replace("/", "$div") | ||
val codeCacheDir = compileCacheDir/'scriptCaches/dir | ||
if(!exists(codeCacheDir)) None | ||
else { | ||
val metadataJson = Try { | ||
read(codeCacheDir/classFilesOrder) | ||
}.toOption | ||
val impFile = Try { | ||
read(codeCacheDir/"imports.list") | ||
}.toOption | ||
val impTrees = Try{ | ||
read(codeCacheDir/"importTrees.json") | ||
}.toOption | ||
(metadataJson, impFile, impTrees) match{ | ||
case (Some(metadata), Some(imp), Some(importTrees)) => | ||
val (loadedTag, classFilesList) = | ||
upickle.default.read[(String, Seq[(String, String)])](metadata) | ||
if (cacheTag == loadedTag){ | ||
val res = { | ||
for((path, tag) <- classFilesList) yield { | ||
compileCacheLoad(path, tag) | ||
} | ||
}.flatten | ||
val imports = upickle.default.read[Imports](imp) | ||
val impTreeList = upickle.default.read[Seq[ImportTree]](importTrees) | ||
Some((classFilesList.unzip._1, res.unzip._1, imports, impTreeList)) | ||
} | ||
else None | ||
case _ => None | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This if-statement is copy-pasted from above. Can you do something about it? |
||
} | ||
} | ||
|
||
def compileCacheLoad(path: String, tag: String): Option[CompileCache] = { | ||
val tagCacheDir = compileCacheDir/path.replace("/", "$div").replace(":", "$colon") | ||
if(!exists(tagCacheDir)) None | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This
exists
check is not necessary;mkdir
does it for you already