Skip to content

Commit 5c69dbf

Browse files
add default version feature
1 parent 6d307d2 commit 5c69dbf

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

ansible/files/whisks_design_document_for_entities_db_v2.1.0.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"reduce": "_count"
2424
},
2525
"action-versions": {
26-
"map": "function (doc) {\n var isAction = function (doc) { return (doc.exec !== undefined) };\n if (isAction(doc)) try {\n var published = true;\n doc.annotations.forEach(function(anno) {\n if(anno[\"key\"] == \"publish\")\n published = anno[\"value\"]\n });\n \n var value = {\n namespace: doc.namespace,\n name: doc.name,\n _id: doc._id,\n version: doc.version,\n publish: published,\n };\n var versions = doc.version.split(\".\")\n emit([doc.namespace + \"/\" + doc.name], value);\n } catch (e) {}\n}"
26+
"map": "function (doc) {\n var isAction = function (doc) { return (doc.exec !== undefined) };\n if (isAction(doc)) try {\n var value = {\n _id: doc.namespace + \"/\" + doc.name + \"/default\",\n namespace: doc.namespace,\n name: doc.name,\n id: doc._id,\n version: doc.version\n };\n emit([doc.namespace + \"/\" + doc.name], value);\n } catch (e) {}\n}"
2727
}
2828
}
2929
}

common/scala/src/main/scala/org/apache/openwhisk/core/entity/WhiskAction.scala

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -361,14 +361,19 @@ case class ExecutableWhiskActionMetaData(namespace: EntityPath,
361361
case class WhiskActionVersion(id: String, namespace: EntityPath, name: EntityName, version: SemVer, publish: Boolean)
362362

363363
object WhiskActionVersion {
364-
val serdes = jsonFormat(WhiskActionVersion.apply, "_id", "namespace", "name", "version", "publish")
364+
val serdes = jsonFormat(WhiskActionVersion.apply, "id", "namespace", "name", "version", "publish")
365365
}
366366

367-
case class WhiskActionVersionList(namespace: EntityPath, name: EntityName, versions: Map[SemVer, String]) {
367+
case class WhiskActionVersionList(namespace: EntityPath,
368+
name: EntityName,
369+
versions: Map[SemVer, String],
370+
defaultVersion: Option[String]) {
368371
def matchedDocId(version: Option[SemVer]): Option[DocId] = {
369372
version match {
370373
case Some(ver) =>
371374
versions.get(ver).map(DocId(_))
375+
case None if defaultVersion.nonEmpty =>
376+
versions.get(SemVer(defaultVersion.get)).map(DocId(_))
372377
case None if versions.nonEmpty =>
373378
Some(DocId(versions.maxBy(_._1)._2))
374379
case _ =>
@@ -385,7 +390,7 @@ object WhiskActionVersionList extends MultipleReadersSingleWriterCache[WhiskActi
385390
CacheKey(action.fullPath.asString)
386391
}
387392

388-
def get(action: FullyQualifiedEntityName, datastore: EntityStore, fetchAll: Boolean = true)(
393+
def get(action: FullyQualifiedEntityName, datastore: EntityStore, fromCache: Boolean = true)(
389394
implicit transId: TransactionId): Future[WhiskActionVersionList] = {
390395
implicit val logger: Logging = datastore.logging
391396
implicit val ec = datastore.executionContext
@@ -401,7 +406,7 @@ object WhiskActionVersionList extends MultipleReadersSingleWriterCache[WhiskActi
401406
endKey = endKey,
402407
skip = 0,
403408
limit = 0,
404-
includeDocs = false,
409+
includeDocs = true,
405410
descending = false,
406411
reduce = false,
407412
stale = StaleParameter.No)
@@ -411,24 +416,32 @@ object WhiskActionVersionList extends MultipleReadersSingleWriterCache[WhiskActi
411416
}
412417
val mappings = values
413418
.map(WhiskActionVersion.serdes.read(_))
414-
.filter(_.publish || fetchAll)
415419
.map { actionVersion =>
416420
(actionVersion.version, actionVersion.id)
417421
}
418422
.toMap
419-
WhiskActionVersionList(action.namespace.toPath, action.name, mappings)
420-
})
423+
val defaultVersion = if (result.nonEmpty) {
424+
val doc = result.head.fields.getOrElse("doc", JsNull)
425+
if (doc != JsNull) doc.asJsObject.fields.get("default").map(_.convertTo[String])
426+
else
427+
None
428+
} else None
429+
WhiskActionVersionList(action.namespace.toPath, action.name, mappings, defaultVersion)
430+
},
431+
fromCache)
421432
}
422433

423434
def getMatchedDocId(
424435
action: FullyQualifiedEntityName,
425436
version: Option[SemVer],
426437
datastore: EntityStore,
427438
tryAgain: Boolean = true)(implicit transId: TransactionId, ec: ExecutionContext): Future[Option[DocId]] = {
428-
get(action, datastore, version.nonEmpty).flatMap { res =>
439+
get(action, datastore).flatMap { res =>
429440
val docId = version match {
430441
case Some(ver) =>
431442
res.versions.get(ver).map(DocId(_))
443+
case None if res.defaultVersion.nonEmpty =>
444+
res.versions.get(SemVer(res.defaultVersion.get)).map(DocId(_))
432445
case None if res.versions.nonEmpty =>
433446
Some(DocId(res.versions.maxBy(_._1)._2))
434447
case _ =>

core/controller/src/main/scala/org/apache/openwhisk/core/controller/Actions.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ trait WhiskActionsApi extends WhiskCollectionAPI with PostActionActivation with
229229

230230
onComplete(checkAdditionalPrivileges) {
231231
case Success(_) =>
232-
onComplete(WhiskActionVersionList.get(entityName, entityStore)) {
232+
onComplete(WhiskActionVersionList.get(entityName, entityStore, false)) {
233233
case Success(result) if (result.versions.size >= actionMaxVersionLimit && !deleteOld) =>
234234
terminate(
235235
Forbidden,
@@ -377,7 +377,7 @@ trait WhiskActionsApi extends WhiskCollectionAPI with PostActionActivation with
377377
*/
378378
override def remove(user: Identity, entityName: FullyQualifiedEntityName)(implicit transid: TransactionId) = {
379379
parameter('version.as[SemVer] ?, 'deleteAll ? false) { (version, deleteAll) =>
380-
onComplete(WhiskActionVersionList.get(entityName, entityStore)) {
380+
onComplete(WhiskActionVersionList.get(entityName, entityStore, false)) {
381381
case Success(results) =>
382382
version match {
383383
case Some(_) =>

0 commit comments

Comments
 (0)