@ultipa-graph/ultipa-node-sdk
Version:
NodeJS SDK for ultipa-server 4.0
785 lines • 39.8 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConnectionFinal = void 0;
const connection_1 = require("./extra/connection");
const types_1 = require("../types");
const utils_1 = require("../utils");
const utils_2 = require("../utils");
const ultipa_pb_1 = require("../proto/ultipa_pb");
const fs = __importStar(require("fs-extra"));
const crypto_1 = __importDefault(require("crypto"));
const path_1 = __importDefault(require("path"));
const lodash_1 = __importDefault(require("lodash"));
const connection_base_1 = require("./extra/connection.base");
const { CommandList, SchemaStringWithDefault } = utils_1.UQLMAKER;
// 做一些扩展性的接口
class ConnectionFinal extends connection_1.Connection {
setGraph(graphName) {
this.defaultConfig.graphSetName = graphName;
}
clusterTest(commonReq, timeoutSeconds = 0) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
var _a, _b;
if (timeoutSeconds > 0) {
setTimeout(() => {
resolve([]);
}, timeoutSeconds * 1000);
}
yield this.refreshRaftLeader(null, commonReq);
let res = yield this.getRaftLeader(commonReq);
let raftPeers = ((_b = (_a = res.status) === null || _a === void 0 ? void 0 : _a.clusterInfo) === null || _b === void 0 ? void 0 : _b.raftPeers) || [];
resolve(raftPeers);
}));
});
}
/**
* 集群信息
*/
clusterInfo(commonReq) {
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
let _TIME_MAP = {
main: "Cluster info",
refreshRaft: "Refresh raft",
raftLeader: "Get raft leader",
stat: (host) => {
return `Get Stat[${host}]`;
}
};
let timeRecordManager = new utils_1.TimeRecordManager(_TIME_MAP.main);
timeRecordManager.start(_TIME_MAP.refreshRaft);
yield this.refreshRaftLeader(null, commonReq);
timeRecordManager.stop(_TIME_MAP.refreshRaft);
timeRecordManager.start(_TIME_MAP.raftLeader);
let res = yield this.getRaftLeader(commonReq);
timeRecordManager.stop(_TIME_MAP.raftLeader);
let raftPeers = ((_b = (_a = res.status) === null || _a === void 0 ? void 0 : _a.clusterInfo) === null || _b === void 0 ? void 0 : _b.raftPeers) || [];
let result = [];
for (const peer of raftPeers) {
let info = Object.assign({}, peer);
if (peer.status) {
timeRecordManager.start(_TIME_MAP.stat(peer.host));
let res = yield this.stats({
useHost: peer.host
});
timeRecordManager.stop(_TIME_MAP.stat(peer.host));
if (res.status.code == types_1.ULTIPA.Code.SUCCESS) {
info = Object.assign(Object.assign({}, info), res.data);
}
}
result.push(info);
}
timeRecordManager.stopTotal();
return {
status: {
code: types_1.ULTIPA.Code.SUCCESS,
code_desc: types_1.ULTIPA.Code[types_1.ULTIPA.Code.SUCCESS],
},
data: {
raft: ((_c = res.status) === null || _c === void 0 ? void 0 : _c.code) != types_1.ULTIPA.Code.NOT_RAFT_MODE,
infos: result,
},
req: {
time_record: this.defaultConfig.responseWithRequestInfo ? timeRecordManager.toString() : null
}
};
});
}
/**
* 拷贝数据集
*/
copyGraph(req, commonReq) {
return __awaiter(this, void 0, void 0, function* () {
let success = types_1.ULTIPA.Code.SUCCESS;
let resCreate = yield this.createGraph({ name: req.newGraphSetName }, commonReq);
if (resCreate.status.code !== success) {
return resCreate;
}
yield (0, utils_2.sleepSeconds)(10); // 新创建的图集集群需要同步
// let resListSchema = await this.showSchema({ graphSetName: req.fromGraphSetName });
let resNodeListSchema = yield this.showNodeSchema({ graphSetName: req.fromGraphSetName });
let resEdgeListSchema = yield this.showEdgeSchema({ graphSetName: req.fromGraphSetName });
let copyCommonReq = {
graphSetName: req.newGraphSetName
};
let resListSchema = [{
listSchema: resNodeListSchema,
dbType: types_1.ULTIPA.DBType.DBNODE
}, {
listSchema: resEdgeListSchema,
dbType: types_1.ULTIPA.DBType.DBEDGE
}];
const createSchema = (listSchema, dbType) => __awaiter(this, void 0, void 0, function* () {
for (let schemaObj of listSchema === null || listSchema === void 0 ? void 0 : listSchema.data) {
if (schemaObj.name != "default") {
let res = yield this.createSchema({
name: schemaObj.name,
dbType,
desc: schemaObj.description
}, false, copyCommonReq);
if (res.status.code !== success) {
return res;
}
}
for (let p of schemaObj.properties) {
if (p.name.startsWith('#'))
continue;
let typeCfg = utils_1.PropertyUtils.GetPropertyType(p.type);
let res = yield this.createProperty(dbType, p.name, Object.assign(Object.assign({}, p), typeCfg), copyCommonReq);
if (res.status.code !== success) {
return res;
}
}
// }
}
});
resListSchema.forEach((list) => { createSchema(list.listSchema, list.dbType); });
return resCreate;
});
}
/** 卸载算法 */
uninstallAlgo(algoName, config) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
try {
let clientInfo = yield this.getClientInfo({
graphSetName: config === null || config === void 0 ? void 0 : config.graphSetName,
clusterId: config === null || config === void 0 ? void 0 : config.clusterId,
type: types_1.ULTIPA.ClientType.Update,
isGlobal: true,
useMaster: config === null || config === void 0 ? void 0 : config.useMaster,
timeZone: config === null || config === void 0 ? void 0 : config.timeZone,
timeZoneOffset: config === null || config === void 0 ? void 0 : config.timeZoneOffset,
});
let request = new ultipa_pb_1.UninstallAlgoRequest();
request.setAlgoName(algoName);
clientInfo.client.controlsClient.uninstallAlgo(request, clientInfo.metadata, (err, reply) => {
let response = new types_1.ULTIPA.Response();
if (err) {
response = utils_1.FormatResponse.unknownError(err, response);
resolve(response);
return;
}
response = {
status: utils_1.FormatType.status(reply.getStatus()),
data: true
};
resolve(response);
});
}
catch (error) {
reject(error);
}
}));
});
}
/** 卸载算法 */
uninstallExta(extaName, requestConfig) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
try {
let clientInfo = yield this.getClientInfo({
graphSetName: requestConfig === null || requestConfig === void 0 ? void 0 : requestConfig.graphSetName,
clusterId: requestConfig === null || requestConfig === void 0 ? void 0 : requestConfig.clusterId,
type: types_1.ULTIPA.ClientType.Update,
isGlobal: true,
useMaster: requestConfig === null || requestConfig === void 0 ? void 0 : requestConfig.useMaster,
timeZone: requestConfig === null || requestConfig === void 0 ? void 0 : requestConfig.timeZone,
timeZoneOffset: requestConfig === null || requestConfig === void 0 ? void 0 : requestConfig.timeZoneOffset,
});
let request = new ultipa_pb_1.UninstallExtaRequest();
request.setExtaName(extaName);
clientInfo.client.controlsClient.uninstallExta(request, clientInfo.metadata, (err, reply) => {
let response = new types_1.ULTIPA.Response();
if (err) {
response = utils_1.FormatResponse.unknownError(err, response);
resolve(response);
return;
}
response = {
status: utils_1.FormatType.status(reply.getStatus()),
data: true
};
resolve(response);
});
}
catch (error) {
reject(error);
}
}));
});
}
/** 安装算法 */
// async installAlgo(req: RequestType.InstallAlgo, commonReq?: RequestType.RequestConfig) {
installAlgo(soFilePath, infoFilePath, config) {
return __awaiter(this, void 0, void 0, function* () {
return this.installAlgoAio({
paths: {
algoFilePath: soFilePath,
infoFilePath,
},
isExta: false,
}, config);
});
}
/** 安装算法 */
// async installExta(req: RequestType.InstallAlgo, commonReq?: RequestType.RequestConfig) {
installExta(soFilePath, infoFilePath, config) {
return __awaiter(this, void 0, void 0, function* () {
return this.installAlgoAio({
paths: {
algoFilePath: soFilePath,
infoFilePath: infoFilePath,
},
isExta: true,
}, config);
});
}
/** 安装算法 */
installAlgoAio(req, commonReq) {
return __awaiter(this, void 0, void 0, function* () {
if (req.datas) {
return this._installAlgo(Object.assign(Object.assign({}, req.datas), { isExta: req === null || req === void 0 ? void 0 : req.isExta }), commonReq);
}
let filePaths = [req.paths.algoFilePath, req.paths.infoFilePath];
let _req = {
datas: [],
isExta: req.isExta
};
for (let i = 0; i < filePaths.length; i++) {
const filePath = filePaths[i];
let chunk = fs.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, commonReq);
});
}
_installAlgo(req, commonReq) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
try {
let clientInfo = yield this.getClientInfo({
graphSetName: commonReq === null || commonReq === void 0 ? void 0 : commonReq.graphSetName,
clusterId: commonReq === null || commonReq === void 0 ? void 0 : commonReq.clusterId,
type: types_1.ULTIPA.ClientType.Update,
isGlobal: true,
useMaster: commonReq === null || commonReq === void 0 ? void 0 : commonReq.useMaster,
timeZone: commonReq === null || commonReq === void 0 ? void 0 : commonReq.timeZone,
timeZoneOffset: commonReq === null || commonReq === void 0 ? void 0 : commonReq.timeZoneOffset,
});
let isError = null;
let callback = (err, reply) => {
let response = new types_1.ULTIPA.Response();
if (err) {
isError = err;
response = utils_1.FormatResponse.unknownError(err, response);
resolve(response);
return;
}
response = {
status: utils_1.FormatType.status(reply.getStatus()),
data: true
};
resolve(response);
};
let v = (req === null || req === void 0 ? void 0 : req.isExta) ? clientInfo.client.controlsClient.installExta(clientInfo.metadata, callback) : 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 = (req === null || req === void 0 ? void 0 : req.isExta) ? new ultipa_pb_1.InstallExtaRequest() : new ultipa_pb_1.InstallAlgoRequest();
request.setFileName(fileName);
request.setMd5(md5);
let bufferSize = 1 * 1024 * 1024;
let sum = 0;
console.log(`install ${(req === null || req === void 0 ? void 0 : req.isExta) ? "Exta" : "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 = () => __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
v.write(request, null, () => {
// console.log("sending ",index, sub_chunk.length);
resolve(1);
});
});
});
yield 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);
}
}));
});
}
insertNodesBatchBySchema(schema, rows, config) {
return __awaiter(this, void 0, void 0, function* () {
let nodeTable = utils_1.FormatType.toNodeTableWithSchema(schema, rows, this.getTimeZoneType(config));
return this.insertNodesBatch(nodeTable, config);
});
}
insertNodesBatchAuto(rows, config) {
var _a, _b, _c, _d, _e;
return __awaiter(this, void 0, void 0, function* () {
let lastResponse = new types_1.ULTIPA.Response();
lastResponse.status = utils_1.FormatResponse.successStatus();
lastResponse.statistics = {
engineCost: 0,
totalCost: 0,
edgeAffected: 0,
nodeAffected: 0,
};
lastResponse.data = {
ids: [],
uuids: [],
ignore_indexes: [],
ignore_error_codes: [],
};
if (!rows || rows.length == 0) {
return lastResponse;
}
let schemas = yield this.showNodeSchema(config);
let schemaMap = {};
(_a = schemas.data) === null || _a === void 0 ? void 0 : _a.forEach(s => {
schemaMap[s.name] = s;
});
let batchMap = {};
rows.forEach((r, index) => {
r.set("_index", index);
if (!schemaMap[r.schema]) {
throw new Error(`Row[${index}] Node Schema not found:[${r.schema}]`);
}
if (!batchMap[r.schema]) {
batchMap[r.schema] = [];
}
batchMap[r.schema].push(r);
});
let schemaNames = Object.keys(batchMap);
let mergeResult = lastResponse.data;
let msgs = [];
for (let index = 0; index < schemaNames.length; index++) {
const schemaName = schemaNames[index];
let s = schemaMap[schemaName];
let newS = {
name: s.name,
properties: s.properties.map(o => {
let typeObj = utils_1.PropertyUtils.GetPropertyType(o.type);
return {
name: o.name,
type: typeObj.type,
subTypes: typeObj.subTypes,
};
}),
};
let insertRows = batchMap[schemaName];
let nodeTable = utils_1.FormatType.toNodeTableWithSchema(newS, insertRows, this.getTimeZoneType(config));
let res = yield this.insertNodesBatch(nodeTable, config);
if (res.status.code != types_1.ULTIPA.Code.SUCCESS) {
let code = res.status.code;
lastResponse.status.code = code;
lastResponse.status.code_desc = types_1.ULTIPA.Code[code];
msgs.push(`${schemaName}: ${res.status.message}`);
}
var ignoreIndexes = [];
(_b = res === null || res === void 0 ? void 0 : res.data) === null || _b === void 0 ? void 0 : _b.ignore_indexes.forEach(i => {
ignoreIndexes.push(insertRows[i].get("_index"));
});
mergeResult.ids.push(...(((_c = res === null || res === void 0 ? void 0 : res.data) === null || _c === void 0 ? void 0 : _c.ids) || []));
mergeResult.uuids.push(...(((_d = res === null || res === void 0 ? void 0 : res.data) === null || _d === void 0 ? void 0 : _d.uuids) || []));
mergeResult.ignore_indexes.push(...ignoreIndexes);
mergeResult.ignore_error_codes.push(...(((_e = res === null || res === void 0 ? void 0 : res.data) === null || _e === void 0 ? void 0 : _e.ignore_error_codes) || []));
lastResponse.statistics = utils_1.FormatResponse.statisticsSum(lastResponse === null || lastResponse === void 0 ? void 0 : lastResponse.statistics, res === null || res === void 0 ? void 0 : res.statistics);
}
if (msgs.length > 0) {
lastResponse.status.message = msgs.join(";");
}
return lastResponse;
});
}
insertNodesBatch(table, config) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
try {
let request = new ultipa_pb_1.InsertNodesRequest();
let clientInfo = yield this.getClientInfo({
graphSetName: config === null || config === void 0 ? void 0 : config.graphSetName,
clusterId: config === null || config === void 0 ? void 0 : config.clusterId,
type: types_1.ULTIPA.ClientType.Update,
useMaster: config === null || config === void 0 ? void 0 : config.useMaster,
timeZone: config === null || config === void 0 ? void 0 : config.timeZone,
timeZoneOffset: config === null || config === void 0 ? void 0 : config.timeZoneOffset,
});
request.setSilent((config === null || config === void 0 ? void 0 : config.silent) ? true : false);
request.setNodeTable(table);
request.setGraphName(clientInfo.goGraphName);
request.setInsertType(config === null || config === void 0 ? void 0 : config.insertType);
clientInfo.client.rpcsClient.insertNodes(request, clientInfo.metadata, (err, reply) => __awaiter(this, void 0, void 0, function* () {
let response = new types_1.ULTIPA.Response();
if (err) {
response = utils_1.FormatResponse.unknownError(err, response);
resolve(response);
return;
}
let ignore_indexes = reply.getIgnoreIndexesList();
let ignore_error_codes = reply.getIgnoreErrorCodeList();
if (reply.getStatus().getErrorCode() != ultipa_pb_1.ErrorCode.SUCCESS) {
let error_length = table.getEntityRowsList().length;
ignore_error_codes = new Array(error_length).fill(1);
ignore_indexes = [];
for (let index = 0; index < error_length; index++) {
ignore_indexes.push(index);
}
}
response = {
status: utils_1.FormatType.status(reply.getStatus()),
statistics: {
engineCost: reply.getEngineTimeCost(),
totalCost: reply.getTimeCost(),
},
data: {
ids: reply.getIdsList(),
uuids: reply.getUuidsList(),
ignore_indexes,
ignore_error_codes,
}
};
let { canRetry, nextRetry } = yield utils_1.retryHelper.check(this, response, config, clientInfo.goGraphName);
if (canRetry) {
let res = yield this.insertNodesBatch(table, Object.assign(Object.assign({}, config), { retry: nextRetry }));
resolve(res);
return;
}
resolve(response);
}));
}
catch (error) {
reject(error);
}
}));
});
}
/**
* 插入大量的点
*/
insertHugeNodes(req, config) {
return __awaiter(this, void 0, void 0, function* () {
let res_properties = yield this.showNodeProperty(req.schemaName, config);
let propertyTypeObject = utils_1.FormatType.toPropertyTypeObject(res_properties.data);
let nodeTable = utils_1.FormatType.toNodeTable(propertyTypeObject, req.schemaName, req.headers, req.rows, this.getTimeZoneType(config));
return this.insertNodesBatch(nodeTable, config);
});
}
insertEdgesBatch(table, config) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
try {
let request = new ultipa_pb_1.InsertEdgesRequest();
let clientInfo = yield this.getClientInfo({
graphSetName: config === null || config === void 0 ? void 0 : config.graphSetName,
clusterId: config === null || config === void 0 ? void 0 : config.clusterId,
type: types_1.ULTIPA.ClientType.Update,
useMaster: config === null || config === void 0 ? void 0 : config.useMaster,
timeZone: config === null || config === void 0 ? void 0 : config.timeZone,
timeZoneOffset: config === null || config === void 0 ? void 0 : config.timeZoneOffset,
});
request.setSilent((config === null || config === void 0 ? void 0 : config.silent) ? true : false);
request.setEdgeTable(table);
request.setCreateNodeIfNotExist(config.createNodeIfNotExist ? true : false);
request.setGraphName(clientInfo.goGraphName);
request.setInsertType(config.insertType);
clientInfo.client.rpcsClient.insertEdges(request, clientInfo.metadata, (err, reply) => __awaiter(this, void 0, void 0, function* () {
let response = new types_1.ULTIPA.Response();
if (err) {
response = utils_1.FormatResponse.unknownError(err, response);
resolve(response);
return;
}
let ignore_indexes = reply.getIgnoreIndexesList();
let ignore_error_codes = reply.getIgnoreErrorCodeList();
if (reply.getStatus().getErrorCode() != ultipa_pb_1.ErrorCode.SUCCESS) {
let error_length = table.getEntityRowsList().length;
ignore_error_codes = new Array(error_length).fill(1);
ignore_indexes = [];
for (let index = 0; index < error_length; index++) {
ignore_indexes.push(index);
}
}
response = {
status: utils_1.FormatType.status(reply.getStatus()),
statistics: {
engineCost: reply.getEngineTimeCost(),
totalCost: reply.getTimeCost(),
},
data: {
uuids: reply.getUuidsList(),
ignore_indexes,
ignore_error_codes,
}
};
// console.log(response.data)
let { canRetry, nextRetry } = yield utils_1.retryHelper.check(this, response, config, clientInfo.goGraphName);
if (canRetry) {
let res = yield this.insertEdgesBatch(table, Object.assign(Object.assign({}, config), { retry: nextRetry }));
resolve(res);
return;
}
resolve(response);
}));
}
catch (error) {
reject(error);
}
}));
});
}
validEdgeRows(rows) {
let validResult = {
ok: true
};
let setFailed = (index, msg) => {
validResult.ok = false;
validResult.msg = msg;
validResult.failedIndex = index;
};
for (let index = 0; index < rows.length; index++) {
const row = rows[index];
if (!row.to && !row.from && !row.from_uuid && !row.to_uuid) {
let msg = `_from/_from_uuid and _to/_to_uuid are null`;
setFailed(index, msg);
break;
}
if (row.from && !row.to) {
let msg = `_from has value [${row.from}] but _to got ${row.to}`;
setFailed(index, msg);
break;
}
if (row.to && !row.from) {
let msg = `_to has value [${row.to}] but _from got ${row.from}`;
setFailed(index, msg);
break;
}
if (row.from_uuid && !row.to_uuid) {
let msg = `_from_uuid has value [${row.from_uuid}] but _to_uuid got ${row.to_uuid}`;
setFailed(index, msg);
break;
}
if (row.to_uuid && !row.from_uuid) {
let msg = `_to_uuid has value [${row.to_uuid}] but _from_uuid got ${row.from_uuid}`;
setFailed(index, msg);
break;
}
}
return validResult;
}
insertEdgesBatchBySchema(schema, rows, config) {
return __awaiter(this, void 0, void 0, function* () {
let valid = this.validEdgeRows(rows);
if (!valid.ok) {
throw new Error(`row[${valid.failedIndex}] error: ${valid.msg}`);
}
let table = utils_1.FormatType.toEdgeTableWithSchema(schema, rows, this.getTimeZoneType(config));
return this.insertEdgesBatch(table, config);
});
}
insertEdgesBatchAuto(rows, config) {
var _a, _b, _c, _d;
return __awaiter(this, void 0, void 0, function* () {
let valid = this.validEdgeRows(rows);
if (!valid.ok) {
throw new Error(`row[${valid.failedIndex}] error: ${valid.msg}`);
}
let lastResponse = new types_1.ULTIPA.Response();
lastResponse.status = utils_1.FormatResponse.successStatus();
lastResponse.statistics = { engineCost: 0, totalCost: 0 };
lastResponse.data = {
uuids: [],
ignore_indexes: [],
ignore_error_codes: [],
};
if (!rows || rows.length == 0) {
return lastResponse;
}
let schemas = yield this.showEdgeSchema(config);
let schemaMap = {};
(_a = schemas.data) === null || _a === void 0 ? void 0 : _a.forEach(s => {
schemaMap[s.name] = s;
});
let batchMap = {};
rows.forEach((r, index) => {
r.set("_index", index);
if (!schemaMap[r.schema]) {
throw new Error(`Row[${index}] Edge Schema not found:[${r.schema}]`);
}
if (!batchMap[r.schema]) {
batchMap[r.schema] = [];
}
batchMap[r.schema].push(r);
});
let schemaNames = Object.keys(batchMap);
let mergeResult = lastResponse.data;
let msgs = [];
for (let index = 0; index < schemaNames.length; index++) {
const schemaName = schemaNames[index];
let s = schemaMap[schemaName];
let newS = {
name: s.name,
properties: s.properties.map(o => {
let typeObj = utils_1.PropertyUtils.GetPropertyType(o.type);
return {
name: o.name,
type: typeObj.type,
subTypes: typeObj.subTypes,
};
}),
};
let insertRows = batchMap[schemaName];
let table = utils_1.FormatType.toEdgeTableWithSchema(newS, insertRows, this.getTimeZoneType(config));
let res = yield this.insertEdgesBatch(table, config);
if (res.status.code != types_1.ULTIPA.Code.SUCCESS) {
let code = res.status.code;
lastResponse.status.code = code;
lastResponse.status.code_desc = types_1.ULTIPA.Code[code];
msgs.push(`${schemaName}: ${res.status.message}`);
}
var ignoreIndexes = [];
(_b = res === null || res === void 0 ? void 0 : res.data) === null || _b === void 0 ? void 0 : _b.ignore_indexes.forEach(i => {
ignoreIndexes.push(insertRows[i].get("_index"));
});
mergeResult.uuids.push(...(((_c = res === null || res === void 0 ? void 0 : res.data) === null || _c === void 0 ? void 0 : _c.uuids) || []));
mergeResult.ignore_indexes.push(...ignoreIndexes);
mergeResult.ignore_error_codes.push(...(((_d = res === null || res === void 0 ? void 0 : res.data) === null || _d === void 0 ? void 0 : _d.ignore_error_codes) || []));
lastResponse.statistics = utils_1.FormatResponse.statisticsSum(lastResponse === null || lastResponse === void 0 ? void 0 : lastResponse.statistics, res === null || res === void 0 ? void 0 : res.statistics);
}
if (msgs.length > 0) {
lastResponse.status.message = msgs.join(";");
}
return lastResponse;
});
}
/**
* 插入大量的边
*/
insertHugeEdges(req, config) {
return __awaiter(this, void 0, void 0, function* () {
let res_properties = yield this.showEdgeProperty(req.schemaName, config);
let propertyTypeObject = utils_1.FormatType.toPropertyTypeObject(res_properties.data);
let table = utils_1.FormatType.toEdgeTable(propertyTypeObject, req.schemaName, req.headers, req.rows, this.getTimeZoneType(config));
return this.insertEdgesBatch(table, config);
});
}
/**
* 下载所有算法文件
* @param req
* @param commonReq
*/
// async downloadAllAlgoResultFile(req:RequestType.DownloadAll, requestConfig: RequestType.CommonStreamRequire) {
downloadAllAlgoResultFile(taskId, cb, requestConfig) {
return __awaiter(this, void 0, void 0, function* () {
let commonReqConfig = lodash_1.default.cloneDeep(requestConfig);
delete commonReqConfig.stream;
let res = yield this.showTask(taskId, null, commonReqConfig);
let taskList = res.data;
let filenames = '';
taskList.forEach((task) => {
if (task.task_info.task_id === taskId) {
filenames += task.result.result_files;
}
});
let filenamesGroup = filenames.split(",");
let result = [];
filenamesGroup.forEach((filename) => {
let cbnew = (chunk) => { cb(chunk, filename); };
result.push(this.downloadAlgoResultFile(filename, taskId, cbnew, requestConfig));
});
return result;
// if (filenamesGroup?.length > 1) {
// throw new Error(`Task id [${taskId}] has multiple files, files: ${filenames}, please select one of them`)
// } else if (filenamesGroup.length === 1) {
// }
// if (filenamesGroup?.length > 1 && !req.fileName) {
// else if (req.fileName) {
// return this.downloadAlgoResultFile(filenames, taskId, (chunkData) => {}, requestConfig);
// }
});
}
/** 创建点或者边的schema */
createSchema(schema, isCreateProperties = false, requestConfig) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
let command = schema.dbType === types_1.ULTIPA.DBType.DBNODE
? CommandList.createNodeSchema
: CommandList.createEdgeSchema;
let commandParams = [utils_1.UQLMAKER.ForceString(schema.name), schema.desc || ""];
let uqlMaker = new utils_1.UQLMAKER(command, requestConfig, commandParams);
let response = connection_base_1.ConnectionBase.UqlUpdateSimple(this, uqlMaker);
if (isCreateProperties) {
(_a = schema.properties) === null || _a === void 0 ? void 0 : _a.forEach(prop => {
this.createProperty(schema.dbType, schema.name, Object.assign(Object.assign({}, prop), { schema: schema.name }), requestConfig);
});
}
return response;
});
}
}
exports.ConnectionFinal = ConnectionFinal;
//# sourceMappingURL=connection.final.js.map