You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwhisk.apache.org by ra...@apache.org on 2018/03/06 18:50:02 UTC

[incubator-openwhisk] branch master updated: Adds method deleteAttachments to ArtifactStore (#3366)

This is an automated email from the ASF dual-hosted git repository.

rabbah pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-openwhisk.git


The following commit(s) were added to refs/heads/master by this push:
     new 6711118  Adds method deleteAttachments to ArtifactStore (#3366)
6711118 is described below

commit 6711118387281774c7015b51a763b31a47e94811
Author: Chetan Mehrotra <ch...@apache.org>
AuthorDate: Wed Mar 7 00:19:59 2018 +0530

    Adds method deleteAttachments to ArtifactStore (#3366)
---
 .../scala/whisk/core/database/ArtifactStore.scala     |  5 +++++
 .../scala/whisk/core/database/CouchDbRestStore.scala  |  5 +++++
 .../scala/whisk/core/database/DocumentFactory.scala   | 14 ++++++++++++++
 .../main/scala/whisk/core/entity/WhiskAction.scala    | 18 ++++++++++++++++++
 tests/build.gradle                                    |  3 ++-
 .../scala/whisk/core/entity/test/DatastoreTests.scala | 19 ++++++++++++++++---
 6 files changed, 60 insertions(+), 4 deletions(-)

diff --git a/common/scala/src/main/scala/whisk/core/database/ArtifactStore.scala b/common/scala/src/main/scala/whisk/core/database/ArtifactStore.scala
index 33ea805..631f60a 100644
--- a/common/scala/src/main/scala/whisk/core/database/ArtifactStore.scala
+++ b/common/scala/src/main/scala/whisk/core/database/ArtifactStore.scala
@@ -129,6 +129,11 @@ trait ArtifactStore[DocumentAbstraction] {
   protected[core] def readAttachment[T](doc: DocInfo, name: String, sink: Sink[ByteString, Future[T]])(
     implicit transid: TransactionId): Future[(ContentType, T)]
 
+  /**
+   * Deletes all attachments linked to given document
+   */
+  protected[core] def deleteAttachments[T](doc: DocInfo)(implicit transid: TransactionId): Future[Boolean]
+
   /** Shut it down. After this invocation, every other call is invalid. */
   def shutdown(): Unit
 }
diff --git a/common/scala/src/main/scala/whisk/core/database/CouchDbRestStore.scala b/common/scala/src/main/scala/whisk/core/database/CouchDbRestStore.scala
index efc03cc..d55db65 100644
--- a/common/scala/src/main/scala/whisk/core/database/CouchDbRestStore.scala
+++ b/common/scala/src/main/scala/whisk/core/database/CouchDbRestStore.scala
@@ -456,6 +456,11 @@ class CouchDbRestStore[DocumentAbstraction <: DocumentSerializer](dbProtocol: St
           ErrorLevel))
   }
 
+  override protected[core] def deleteAttachments[T](doc: DocInfo)(implicit transid: TransactionId): Future[Boolean] =
+    // NOTE: this method is not intended for standalone use for CouchDB.
+    // To delete attachments, it is expected that the entire document is deleted.
+    Future.successful(true)
+
   override def shutdown(): Unit = {
     Await.ready(client.shutdown(), 1.minute)
   }
diff --git a/common/scala/src/main/scala/whisk/core/database/DocumentFactory.scala b/common/scala/src/main/scala/whisk/core/database/DocumentFactory.scala
index 59e0487..b6e346b 100644
--- a/common/scala/src/main/scala/whisk/core/database/DocumentFactory.scala
+++ b/common/scala/src/main/scala/whisk/core/database/DocumentFactory.scala
@@ -227,4 +227,18 @@ trait DocumentFactory[W <: DocumentRevisionProvider] extends MultipleReadersSing
       case Failure(t) => Future.failed(t)
     }
   }
+
+  def deleteAttachments[Wsuper >: W](db: ArtifactStore[Wsuper], doc: DocInfo)(
+    implicit transid: TransactionId): Future[Boolean] = {
+    Try {
+      require(db != null, "db defined")
+      require(doc != null, "doc undefined")
+    } map { _ =>
+      implicit val ec = db.executionContext
+      db.deleteAttachments(doc)
+    } match {
+      case Success(f) => f
+      case Failure(t) => Future.failed(t)
+    }
+  }
 }
diff --git a/common/scala/src/main/scala/whisk/core/entity/WhiskAction.scala b/common/scala/src/main/scala/whisk/core/entity/WhiskAction.scala
index 92b7db3..07b9efe 100644
--- a/common/scala/src/main/scala/whisk/core/entity/WhiskAction.scala
+++ b/common/scala/src/main/scala/whisk/core/entity/WhiskAction.scala
@@ -376,6 +376,24 @@ object WhiskAction extends DocumentFactory[WhiskAction] with WhiskEntityQueries[
     }
   }
 
+  override def del[Wsuper >: WhiskAction](db: ArtifactStore[Wsuper], doc: DocInfo)(
+    implicit transid: TransactionId,
+    notifier: Option[CacheChangeNotification]): Future[Boolean] = {
+    Try {
+      require(db != null, "db undefined")
+      require(doc != null, "doc undefined")
+    }.map { _ =>
+      val fa = super.del(db, doc)
+      implicit val ec = db.executionContext
+      fa.flatMap { _ =>
+        super.deleteAttachments(db, doc)
+      }
+    } match {
+      case Success(f) => f
+      case Failure(f) => Future.failed(f)
+    }
+  }
+
   /**
    * Resolves an action name if it is contained in a package.
    * Look up the package to determine if it is a binding or the actual package.
diff --git a/tests/build.gradle b/tests/build.gradle
index 9d68ed2..70eaa30 100644
--- a/tests/build.gradle
+++ b/tests/build.gradle
@@ -66,7 +66,8 @@ dependencies {
     compile 'org.scalamock:scalamock-scalatest-support_2.11:3.4.2'
     compile 'com.typesafe.akka:akka-testkit_2.11:2.4.16'
     compile 'com.typesafe.akka:akka-http-testkit_2.11:10.0.10'
-    compile 'com.github.java-json-tools:json-schema-validator:2.2.8';
+    compile 'com.github.java-json-tools:json-schema-validator:2.2.8'
+    compile "org.mockito:mockito-core:2.15.0"
 
     compile project(':common:scala')
     compile project(':core:controller')
diff --git a/tests/src/test/scala/whisk/core/entity/test/DatastoreTests.scala b/tests/src/test/scala/whisk/core/entity/test/DatastoreTests.scala
index ec4a998..601efec 100644
--- a/tests/src/test/scala/whisk/core/entity/test/DatastoreTests.scala
+++ b/tests/src/test/scala/whisk/core/entity/test/DatastoreTests.scala
@@ -19,18 +19,17 @@ package whisk.core.entity.test
 
 import java.time.Instant
 
-import scala.Vector
 import scala.concurrent.Await
-
 import org.junit.runner.RunWith
 import org.scalatest.BeforeAndAfterEach
 import org.scalatest.BeforeAndAfterAll
 import org.scalatest.FlatSpec
 import org.scalatest.junit.JUnitRunner
-
 import akka.stream.ActorMaterializer
 import common.StreamLogging
 import common.WskActorSystem
+import org.scalatest.mockito.MockitoSugar
+import org.mockito.Mockito._
 import whisk.core.WhiskConfig
 import whisk.core.database.DocumentConflictException
 import whisk.core.database.CacheChangeNotification
@@ -46,6 +45,7 @@ class DatastoreTests
     with WskActorSystem
     with DbUtils
     with ExecHelpers
+    with MockitoSugar
     with StreamLogging {
 
   implicit val materializer = ActorMaterializer()
@@ -208,6 +208,19 @@ class DatastoreTests
     putGetCheck(datastore, revAction, WhiskAction)
   }
 
+  it should "delete action attachments" in {
+    implicit val tid = transid()
+    implicit val basename = EntityName("attachment action")
+    val javaAction =
+      WhiskAction(namespace, aname, javaDefault("ZHViZWU=", Some("hello")), annotations = Parameters("exec", "java"))
+    val docinfo = putGetCheck(datastore, javaAction, WhiskAction, false)._2.docinfo
+
+    val proxy = spy(datastore)
+    Await.result(WhiskAction.del(proxy, docinfo), dbOpTimeout)
+
+    verify(proxy).deleteAttachments(docinfo)
+  }
+
   it should "update trigger with a revision" in {
     implicit val tid = transid()
     implicit val basename = EntityName("update trigger")

-- 
To stop receiving notification emails like this one, please contact
rabbah@apache.org.