forked from DEIB-GECO/Metadata-Manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileDatabase.scala
443 lines (402 loc) · 15 KB
/
FileDatabase.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package it.polimi.genomics.metadata.database
import it.polimi.genomics.metadata.step.xml.Dataset
/**
* Created by Nacho
* this object handles the uniqueness of the database and keeps it instantiated.
* also works as a facade for the dbContainer.
*
* remember always to end the run and close the database after finishing its use.
*/
object FileDatabase {
private val db = new DbContainer
/**
* connects to the database
*
* @param path storage path for the database
*/
def setDatabase(path: String): Unit = {
db.setDatabase(path)
}
/**
* closes the connection to the database
*/
def closeDatabase(): Unit = {
db.closeDatabase()
}
/**
* Tries to create a source with the given name and returns its id, if already exists is not replaced.
*
* @param name name of the source, should be unique.
* @return id of the source.
*/
def sourceId(name: String): Int = {
db.sourceId(name)
}
/**
* Tries to create a dataset and returns its id, if already exists is not replaced.
*
* @param sourceId dataset owner's id
* @param name name of the dataset should be unique for each source.
* @return id of the dataset.
*/
def datasetId(sourceId: Int, name: String): Int = {
db.datasetId(sourceId, name)
}
/**
* Tries to create a file and returns its id, if already exists is not replaced.
*
* @param datasetId file owner's id.
* @param url origin url for the file.
* @param stage stage of the process the file is used Download/Transform.
* @param candidateName the name the file should have.
* @return id of the file.
*/
def fileId(datasetId: Int, url: String, stage: Stage.Value, candidateName: String, useUrl: Boolean = true): Int = {
db.fileId(datasetId, url, stage, candidateName, useUrl)
}
/**
* Creates a run, with its general settings and the actual datetime.
*
* @param downloadEnabled indicates if downloading was enabled during the run.
* @param transformEnabled indicates if transforming was enabled during the run.
* @param loadEnabled indicates if loading was enabled during the run.
* @param outputFolder indicates the outputFolder defined as working directory.
* @return the run's id.
*/
def runId(downloadEnabled: String, transformEnabled: String, loadEnabled: String, outputFolder: String): Int = {
db.runId(downloadEnabled, transformEnabled, loadEnabled, outputFolder)
}
/**
* gets the last runId
*
* @return max id of runs
*/
def getMaxRunNumber: Int = {
db.getMaxRunNumber
}
/**
* by receiving a run number, gets the previous one
*
* @param runNumber run number to check the previous
* @return previous run number or 0 if no run before
*/
def getPreviousRunNumber(runNumber: Int): Int = {
db.getPreviousRunNumber(runNumber)
}
/**
* counts the number of executions already run in the database
*
* @return count of runs
*/
def getNumberOfRuns: Int = {
var numberOfRuns = 0
var runAux = getMaxRunNumber
while (runAux > 0) {
runAux = getPreviousRunNumber(runAux)
numberOfRuns += 1
}
numberOfRuns
}
//--------------------------SECONDARY INSERTIONS RUNSOURCE/RUNDATASET/RUNFILE/PARAMETERS------------------------------
/**
* generates the last representation of the source in the last run.
*
* @param runId id for the run.
* @param sourceId id for the source.
* @param url url for the source.
* @param outputFolder working directory for the source.
* @param downloadEnabled indicates if the source is being downloaded.
* @param downloader indicates the downloader used for the source.
* @param transformEnabled indicates if the source is being transformed.
* @param transformer indicates the transformer used by the source.
* @param loadEnabled indicates if the source is bein loaded.
* @param loader indicates the loader used by the source.
* @return the runSource id.
*/
def runSourceId(runId: Int, sourceId: Int, url: String, outputFolder: String, downloadEnabled: String,
downloader: String, transformEnabled: String, transformer: String, loadEnabled: String, loader: String
): Int = {
db.runSourceId(sourceId, url, outputFolder, downloadEnabled, downloader, transformEnabled, transformer,
loadEnabled, loader)
}
/**
* Inserts the parameters used by a source
*
* @param runSourceId source who is using the parameters
* @param description explains what the parameter is used for
* @param key indicates the name of the parameter
* @param value indicates the value of the parameter
* @return id of the parameter.
*/
def runSourceParameterId(runSourceId: Int, description: String, key: String, value: String): Int = {
db.runSourceParameterId(runSourceId, description, key, value)
}
/**
* generates the last representation of the dataset in the last run.
*
* @param runId id for the run.
* @param datasetId id for the dataset.
* @param outputFolder working directory for the dataset.
* @param downloadEnabled indicates if the dataset is being downloaded.
* @param transformEnabled indicates if the dataset is being transformed.
* @param loadEnabled indicates if the source is being loaded.
* @param schemaUrl indicates the url of the schema.
* @param schemaLocation indicates whether the schema is local or remote.
* @return the runDataset id.
*/
def runDatasetId(runId: Int, datasetId: Int, outputFolder: String, downloadEnabled: String, transformEnabled: String,
loadEnabled: String, schemaUrl: String, schemaLocation: String
): Int = {
db.runDatasetId(datasetId, outputFolder, downloadEnabled, transformEnabled, loadEnabled, schemaUrl, schemaLocation, Option(runId))
}
/**
* prints the log for dataset downloaded files for a specified run
*
* @param runDatasetId identifies of the rundataset.
*/
def printRunDatasetDownloadLog(runDatasetId: Int, datasetId: Int, runId: Int): Unit = {
if (runDatasetId > 0) {
db.printRunDatasetLog(runDatasetId, Stage.DOWNLOAD)
db.printNewReadyFailedFiles(datasetId, runId)
}
}
/**
* prints the log for dataset transformed files for a specified run
*
* @param runDatasetId identifies of the rundataset.
*/
def printRunDatasetTransformLog(runDatasetId: Int): Unit = {
if (runDatasetId > 0)
db.printRunDatasetLog(runDatasetId, Stage.TRANSFORM)
}
/**
* Inserts the parameters used by a source
*
* @param runDatasetId dataset who is using the parameters
* @param description explains what the parameter is used for
* @param key indicates the name of the parameter
* @param value indicates the value of the parameter
* @return id of the parameter.
*/
def runDatasetParameterId(runDatasetId: Int, description: String, key: String, value: String): Int = {
db.runDatasetParameterId(runDatasetId, description, key, value)
}
/**
* feeds the total downloaded files and possible downloads for a dataset
*
* @param datasetId dataset identifier in the database
* @param dataset dataset where the files are being downloaded
* @param totalFiles total number of files to be downloaded
* @param downloadedFiles total of successful downloads out of totalFiles
* @return id for the runDatasetLog created
*/
def runDatasetDownloadAppend(datasetId: Int, dataset: Dataset, totalFiles: Int, downloadedFiles: Int): Int = {
val runId = db.getMaxRunNumber
val runDatasetId = db.runDatasetId(
datasetId,
dataset.outputFolder,
dataset.downloadEnabled.toString,
dataset.transformEnabled.toString,
dataset.loadEnabled.toString,
dataset.schemaUrl,
dataset.schemaLocation.toString,
Option(runId)
)
db.runDatasetLogId(runDatasetId, Stage.DOWNLOAD, totalFiles, downloadedFiles)
}
/**
* feeds the total downloaded files and possible transforms for a dataset
*
* @param datasetId dataset identifier in the database
* @param dataset dataset where the files are being transformed
* @param totalFiles total number of files to be transformed
* @param transformedFiles total of successful transformed out of totalFiles
* @return id for the runDatasetLog created
*/
def runDatasetTransformAppend(datasetId: Int, dataset: Dataset, totalFiles: Int, transformedFiles: Int): Int = {
val runId = db.getMaxRunNumber
val runDatasetId = db.runDatasetId(
datasetId,
dataset.outputFolder,
dataset.downloadEnabled.toString,
dataset.transformEnabled.toString,
dataset.loadEnabled.toString,
dataset.schemaUrl,
dataset.schemaLocation.toString,
Option(runId)
)
db.runDatasetLogId(runDatasetId, Stage.TRANSFORM, totalFiles, transformedFiles)
}
/**
* Generates the versioning for the metadata of the files.
*
* @param fileId indicats the file whose verions are.
* @return id of the runFile.
*/
def runFileId(fileId: Int): Int = {
db.runFileId(fileId)
}
//-------------------------------Run closing--------------------------------------------------------------------------
/**
* puts the time finished of the run
*
* @param runId id for the run.
*/
def endRun(runId: Int): Unit = {
db.endRun(runId)
}
//------------------------------FILE OPERATIONS SECTION FILENAME/CHECKIFUPDATE/PROCESS--------------------------------
/**
* By receiving a candidate name returns a unique name inside the dataset.
*
* @param fileId id for the file.
* @return unique name among the dataset's files. -1 as the Int indicates the file should not exist.
*/
def getFileNameAndCopyNumber(fileId: Int): (String, Int) = {
db.getFileNameAndCopyNumber(fileId)
}
/**
* returns hash, size and last update.
*
* @param fileId identifier of the file.
* @return hash, size and last update.
*/
def getFileDetails(fileId: Int): (String, String, String) = {
db.getFileDetails(fileId)
}
/**
* returns hash, size and last update.
*
* @param fileId identifier of the file.
* @return hash, size and last update.
*/
def getFileAllDetails(fileId: Int) = {
db.getFileAllDetail(fileId)
}
/**
* indicates which is the maximum copy number for the same filename inside the same dataset.
*
* @param datasetId datast where the file belongs
* @param fileName original file name
* @param stage indicates whether download/transform
* @return max copy number
*/
def getMaxCopyNumber(datasetId: Int, fileName: String, stage: Stage.Value): Int = {
db.getMaxCopyNumber(datasetId, fileName, stage)
}
/**
* checks if the given file has to be updated based on its hash, size and last update.
*
* @param fileId id for the file.
* @param hash hash of the file.
* @param originSize original size in the source.
* @param originLastUpdate original last updated in the source.
* @return true = has to be updated.
*/
def checkIfUpdateFile(fileId: Int, hash: String, originSize: String, originLastUpdate: String): Boolean = {
db.checkIfUpdateFile(fileId, hash, originSize, originLastUpdate)
}
/**
* returns all the non outdated files with its copy number
*
* @param datasetId dataset from where files are required.
* @return non outdated files id, name, copy number
*/
def getFilesToProcess(datasetId: Int, stage: Stage.Value): Seq[(Int, String, Int)] = {
db.getFilesToProcess(datasetId, stage)
}
/**
* returns all the failed files with its copy number
*
* @param datasetId dataset from where files are required.
* @return failed files id, name, copy number,url, hash
*/
def getFailedFiles(datasetId: Int, stage: Stage.Value): Seq[(Int, String, Int, String, String)] = {
db.getFailedFiles(datasetId, stage)
}
/**
* marks indicated file as to be UPDATED.
*
* @param fileId identifier for the file.
*/
def markAsUpdated(fileId: Int, size: String): Unit = {
db.markAsUpdated(fileId, size)
}
/**
* marks indicated file as to be UPDATED.
*
* @param fileId identifier for the file.
*/
def markAsUpdated(fileId: Int, size: String, hash: String): Unit = {
db.markAsUpdated(fileId, size, hash)
}
/**
* to be used when the file download or transformation fails, puts file status into FAILED
*
* @param fileId identifier for the file.
*/
def markAsFailed(fileId: Int): Unit = {
db.markAsFailed(fileId)
}
/**
* mark all files that have not been compared into the log as outdated.
* meant to be used at the end of all comparisons (all check if udpate)
* changes COMPARE to OUTDATED.
*
* @param datasetId identifier for the dataset.
* @param stage indicates whether refers to download or transformed files.
*/
def markAsOutdated(datasetId: Int, stage: Stage.Value): Unit = {
db.markAsOutdated(datasetId, stage)
}
/**
* mark all the files with status NOTHING into status COMPARE
* meant to be used to check which files have been deleted from the source.
*
* @param datasetId identifier for the dataset.
* @param stage indicates whether refers to download or transformed files.
*/
def markToCompare(datasetId: Int, stage: Stage.Value): Unit = {
db.markToCompare(datasetId, stage)
}
/**
* mark all the files with status NOTHING into status COMPARE
* meant to be used to check which files have been deleted from the source.
*
* @param datasetId identifier for the dataset.
* @param stage indicates whether refers to download or transformed files.
*/
def delete(datasetId: Int, stage: Stage.Value): Unit = {
db.delete(datasetId, stage)
}
/**
* Gives the current status of a file
*
* @param datasetId dataset where the file belongs to
* @param url origin url for the file
* @param stage whether is download or transform.
*/
def fileStatus(datasetId: Int, url: String, stage: Stage.Value): Option[FILE_STATUS.Value] = {
db.fileStatus(datasetId, url, stage)
}
/**
* returns the url of a specified file.
*
* @param fileName name of the file.
* @param datasetID dataset from where files are required.
* @param stage whether is download or transform.
* @return the url of the file.
*/
def getFileUrl(fileName: String, datasetID: Int, stage: Stage.Value): String = {
db.getFileUrl(fileName, datasetID, stage)
}
/**
* Returns the last date in which a file of the specified dataset is returned.
*
* @param datasetID identifier of the dataset.
* @return the date of the last download.
*/
def getLastDownloadDate(datasetID: Int): String = {
db.getLastDownloadDate(datasetID)
}
}