-
Notifications
You must be signed in to change notification settings - Fork 18
/
RepositoryUpdater.scala
305 lines (261 loc) · 10.9 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
package org.knora.webapi.store.triplestore.upgrade
import java.nio.file.{Files, Path, Paths}
import java.io.File
import scala.reflect.io.Directory
import akka.actor.{ActorRef, ActorSystem}
import akka.http.scaladsl.util.FastFuture
import akka.pattern._
import akka.util.Timeout
import com.typesafe.scalalogging.{LazyLogging, Logger}
import org.knora.webapi.IRI
import org.knora.webapi.exceptions.InconsistentRepositoryDataException
import org.knora.webapi.feature.FeatureFactoryConfig
import org.knora.webapi.messages.StringFormatter
import org.knora.webapi.messages.store.triplestoremessages._
import org.knora.webapi.messages.util.rdf._
import org.knora.webapi.settings.{KnoraDispatchers, KnoraSettingsImpl}
import org.knora.webapi.store.triplestore.upgrade.RepositoryUpdatePlan.PluginForKnoraBaseVersion
import org.knora.webapi.util.FileUtil
import scala.concurrent.{ExecutionContext, Future}
/**
* Updates a Knora repository to work with the current version of Knora.
*
* @param system the Akka [[ActorSystem]].
* @param appActor a reference to the main application actor.
* @param featureFactoryConfig the feature factory configuration.
* @param settings the Knora application settings.
*/
class RepositoryUpdater(
system: ActorSystem,
appActor: ActorRef,
featureFactoryConfig: FeatureFactoryConfig,
settings: KnoraSettingsImpl
) extends LazyLogging {
private val rdfFormatUtil: RdfFormatUtil = RdfFeatureFactory.getRdfFormatUtil(featureFactoryConfig)
// 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
/**
* The execution context for futures created in Knora actors.
*/
private implicit val executionContext: ExecutionContext =
system.dispatchers.lookup(KnoraDispatchers.KnoraActorDispatcher)
/**
* A string formatter.
*/
private implicit val stringFormatter: StringFormatter = StringFormatter.getGeneralInstance
/**
* The application's default timeout for `ask` messages.
*/
private implicit val timeout: Timeout = settings.defaultTimeout
/**
* Provides logging.
*/
private val log: Logger = logger
/**
* A list of available plugins.
*/
private val plugins: Seq[PluginForKnoraBaseVersion] = RepositoryUpdatePlan.makePluginsForVersions(
featureFactoryConfig = featureFactoryConfig,
log = log
)
private val tempDirNamePrefix: String = "knora"
/**
* Deletes directories inside temp directory starting with `tempDirNamePrefix`.
*/
def deleteTempDirectories(): Unit = {
val rootDir = new File("/tmp/")
val getTempToDelete = rootDir.listFiles.filter(_.getName.startsWith(tempDirNamePrefix))
if (getTempToDelete.length != 0) {
getTempToDelete.foreach(dir => {
val dirToDelete = new Directory(dir)
dirToDelete.deleteRecursively()
})
log.info(s"Deleted temp directories: ${getTempToDelete.map(_.getName()).mkString(", ")}")
}
}
/**
* Updates the repository, if necessary, to work with the current version of Knora.
*
* @return a response indicating what was done.
*/
def maybeUpdateRepository: Future[RepositoryUpdatedResponse] =
for {
foundRepositoryVersion: Option[String] <- getRepositoryVersion
requiredRepositoryVersion = org.knora.webapi.KnoraBaseVersion
// Is the repository up to date?
repositoryUpToDate: Boolean = foundRepositoryVersion.contains(requiredRepositoryVersion)
repositoryUpdatedResponse: RepositoryUpdatedResponse <-
if (repositoryUpToDate) {
// Yes. Nothing more to do.
FastFuture.successful(RepositoryUpdatedResponse(s"Repository is up to date at $requiredRepositoryVersion"))
} else {
// No. Construct the list of updates that it needs.
log.info(
s"Repository not up-to-date. Found: ${foundRepositoryVersion.getOrElse("None")}, Required: $requiredRepositoryVersion"
)
deleteTempDirectories()
val selectedPlugins: Seq[PluginForKnoraBaseVersion] = selectPluginsForNeededUpdates(foundRepositoryVersion)
log.info(s"Updating repository with transformations: ${selectedPlugins.map(_.versionString).mkString(", ")}")
// Update it with those plugins.
updateRepositoryWithSelectedPlugins(selectedPlugins)
}
} yield repositoryUpdatedResponse
/**
* Determines the `knora-base` version in the repository.
*
* @return the `knora-base` version string, if any, in the repository.
*/
private def getRepositoryVersion: Future[Option[String]] =
for {
repositoryVersionResponse: SparqlSelectResult <- (appActor ? SparqlSelectRequest(knoraBaseVersionQuery))
.mapTo[SparqlSelectResult]
bindings = repositoryVersionResponse.results.bindings
versionString =
if (bindings.nonEmpty) {
Some(bindings.head.rowMap("knoraBaseVersion"))
} else {
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]
): Seq[PluginForKnoraBaseVersion] =
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
}
/**
* 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]
): Future[RepositoryUpdatedResponse] = {
val downloadDir: Path = Files.createTempDirectory(tempDirNamePrefix)
log.info(s"Repository update using download directory $downloadDir")
// The file to save the repository in.
val downloadedRepositoryFile = downloadDir.resolve("downloaded-repository.nq")
val transformedRepositoryFile = downloadDir.resolve("transformed-repository.nq")
log.info("Downloading repository file...")
for {
// Ask the store actor to download the repository to the file.
_: FileWrittenResponse <- (appActor ? DownloadRepositoryRequest(
outputFile = downloadedRepositoryFile,
featureFactoryConfig = featureFactoryConfig
)).mapTo[FileWrittenResponse]
// Run the transformations to produce an output file.
_ = doTransformations(
downloadedRepositoryFile = downloadedRepositoryFile,
transformedRepositoryFile = transformedRepositoryFile,
pluginsForNeededUpdates = pluginsForNeededUpdates
)
_ = log.info("Emptying the repository...")
// Empty the repository.
_: DropAllRepositoryContentACK <- (appActor ? DropAllTRepositoryContent()).mapTo[DropAllRepositoryContentACK]
_ = log.info("Uploading transformed repository data...")
// Upload the transformed repository.
_: RepositoryUploadedResponse <- (appActor ? UploadRepositoryRequest(transformedRepositoryFile))
.mapTo[RepositoryUploadedResponse]
} yield RepositoryUpdatedResponse(
message = s"Updated repository to ${org.knora.webapi.KnoraBaseVersion}"
)
}
/**
* 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]
): Unit = {
// 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
)
}
/**
* 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.
*/
def readResourceIntoModel(filename: String, rdfFormat: NonJsonLD): RdfModel = {
val fileContent: String = FileUtil.readTextResource(filename)
rdfFormatUtil.parseToRdfModel(fileContent, rdfFormat)
}
}