/
RepositoryUpdater.scala
306 lines (266 loc) · 11.3 KB
/
RepositoryUpdater.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
* Copyright © 2021 - 2022 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors.
* SPDX-License-Identifier: Apache-2.0
*/
package org.knora.webapi.store.triplestore.upgrade
import com.typesafe.scalalogging.Logger
import org.slf4j.LoggerFactory
import zio._
import zio.macros.accessible
import java.io.File
import java.nio.file.Files
import java.nio.file.Path
import scala.reflect.io.Directory
import dsp.errors.InconsistentRepositoryDataException
import org.knora.webapi.IRI
import org.knora.webapi.messages.store.triplestoremessages._
import org.knora.webapi.messages.util.rdf._
import org.knora.webapi.store.triplestore.api.TriplestoreService
import org.knora.webapi.store.triplestore.upgrade.RepositoryUpdatePlan.PluginForKnoraBaseVersion
import org.knora.webapi.util.FileUtil
@accessible
trait RepositoryUpdater {
/**
* Upgrades the repository, if necessary, to work with the current version of DSP-API.
*
* @return a response indicating what was done.
*/
val maybeUpgradeRepository: UIO[RepositoryUpdatedResponse]
}
/**
* Updates a DSP repository to work with the current version of DSP-API.
*
* Depends on [[TriplestoreService]].
*/
object RepositoryUpdater {
val layer: ZLayer[TriplestoreService, Nothing, RepositoryUpdater] =
ZLayer {
for {
ts <- ZIO.service[TriplestoreService]
} yield new RepositoryUpdaterImpl(ts) {}
}
sealed abstract private class RepositoryUpdaterImpl(triplestoreService: TriplestoreService)
extends RepositoryUpdater {
private val rdfFormatUtil: RdfFormatUtil = RdfFeatureFactory.getRdfFormatUtil()
// A SPARQL query to find out the knora-base version in a repository.
private val knoraBaseVersionQuery =
"""PREFIX knora-base: <http://www.knora.org/ontology/knora-base#>
|
|SELECT ?knoraBaseVersion WHERE {
| <http://www.knora.org/ontology/knora-base> knora-base:ontologyVersion ?knoraBaseVersion .
|}""".stripMargin
/**
* Provides logging.
*/
private val log: Logger = Logger(LoggerFactory.getLogger(getClass.getName))
private val tmpDirNamePrefix: String = "knora"
/**
* Updates the repository, if necessary, to work with the current version of DSP-API.
*
* @return a response indicating what was done.
*/
override val maybeUpgradeRepository: UIO[RepositoryUpdatedResponse] =
for {
foundRepositoryVersion <- getRepositoryVersion()
requiredRepositoryVersion <- ZIO.succeed(org.knora.webapi.KnoraBaseVersion)
// Is the repository up to date?
repositoryUpToDate <- ZIO.succeed(foundRepositoryVersion.contains(requiredRepositoryVersion))
repositoryUpdatedResponse <-
if (repositoryUpToDate) {
// Yes. Nothing more to do.
ZIO.succeed(RepositoryUpdatedResponse(s"Repository is up to date at $requiredRepositoryVersion"))
} else {
for {
// No. Construct the list of updates that it needs.
_ <-
foundRepositoryVersion match {
case Some(foundRepositoryVersion) =>
ZIO.logInfo(
s"Repository not up to date. Found: $foundRepositoryVersion, Required: $requiredRepositoryVersion"
)
case None =>
ZIO.logWarning(
s"Repository not up to date. Found: None, Required: $requiredRepositoryVersion"
)
}
_ <- deleteTmpDirectories()
selectedPlugins <- selectPluginsForNeededUpdates(foundRepositoryVersion)
_ <-
ZIO.logInfo(
s"Updating repository with transformations: ${selectedPlugins.map(_.versionString).mkString(", ")}"
)
// Update it with those plugins.
result <- updateRepositoryWithSelectedPlugins(selectedPlugins)
} yield result
}
} yield repositoryUpdatedResponse
/**
* Deletes directories inside tmp directory starting with `tmpDirNamePrefix`.
*/
private def deleteTmpDirectories(): UIO[Unit] = ZIO.attempt {
val rootDir = new File("/tmp/")
val getTmpToDelete = rootDir.listFiles.filter(_.getName.startsWith(tmpDirNamePrefix))
if (getTmpToDelete.length != 0) {
getTmpToDelete.foreach { dir =>
val dirToDelete = new Directory(dir)
dirToDelete.deleteRecursively()
}
log.info(s"Deleted tmp directories: ${getTmpToDelete.map(_.getName()).mkString(", ")}")
}
()
}.orDie
/**
* Determines the `knora-base` version in the repository.
*
* @return the `knora-base` version string, if any, in the repository.
*/
private def getRepositoryVersion(): UIO[Option[String]] =
for {
repositoryVersionResponse <- triplestoreService.sparqlHttpSelect(knoraBaseVersionQuery)
bindings <- ZIO.succeed(repositoryVersionResponse.results.bindings)
versionString <-
if (bindings.nonEmpty) {
ZIO.succeed(Some(bindings.head.rowMap("knoraBaseVersion")))
} else {
ZIO.none
}
} yield versionString
/**
* Constructs a list of update plugins that need to be run to update the repository.
*
* @param maybeRepositoryVersionString the `knora-base` version string, if any, in the repository.
* @return the plugins needed to update the repository.
*/
private def selectPluginsForNeededUpdates(
maybeRepositoryVersionString: Option[String]
): UIO[Seq[PluginForKnoraBaseVersion]] = {
// A list of available plugins.
val plugins: Seq[PluginForKnoraBaseVersion] =
RepositoryUpdatePlan.makePluginsForVersions(log)
ZIO.attempt {
maybeRepositoryVersionString match {
case Some(repositoryVersion) =>
// The repository has a version string. Get the plugins for all subsequent versions.
// Make a map of version strings to plugins.
val versionsToPluginsMap: Map[String, PluginForKnoraBaseVersion] = plugins.map { plugin =>
plugin.versionString -> plugin
}.toMap
val pluginForRepositoryVersion: PluginForKnoraBaseVersion =
versionsToPluginsMap.getOrElse(
repositoryVersion,
throw InconsistentRepositoryDataException(s"No such repository version $repositoryVersion")
)
plugins.filter { plugin =>
plugin.versionNumber > pluginForRepositoryVersion.versionNumber
}
case None =>
// The repository has no version string. Include all updates.
plugins
}
}.orDie
}
/**
* Updates the repository with the specified list of plugins.
*
* @param pluginsForNeededUpdates the plugins needed to update the repository.
* @return a [[RepositoryUpdatedResponse]] indicating what was done.
*/
private def updateRepositoryWithSelectedPlugins(
pluginsForNeededUpdates: Seq[PluginForKnoraBaseVersion]
): UIO[RepositoryUpdatedResponse] =
(for {
downloadDir <- ZIO.attempt(Files.createTempDirectory(tmpDirNamePrefix))
_ <- ZIO.logInfo(s"Repository update using download directory $downloadDir")
// The file to save the repository in.
downloadedRepositoryFile <- ZIO.attempt(downloadDir.resolve("downloaded-repository.nq"))
transformedRepositoryFile <- ZIO.attempt(downloadDir.resolve("transformed-repository.nq"))
// Ask the store actor to download the repository to the file.
_ <- ZIO.logInfo("Downloading repository file...")
_ <- triplestoreService.downloadRepository(downloadedRepositoryFile)
// Run the transformations to produce an output file.
_ <- doTransformations(
downloadedRepositoryFile = downloadedRepositoryFile,
transformedRepositoryFile = transformedRepositoryFile,
pluginsForNeededUpdates = pluginsForNeededUpdates
)
// Empty the repository.
_ <- triplestoreService.dropDataGraphByGraph()
// Upload the transformed repository.
_ <- ZIO.logInfo("Uploading transformed repository data...")
_ <- triplestoreService.uploadRepository(transformedRepositoryFile)
} yield RepositoryUpdatedResponse(
message = s"Updated repository to ${org.knora.webapi.KnoraBaseVersion}"
)).orDie
/**
* Transforms a file containing a downloaded repository.
*
* @param downloadedRepositoryFile the downloaded repository.
* @param transformedRepositoryFile the transformed file.
* @param pluginsForNeededUpdates the plugins needed to update the repository.
*/
private def doTransformations(
downloadedRepositoryFile: Path,
transformedRepositoryFile: Path,
pluginsForNeededUpdates: Seq[PluginForKnoraBaseVersion]
): UIO[Unit] = ZIO.attempt {
// Parse the input file.
log.info("Reading repository file...")
val model = rdfFormatUtil.fileToRdfModel(file = downloadedRepositoryFile, rdfFormat = NQuads)
log.info(s"Read ${model.size} statements.")
// Run the update plugins.
for (pluginForNeededUpdate <- pluginsForNeededUpdates) {
log.info(s"Running transformation for ${pluginForNeededUpdate.versionString}...")
pluginForNeededUpdate.plugin.transform(model)
}
// Update the built-in named graphs.
log.info("Updating built-in named graphs...")
addBuiltInNamedGraphsToModel(model)
// Write the output file.
log.info(s"Writing output file (${model.size} statements)...")
rdfFormatUtil.rdfModelToFile(
rdfModel = model,
file = transformedRepositoryFile,
rdfFormat = NQuads
)
}.orDie
/**
* Adds Knora's built-in named graphs to an [[RdfModel]].
*
* @param model the [[RdfModel]].
*/
private def addBuiltInNamedGraphsToModel(model: RdfModel): Unit =
// Add each built-in named graph to the model.
for (builtInNamedGraph <- RepositoryUpdatePlan.builtInNamedGraphs) {
val context: IRI = builtInNamedGraph.iri
// Remove the existing named graph from the model.
model.remove(
subj = None,
pred = None,
obj = None,
context = Some(context)
)
// Read the current named graph from a file.
val namedGraphModel: RdfModel = readResourceIntoModel(builtInNamedGraph.filename, Turtle)
// Copy it into the model, adding the named graph IRI to each statement.
for (statement: Statement <- namedGraphModel) {
model.add(
subj = statement.subj,
pred = statement.pred,
obj = statement.obj,
context = Some(context)
)
}
}
/**
* Reads a file from the CLASSPATH into an [[RdfModel]].
*
* @param filename the filename.
* @param rdfFormat the file format.
* @return an [[RdfModel]] representing the contents of the file.
*/
private def readResourceIntoModel(filename: String, rdfFormat: NonJsonLD): RdfModel = {
val fileContent: String = FileUtil.readTextResource(filename)
rdfFormatUtil.parseToRdfModel(fileContent, rdfFormat)
}
}
}