handler.ts 39.8 KB
Newer Older
Diego Molteni's avatar
Diego Molteni committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// ============================================================================
// Copyright 2017-2020, Schlumberger
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ============================================================================

import { Request as expRequest, Response as expResponse } from 'express';
import { Auth } from '../../auth';
Diego Molteni's avatar
Diego Molteni committed
19
import { Config, JournalFactoryTenantClient, StorageFactory } from '../../cloud';
Diego Molteni's avatar
Diego Molteni committed
20
import { DESStorage, DESUtils } from '../../dataecosystem';
Diego Molteni's avatar
Diego Molteni committed
21
22
import { Error, Feature, FeatureFlags, Params, Response, Utils } from '../../shared';
import { SubProjectDAO } from '../subproject';
Diego Molteni's avatar
Diego Molteni committed
23
24
import { TenantDAO, TenantModel } from '../tenant';
import { DatasetDAO } from './dao';
Diego Molteni's avatar
Diego Molteni committed
25
import { Locker, IWriteLockSession } from './locker';
Diego Molteni's avatar
Diego Molteni committed
26
27
import { DatasetOP } from './optype';
import { DatasetParser } from './parser';
Diego Molteni's avatar
Diego Molteni committed
28
import { v4 as uuidv4 } from 'uuid';
Diego Molteni's avatar
Diego Molteni committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42

export class DatasetHandler {

    // handler for the [ /dataset ] endpoints
    public static async handler(req: expRequest, res: expResponse, op: DatasetOP) {
        try {

            if (op === DatasetOP.CheckCTag) {
                Response.writeOK(res, await this.checkCTag(req));
            } else {

                const tenant = await TenantDAO.get(req.params.tenantid);

                if (op === DatasetOP.Register) {
Diego Molteni's avatar
Diego Molteni committed
43
                    Response.writeOK(res, await this.register(req, tenant));
Diego Molteni's avatar
Diego Molteni committed
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
                } else if (op === DatasetOP.Get) {
                    Response.writeOK(res, await this.get(req, tenant));
                } else if (op === DatasetOP.List) {
                    Response.writeOK(res, await this.list(req, tenant));
                } else if (op === DatasetOP.Delete) {
                    Response.writeOK(res, await this.delete(req, tenant));
                } else if (op === DatasetOP.Patch) {
                    Response.writeOK(res, await this.patch(req, tenant));
                } else if (op === DatasetOP.Lock) {
                    Response.writeOK(res, await this.lock(req, tenant));
                } else if (op === DatasetOP.UnLock) {
                    Response.writeOK(res, await this.unlock(req, tenant));
                } else if (op === DatasetOP.Exists) {
                    Response.writeOK(res, await this.exists(req, tenant));
                } else if (op === DatasetOP.Sizes) {
                    Response.writeOK(res, await this.sizes(req, tenant));
                } else if (op === DatasetOP.Permission) {
                    Response.writeOK(res, await this.checkPermissions(req, tenant));
                } else if (op === DatasetOP.ListContent) {
                    Response.writeOK(res, await this.listContent(req, tenant));
                } else if (op === DatasetOP.PutTags) {
                    Response.writeOK(res, await this.putTags(req, tenant));
                } else { throw (Error.make(Error.Status.UNKNOWN, 'Internal Server Error')); }
            }

        } catch (error) { Response.writeError(res, error); }

    }

    private static async checkCTag(req: expRequest): Promise<boolean> {

        // parse user request
        const userInput = DatasetParser.checkCTag(req);

        // init journalClient client
        const journalClient = JournalFactoryTenantClient.get({
Diego Molteni's avatar
Diego Molteni committed
80
81
            gcpid: userInput.tenantID, esd: userInput.dataPartitionID, default_acls: 'any', name: 'any'
        });
Diego Molteni's avatar
Diego Molteni committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

        // Retrieve the dataset metadata
        const datasetOUT = (await DatasetDAO.get(journalClient, userInput.dataset))[0];

        // check if the dataset does not exist
        if (!datasetOUT) {
            throw (Error.make(Error.Status.NOT_FOUND, 'The dataset ' +
                Config.SDPATHPREFIX + userInput.dataset.tenant + '/' +
                userInput.dataset.subproject + userInput.dataset.path +
                userInput.dataset.name + ' does not exist'));
        }

        // return the cTag check validation
        return datasetOUT.ctag === userInput.dataset.ctag;
    }

    // register a new dataset
Diego Molteni's avatar
Diego Molteni committed
99
    private static async register(req: expRequest, tenant: TenantModel) {
Diego Molteni's avatar
Diego Molteni committed
100
101

        // parse the user input and create the dataset metadata model
102
        const userInput = await DatasetParser.register(req);
Diego Molteni's avatar
Diego Molteni committed
103
104
        const dataset = userInput[0];
        const seismicmeta = userInput[1];
Diego Molteni's avatar
Diego Molteni committed
105
        let writeLockSession: IWriteLockSession;
Diego Molteni's avatar
Diego Molteni committed
106
107

        const journalClient = JournalFactoryTenantClient.get(tenant);
Diego Molteni's avatar
Diego Molteni committed
108
        const transaction = journalClient.getTransaction();
Diego Molteni's avatar
Diego Molteni committed
109
110
111

        try {

Diego Molteni's avatar
Diego Molteni committed
112
113
            await transaction.run();

Diego Molteni's avatar
Diego Molteni committed
114
115
116
117
            // attempt to acquire a mutex on the dataset name and set the lock for the dataset in redis
            // a mutex is applied on the resource on the shared cahce (removed at the end of the method)
            writeLockSession = await Locker.createWriteLock(
                dataset, req.headers['x-seismic-dms-lockid'] as string);
Diego Molteni's avatar
Diego Molteni committed
118

Diego Molteni's avatar
Diego Molteni committed
119
120
121
122
123
124
            // if the call is idempotent return the dataset value
            if(writeLockSession.idempotent) {
                const alreadyRegisteredDataset =  (await DatasetDAO.get(journalClient, dataset))[0];
                await Locker.removeWriteLock(writeLockSession, true); // Keep the lock session
                return alreadyRegisteredDataset;
            }
Diego Molteni's avatar
Diego Molteni committed
125
126
127
128
129
130
131
132
133
134

            const spkey = journalClient.createKey({
                namespace: Config.SEISMIC_STORE_NS + '-' + tenant.name,
                path: [Config.SUBPROJECTS_KIND, req.params.subprojectid],
            });

            // get the subproject info
            const subproject = await SubProjectDAO.get(journalClient, tenant.name, req.params.subprojectid, spkey);

            // set gcs URL and LegaTag with the subproject information
Diego Molteni's avatar
Diego Molteni committed
135
            dataset.gcsurl = subproject.gcs_bucket + '/' + uuidv4()
Diego Molteni's avatar
Diego Molteni committed
136
137
138
139
            dataset.ltag = dataset.ltag || subproject.ltag;

            // ensure that a legal tag exist
            if (!dataset.ltag) {
Diego Molteni's avatar
Diego Molteni committed
140
                throw Error.make(Error.Status.NOT_FOUND,
Diego Molteni's avatar
Diego Molteni committed
141
142
143
144
145
146
147
148
149
                    'No legal-tag has been found for the subproject resource ' +
                    Config.SDPATHPREFIX + dataset.tenant + '/' + dataset.subproject +
                    ' the storage metdatada cannot be updated without a valida legal-tag');
            }

            // check if has read access, if legal tag is valid, and if the dataset does not already exist
            await Promise.all([
                FeatureFlags.isEnabled(Feature.AUTHORIZATION) ?
                    Auth.isWriteAuthorized(req.headers.authorization,
Diego Molteni's avatar
Diego Molteni committed
150
                        subproject.acls.admins,
Diego Molteni's avatar
Diego Molteni committed
151
152
153
154
                        dataset.tenant, dataset.subproject, tenant.esd, req[Config.DE_FORWARD_APPKEY]) : undefined,
                FeatureFlags.isEnabled(Feature.LEGALTAG) ?
                    dataset.ltag ? Auth.isLegalTagValid(
                        req.headers.authorization, dataset.ltag,
Diego Molteni's avatar
Diego Molteni committed
155
                        tenant.esd, req[Config.DE_FORWARD_APPKEY]) : undefined : undefined,
Diego Molteni's avatar
Diego Molteni committed
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
            ]);

            // check if dataset already exist
            if ((await DatasetDAO.get(journalClient, dataset))[0]) {
                throw (Error.make(Error.Status.ALREADY_EXISTS,
                    'The dataset ' + Config.SDPATHPREFIX + dataset.tenant + '/' +
                    dataset.subproject + dataset.path + dataset.name +
                    ' already exists'));
            }

            // Populate the storage record with other mandatory field if not supplied.
            if (seismicmeta) {

                // if id is given, take it. otherwise generate
                if (!seismicmeta.id) {
                    dataset.seismicmeta_guid = DESUtils.getDataPartitionID(tenant.esd) + seismicmeta.recordType
                        + Utils.makeID(16);
                    seismicmeta.id = dataset.seismicmeta_guid;
                } else {
                    dataset.seismicmeta_guid = seismicmeta.id;
                }

                // remove the recordType attribute as guid is now computed
                delete seismicmeta.recordType;

                // if acl is given, take it. otherwise generate
                if (!seismicmeta.acl) {
                    seismicmeta.acl = {
                        owners: ['data.default.owners@' + tenant.esd],
                        viewers: ['data.default.viewers@' + tenant.esd],
                    };
                }

                // [TO REVIEW]
                // wrt legaltags, there is a field 'otherRelevantDataCountries' that will have to considered
                // for now force it to US, if does not exist. To review before complete PR

                // this could be included as default in the request
                if (!seismicmeta.legal) {
                    seismicmeta.legal = {
                        legaltags: [dataset.ltag],
                        otherRelevantDataCountries: ['US'],
                    };
                }

            }


            // prepare the keys
            const dskey = journalClient.createKey({
                namespace: Config.SEISMIC_STORE_NS + '-' + dataset.tenant + '-' + dataset.subproject,
                path: [Config.DATASETS_KIND],
            });

Diego Molteni's avatar
Diego Molteni committed
210
            // save the dataset entity
Diego Molteni's avatar
Diego Molteni committed
211
            await Promise.all([
Diego Molteni's avatar
Diego Molteni committed
212
                DatasetDAO.register(transaction, { key: dskey, data: dataset }),
Diego Molteni's avatar
Diego Molteni committed
213
214
215
216
217
218
219
220
                (seismicmeta && (FeatureFlags.isEnabled(Feature.SEISMICMETA_STORAGE))) ?
                    DESStorage.insertRecord(req.headers.authorization,
                        [seismicmeta], tenant.esd, req[Config.DE_FORWARD_APPKEY]) : undefined,
            ]);

            // attach the gcpid for fast check
            dataset.ctag = dataset.ctag + tenant.gcpid + ';' + DESUtils.getDataPartitionID(tenant.esd);

Diego Molteni's avatar
Diego Molteni committed
221
222
            // release the mutex and keep the lock session
            await Locker.removeWriteLock(writeLockSession, true);
Diego Molteni's avatar
Diego Molteni committed
223
            await transaction.commit();
Diego Molteni's avatar
Diego Molteni committed
224
            return dataset;
Diego Molteni's avatar
Diego Molteni committed
225

Diego Molteni's avatar
Diego Molteni committed
226
227
        } catch (err) {

Diego Molteni's avatar
Diego Molteni committed
228
229
            // release the mutex and unlock the resource
            await Locker.removeWriteLock(writeLockSession);
Diego Molteni's avatar
Diego Molteni committed
230
            await transaction.rollback();
Diego Molteni's avatar
Diego Molteni committed
231
232
233
234
235
236
237
238
239
240
241
242
243
            throw (err);

        }

    }

    // retrieve the dataset metadata
    private static async get(req: expRequest, tenant: TenantModel) {

        // parse user request
        const userInput = DatasetParser.get(req);
        const datasetIN = userInput[0];

Diego Molteni's avatar
Diego Molteni committed
244
        // retrieve journalClient client
Diego Molteni's avatar
Diego Molteni committed
245
246
247
248
249
        const journalClient = JournalFactoryTenantClient.get(tenant);

        // Retrieve the dataset metadata
        const datasetOUT = (await DatasetDAO.get(journalClient, datasetIN))[0];

Diego Molteni's avatar
Diego Molteni committed
250
251
252
253
254
255
256
        const spkey = journalClient.createKey({
            namespace: Config.SEISMIC_STORE_NS + '-' + tenant.name,
            path: [Config.SUBPROJECTS_KIND, datasetIN.subproject],
        });

        const subproject = await SubProjectDAO.get(journalClient, tenant.name, datasetIN.subproject, spkey)

Diego Molteni's avatar
Diego Molteni committed
257
258
259
260
261
262
263
264
265
266
267
268
269
        // check if the dataset does not exist
        if (!datasetOUT) {
            throw (Error.make(Error.Status.NOT_FOUND,
                'The dataset ' + Config.SDPATHPREFIX + datasetIN.tenant + '/' +
                datasetIN.subproject + datasetIN.path + datasetIN.name + ' does not exist'));
        }

        // Check if retrieve the seismic metadata storage record
        const getSeismicMeta = datasetOUT.seismicmeta_guid !== undefined && userInput[1];

        // Check if user has read access and legal tag is valid
        await Promise.all([
            FeatureFlags.isEnabled(Feature.AUTHORIZATION) ?
Diego Molteni's avatar
Diego Molteni committed
270
271
272
                Auth.isReadAuthorized(req.headers.authorization,
                    subproject.acls.viewers.concat(subproject.acls.admins),
                    datasetIN.tenant, datasetIN.subproject, tenant.esd, req[Config.DE_FORWARD_APPKEY]) : undefined,
Diego Molteni's avatar
Diego Molteni committed
273
            FeatureFlags.isEnabled(Feature.LEGALTAG) ?
Diego Molteni's avatar
Diego Molteni committed
274
275
                datasetOUT.ltag ? Auth.isLegalTagValid(
                    req.headers.authorization, datasetOUT.ltag,
Diego Molteni's avatar
Diego Molteni committed
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
                    tenant.esd, req[Config.DE_FORWARD_APPKEY]) : undefined : undefined
        ]);

        // return the seismicmetadata (if exist)
        if (getSeismicMeta) {
            const seismicMeta = await DESStorage.getRecord(req.headers.authorization, datasetOUT.seismicmeta_guid,
                tenant.esd, req[Config.DE_FORWARD_APPKEY]);
            if (seismicMeta) {
                (datasetOUT as any).seismicmeta = seismicMeta;
            }
        }

        // attach the gcpid for fast check
        datasetOUT.ctag = datasetOUT.ctag + tenant.gcpid + ';' + DESUtils.getDataPartitionID(tenant.esd);

        return datasetOUT;

    }

    // list the datasets in a subproject
    private static async list(req: expRequest, tenant: TenantModel) {

        // Retrieve the dataset path information
        const dataset = DatasetParser.list(req);

Diego Molteni's avatar
Diego Molteni committed
301
302
303
304
305
306
307
308
309
310
311
        // init journalClient client
        const journalClient = JournalFactoryTenantClient.get(tenant);

        const spkey = journalClient.createKey({
            namespace: Config.SEISMIC_STORE_NS + '-' + tenant.name,
            path: [Config.SUBPROJECTS_KIND, dataset.subproject],
        });

        // Retrieve subproject information
        const subproject = await SubProjectDAO.get(journalClient, tenant.name, dataset.subproject, spkey)

Diego Molteni's avatar
Diego Molteni committed
312
313
314
        if (FeatureFlags.isEnabled(Feature.AUTHORIZATION)) {
            // Check authorizations
            await Auth.isReadAuthorized(req.headers.authorization,
Diego Molteni's avatar
Diego Molteni committed
315
                subproject.acls.viewers.concat(subproject.acls.admins),
Diego Molteni's avatar
Diego Molteni committed
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
                dataset.tenant, dataset.subproject, tenant.esd, req[Config.DE_FORWARD_APPKEY]);
        }


        // Retrieve the list of datasets metadata
        const datasets = await DatasetDAO.list(journalClient, dataset);

        // attach the gcpid for fast check
        for (const item of datasets) {
            item.ctag = item.ctag + tenant.gcpid + ';' + DESUtils.getDataPartitionID(tenant.esd);
        }

        // Retrieve the list of datasets metadata
        return datasets;

    }

    // delete a dataset
    private static async delete(req: expRequest, tenant: TenantModel) {

        // Retrieve the dataset path information
        const datasetIn = DatasetParser.delete(req);

Diego Molteni's avatar
Diego Molteni committed
339
340
341
342
343
344
345
        // ensure is not write locked
        if(!Config.SKIP_WRITE_LOCK_CHECK_ON_MUTABLE_OPERATIONS) {
            if (Locker.isWriteLock(await Locker.getLockFromModel(datasetIn))) {
                throw (Error.make(Error.Status.LOCKED,
                    'The dataset ' + Config.SDPATHPREFIX + datasetIn.tenant + '/' +
                    datasetIn.subproject + datasetIn.path + datasetIn.name + ' is write locked'));
            }
Diego Molteni's avatar
Diego Molteni committed
346
347
348
349
        }

        // init datastore client
        const journalClient = JournalFactoryTenantClient.get(tenant);
Diego Molteni's avatar
Diego Molteni committed
350
351
352
353
354
355
356
357

        // retrieve subproject meta info
        const spkey = journalClient.createKey({
            namespace: Config.SEISMIC_STORE_NS + '-' + tenant.name,
            path: [Config.SUBPROJECTS_KIND, datasetIn.subproject],
        });
        const subproject = await SubProjectDAO.get(journalClient, tenant.name, datasetIn.subproject, spkey);

Diego Molteni's avatar
Diego Molteni committed
358
        try {
Diego Molteni's avatar
Diego Molteni committed
359

360
361
362
363
364
365
366
367
            // check authorization (write)
            if (FeatureFlags.isEnabled(Feature.AUTHORIZATION)) {
                // Check authorizations
                await Auth.isWriteAuthorized(req.headers.authorization,
                    subproject.acls.admins,
                    tenant.name, subproject.name, tenant.esd, req[Config.DE_FORWARD_APPKEY]);
            }

Diego Molteni's avatar
Diego Molteni committed
368
369
370
            // Retrieve the dataset metadata
            const dataset = (await DatasetDAO.get(journalClient, datasetIn))[0];

Diego Molteni's avatar
Diego Molteni committed
371
            // if the dataset does not exist return ok
Diego Molteni's avatar
Diego Molteni committed
372
            if (!dataset) { return; }
Diego Molteni's avatar
Diego Molteni committed
373
374
375
376
377
378
379
380
381
382
383
384

            // check if valid url
            if (!dataset.gcsurl || dataset.gcsurl.indexOf('/') === -1) {
                throw (Error.make(Error.Status.UNKNOWN,
                    'The dataset ' + Config.SDPATHPREFIX + datasetIn.tenant + '/' +
                    datasetIn.subproject + datasetIn.path + datasetIn.name +
                    ' cannot be deleted as it does not have a valid gcs url in the metadata catalogue.'));
            }

            // Delete the dataset metadata (both firestore and DEStorage)
            await Promise.all([
                // delete the dataset entity
Diego Molteni's avatar
Diego Molteni committed
385
                DatasetDAO.delete(journalClient, dataset),
Diego Molteni's avatar
Diego Molteni committed
386
387
388
389
390
391
                // delete des storage record
                (dataset.seismicmeta_guid && (FeatureFlags.isEnabled(Feature.SEISMICMETA_STORAGE))) ?
                    DESStorage.deleteRecord(req.headers.authorization,
                        dataset.seismicmeta_guid, tenant.esd, req[Config.DE_FORWARD_APPKEY]) : undefined,
            ]);

Diego Molteni's avatar
Diego Molteni committed
392
            // Delete all phisical objects (not wait for full objects deletion)
Diego Molteni's avatar
Diego Molteni committed
393
394
395
396
397
            const bucketName = dataset.gcsurl.split('/')[0];
            const gcsprefix = dataset.gcsurl.split('/')[1];
            const storage = StorageFactory.build(Config.CLOUDPROVIDER, tenant);
            await storage.deleteObjects(bucketName, gcsprefix);

Diego Molteni's avatar
Diego Molteni committed
398
399
            // remove any remaining locks (this should be removed with SKIP_WRITE_LOCK_CHECK_ON_MUTABLE_OPERATIONS)
            await Locker.unlock(journalClient, dataset)
Diego Molteni's avatar
Diego Molteni committed
400
401
402

        } catch (err) {
            throw (err);
Diego Molteni's avatar
Diego Molteni committed
403

Diego Molteni's avatar
Diego Molteni committed
404
405
406
407
408
409
410
411
        }
    }

    // patch the dataset metadata
    private static async patch(req: expRequest, tenant: TenantModel) {

        // Retrieve the dataset path information
        const [datasetIN, seismicmeta, newName, wid] = DatasetParser.patch(req);
412

Diego Molteni's avatar
Diego Molteni committed
413
        // retrieve datastore client
Diego Molteni's avatar
Diego Molteni committed
414
        const journalClient = JournalFactoryTenantClient.get(tenant);
Diego Molteni's avatar
Diego Molteni committed
415
        const transaction = journalClient.getTransaction();
Diego Molteni's avatar
Diego Molteni committed
416

417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
        // return immediately if it is a simple close wiht empty body (no patch to apply)
        if (Object.keys(req.body).length === 0 && req.body.constructor === Object && wid) {

            // Retrieve the dataset metadata
            const dataset = (await DatasetDAO.get(journalClient, datasetIN))[0];

            // check if the dataset does not exist
            if (!dataset) {
                throw (Error.make(Error.Status.NOT_FOUND,
                    'The dataset ' + Config.SDPATHPREFIX + datasetIN.tenant + '/' +
                    datasetIN.subproject + datasetIN.path + datasetIN.name + ' does not exist'));
            }

            // unlock the detaset
            const unlockRes = await Locker.unlock(journalClient, datasetIN, wid)
            dataset.sbit = unlockRes.id;
            dataset.sbit_count = unlockRes.cnt;

            return dataset;
        }

        // unlock the detaset for close opeartion (and patch)
Diego Molteni's avatar
Diego Molteni committed
439
440
        const lockres = wid ? await Locker.unlock(journalClient, datasetIN, wid) : { id: null, cnt: 0 };

Diego Molteni's avatar
Diego Molteni committed
441
442
443
444
445
446
447
448
449
        // ensure nobody got the lock between the close and the mutext acquistion
        if(!Config.SKIP_WRITE_LOCK_CHECK_ON_MUTABLE_OPERATIONS) {
            if (Locker.isWriteLock(await Locker.getLockFromModel(datasetIN))) {
                throw (Error.make(Error.Status.LOCKED,
                    'The dataset ' + Config.SDPATHPREFIX + datasetIN.tenant + '/' +
                    datasetIN.subproject + datasetIN.path + datasetIN.name + ' is write locked'));
            }
        }

Diego Molteni's avatar
Diego Molteni committed
450
451
        try {

Diego Molteni's avatar
Diego Molteni committed
452
453
            await transaction.run();

Diego Molteni's avatar
Diego Molteni committed
454
455
456
457
458
459
460
            const spkey = journalClient.createKey({
                namespace: Config.SEISMIC_STORE_NS + '-' + tenant.name,
                path: [Config.SUBPROJECTS_KIND, datasetIN.subproject]
            });

            // Retrieve subproject information
            const subproject = await SubProjectDAO.get(journalClient, tenant.name, req.params.subprojectid, spkey);
Diego Molteni's avatar
Diego Molteni committed
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476

            // Retrieve the dataset metadata
            const results = await DatasetDAO.get(journalClient, datasetIN);
            const datasetOUT = results[0];
            const datasetOUTKey = results[1];

            // check if the dataset does not exist
            if (!datasetOUT) {
                throw (Error.make(Error.Status.NOT_FOUND,
                    'The dataset ' + Config.SDPATHPREFIX + datasetIN.tenant + '/' +
                    datasetIN.subproject + datasetIN.path + datasetIN.name + ' does not exist'));
            }

            if (FeatureFlags.isEnabled(Feature.AUTHORIZATION)) {
                // Check authorizations
                await Auth.isWriteAuthorized(req.headers.authorization,
Diego Molteni's avatar
Diego Molteni committed
477
478
                    subproject.acls.admins,
                    datasetIN.tenant, subproject.name, tenant.esd, req[Config.DE_FORWARD_APPKEY]);
Diego Molteni's avatar
Diego Molteni committed
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
            }

            // patch datasetOUT with datasetIN
            if (datasetIN.metadata) { datasetOUT.metadata = datasetIN.metadata; }
            if (datasetIN.filemetadata) { datasetOUT.filemetadata = datasetIN.filemetadata; }
            if (datasetIN.last_modified_date) { datasetOUT.last_modified_date = datasetIN.last_modified_date; }
            if (datasetIN.readonly !== undefined) { datasetOUT.readonly = datasetIN.readonly; }
            if (datasetIN.gtags !== undefined && datasetIN.gtags.length > 0) { datasetOUT.gtags = datasetIN.gtags; }
            if (datasetIN.ltag) {
                if (FeatureFlags.isEnabled(Feature.LEGALTAG)) {
                    await Auth.isLegalTagValid(
                        req.headers.authorization, datasetIN.ltag, tenant.esd, req[Config.DE_FORWARD_APPKEY]);
                }
                datasetOUT.ltag = datasetIN.ltag;
            }

            if (newName) {
                if (newName === datasetIN.name) {
                    throw (Error.make(Error.Status.ALREADY_EXISTS,
                        'The dataset ' + Config.SDPATHPREFIX + datasetIN.tenant + '/' +
                        datasetIN.subproject + datasetIN.path + newName + ' already exists'));
                }

                datasetIN.name = newName;

                const renameResults = await DatasetDAO.get(journalClient, datasetIN);
                if (renameResults[1] !== undefined) {
                    throw (Error.make(Error.Status.ALREADY_EXISTS,
                        'The dataset ' + Config.SDPATHPREFIX + datasetIN.tenant + '/' +
                        datasetIN.subproject + datasetIN.path + newName + ' already exists'));

                }

                datasetOUT.name = newName;
            }

            // Populate the storage record with other mandatory field if not supplied.
Diego Molteni's avatar
Diego Molteni committed
516
            let seismicmetaDE: any;
Diego Molteni's avatar
Diego Molteni committed
517
518
519
            if (seismicmeta) {

                // return the seismicmetadata (if exists)
Diego Molteni's avatar
Diego Molteni committed
520
                if (datasetOUT.seismicmeta_guid) {
Diego Molteni's avatar
Diego Molteni committed
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536

                    // seismicmeta is already there, need to patch
                    seismicmetaDE = await DESStorage.getRecord(req.headers.authorization, datasetOUT.seismicmeta_guid,
                        tenant.esd, req[Config.DE_FORWARD_APPKEY]);

                    for (const key of Object.keys(seismicmeta)) {
                        seismicmetaDE[key] = seismicmeta[key];
                    }

                    datasetOUT.seismicmeta_guid = seismicmeta.id;

                } else {

                    // mandatory field required if a new seismic metadata record is ingested (kind/data required)
                    Params.checkString(seismicmeta.kind, 'kind');
                    Params.checkObject(seismicmeta.data, 'data');
537
538
539
540
541
542
543
544

                    // {data-parititon(delfi)|auhtority(osdu)}.{source}.{entityType}.{semanticSchemaVersion}
                    if((seismicmeta.kind as string).split(':').length !== 4) {
                        throw (Error.make(Error.Status.BAD_REQUEST, 'The seismicmeta kind is in a wrong format'));
                    }

                    // (recortdType == entityType)
                    seismicmeta.recordType = ':' + (seismicmeta.kind as string).split(':')[2] + ':';
Diego Molteni's avatar
Diego Molteni committed
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

                    // if id is given, take it. otherwise generate
                    if (!seismicmeta.id) {
                        datasetOUT.seismicmeta_guid = DESUtils.getDataPartitionID(tenant.esd) + seismicmeta.recordType
                            + Utils.makeID(16);
                        seismicmeta.id = datasetOUT.seismicmeta_guid;
                    } else {
                        datasetOUT.seismicmeta_guid = seismicmeta.id;
                    }

                    // remove the recordType attribute as guid is now computed
                    delete seismicmeta.recordType;

                    // if acl is given, take it. otherwise generate
                    if (!seismicmeta.acl) {
                        seismicmeta.acl = {
                            owners: ['data.default.owners@' + tenant.esd],
                            viewers: ['data.default.viewers@' + tenant.esd],
                        };
                    }

                    // [TO REVIEW]
                    // wrt legaltags, there is a field 'otherRelevantDataCountries' that will have to considered
                    // for now force it to US, if does not exist. To review before complete PR

                    // this could be included as default in the request
                    if (!seismicmeta.legal) {

                        // ensure that a legal tag exist
                        if (!datasetOUT.ltag) {

                            throw (!subproject.ltag ?
                                Error.make(Error.Status.NOT_FOUND,
                                    'No legal-tag has been found for the subproject resource ' +
                                    Config.SDPATHPREFIX + datasetIN.tenant + '/' + datasetIN.subproject +
                                    ' the storage metdatada cannot be updated without a valida legal-tag') :
                                Error.make(Error.Status.NOT_FOUND,
                                    'No legal-tag has been found on the dataset resource ' +
                                    Config.SDPATHPREFIX + datasetIN.tenant + '/' + datasetIN.subproject +
                                    datasetIN.path + datasetIN.name +
                                    ' the storage metdatada cannot be updated without a valida legal-tag'));
                        }

                        // insert legal tag
                        seismicmeta.legal = {
                            legaltags: [datasetOUT.ltag],
                            otherRelevantDataCountries: ['US'],
                        };
                    }

                    seismicmetaDE = seismicmeta;
                }
            }

            await Promise.all([
Diego Molteni's avatar
Diego Molteni committed
600
                DatasetDAO.update(transaction, datasetOUT, datasetOUTKey),
Diego Molteni's avatar
Diego Molteni committed
601
602
603
604
605
606
607
608
609
610
                (seismicmeta && (FeatureFlags.isEnabled(Feature.SEISMICMETA_STORAGE)))
                    ? DESStorage.insertRecord(req.headers.authorization, [seismicmetaDE],
                        tenant.esd, req[Config.DE_FORWARD_APPKEY]) : undefined]);


            // attach lock information
            if (wid) {
                datasetOUT.sbit = lockres.id;
                datasetOUT.sbit_count = lockres.cnt;
            }
Diego Molteni's avatar
Diego Molteni committed
611
            await transaction.commit();
Diego Molteni's avatar
Diego Molteni committed
612
613
614
615

            // attach the gcpid for fast check
            datasetOUT.ctag = datasetOUT.ctag + tenant.gcpid + ';' + DESUtils.getDataPartitionID(tenant.esd);

Diego Molteni's avatar
Diego Molteni committed
616
            return datasetOUT;
Diego Molteni's avatar
Diego Molteni committed
617

Diego Molteni's avatar
Diego Molteni committed
618
        } catch (err) {
Diego Molteni's avatar
Diego Molteni committed
619
            await transaction.rollback();
Diego Molteni's avatar
Diego Molteni committed
620
621
622
623
624
625
626
627
628
629
630
631
632
            throw (err);
        }
    }

    // lock the dataset metadata for opening
    private static async lock(req: expRequest, tenant: TenantModel) {

        // parse user request
        const userInput = DatasetParser.lock(req);
        const datasetIN = userInput.dataset;
        const open4write = userInput.open4write;
        const wid = userInput.wid;

Diego Molteni's avatar
Diego Molteni committed
633
        // retrieve datastore client
Diego Molteni's avatar
Diego Molteni committed
634
635
636
637
638
        const journalClient = JournalFactoryTenantClient.get(tenant);

        // Retrieve the dataset metadata
        const datasetOUT = (await DatasetDAO.get(journalClient, datasetIN))[0];

Diego Molteni's avatar
Diego Molteni committed
639
640
641
642
643
644
645
646
        const spkey = journalClient.createKey({
            namespace: Config.SEISMIC_STORE_NS + '-' + tenant.name,
            path: [Config.SUBPROJECTS_KIND, datasetIN.subproject],
        });


        const subproject = await SubProjectDAO.get(journalClient, tenant.name, datasetIN.subproject, spkey)

Diego Molteni's avatar
Diego Molteni committed
647
648
649
650
651
652
653
654
655
656
657
        // check if the dataset does not exist
        if (!datasetOUT) {
            throw (Error.make(Error.Status.NOT_FOUND,
                'The dataset ' + Config.SDPATHPREFIX + datasetIN.tenant + '/' +
                datasetIN.subproject + datasetIN.path + datasetIN.name + ' does not exist'));
        }

        await Promise.all([
            FeatureFlags.isEnabled(Feature.AUTHORIZATION) ?
                open4write ?
                    Auth.isWriteAuthorized(req.headers.authorization,
Diego Molteni's avatar
Diego Molteni committed
658
                        subproject.acls.admins,
Diego Molteni's avatar
Diego Molteni committed
659
660
                        datasetIN.tenant, datasetIN.subproject, tenant.esd, req[Config.DE_FORWARD_APPKEY]) :
                    Auth.isReadAuthorized(req.headers.authorization,
Diego Molteni's avatar
Diego Molteni committed
661
                        subproject.acls.viewers.concat(subproject.acls.admins),
Diego Molteni's avatar
Diego Molteni committed
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
                        datasetIN.tenant, datasetIN.subproject, tenant.esd, req[Config.DE_FORWARD_APPKEY]) :
                undefined,
            FeatureFlags.isEnabled(Feature.LEGALTAG) ?
                datasetOUT.ltag ?
                    Auth.isLegalTagValid(req.headers.authorization, datasetOUT.ltag,
                        tenant.esd, req[Config.DE_FORWARD_APPKEY]) :
                    undefined :
                undefined
        ]);

        // managing read-only datasets
        if (datasetOUT.readonly) {
            if (open4write) {
                throw (Error.make(Error.Status.BAD_REQUEST,
                    'The dataset ' + Config.SDPATHPREFIX + datasetIN.tenant + '/' +
                    datasetIN.subproject + datasetIN.path + datasetIN.name +
                    ' cannot be locked (read-only dataset)'));
            } else {
                // attach the gcpid for fast check
                datasetOUT.ctag = datasetOUT.ctag + tenant.gcpid + ';' + DESUtils.getDataPartitionID(tenant.esd);
                return datasetOUT;
            }
        }

        // lock in cache
        const lockres = open4write ?
Diego Molteni's avatar
Diego Molteni committed
688
689
690
691
            await Locker.acquireWriteLock(
                journalClient, datasetIN, req.headers['x-seismic-dms-lockid'] as string, wid) :
            await Locker.acquireReadLock(
                journalClient, datasetIN, req.headers['x-seismic-dms-lockid'] as string, wid);
Diego Molteni's avatar
Diego Molteni committed
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706

        // attach lock information
        datasetOUT.sbit = lockres.id;
        datasetOUT.sbit_count = lockres.cnt;

        // attach the gcpid for fast check
        datasetOUT.ctag = datasetOUT.ctag + tenant.gcpid + ';' + DESUtils.getDataPartitionID(tenant.esd);

        return datasetOUT;

    }

    // unlock the dataset metadata for opening
    private static async unlock(req: expRequest, tenant: TenantModel) {

Diego Molteni's avatar
Diego Molteni committed
707
        // parse user request
Diego Molteni's avatar
Diego Molteni committed
708
709
        const datasetIN = DatasetParser.unlock(req);

Diego Molteni's avatar
Diego Molteni committed
710
        // retrieve datastore client
Diego Molteni's avatar
Diego Molteni committed
711
712
713
714
715
        const journalClient = JournalFactoryTenantClient.get(tenant);

        // Retrieve the dataset metadata
        const dataset = (await DatasetDAO.get(journalClient, datasetIN))[0];

Diego Molteni's avatar
Diego Molteni committed
716
717
718
719
720
721
722
723

        const spkey = journalClient.createKey({
            namespace: Config.SEISMIC_STORE_NS + '-' + tenant.name,
            path: [Config.SUBPROJECTS_KIND, datasetIN.subproject],
        });

        const subproject = await SubProjectDAO.get(journalClient, tenant.name, datasetIN.subproject, spkey)

Diego Molteni's avatar
Diego Molteni committed
724
725
726
727
728
729
730
731
732
        // check if the dataset does not exist
        if (!dataset) {
            throw (Error.make(Error.Status.NOT_FOUND,
                'The dataset ' + Config.SDPATHPREFIX + datasetIN.tenant + '/' +
                datasetIN.subproject + datasetIN.path + datasetIN.name + ' does not exist'));
        }

        // check if user is write authorized
        await Auth.isWriteAuthorized(req.headers.authorization,
Diego Molteni's avatar
Diego Molteni committed
733
            subproject.acls.admins,
Diego Molteni's avatar
Diego Molteni committed
734
735
736
737
738
739
740
741
742
743
744
745
746
            tenant.name, dataset.subproject, tenant.esd, req[Config.DE_FORWARD_APPKEY]);

        // unlock
        await Locker.unlock(journalClient, dataset);

    }

    // check if a list of datasets exist in a subproject
    private static async exists(req: expRequest, tenant: TenantModel) {

        // Retrieve the dataset path information
        const datasets = DatasetParser.exists(req);

Diego Molteni's avatar
Diego Molteni committed
747
748
749
750
751
752
753
754
755
756
757
        // init journalClient client
        const journalClient = JournalFactoryTenantClient.get(tenant);

        const spkey = journalClient.createKey({
            namespace: Config.SEISMIC_STORE_NS + '-' + datasets[0].tenant,
            path: [Config.SUBPROJECTS_KIND, datasets[0].subproject],
        });

        // Retrieve subproject information
        const subproject = await SubProjectDAO.get(journalClient, tenant.name, req.params.subprojectid, spkey);

Diego Molteni's avatar
Diego Molteni committed
758
759
        if (FeatureFlags.isEnabled(Feature.AUTHORIZATION)) {
            await Auth.isReadAuthorized(req.headers.authorization,
Diego Molteni's avatar
Diego Molteni committed
760
                subproject.acls.viewers.concat(subproject.acls.admins),
Diego Molteni's avatar
Diego Molteni committed
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
                datasets[0].tenant, datasets[0].subproject, tenant.esd, req[Config.DE_FORWARD_APPKEY]);
        }


        // Check if the required datasets exist
        const results: boolean[] = [];
        for (const dataset of datasets) {
            results.push((await DatasetDAO.get(journalClient, dataset))[0] !== undefined);
        }

        return results;
    }

    // retrieve the dataset size for a list of datasets
    private static async sizes(req: expRequest, tenant: TenantModel) {

        // Retrieve the dataset path information
        const datasets = DatasetParser.sizes(req);

Diego Molteni's avatar
Diego Molteni committed
780
781
782
783
784
785
786
787
788
789
        // init journalClient client
        const journalClient = JournalFactoryTenantClient.get(tenant);

        const spkey = journalClient.createKey({
            namespace: Config.SEISMIC_STORE_NS + '-' + datasets[0].tenant,
            path: [Config.SUBPROJECTS_KIND, datasets[0].subproject],
        });

        const subproject = await SubProjectDAO.get(journalClient, datasets[0].tenant, datasets[0].subproject, spkey);

Diego Molteni's avatar
Diego Molteni committed
790
791
        if (FeatureFlags.isEnabled(Feature.AUTHORIZATION)) {
            await Auth.isReadAuthorized(req.headers.authorization,
Diego Molteni's avatar
Diego Molteni committed
792
                subproject.acls.viewers.concat(subproject.acls.admins),
Diego Molteni's avatar
Diego Molteni committed
793
794
795
                datasets[0].tenant, datasets[0].subproject, tenant.esd, req[Config.DE_FORWARD_APPKEY]);
        }

Diego Molteni's avatar
Diego Molteni committed
796

Diego Molteni's avatar
Diego Molteni committed
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819

        // Check if the required datasets exist
        const results: number[] = [];
        for (let dataset of datasets) {
            dataset = (await DatasetDAO.get(journalClient, dataset))[0];
            if (dataset === undefined) {
                results.push(-1);
                continue;
            }
            results.push(!dataset.filemetadata || !dataset.filemetadata.size ? -1 : dataset.filemetadata.size);

        }

        return results;

    }

    // list the path content
    private static async listContent(req: expRequest, tenant: TenantModel) {

        // Retrieve the dataset information
        const dataset = DatasetParser.listContent(req);

Diego Molteni's avatar
Diego Molteni committed
820
821
822
823
824
825
826
827
828
829
830
        // init journalClient client
        const journalClient = JournalFactoryTenantClient.get(tenant);

        const spkey = journalClient.createKey({
            namespace: Config.SEISMIC_STORE_NS + '-' + dataset.tenant,
            path: [Config.SUBPROJECTS_KIND, dataset.subproject],
        });

        const subproject = await SubProjectDAO.get(journalClient, dataset.tenant, dataset.subproject, spkey)

        if (FeatureFlags.isEnabled(Feature.AUTHORIZATION)) {
Diego Molteni's avatar
Diego Molteni committed
831
832
            // Check authorizations
            await Auth.isReadAuthorized(req.headers.authorization,
Diego Molteni's avatar
Diego Molteni committed
833
                subproject.acls.viewers.concat(subproject.acls.admins),
Diego Molteni's avatar
Diego Molteni committed
834
835
836
837
838
839
840
841
842
843
844
845
846
847
                dataset.tenant, dataset.subproject, tenant.esd, req[Config.DE_FORWARD_APPKEY]);
        }

        // list the folder content
        return await DatasetDAO.listContent(journalClient, dataset);

    }

    private static async putTags(req: expRequest, tenant: TenantModel) {

        const datasetIN = DatasetParser.putTags(req);

        // init journalClient client
        const journalClient = JournalFactoryTenantClient.get(tenant);
Diego Molteni's avatar
Diego Molteni committed
848
        const transaction = journalClient.getTransaction();
Diego Molteni's avatar
Diego Molteni committed
849

Diego Molteni's avatar
Diego Molteni committed
850
851
852
853
854
855
856
857
        // ensure is not write locked
        if(!Config.SKIP_WRITE_LOCK_CHECK_ON_MUTABLE_OPERATIONS) {
            if (Locker.isWriteLock(await Locker.getLockFromModel(datasetIN))) {
                throw (Error.make(Error.Status.LOCKED,
                    'The dataset ' + Config.SDPATHPREFIX + datasetIN.tenant + '/' +
                    datasetIN.subproject + datasetIN.path + datasetIN.name + ' is write locked'));
            }
        }
Diego Molteni's avatar
Diego Molteni committed
858
859

        try {
Diego Molteni's avatar
Diego Molteni committed
860

Diego Molteni's avatar
Diego Molteni committed
861
862
            await transaction.run();

Diego Molteni's avatar
Diego Molteni committed
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
            const results = await DatasetDAO.get(journalClient, datasetIN);
            const datasetOUT = results[0];
            const datasetOUTKey = results[1];

            // Check if the dataset does not exist
            if (!datasetOUT) {
                throw (Error.make(Error.Status.NOT_FOUND,
                    'The dataset ' + Config.SDPATHPREFIX + datasetIN.tenant + '/' +
                    datasetIN.subproject + datasetIN.path + datasetIN.name + ' does not exist'));
            }

            if (datasetOUT.gtags) {
                datasetOUT.gtags = datasetOUT.gtags.concat(datasetIN.gtags);
                datasetOUT.gtags = datasetOUT.gtags.filter((item, index) => datasetOUT.gtags.indexOf(item) === index);
            } else {
                datasetOUT.gtags = datasetIN.gtags;
            }

Diego Molteni's avatar
Diego Molteni committed
881
882
883
884
885
886
887
888
            // Retrieve subproject information
            const spkey = journalClient.createKey({
                namespace: Config.SEISMIC_STORE_NS + '-' + tenant.name,
                path: [Config.SUBPROJECTS_KIND, datasetIN.subproject],
            });

            const subproject = await SubProjectDAO.get(journalClient, tenant.name, datasetIN.subproject, spkey);

Diego Molteni's avatar
Diego Molteni committed
889
890
            if (FeatureFlags.isEnabled(Feature.AUTHORIZATION)) {
                await Auth.isWriteAuthorized(req.headers.authorization,
Diego Molteni's avatar
Diego Molteni committed
891
                    subproject.acls.admins,
Diego Molteni's avatar
Diego Molteni committed
892
893
894
                    datasetIN.tenant, datasetIN.subproject, tenant.esd, req[Config.DE_FORWARD_APPKEY]);
            }

Diego Molteni's avatar
Diego Molteni committed
895
896
            await DatasetDAO.update(transaction, datasetOUT, datasetOUTKey);
            await transaction.commit();
Diego Molteni's avatar
Diego Molteni committed
897
898

        } catch (err) {
Diego Molteni's avatar
Diego Molteni committed
899
            await transaction.rollback();
Diego Molteni's avatar
Diego Molteni committed
900
901
902
903
904
905
906
907
            throw (err);
        }

    }

    // check the permissions of a user on a dataset
    private static async checkPermissions(req: expRequest, tenant: TenantModel) {

Diego Molteni's avatar
Diego Molteni committed
908
        // Retrieve the dataset path information
Diego Molteni's avatar
Diego Molteni committed
909
910
911
912
913
914
915
916
917
918
919
920
        const dataset = DatasetParser.checkPermissions(req);

        // init journalClient client
        const journalClient = JournalFactoryTenantClient.get(tenant);

        // check if the dataset does not exist
        if (!(await DatasetDAO.get(journalClient, dataset))[0]) {
            throw (Error.make(Error.Status.NOT_FOUND,
                'The dataset ' + Config.SDPATHPREFIX + dataset.tenant + '/' +
                dataset.subproject + dataset.path + dataset.name + ' does not exist'));
        }

Diego Molteni's avatar
Diego Molteni committed
921
922
923
924
925
926
927
        const spkey = journalClient.createKey({
            namespace: Config.SEISMIC_STORE_NS + '-' + tenant.name,
            path: [Config.SUBPROJECTS_KIND, dataset.subproject],
        });

        const subproject = await SubProjectDAO.get(journalClient, tenant.name, dataset.subproject, spkey)

Diego Molteni's avatar
Diego Molteni committed
928
929
930
931
932
        const res = { read: false, write: false, delete: false };

        if (FeatureFlags.isEnabled(Feature.AUTHORIZATION)) {
            // Check if has write and read access
            res.write = await Auth.isWriteAuthorized(req.headers.authorization,
Diego Molteni's avatar
Diego Molteni committed
933
                subproject.acls.admins,
Diego Molteni's avatar
Diego Molteni committed
934
935
936
                dataset.tenant, dataset.subproject, tenant.esd, req[Config.DE_FORWARD_APPKEY], false);
            res.read = await Auth.isReadAuthorized(
                req.headers.authorization,
Diego Molteni's avatar
Diego Molteni committed
937
                subproject.acls.viewers.concat(subproject.acls.admins),
Diego Molteni's avatar
Diego Molteni committed
938
939
940
941
942
943
944
945
946
947
948
                dataset.tenant, dataset.subproject, tenant.esd, req[Config.DE_FORWARD_APPKEY], false);
        } else {
            res.write = true;
            res.read = true;
        }
        res.delete = res.write;
        return res;

    }

}