Skip to content

Commit

Permalink
Add tags to indices for resources, files and schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
dantb committed Oct 5, 2023
1 parent d7fdcd5 commit 4736064
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
"type": "long",
"copy_to": "_all_fields"
},
"_tags": {
"type": "text",
"copy_to": "_all_fields"
},
"_deprecated": {
"type": "boolean",
"copy_to": "_all_fields"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package ch.epfl.bluebrain.nexus.delta.plugins.storage.files.model

import ch.epfl.bluebrain.nexus.delta.plugins.storage.files.Files
import ch.epfl.bluebrain.nexus.delta.plugins.storage.files.model.File.Metadata
import ch.epfl.bluebrain.nexus.delta.plugins.storage.storages.StoragesConfig.StorageTypeConfig
import ch.epfl.bluebrain.nexus.delta.plugins.storage.storages.model.StorageType
import ch.epfl.bluebrain.nexus.delta.rdf.IriOrBNode.Iri
import ch.epfl.bluebrain.nexus.delta.rdf.Vocabulary.contexts
import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.context.ContextValue
import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.context.JsonLdContext.keywords
import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.encoder.JsonLdEncoder
import ch.epfl.bluebrain.nexus.delta.sdk.ResourceShift
import ch.epfl.bluebrain.nexus.delta.sdk.jsonld.JsonLdContent
import ch.epfl.bluebrain.nexus.delta.sdk.model.{BaseUri, IdSegmentRef, Tags}
import ch.epfl.bluebrain.nexus.delta.sourcing.model.ResourceRef
import ch.epfl.bluebrain.nexus.delta.sourcing.model.ProjectRef
import ch.epfl.bluebrain.nexus.delta.sourcing.model.Tag.UserTag
import io.circe.syntax._
import io.circe.{Encoder, Json}

Expand All @@ -37,32 +41,42 @@ final case class File(
storageType: StorageType,
attributes: FileAttributes,
tags: Tags
)
) {
def metadata: Metadata = Metadata(tags.tags)
}

object File {

implicit def fileEncoder(implicit config: StorageTypeConfig): Encoder.AsObject[File] =
Encoder.encodeJsonObject.contramapObject { file =>
implicit val storageType: StorageType = file.storageType
val storageJson = Json.obj(
keywords.id -> file.storage.iri.asJson,
keywords.tpe -> storageType.iri.asJson,
"_rev" -> file.storage.rev.asJson
)
file.attributes.asJsonObject.add("_storage", storageJson)
}
final case class Metadata(tags: List[UserTag])

implicit def fileEncoder(implicit config: StorageTypeConfig): Encoder[File] = { file =>
implicit val storageType: StorageType = file.storageType
val storageJson = Json.obj(
keywords.id -> file.storage.iri.asJson,
keywords.tpe -> storageType.iri.asJson,
"_rev" -> file.storage.rev.asJson
)
file.attributes.asJson.mapObject(_.add("_storage", storageJson))
}

implicit def fileJsonLdEncoder(implicit config: StorageTypeConfig): JsonLdEncoder[File] =
JsonLdEncoder.computeFromCirce(_.id, Files.context)

type Shift = ResourceShift[FileState, File, Nothing]
implicit private val fileMetadataEncoder: Encoder[Metadata] = { m =>
Json.obj("_tags" -> m.tags.asJson)
}

implicit val fileMetadataJsonLdEncoder: JsonLdEncoder[Metadata] =
JsonLdEncoder.computeFromCirce(ContextValue(contexts.metadata))

type Shift = ResourceShift[FileState, File, Metadata]

def shift(files: Files)(implicit baseUri: BaseUri, config: StorageTypeConfig): Shift =
ResourceShift.apply[FileState, File](
ResourceShift.withMetadata[FileState, File, Metadata](
Files.entityType,
(ref, project) => files.fetch(IdSegmentRef(ref), project),
state => state.toResource,
value => JsonLdContent(value, value.value.asJson, None)
value => JsonLdContent(value, value.value.asJson, Some(value.value.metadata))
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ object Vocabulary {
val score = Metadata("score")
val self = Metadata("self")
val source = Metadata("source")
val tags = Metadata("tags")
val tokenEndpoint = Metadata("tokenEndpoint")
val total = Metadata("total")
val types = Metadata("types")
Expand Down Expand Up @@ -216,7 +217,6 @@ object Vocabulary {
val acls = contexts + "acls.json"
val aclsMetadata = contexts + "acls-metadata.json"
val error = contexts + "error.json"
val validation = contexts + "validation.json"
val identities = contexts + "identities.json"
val metadata = contexts + "metadata.json"
val offset = contexts + "offset.json"
Expand All @@ -236,10 +236,11 @@ object Vocabulary {
val search = contexts + "search.json"
val schemasMetadata = contexts + "schemas-metadata.json"
val shacl = contexts + "shacl-20170720.json"
val statistics = contexts + "statistics.json"
val supervision = contexts + "supervision.json"
val tags = contexts + "tags.json"
val validation = contexts + "validation.json"
val version = contexts + "version.json"
val statistics = contexts + "statistics.json"

}

Expand Down
4 changes: 4 additions & 0 deletions delta/sdk/src/main/resources/contexts/metadata.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"@context": {
"_rev": "https://bluebrain.github.io/nexus/vocabulary/rev",
"_tags": {
"@id": "https://bluebrain.github.io/nexus/vocabulary/tags",
"@container": "@set"
},
"_deprecated": "https://bluebrain.github.io/nexus/vocabulary/deprecated",
"_createdAt": {
"@id": "https://bluebrain.github.io/nexus/vocabulary/createdAt",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import io.circe.Json
import monix.bio.{IO, Task, UIO}

/**
* Aggregates the different [[ResourceShift]] to perform operations on resources indepently of their types
* Aggregates the different [[ResourceShift]] to perform operations on resources independently of their types
*/
trait ResourceShifts {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ final case class Tags(value: Map[UserTag, Int]) extends AnyVal {
def contains(tag: UserTag): Boolean = value.contains(tag)
def +(tag: (UserTag, Int)): Tags = Tags(value + tag)
def -(tag: UserTag): Tags = Tags(value - tag)
def tags: List[UserTag] = value.keys.toList
}

object Tags {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ch.epfl.bluebrain.nexus.delta.sdk.resources.model

import ch.epfl.bluebrain.nexus.delta.rdf.IriOrBNode.Iri
import ch.epfl.bluebrain.nexus.delta.rdf.RdfError
import ch.epfl.bluebrain.nexus.delta.rdf.Vocabulary.contexts
import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.api.{JsonLdApi, JsonLdOptions}
import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.context.{ContextValue, RemoteContextResolution}
import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.encoder.JsonLdEncoder
Expand All @@ -10,9 +11,12 @@ import ch.epfl.bluebrain.nexus.delta.sdk.ResourceShift
import ch.epfl.bluebrain.nexus.delta.sdk.jsonld.JsonLdContent
import ch.epfl.bluebrain.nexus.delta.sdk.model.{BaseUri, IdSegmentRef, Tags}
import ch.epfl.bluebrain.nexus.delta.sdk.resources.Resources
import ch.epfl.bluebrain.nexus.delta.sdk.resources.model.Resource.Metadata
import ch.epfl.bluebrain.nexus.delta.sdk.syntax._
import ch.epfl.bluebrain.nexus.delta.sourcing.model.Tag.UserTag
import ch.epfl.bluebrain.nexus.delta.sourcing.model.{ProjectRef, ResourceRef}
import io.circe.Json
import io.circe.syntax.EncoderOps
import io.circe.{Encoder, Json}
import monix.bio.IO

/**
Expand Down Expand Up @@ -41,10 +45,14 @@ final case class Resource(
source: Json,
compacted: CompactedJsonLd,
expanded: ExpandedJsonLd
)
) {
def metadata: Metadata = Metadata(tags.tags)
}

object Resource {

final case class Metadata(tags: List[UserTag])

implicit val resourceJsonLdEncoder: JsonLdEncoder[Resource] =
new JsonLdEncoder[Resource] {

Expand All @@ -62,13 +70,20 @@ object Resource {
value.source.topContextValueOrEmpty
}

type Shift = ResourceShift[ResourceState, Resource, Nothing]
implicit private val fileMetadataEncoder: Encoder[Metadata] = { m =>
Json.obj("_tags" -> m.tags.asJson)
}

implicit val fileMetadataJsonLdEncoder: JsonLdEncoder[Metadata] =
JsonLdEncoder.computeFromCirce(ContextValue(contexts.metadata))

type Shift = ResourceShift[ResourceState, Resource, Metadata]

def shift(resources: Resources)(implicit baseUri: BaseUri): Shift =
ResourceShift.apply[ResourceState, Resource](
ResourceShift.withMetadata[ResourceState, Resource, Metadata](
Resources.entityType,
(ref, project) => resources.fetch(IdSegmentRef(ref), project, None),
state => state.toResource,
value => JsonLdContent(value, value.value.source, None)
value => JsonLdContent(value, value.value.source, Some(value.value.metadata))
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ import ch.epfl.bluebrain.nexus.delta.sdk.model.{BaseUri, IdSegmentRef, Tags}
import ch.epfl.bluebrain.nexus.delta.sdk.schemas.Schemas
import ch.epfl.bluebrain.nexus.delta.sdk.syntax._
import ch.epfl.bluebrain.nexus.delta.sourcing.model.ProjectRef
import io.circe.Json
import io.circe.{Encoder, Json}
import monix.bio.IO
import ch.epfl.bluebrain.nexus.delta.kernel.effect.migration._
import ch.epfl.bluebrain.nexus.delta.sdk.schemas.model.Schema.Metadata
import ch.epfl.bluebrain.nexus.delta.sourcing.model.Tag.UserTag
import io.circe.syntax.EncoderOps

/**
* A schema representation
Expand Down Expand Up @@ -67,10 +70,13 @@ final case class Schema(
Graph.empty(id).add(triples)
}

def metadata: Metadata = Metadata(tags.tags)
}

object Schema {

final case class Metadata(tags: List[UserTag])

implicit val schemaJsonLdEncoder: JsonLdEncoder[Schema] =
new JsonLdEncoder[Schema] {

Expand All @@ -88,14 +94,21 @@ object Schema {
value.source.topContextValueOrEmpty.merge(ContextValue(contexts.shacl))
}

type Shift = ResourceShift[SchemaState, Schema, Nothing]
implicit private val fileMetadataEncoder: Encoder[Metadata] = { m =>
Json.obj("_tags" -> m.tags.asJson)
}

implicit val fileMetadataJsonLdEncoder: JsonLdEncoder[Metadata] =
JsonLdEncoder.computeFromCirce(ContextValue(contexts.schemasMetadata))

type Shift = ResourceShift[SchemaState, Schema, Metadata]

def shift(schemas: Schemas)(implicit baseUri: BaseUri): Shift =
ResourceShift.apply[SchemaState, Schema](
ResourceShift.withMetadata[SchemaState, Schema, Metadata](
Schemas.entityType,
(ref, project) => schemas.fetch(IdSegmentRef(ref), project).toBIO[SchemaRejection],
state => state.toResource,
value => JsonLdContent(value, value.value.source, None)
value => JsonLdContent(value, value.value.source, Some(value.value.metadata))
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"_deprecated": false,
"_project": "{{deltaUri}}/projects/{{org2}}/{{proj3}}",
"_rev": 2,
"_tags": ["v1.0.1"],
"_schemaProject": "{{deltaUri}}/projects/{{org2}}/{{proj3}}",
"_updatedBy": "{{deltaUri}}/realms/{{realm}}/users/{{user}}"
},
Expand All @@ -47,6 +48,7 @@
"_deprecated": false,
"_project": "{{deltaUri}}/projects/{{org1}}/{{proj1}}",
"_rev": 2,
"_tags": ["v1.0.0"],
"_schemaProject": "{{deltaUri}}/projects/{{org1}}/{{proj1}}",
"_updatedBy": "{{deltaUri}}/realms/{{realm}}/users/{{user}}"
}
Expand Down

0 comments on commit 4736064

Please sign in to comment.