From 6906ad4ad5f5b018e3493af8d3d4d9df8ac1e6e7 Mon Sep 17 00:00:00 2001 From: Christopher Vogt Date: Tue, 8 Nov 2016 00:49:25 +0000 Subject: add context based task result cache --- compatibility/Context.java | 1 + stage1/ContextImplementation.scala | 1 + stage1/cbt.scala | 1 + stage2/BasicBuild.scala | 17 +++++++++++++++++ stage2/Stage2.scala | 1 + test/test.scala | 1 + 6 files changed, 22 insertions(+) diff --git a/compatibility/Context.java b/compatibility/Context.java index 921087f..0e2fff5 100644 --- a/compatibility/Context.java +++ b/compatibility/Context.java @@ -14,6 +14,7 @@ public abstract class Context{ public abstract String scalaVersionOrNull(); // needed to propagate scalaVersion to dependendee builds public abstract ConcurrentHashMap permanentKeys(); public abstract ConcurrentHashMap permanentClassLoaders(); + public abstract ConcurrentHashMap taskCache(); public abstract File cache(); public abstract File cbtHome(); public abstract File cbtRootHome(); diff --git a/stage1/ContextImplementation.scala b/stage1/ContextImplementation.scala index 91c54f4..c3f25fd 100644 --- a/stage1/ContextImplementation.scala +++ b/stage1/ContextImplementation.scala @@ -14,6 +14,7 @@ case class ContextImplementation( scalaVersionOrNull: String, permanentKeys: ConcurrentHashMap[String,AnyRef], permanentClassLoaders: ConcurrentHashMap[AnyRef,ClassLoader], + taskCache: ConcurrentHashMap[AnyRef,AnyRef], cache: File, cbtHome: File, cbtRootHome: File, diff --git a/stage1/cbt.scala b/stage1/cbt.scala index 7a239a1..08394f0 100644 --- a/stage1/cbt.scala +++ b/stage1/cbt.scala @@ -88,6 +88,7 @@ object `package`{ scalaVersion.getOrElse(null), permanentKeys, permanentClassLoaders, + taskCache, cache, cbtHome, cbtRootHome, diff --git a/stage2/BasicBuild.scala b/stage2/BasicBuild.scala index 128d2f8..ef4757e 100644 --- a/stage2/BasicBuild.scala +++ b/stage2/BasicBuild.scala @@ -244,4 +244,21 @@ trait BaseBuild extends BuildInterface with DependencyImplementation with Trigge // ========== cbt internals ========== def finalBuild: BuildInterface = this override def show = this.getClass.getSimpleName ++ "(" ++ projectDirectory.string ++ ")" + + // TODO: allow people not provide the method name, maybe via macro + /** + caches given value in context keyed with given key and projectDirectory + the context is fresh on every complete run of cbt + */ + def cached[T <: AnyRef](name: String)(task: => T): T = { + val cache = context.taskCache + val key = (projectDirectory,name) + if( cache.containsKey(key) ){ + cache.get(key).asInstanceOf[T] + } else{ + val value = task + cache.put( key, value ) + value + } + } } diff --git a/stage2/Stage2.scala b/stage2/Stage2.scala index b1539bf..d585cd0 100644 --- a/stage2/Stage2.scala +++ b/stage2/Stage2.scala @@ -39,6 +39,7 @@ object Stage2 extends Stage2Base{ null, args.permanentKeys, args.permanentClassLoaders, + new java.util.concurrent.ConcurrentHashMap, args.cache, args.cbtHome, args.cbtHome, diff --git a/test/test.scala b/test/test.scala index 039ee6a..72360d0 100644 --- a/test/test.scala +++ b/test/test.scala @@ -118,6 +118,7 @@ object Main{ null, new ConcurrentHashMap[String,AnyRef], new ConcurrentHashMap[AnyRef,ClassLoader], + new java.util.concurrent.ConcurrentHashMap[AnyRef,AnyRef], cache, cbtHome, cbtHome, -- cgit v1.2.3