-
Notifications
You must be signed in to change notification settings - Fork 4
/
bulkblocks.go
777 lines (741 loc) · 24.9 KB
/
bulkblocks.go
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
package dbs
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"strings"
"time"
"github.com/dmwm/dbs2go/utils"
)
// BulkBlocks represents bulk block structure used by `/bulkblocks` DBS API
type BulkBlocks struct {
DatasetConfigList []DatasetConfig `json:"dataset_conf_list"`
FileConfigList []FileConfig `json:"file_conf_list"`
Files []File `json:"files"`
ProcessingEra ProcessingEra `json:"processing_era"`
PrimaryDataset PrimaryDataset `json:"primds"`
Dataset Dataset `json:"dataset"`
AcquisitionEra AcquisitionEra `json:"acquisition_era"`
Block Block `json:"block"`
FileParentList []FileParentRecord `json:"file_parent_list"`
BlockParentList []BlockParent `json:"block_parent_list"`
DatasetParentList []string `json:"dataset_parent_list"` // used by bulkblocks API
DsParentList []DatasetParent `json:"ds_parent_list"` // provided by bulkdump API
}
// DatasetConfig represents dataset config structure used in BulkBlocks structure
type DatasetConfig struct {
ReleaseVersion string `json:"release_version"`
PsetHash string `json:"pset_hash"`
PsetName string `json:"pset_name"`
AppName string `json:"app_name"`
OutputModuleLabel string `json:"output_module_label"`
GlobalTag string `json:"global_tag"`
CreateBy string `json:"create_by"`
CreationDate int64 `json:"creation_date"`
}
// FileConfig represents file config structure used in BulkBlocks structure
type FileConfig struct {
ReleaseVersion string `json:"release_version"`
PsetHash string `json:"pset_hash"`
PsetName string `json:"pset_name"`
LFN string `json:"lfn"`
AppName string `json:"app_name"`
OutputModuleLabel string `json:"output_module_label"`
GlobalTag string `json:"global_tag"`
CreateBy string `json:"create_by"`
CreationDate int64 `json:"creation_date"`
}
// FileLumi represents file lumi structure used in File structure of BulkBlocks structure
type FileLumi struct {
LumiSectionNumber int64 `json:"lumi_section_num"`
RunNumber int64 `json:"run_num"`
EventCount int64 `json:"event_count"`
}
// File represents file structure used in BulkBlocks structure
type File struct {
CheckSum string `json:"check_sum"`
FileLumiList []FileLumi `json:"file_lumi_list"`
Adler32 string `json:"adler32"`
FileSize int64 `json:"file_size"`
EventCount int64 `json:"event_count"`
FileType string `json:"file_type"`
LastModifiedBy string `json:"last_modified_by"`
LastModificationDate int64 `json:"last_modification_date"`
LogicalFileName string `json:"logical_file_name"`
MD5 string `json:"md5"`
AutoCrossSection float64 `json:"auto_cross_section"`
IsFileValid int64 `json:"is_file_valid"`
}
// ProcessingEra represents processing era structure used in BulkBlocks structure
type ProcessingEra struct {
CreateBy string `json:"create_by"`
CreationDate int64 `json:"creation_date"`
ProcessingVersion int64 `json:"processing_version"`
Description string `json:"description"`
}
// PrimaryDataset represents primary dataset structure used in BulkBlocks structure
type PrimaryDataset struct {
PrimaryDSId int64 `json:"primary_ds_id"`
CreateBy string `json:"create_by"`
PrimaryDSType string `json:"primary_ds_type"`
PrimaryDSName string `json:"primary_ds_name"`
CreationDate int64 `json:"creation_date"`
}
// Dataset represents dataset structure used in BulkBlocks structure
type Dataset struct {
DatasetID int64 `json:"dataset_id"`
CreateBy string `json:"create_by"`
CreationDate int64 `json:"creation_date"`
PhysicsGroupName string `json:"physics_group_name"`
DatasetAccessType string `json:"dataset_access_type"`
DataTierName string `json:"data_tier_name"`
LastModifiedBy string `json:"last_modified_by"`
ProcessedDSName string `json:"processed_ds_name"`
Xtcrosssection float64 `json:"xtcrosssection"`
LastModificationDate int64 `json:"last_modification_date"`
Dataset string `json:"dataset"`
PrepID string `json:"prep_id"`
}
// AcquisitionEra represents AcquisitionEra structure use in BulkBlocks structure
type AcquisitionEra struct {
AcquisitionEraName string `json:"acquisition_era_name"`
StartDate int64 `json:"start_date"`
CreationDate int64 `json:"creation_date"`
EndDate int64 `json:"end_date"`
CreateBy string `json:"create_by"`
Description string `json:"description"`
}
// Block represents Block structure used in BulkBlocks structure
type Block struct {
BlockID int64 `json:"block_id"`
DatasetID int64 `json:"dataset_id"`
CreateBy string `json:"create_by"`
CreationDate int64 `json:"creation_date"`
OpenForWriting int64 `json:"open_for_writing"`
BlockName string `json:"block_name"`
FileCount int64 `json:"file_count"`
OriginSiteName string `json:"origin_site_name"`
BlockSize int64 `json:"block_size"`
LastModifiedBy string `json:"last_modified_by"`
LastModificationDate int64 `json:"last_modification_date"`
}
// BlockParent represents block parent structure used in BulkBlocks structure
type BlockParent struct {
ParentBlockName string `json:"parent_block_name"`
ThisBlockName string `json:"this_block_name"`
}
// DatasetParent represents dataset parent structure used in BulkBlocks structure
type DatasetParent struct {
ThisDatasetID string `json:"this_dataset_id"`
ParentDataset string `json:"parent_dataset"`
}
// InsertBulkBlocks DBS API. It relies on BulkBlocks record which by itself
// contains series of other records. The logic of this API is the following:
// we read dataset_conf_list part of the record and insert output config data,
// then we insert recursively PrimaryDSTypes, PrimaryDataset, ProcessingEras,
// AcquisitionEras, ..., Datasets, Blocks, Files, FileLumis, FileCofig list,
// and dataset parent lists.
//gocyclo:ignore
func (a *API) InsertBulkBlocks() error {
// read input data
data, err := io.ReadAll(a.Reader)
if err != nil {
log.Println("unable to read bulkblock input", err)
return Error(err, ReaderErrorCode, "", "dbs.bulkblocks.InsertBulkBlocks")
}
// get our request hash ID to be able to trace concurrent requests
hash := utils.GetHash(data)
// unmarshal the data into BulkBlocks record
var rec BulkBlocks
err = json.Unmarshal(data, &rec)
if err != nil {
log.Printf("unable to unmarshal bulkblock record %s, error %v", string(data), err)
return Error(err, UnmarshalErrorCode, "", "dbs.bulkblocks.InsertBulkBlocks")
}
// prepare file parentage map, i.e. find out file ids we need for FileParentList
parentFilesMap := make(map[string]int64)
for _, r := range rec.FileParentList {
// parent lfn should be already in DB
plfn := r.ParentLogicalFileName
pfid, err := QueryRow("FILES", "file_id", "logical_file_name", plfn)
if err != nil {
msg := fmt.Sprintf("unable to find parent lfn %s", plfn)
return Error(err, DatabaseErrorCode, msg, "dbs.bulkblocks.InsertBulkBlocksConcurrently")
}
parentFilesMap[plfn] = pfid
}
// start transaction
tx, err := DB.Begin()
if err != nil {
return Error(err, TransactionErrorCode, "", "dbs.bulkblocks.InsertBulkBlocks")
}
defer tx.Rollback()
var reader *bytes.Reader
api := &API{
Reader: reader,
CreateBy: a.CreateBy,
Params: make(Record),
}
var isFileValid, datasetID, blockID, fileID, fileTypeID int64
var primaryDatasetTypeID, primaryDatasetID, acquisitionEraID, processingEraID int64
var dataTierID, physicsGroupID, processedDatasetID, datasetAccessTypeID int64
creationDate := time.Now().Unix()
// check if is_file_valid was present in request, if not set it to 1
if !strings.Contains(string(data), "is_file_valid") {
isFileValid = 1
}
// insert dataset configuration
if utils.VERBOSE > 1 {
log.Println("insert output configs")
}
for _, rrr := range rec.DatasetConfigList {
data, err = json.Marshal(rrr)
if err != nil {
log.Println("unable to marshal dataset config list", err)
return Error(err, MarshalErrorCode, "", "dbs.bulkblocks.InsertBulkBlocks")
}
api.Reader = bytes.NewReader(data)
err = api.InsertOutputConfigsTx(tx)
if err != nil {
return Error(err, InsertErrorCode, "", "dbs.bulkblocks.InsertBulkBlocks")
}
}
// get primaryDatasetTypeID and insert record if it does not exists
if utils.VERBOSE > 1 {
log.Println("get primary dataset type ID")
}
pdstDS := PrimaryDSTypes{
PRIMARY_DS_TYPE: rec.PrimaryDataset.PrimaryDSType,
}
primaryDatasetTypeID, err = GetRecID(
tx,
&pdstDS,
"PRIMARY_DS_TYPES",
"primary_ds_type_id",
"primary_ds_type",
rec.PrimaryDataset.PrimaryDSType,
)
if err != nil {
if utils.VERBOSE > 1 {
log.Println("unable to find primary_ds_type_id for", rec.PrimaryDataset.PrimaryDSType)
}
return Error(err, GetIDErrorCode, "", "dbs.bulkblocks.InsertBulkBlocks")
}
// get primarayDatasetID and insert record if it does not exists
if utils.VERBOSE > 1 {
log.Println("get primary dataset ID")
}
if rec.PrimaryDataset.CreateBy == "" {
rec.PrimaryDataset.CreateBy = a.CreateBy
}
primDS := PrimaryDatasets{
PRIMARY_DS_NAME: rec.PrimaryDataset.PrimaryDSName,
PRIMARY_DS_TYPE_ID: primaryDatasetTypeID,
CREATION_DATE: rec.PrimaryDataset.CreationDate,
CREATE_BY: rec.PrimaryDataset.CreateBy,
}
primaryDatasetID, err = GetRecID(
tx,
&primDS,
"PRIMARY_DATASETS",
"primary_ds_id",
"primary_ds_name",
rec.PrimaryDataset.PrimaryDSName,
)
if err != nil {
if utils.VERBOSE > 1 {
log.Println("unable to find primary_ds_id for", rec.PrimaryDataset.PrimaryDSName)
}
return Error(err, GetIDErrorCode, "", "dbs.bulkblocks.InsertBulkBlocks")
}
// get processing era ID and insert record if it does not exists
if utils.VERBOSE > 1 {
log.Println("get processing era ID")
}
if rec.ProcessingEra.CreateBy == "" {
rec.ProcessingEra.CreateBy = a.CreateBy
}
pera := ProcessingEras{
PROCESSING_VERSION: rec.ProcessingEra.ProcessingVersion,
CREATION_DATE: creationDate,
CREATE_BY: rec.ProcessingEra.CreateBy,
DESCRIPTION: rec.ProcessingEra.Description,
}
processingEraID, err = GetRecID(
tx,
&pera,
"PROCESSING_ERAS",
"processing_era_id",
"processing_version",
rec.ProcessingEra.ProcessingVersion,
)
if err != nil {
if utils.VERBOSE > 1 {
log.Println("unable to find processing_era_id for", rec.ProcessingEra.ProcessingVersion)
}
return Error(err, GetIDErrorCode, "", "dbs.bulkblocks.InsertBulkBlocks")
}
// insert acquisition era if it does not exists
if utils.VERBOSE > 1 {
log.Println("get acquisition era ID")
}
if rec.AcquisitionEra.CreateBy == "" {
rec.AcquisitionEra.CreateBy = a.CreateBy
}
endDate := rec.AcquisitionEra.EndDate
if endDate == 0 {
endDate = time.Now().Unix() // TODO: figure out logic of endDate
}
aera := AcquisitionEras{
ACQUISITION_ERA_NAME: rec.AcquisitionEra.AcquisitionEraName,
START_DATE: rec.AcquisitionEra.StartDate,
END_DATE: endDate,
CREATION_DATE: creationDate,
CREATE_BY: rec.AcquisitionEra.CreateBy,
DESCRIPTION: rec.AcquisitionEra.Description,
}
acquisitionEraID, err = GetRecID(
tx,
&aera,
"ACQUISITION_ERAS",
"acquisition_era_id",
"acquisition_era_name",
rec.AcquisitionEra.AcquisitionEraName,
)
if err != nil {
if utils.VERBOSE > 1 {
log.Println("unable to find acquisition_era_id for", rec.AcquisitionEra.AcquisitionEraName)
}
return Error(err, GetIDErrorCode, "", "dbs.bulkblocks.InsertBulkBlocks")
}
// get dataTierID
if utils.VERBOSE > 1 {
log.Println("get data tier ID")
}
tier := DataTiers{
DATA_TIER_NAME: rec.Dataset.DataTierName,
CREATION_DATE: creationDate,
CREATE_BY: a.CreateBy,
}
dataTierID, err = GetRecID(
tx,
&tier,
"DATA_TIERS",
"data_tier_id",
"data_tier_name",
rec.Dataset.DataTierName,
)
if err != nil {
if utils.VERBOSE > 1 {
log.Println("unable to find data_tier_id for", rec.Dataset.DataTierName)
}
return Error(err, GetIDErrorCode, "", "dbs.bulkblocks.InsertBulkBlocks")
}
// get physicsGroupID
if utils.VERBOSE > 1 {
log.Println("get physics group ID")
}
pgrp := PhysicsGroups{
PHYSICS_GROUP_NAME: rec.Dataset.PhysicsGroupName,
}
physicsGroupID, err = GetRecID(
tx,
&pgrp,
"PHYSICS_GROUPS",
"physics_group_id",
"physics_group_name",
rec.Dataset.PhysicsGroupName,
)
if err != nil {
if utils.VERBOSE > 1 {
log.Println("unable to find physics_group_id for", rec.Dataset.PhysicsGroupName)
}
return Error(err, GetIDErrorCode, "", "dbs.bulkblocks.InsertBulkBlocks")
}
// get datasetAccessTypeID
if utils.VERBOSE > 1 {
log.Println("get dataset access type ID")
}
dat := DatasetAccessTypes{
DATASET_ACCESS_TYPE: rec.Dataset.DatasetAccessType,
}
datasetAccessTypeID, err = GetRecID(
tx,
&dat,
"DATASET_ACCESS_TYPES",
"dataset_access_type_id",
"dataset_access_type",
rec.Dataset.DatasetAccessType,
)
if err != nil {
if utils.VERBOSE > 1 {
log.Println("unable to find dataset_access_type_id for", rec.Dataset.DatasetAccessType)
}
return Error(err, GetIDErrorCode, "", "dbs.bulkblocks.InsertBulkBlocks")
}
if utils.VERBOSE > 1 {
log.Println("get processed dataset ID")
}
procDS := ProcessedDatasets{
PROCESSED_DS_NAME: rec.Dataset.ProcessedDSName,
}
processedDatasetID, err = GetRecID(
tx,
&procDS,
"PROCESSED_DATASETS",
"processed_ds_id",
"processed_ds_name",
rec.Dataset.ProcessedDSName,
)
if err != nil {
if utils.VERBOSE > 1 {
log.Println("unable to find processed_ds_id for", rec.Dataset.ProcessedDSName)
}
err := procDS.Insert(tx)
if err != nil {
if utils.VERBOSE > 1 {
log.Println("unable to insert processed dataset name record", err)
}
return Error(err, InsertErrorCode, "", "dbs.bulkblocks.InsertBulkBlocks")
}
processedDatasetID, err = GetID(
tx,
"PROCESSED_DATASETS",
"processed_ds_id",
"processed_ds_name",
rec.Dataset.ProcessedDSName,
)
if err != nil {
if utils.VERBOSE > 1 {
log.Printf("unable to find processed_ds_id %s error %v", rec.Dataset.ProcessedDSName, err)
}
return Error(err, InsertErrorCode, "", "dbs.bulkblocks.InsertBulkBlocks")
}
}
// insert dataset
if utils.VERBOSE > 1 {
log.Println("insert dataset")
}
if rec.Dataset.CreateBy == "" {
rec.Dataset.CreateBy = a.CreateBy
}
dataset := Datasets{
DATASET: rec.Dataset.Dataset,
IS_DATASET_VALID: 1,
PRIMARY_DS_ID: primaryDatasetID,
PROCESSED_DS_ID: processedDatasetID,
DATA_TIER_ID: dataTierID,
DATASET_ACCESS_TYPE_ID: datasetAccessTypeID,
ACQUISITION_ERA_ID: acquisitionEraID,
PROCESSING_ERA_ID: processingEraID,
PHYSICS_GROUP_ID: physicsGroupID,
XTCROSSSECTION: rec.Dataset.Xtcrosssection,
PREP_ID: rec.Dataset.PrepID,
CREATION_DATE: rec.Dataset.CreationDate,
CREATE_BY: rec.Dataset.CreateBy,
LAST_MODIFICATION_DATE: creationDate,
LAST_MODIFIED_BY: rec.Dataset.CreateBy,
}
// get datasetID
if utils.VERBOSE > 1 {
log.Println("get dataset ID")
}
datasetID, err = GetID(tx, "DATASETS", "dataset_id", "dataset", rec.Dataset.Dataset)
if err != nil {
if utils.VERBOSE > 1 {
log.Println("unable to find dataset_id for", rec.Dataset.Dataset, "will insert")
}
err = dataset.Insert(tx)
if err != nil {
if utils.VERBOSE > 1 {
log.Println("unable to insert dataset record", err)
}
return Error(err, InsertErrorCode, "", "dbs.bulkblocks.InsertBulkBlocks")
}
datasetID, err = GetID(tx, "DATASETS", "dataset_id", "dataset", rec.Dataset.Dataset)
if err != nil {
if utils.VERBOSE > 1 {
log.Printf("unable to get dataset_id for dataset %s error %v", rec.Dataset.Dataset, err)
}
return Error(err, GetIDErrorCode, "", "dbs.bulkblocks.InsertBulkBlocks")
}
}
// get outputModConfigID using datasetID
// since we already inserted records from DatasetConfigList
for _, r := range rec.DatasetConfigList {
var vals []interface{}
vals = append(vals, r.AppName)
vals = append(vals, r.PsetHash)
vals = append(vals, r.ReleaseVersion)
vals = append(vals, r.OutputModuleLabel)
vals = append(vals, r.GlobalTag)
stm := getSQL("datasetoutmodconfigs")
var oid float64
err := tx.QueryRow(stm, vals...).Scan(&oid)
if err != nil {
if utils.VERBOSE > 1 {
log.Printf("fail to get id for %s, %v, error %v", stm, vals, err)
}
}
// insert into DATASET_OUTPUT_MOD_CONFIGS
dsoRec := DatasetOutputModConfigs{
DATASET_ID: datasetID,
OUTPUT_MOD_CONFIG_ID: int64(oid),
}
err = dsoRec.Insert(tx)
if err != nil {
if utils.VERBOSE > 1 {
log.Println("unable to insert dataset output mod configs record", err)
}
return Error(err, InsertErrorCode, "", "dbs.bulkblocks.InsertBulkBlocks")
}
}
// insert block
if utils.VERBOSE > 1 {
log.Println("insert block")
}
if rec.Block.CreateBy == "" {
rec.Block.CreateBy = a.CreateBy
}
blk := Blocks{
BLOCK_NAME: rec.Block.BlockName,
DATASET_ID: datasetID,
OPEN_FOR_WRITING: rec.Block.OpenForWriting,
ORIGIN_SITE_NAME: rec.Block.OriginSiteName,
BLOCK_SIZE: rec.Block.BlockSize,
FILE_COUNT: rec.Block.FileCount,
CREATION_DATE: rec.Block.CreationDate,
CREATE_BY: rec.Block.CreateBy,
LAST_MODIFICATION_DATE: rec.Block.CreationDate,
LAST_MODIFIED_BY: rec.Block.CreateBy,
}
// get blockID
blockID, err = GetID(tx, "BLOCKS", "block_id", "block_name", rec.Block.BlockName)
if err != nil {
if utils.VERBOSE > 1 {
log.Println("unable to find block_id for", rec.Block.BlockName, "will insert")
}
err = blk.Insert(tx)
if err != nil {
if utils.VERBOSE > 1 {
log.Println("unable to insert block record", err)
}
return Error(err, InsertErrorCode, "", "dbs.bulkblocks.InsertBulkBlocks")
}
blockID, err = GetID(tx, "BLOCKS", "block_id", "block_name", rec.Block.BlockName)
if err != nil {
if utils.VERBOSE > 1 {
log.Printf("unable to find block_id for %s, error %v", rec.Block.BlockName, err)
}
return Error(err, GetIDErrorCode, "", "dbs.bulkblocks.InsertBulkBlocks")
}
}
// insert files
if utils.VERBOSE > 1 {
log.Println("insert files")
}
tempTable := fmt.Sprintf("ORA$PTT_TEMP_FILE_LUMIS_%d", time.Now().UnixMicro())
if DBOWNER == "sqlite" {
tempTable = "FILE_LUMIS"
}
filesMap := make(map[string]int64)
for _, rrr := range rec.Files {
// get fileTypeID and insert record if it does not exists
ftype := FileDataTypes{FILE_TYPE: rrr.FileType}
fileTypeID, err = GetRecID(
tx,
&ftype,
"FILE_DATA_TYPES",
"file_type_id",
"file_type",
rrr.FileType,
)
if err != nil {
if utils.VERBOSE > 1 {
log.Println("unable to find file_type_id for", rrr.FileType)
}
return Error(err, GetIDErrorCode, "", "dbs.bulkblocks.InsertBulkBlocks")
}
// get branch hash ID and insert record if it does not exists
// if rrr.BranchHash == "" {
// rrr.BranchHash = "branch-hash"
// }
cBy := rrr.LastModifiedBy
if cBy == "" {
cBy = a.CreateBy
}
lBy := rrr.LastModifiedBy
if lBy == "" {
lBy = a.CreateBy
}
// if the data string does contain the is_file_valid field, use value from request
if isFileValid == 0 {
if rrr.IsFileValid != 0 && rrr.IsFileValid != 1 {
msg := "wrong is_file_valid value"
return Error(InvalidParamErr, ParametersErrorCode, msg, "dbs.bulkblocks.InsertBulkBlocks")
}
isFileValid = rrr.IsFileValid
}
r := Files{
LOGICAL_FILE_NAME: rrr.LogicalFileName,
IS_FILE_VALID: isFileValid,
DATASET_ID: datasetID,
BLOCK_ID: blockID,
FILE_TYPE_ID: fileTypeID,
CHECK_SUM: rrr.CheckSum,
FILE_SIZE: rrr.FileSize,
EVENT_COUNT: rrr.EventCount,
ADLER32: rrr.Adler32,
MD5: rrr.MD5,
AUTO_CROSS_SECTION: rrr.AutoCrossSection,
CREATION_DATE: creationDate,
CREATE_BY: cBy,
LAST_MODIFICATION_DATE: creationDate,
LAST_MODIFIED_BY: lBy,
}
// insert file lumi list
fileID, err = GetID(tx, "FILES", "file_id", "logical_file_name", rrr.LogicalFileName)
if err != nil {
if utils.VERBOSE > 1 {
log.Println("unable to find file_id for", rrr.LogicalFileName, "will insert")
}
err = r.Insert(tx)
if err != nil {
if utils.VERBOSE > 1 {
log.Println("unable to insert File record", err)
}
return Error(err, InsertErrorCode, "", "dbs.bulkblocks.InsertBulkBlocks")
}
fileID, err = GetID(tx, "FILES", "file_id", "logical_file_name", rrr.LogicalFileName)
if err != nil {
if utils.VERBOSE > 1 {
log.Printf("unable to find block_id for %s, error %v", rec.Block.BlockName, err)
}
return Error(err, GetIDErrorCode, "", "dbs.bulkblocks.InsertBulkBlocks")
}
}
filesMap[rrr.LogicalFileName] = fileID
err = api.SelectFileLumiListInsert(tx, rrr.FileLumiList, tempTable, fileID, "dbs.bulkblocks.InsertBulkBlocks")
if err != nil {
return err
}
}
// insert file configuration
for _, rrr := range rec.FileConfigList {
data, err = json.Marshal(rrr)
if err != nil {
if utils.VERBOSE > 1 {
log.Println("unable to marshal file config list", err)
}
return Error(err, MarshalErrorCode, "", "dbs.bulkblocks.InsertBulkBlocks")
}
api.Reader = bytes.NewReader(data)
err = api.InsertFileOutputModConfigs(tx)
if err != nil {
if utils.VERBOSE > 1 {
log.Println("unable to insert file output mod config", err)
}
return Error(err, InsertErrorCode, "", "dbs.bulkblocks.InsertBulkBlocks")
}
}
// find out file ids we need for FileParentList
for _, r := range rec.FileParentList {
rrr := FileParents{}
lfn := r.LogicalFileName
if lfn == "" {
lfn = r.ThisLogicalFileName
}
if lfn == "" {
err := errors.New("mailformed file parent record")
msg := fmt.Sprintf("file parent record %+v does not contain LFN", r)
log.Println(msg)
return Error(err, NotImplementedApiCode, msg, "dbs.bulkblocks.InsertBulkBlocksConcurrently")
}
if fileID, ok := filesMap[lfn]; ok {
rrr.THIS_FILE_ID = fileID
} else {
err := errors.New("unable to locate LFN file id")
msg := fmt.Sprintf("no file id found for '%s'", lfn)
log.Println(msg)
return Error(err, SessionErrorCode, msg, "dbs.bulkblocks.InsertBulkBlocksConcurrently")
}
// parent lfn should be already in DB
plfn := r.ParentLogicalFileName
if pfid, ok := parentFilesMap[plfn]; ok {
rrr.PARENT_FILE_ID = pfid
// log.Println("### parent_logical_file_name", plfn, pfid)
} else {
err := errors.New("unable to locate parent file id")
msg := fmt.Sprintf("no file id found for parent '%s'", lfn)
log.Println(msg)
return Error(err, DatabaseErrorCode, msg, "dbs.bulkblocks.InsertBulkBlocksConcurrently")
}
err := rrr.Insert(tx)
if err != nil {
msg := fmt.Sprintf("%s unable to insert file parents record %+v, error %v", hash, rrr, err)
log.Println(msg)
return Error(err, InsertErrorCode, msg, "dbs.bulkblocks.InsertBulkBlocksConcurrently")
}
}
/*
// insert file parent list
data, err = json.Marshal(rec.FileParentList)
if err != nil {
if utils.VERBOSE > 1 {
log.Println("unable to marshal file parent list", err)
}
return Error(err, MarshalErrorCode, "", "dbs.bulkblocks.InsertBulkBlocks")
}
api.Reader = bytes.NewReader(data)
api.Params = make(Record)
err = api.InsertFileParentsTxt(tx)
if err != nil {
if utils.VERBOSE > 1 {
log.Println("unable to insert file parents", err)
}
msg := fmt.Sprintf("failed record %+v", rec)
return Error(err, InsertErrorCode, msg, "dbs.bulkblocks.InsertBulkBlocks")
}
*/
// insert dataset parent list
datasetParentList := rec.DatasetParentList
// use both DatasetParentList and DsParentList (for backward compatibility)
// and compose unique set of dataset parents
for _, d := range rec.DsParentList {
datasetParentList = append(datasetParentList, d.ParentDataset)
}
datasetParentList = utils.Set(datasetParentList)
for _, ds := range datasetParentList {
// get file id for parent dataset
pid, err := GetID(tx, "DATASETS", "dataset_id", "dataset", ds)
if err != nil {
if utils.VERBOSE > 1 {
log.Println("unable to find dataset_id for", ds)
}
return Error(err, GetIDErrorCode, "", "dbs.bulkblocks.InsertBulkBlocks")
}
r := DatasetParents{THIS_DATASET_ID: datasetID, PARENT_DATASET_ID: pid}
err = r.Insert(tx)
if err != nil {
if utils.VERBOSE > 1 {
log.Println("unable to insert parent dataset record", err)
}
return Error(err, InsertErrorCode, "", "dbs.bulkblocks.InsertBulkBlocks")
}
}
// commit transaction
err = tx.Commit()
if err != nil {
if utils.VERBOSE > 1 {
log.Println("fail to commit transaction", err)
}
return Error(err, CommitErrorCode, "", "dbs.bulkblocks.InsertBulkBlocks")
}
if a.Writer != nil {
a.Writer.Write([]byte(`[]`))
}
return nil
}