<commit_before>"use strict";
var fs = require('fs');
var path = require('path');
var util = require('util');

var opInfo;
var filename;
var functionBody = null;
var finalCallback;
var session; // MMS session
var doDeploy;

exports.deployVirtualOp = function deployVirtualOp(mmsSession,callback)
{
    doVirtualOp(mmsSession,true,callback);
}
exports.undeployVirtualOp = function undeployVirtualOp(mmsSession,callback)
{
    doVirtualOp(mmsSession,false,callback);
}

function doVirtualOp(mmsSession,_deDeploy,callback)
{
    doDeploy = _deDeploy;
    finalCallback = callback;
    session = mmsSession;

    opInfo = JSON.parse(fs.readFileSync('operation.json','utf-8'));
    functionBody = null;

    var ls = fs.readdirSync('.');
    ls.forEach(function(name) {
        var end = name.substring(name.length - 3);
        if(end === '.js') {
            if(functionBody) {
                console.log('More than one .js file in operation is not allowed: ' + opInfo.name);
                process.exit(1);
            } else {
                filename = name;
                functionBody = fs.readFileSync(name, "utf-8");
            }
        }
    });

    //
    //  Delete the existing operation, if any
    //
    var obj = new Object();
    obj.op = "INVOKE";
    obj.targetType = "CdmOperation";
    obj.name = "deleteOperation";
    obj.params = new Object();
        obj.params.typeName = opInfo.type;
        obj.params.name = opInfo.name;

    session.directive(obj,'rest.operation.does.not.exist',onDeleteOperationComplete);
}

function onDeleteOperationComplete(err,restResponse)
{
    if (restResponse.status == "SUCCESS")
    {
        console.log('Operation "' + opInfo.name + '" deleted');
    }
    else if (	restResponse.errors
			&& 	(restResponse.errors.length == 1)
			&&	(restResponse.errors[0].errorCode == 'rest.operation.does.not.exist'))
	{
		if (!doDeploy)
        {
            console.log("Operation " + opInfo.name + " was not found");
        }
	}
	else
	{
		console.log('Error deleting operation "' + opInfo.name + '": ' + processRestResponse(restResponse));
	}


    //
    //  Delete the virtual CdmType, if any
    //
    var obj = new Object();
    obj.op = "INVOKE";
    obj.targetType = "CdmType";
    obj.name = "deleteCdmType";
    obj.params = new Object();
        obj.params.typeName = opInfo.type;
    obj.options = new Object();

    //console.log('Deleting type "' + opInfo.type + '...');
    session.directive(obj,'rest.target.type.does.not.exist',onDeleteTypeComplete);

}
function onDeleteTypeComplete(err,restResponse)
{
    if (restResponse.status == "SUCCESS")
    {
        console.log('CdmType "' + opInfo.type + '" deleted');
    }
    else if (	restResponse.errors
			&& 	(restResponse.errors.length == 1)
			&&	(restResponse.errors[0].errorCode == 'rest.target.type.does.not.exist'))
	{
        if (!doDeploy)
        {
            console.log("CdmType " + opInfo.type + " was not found");
        }
    }
    else
    {
        console.log('Error deleting Type "' + opInfo.type + '": ' +  processRestResponse(restResponse));
    }

    if (!doDeploy)
    {
        finalCallback();
        return;
    }
    var returns;

    if (opInfo.returns && opInfo.returns.splice)
    {
        returns = opInfo.returns;
    }
    else
    {
        returns = new Array();
    }

    var ary = new Array();
    var obj;

    //
    //  Create the Virtual CdmType
    //
    obj = new Object();
    obj.op = "INVOKE";

    obj.targetType = "CdmType";
    obj.name = "createCdmType";
    obj.params = new Object();
        obj.params.typeName = opInfo.type;
        obj.params.storage = "virtual";
        obj.params.externallySourced = false;
        obj.params.sourceStrategy = "sync";
        obj.params.uniqueExternalId = true;
        obj.params.overrideAllowed = true;
        obj.params.multiTypes = returns;
        obj.params.implementingOpName = opInfo.name;

    session.directive(obj,createOperation);

}

function createOperation(err,result) {
    if (result.status == "SUCCESS")
    {
        console.log('CdmType "' + opInfo.type + '" created');
    }
    else
    {
        console.log('Creation of CdmType "' + opInfo.type + '" failed: ' + processRestResponse(result));
        process.exit(1);
    }

    var parameters = new Array();

    var parmsToAddString = opInfo.parameters;

    if (parmsToAddString)<commit_msg>When deploying a virtual operation we no longer delete and re-create the CdmType; instead we just attempt to create the type and ignore the "type already exists" error. This will be dramatically faster when deploying virtual ops to the global tenant.<commit_after>"use strict";
var fs = require('fs');
var path = require('path');
var util = require('util');

var opInfo;
var filename;
var functionBody = null;
var finalCallback;
var session; // MMS session
var doDeploy;

exports.deployVirtualOp = function deployVirtualOp(mmsSession,callback)
{
    doVirtualOp(mmsSession,true,callback);
}
exports.undeployVirtualOp = function undeployVirtualOp(mmsSession,callback)
{
    doVirtualOp(mmsSession,false,callback);
}

function doVirtualOp(mmsSession,_deDeploy,callback)
{
    doDeploy = _deDeploy;
    finalCallback = callback;
    session = mmsSession;

    opInfo = JSON.parse(fs.readFileSync('operation.json','utf-8'));
    functionBody = null;

    var ls = fs.readdirSync('.');
    ls.forEach(function(name) {
        var end = name.substring(name.length - 3);
        if(end === '.js') {
            if(functionBody) {
                console.log('More than one .js file in operation is not allowed: ' + opInfo.name);
                process.exit(1);
            } else {
                filename = name;
                functionBody = fs.readFileSync(name, "utf-8");
            }
        }
    });

    //
    //  Delete the existing operation, if any
    //
    var obj = new Object();
    obj.op = "INVOKE";
    obj.targetType = "CdmOperation";
    obj.name = "deleteOperation";
    obj.params = new Object();
        obj.params.typeName = opInfo.type;
        obj.params.name = opInfo.name;

    session.directive(obj,'rest.operation.does.not.exist',onDeleteOperationComplete);
}

function onDeleteOperationComplete(err,restResponse)
{
    if (restResponse.status == "SUCCESS")
    {
        console.log('Operation "' + opInfo.name + '" deleted');
    }
    else if (	restResponse.errors
			&& 	(restResponse.errors.length == 1)
			&&	(restResponse.errors[0].errorCode == 'rest.operation.does.not.exist'))
	{
		if (!doDeploy)
        {
            console.log("Operation " + opInfo.name + " was not found");
        }
	}
	else
	{
		console.log('Error deleting operation "' + opInfo.name + '": ' + processRestResponse(restResponse));
	}

    //
    //  If we are deploying the operation just try to create it and ignore errors if it already exists
    //
    if (doDeploy)
    {
        onDeleteTypeComplete(null,null);
        return
    }

    //
    //  Delete the virtual CdmType, if any
    //
    var obj = new Object();
    obj.op = "INVOKE";
    obj.targetType = "CdmType";
    obj.name = "deleteCdmType";
    obj.params = new Object();
        obj.params.typeName = opInfo.type;
    obj.options = new Object();

    //console.log('Deleting type "' + opInfo.type + '...');
    session.directive(obj,'rest.target.type.does.not.exist',onDeleteTypeComplete);

}
function onDeleteTypeComplete(err,restResponse)
{
    if (restResponse)
    {
        if (restResponse.status == "SUCCESS")
        {
            console.log('CdmType "' + opInfo.type + '" deleted');
        }
        else if (	restResponse.errors
                && 	(restResponse.errors.length == 1)
                &&	(restResponse.errors[0].errorCode == 'rest.target.type.does.not.exist'))
        {
            if (!doDeploy)
            {
                console.log("CdmType " + opInfo.type + " was not found");
            }
        }
        else
        {
            console.log('Error deleting Type "' + opInfo.type + '": ' +  processRestResponse(restResponse));
        }
    }

    if (!doDeploy)
    {
        finalCallback();
        return;
    }
    var returns;

    if (opInfo.returns && opInfo.returns.splice)
    {
        returns = opInfo.returns;
    }
    else
    {
        returns = new Array();
    }

    var ary = new Array();
    var obj;

    //
    //  Create the Virtual CdmType
    //
    obj = new Object();
    obj.op = "INVOKE";

    obj.targetType = "CdmType";
    obj.name = "createCdmType";
    obj.params = new Object();
        obj.params.typeName = opInfo.type;
        obj.params.storage = "virtual";
        obj.params.externallySourced = false;
        obj.params.sourceStrategy = "sync";
        obj.params.uniqueExternalId = true;
        obj.params.overrideAllowed = true;
        obj.params.multiTypes = returns;
        obj.params.implementingOpName = opInfo.name;

    session.directive(obj,'cdm.type.exists',createOperation);

}

function createOperation(err,result) {
    if (result.status == "SUCCESS")
    {
        console.log('CdmType "' + opInfo.type + '" created');
    }
    else
    {
        if (doDeploy && (result.errors.length == 1) &&  (result.errors[0].errorCode == 'cdm.type.exists'))
        {
            console.log('CdmType "' + opInfo.type + '" already exists');
        }
        else
        {

            console.log('Creation of CdmType "' + opInfo.type + '" failed: ' + processRestResponse(result));
            process.exit(1);
        }
    }

    var parameters = new Array();

    var parmsToAddString = opInfo.parameters;

    if (parmsToAddString)
{'fraction_non_alphanumeric': 0.13673788431474726, 'fraction_numerical': 0.0020844189682126106, 'mean_word_length': 2.371749824314828, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '757905', 'n_tokens_mistral': 3017, 'n_tokens_neox': 2935, 'n_words': 598}