@ultipa-graph/ultipa-driver
Version:
NodeJS SDK for ultipa-server 5.2
322 lines • 12.5 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HDCExtra = exports.HDCBuilder = exports.HDCDirection = exports.FORMAT_ALGO = void 0;
const connection_base_1 = require("./connection.base");
const utils_1 = require("../..//utils");
const ultipa_pb_1 = require("../../proto/ultipa_pb");
const types_1 = require("../../types/types");
const path_1 = __importDefault(require("path"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const crypto_1 = __importDefault(require("crypto"));
const types_extra_1 = require("../../types/types.extra");
const { CommandList } = utils_1.UQLMAKER;
const FORMAT_ALGO = (algo) => {
return {
name: algo.name,
type: algo.type,
writeSupportType: algo.write_support_type,
canRollback: algo.can_rollback,
configContext: algo.config_context,
version: algo.version,
params: algo.params,
description: algo.description,
};
};
exports.FORMAT_ALGO = FORMAT_ALGO;
var HDCDirection;
(function (HDCDirection) {
HDCDirection["OUT"] = "out";
HDCDirection["IN"] = "in";
HDCDirection["UNDIRECTED"] = "undirected";
})(HDCDirection = exports.HDCDirection || (exports.HDCDirection = {}));
class HDCBuilder {
syncType = types_1.HDCSyncType.STATIC;
hdcGraphName;
hdcServerName;
nodeSchema;
edgeSchema;
direction = HDCDirection.UNDIRECTED;
loadId = true;
isDefault = false;
}
exports.HDCBuilder = HDCBuilder;
class HDCExtra extends connection_base_1.ConnectionBase {
/**
* Show HDC graph
* @param hdcGraphName
* @param config
* @returns
*/
async showHDCGraph(config) {
let command = CommandList.showHDCGraph;
let uqlMaker = new utils_1.UQLMAKER(command, config);
let res = await this.uql(uqlMaker.toString(), config);
return res.items[types_extra_1.ResponseTableName.HDC_GRAPH_LIST].asHDCGraphs();
}
/**
* Drop HDC graph
* @param hdcGraphName
* @param config
* @returns
*/
async dropHDCGraph(hdcGraphName, config) {
let command = CommandList.dropHDCGraph;
let uqlMaker = new utils_1.UQLMAKER(command, config, hdcGraphName);
return this.uql(uqlMaker.toString(), config);
}
/**
* Create HDC graph by schema
* @param hdcGraphName
* @param nodeSchemas
* @param edgeSchemas
* @param hdcName
* @param update
* @param config
*/
async createHDCGraphBySchema(builder, config) {
let command = CommandList.createHDCGraph;
let req = {
nodes: Object.fromEntries(builder.nodeSchema),
edges: Object.fromEntries(builder.edgeSchema),
direction: builder.direction,
load_id: builder.loadId,
update: builder.syncType,
query: "query",
default: builder.isDefault
};
let commandP = [builder.hdcServerName, builder.hdcGraphName, req];
let uqlMaker = new utils_1.UQLMAKER(command, config, commandP);
let res = await this.gql('CALL ' + uqlMaker.toString(), config);
return {
status: utils_1.FormatResponse.successStatus(),
statisics: res.statistics,
jobId: res.items[types_extra_1.ResponseTableName.JOB_RESULT]?.asTable()?.toKV()?.pop().new_job_id
};
}
/**
* Show HDC algo
* @param hdcServerName
* @param config
*/
async showHDCAlgo(hdcServerName, config) {
let command = utils_1.UQLMAKER.CommandList.showHDCAlgo;
let uqlMaker = new utils_1.UQLMAKER(command, config, hdcServerName);
let res = await this.uql(uqlMaker.toString(), config);
return res.items[types_extra_1.ResponseTableName.ALGO_LIST].asAlgos();
}
/**
* Uninstalls an Ultipa graph algorithm in the instance.
* @param algoName
* @param hdcServerName
* @param config
* @returns Response<boolean>
*/
async uninstallHDCAlgo(algoName, hdcServerName, config) {
return new Promise(async (resolve, reject) => {
try {
let clientInfo = await this.getClientInfo({
graphSetName: config?.graph,
isGlobal: true,
timezone: config?.timezone,
timezoneOffset: config?.timezoneOffset,
});
let request = new ultipa_pb_1.UninstallAlgoRequest();
request.setAlgoName(algoName);
if (hdcServerName) {
let with_server = new ultipa_pb_1.WithServer();
with_server.setHdcServerName(hdcServerName);
request.setWithServer(with_server);
}
clientInfo.client.controlsClient.uninstallAlgo(request, clientInfo.metadata, (err, reply) => {
let response = new types_1.Response();
if (err) {
response = utils_1.FormatResponse.unknownError(err, response);
resolve(response);
return;
}
response.status = utils_1.FormatType.status(reply.getStatus()),
resolve(response);
});
}
catch (error) {
reject(error);
}
});
}
/**
* Rollback algo
* @param algoName
* @param hdcServerName
* @param config
* @returns Response<boolean>
*/
async rollbackHDCAlgo(algoName, hdcServerName, config) {
return new Promise(async (resolve, reject) => {
try {
let clientInfo = await this.getClientInfo({
graphSetName: config?.graph,
isGlobal: true,
timezone: config?.timezone,
timezoneOffset: config?.timezoneOffset,
});
let request = new ultipa_pb_1.RollbackAlgoRequest();
request.setAlgoName(algoName);
let with_server = new ultipa_pb_1.WithServer();
with_server.setHdcServerName(hdcServerName);
request.setWithServer(with_server);
clientInfo.client.controlsClient.rollbackAlgo(request, clientInfo.metadata, (err, reply) => {
let response = new types_1.Response();
if (err) {
response = utils_1.FormatResponse.unknownError(err, response);
resolve(response);
return;
}
response.status = utils_1.FormatType.status(reply.getStatus()),
resolve(response);
});
}
catch (error) {
reject(error);
}
});
}
/**
*Installs an Ultipa graph algorithm in the instance. files includes all file paths required to install the algo.
* @param soFile
* @param ymlFile
* @param hdcName
* @param config
* @returns Response<boolean>
*/
async installHDCAlgo(files, hdcServerName, config) {
const soFile = files.find(file => file.endsWith('.so'));
const ymlFile = files.find(file => file.endsWith('.yml') || file.endsWith('.yaml'));
if (!soFile || !ymlFile) {
throw new Error('Must provide a .so file and a .yml/.yaml file');
}
return this.installAlgoAio({
hdc: hdcServerName,
paths: {
algoFilePath: soFile,
infoFilePath: ymlFile
},
isHdc: true
}, config);
}
/**
* install algo aio
* @param req
* @param config
* @returns
*/
async installAlgoAio(req, config) {
if (req.datas) {
return this._installAlgo({
...req.datas,
isHdc: req?.isHdc
}, config);
}
let filePaths = [req.paths.algoFilePath, req.paths.infoFilePath];
let _req = {
datas: [],
isHdc: req.isHdc,
hdc: req.hdc
};
for (let i = 0; i < filePaths.length; i++) {
const filePath = filePaths[i];
let chunk = fs_extra_1.default.readFileSync(filePath);
let md5 = crypto_1.default.createHash("md5").update(chunk).digest("hex");
let fileName = path_1.default.basename(filePath);
_req.datas.push({
chunk,
md5,
fileName,
});
}
return this._installAlgo(_req, config);
}
/**
* install algo
* @param req
* @param config
* @returns
* */
async _installAlgo(req, config) {
return new Promise(async (resolve, reject) => {
try {
let clientInfo = await this.getClientInfo({
graphSetName: config?.graph,
isGlobal: true,
timezone: config?.timezone,
timezoneOffset: config?.timezoneOffset,
});
let isError = null;
let callback = (err, reply) => {
let response = new types_1.Response();
if (err) {
isError = err;
response = utils_1.FormatResponse.unknownError(err, response);
resolve(response);
return;
}
response.status = utils_1.FormatType.status(reply.getStatus()),
resolve(response);
};
let v = clientInfo.client.controlsClient.installAlgo(clientInfo.metadata, callback);
for (let i = 0; i < req.datas.length; i++) {
if (isError) {
break;
}
const { chunk, md5, fileName } = req.datas[i];
let request = new ultipa_pb_1.InstallAlgoRequest();
request.setFileName(fileName);
request.setMd5(md5);
if (req?.isHdc) {
let ws = new ultipa_pb_1.WithServer();
ws.setHdcServerName(req.hdc);
request.setWithServer(ws);
}
let bufferSize = 1 * 1024 * 1024;
let sum = 0;
console.log("Install Algo", {
fileName,
chunkLength: chunk.length,
md5,
});
for (let index = 0; index < chunk.length; index += bufferSize) {
if (isError) {
break;
}
let sub_chunk = chunk.slice(index, index + bufferSize);
request.setChunk(sub_chunk);
sum += sub_chunk.length;
let sendAlready = async () => {
return new Promise((resolve, reject) => {
v.write(request, null, () => {
// console.log("sending ",index, sub_chunk.length);
resolve(1);
});
});
};
await sendAlready();
}
if (!isError) {
console.log("End and sending sum: ", fileName, sum);
}
}
v.end();
if (!isError) {
console.log(`${JSON.stringify(req.datas.map(o => o.fileName))} upload finished!`);
}
}
catch (error) {
reject(error);
}
});
}
}
exports.HDCExtra = HDCExtra;
//# sourceMappingURL=hdc.extra.js.map