@dbcube/core 4.1.13 → 4.1.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -934,7 +934,7 @@ var QueryEngine = class {
934
934
  this.config = this.setConfig(name);
935
935
  this.arguments = this.setArguments();
936
936
  this.timeout = timeout;
937
- this.connectionId = `${name}_${this.config.type}_${this.config.config.DATABASE}_${this.config.config.HOST || "localhost"}`;
937
+ this.connectionId = `${name}_query_engine_${this.config.type}_${this.config.config.DATABASE}_${this.config.config.HOST || "localhost"}`;
938
938
  this.tcpPort = this.generatePort();
939
939
  }
940
940
  generatePort() {
@@ -1030,30 +1030,27 @@ var QueryEngine = class {
1030
1030
  return this.config;
1031
1031
  }
1032
1032
  async run(binary, args) {
1033
- const isQueryEngine = binary === "query_engine";
1034
- const isSchemaEngine = binary === "schema_engine";
1035
1033
  const actionIndex = args.findIndex((arg) => arg === "--action");
1036
- const action = actionIndex !== -1 ? args[actionIndex + 1] : "";
1037
- const isExecuteAction = action === "execute";
1038
- if (isQueryEngine && isExecuteAction || isSchemaEngine) {
1039
- return this.executeWithTcpServer(args, binary);
1034
+ const isExecuteAction = actionIndex !== -1 && args[actionIndex + 1] === "execute";
1035
+ const isQueryEngine = binary === "query_engine";
1036
+ if (isQueryEngine && isExecuteAction) {
1037
+ return this.executeWithTcpServer(args);
1040
1038
  } else {
1041
1039
  return this.createProcess(binary, args);
1042
1040
  }
1043
1041
  }
1044
- async executeWithTcpServer(args, binary = "query_engine") {
1045
- const serverConnectionId = `${this.connectionId}_${binary}`;
1046
- const serverInfo = globalTcpServers.get(serverConnectionId);
1042
+ async executeWithTcpServer(args) {
1043
+ const serverInfo = globalTcpServers.get(this.connectionId);
1047
1044
  if (!serverInfo || !serverInfo.process || serverInfo.process.killed) {
1048
- await this.startTcpServer(binary, serverConnectionId);
1049
- await this.waitForServerReady(serverConnectionId);
1045
+ await this.startTcpServer();
1046
+ await this.waitForServerReady();
1050
1047
  }
1051
- return this.sendTcpRequestFast(args, serverConnectionId);
1048
+ return this.sendTcpRequestFast(args);
1052
1049
  }
1053
- async waitForServerReady(serverConnectionId) {
1050
+ async waitForServerReady() {
1054
1051
  const maxRetries = 10;
1055
1052
  const retryDelay = 500;
1056
- const serverInfo = globalTcpServers.get(serverConnectionId);
1053
+ const serverInfo = globalTcpServers.get(this.connectionId);
1057
1054
  if (!serverInfo) {
1058
1055
  throw new Error("Server not found");
1059
1056
  }
@@ -1097,16 +1094,15 @@ var QueryEngine = class {
1097
1094
  }
1098
1095
  return parsed;
1099
1096
  }
1100
- async startTcpServer(binary, serverConnectionId) {
1097
+ async startTcpServer() {
1101
1098
  await this.initializeBinary();
1102
1099
  if (!this.binary) {
1103
1100
  throw new Error("Binary not initialized");
1104
1101
  }
1105
- const basePort = binary === "schema_engine" ? 9900 : 9944;
1106
- const tcpPort = await this.findAvailablePort(basePort);
1102
+ this.tcpPort = await this.findAvailablePort(this.tcpPort);
1107
1103
  return new Promise((resolve5, reject) => {
1108
- const serverArgs = [...this.arguments, "--action", "server", "--tcp-port", tcpPort.toString()];
1109
- const serverProcess = (0, import_child_process2.spawn)(this.binary[binary], serverArgs);
1104
+ const serverArgs = [...this.arguments, "--action", "server", "--tcp-port", this.tcpPort.toString()];
1105
+ const serverProcess = (0, import_child_process2.spawn)(this.binary["query_engine"], serverArgs);
1110
1106
  let started = false;
1111
1107
  const timeout = setTimeout(() => {
1112
1108
  if (!started) {
@@ -1120,8 +1116,8 @@ var QueryEngine = class {
1120
1116
  if (!started) {
1121
1117
  started = true;
1122
1118
  clearTimeout(timeout);
1123
- globalTcpServers.set(serverConnectionId, {
1124
- port: tcpPort,
1119
+ globalTcpServers.set(this.connectionId, {
1120
+ port: this.tcpPort,
1125
1121
  process: serverProcess
1126
1122
  });
1127
1123
  resolve5();
@@ -1129,10 +1125,10 @@ var QueryEngine = class {
1129
1125
  }
1130
1126
  });
1131
1127
  serverProcess.stderr.on("data", (data) => {
1132
- console.error(`[${binary} TCP Server Error] ${data.toString().trim()}`);
1128
+ console.error(`[query_engine TCP Server Error] ${data.toString().trim()}`);
1133
1129
  });
1134
1130
  serverProcess.on("close", (code) => {
1135
- globalTcpServers.delete(serverConnectionId);
1131
+ globalTcpServers.delete(this.connectionId);
1136
1132
  });
1137
1133
  serverProcess.on("error", (error) => {
1138
1134
  if (!started) {
@@ -1142,94 +1138,28 @@ var QueryEngine = class {
1142
1138
  });
1143
1139
  });
1144
1140
  }
1145
- async sendTcpRequest(args) {
1141
+ async sendTcpRequestFast(args) {
1146
1142
  return new Promise((resolve5, reject) => {
1147
- const client = new net.Socket();
1148
- let responseBuffer = "";
1149
- let isResolved = false;
1150
- const timeout = setTimeout(() => {
1151
- if (!isResolved) {
1152
- isResolved = true;
1153
- client.destroy();
1154
- reject(new Error("TCP request timeout"));
1155
- }
1156
- }, this.timeout * 2);
1157
- client.connect(this.tcpPort, "127.0.0.1", () => {
1158
- const dmlIndex = args.findIndex((arg) => arg === "--dml");
1159
- const dmlJson = dmlIndex !== -1 ? args[dmlIndex + 1] : null;
1160
- if (!dmlJson) {
1161
- if (!isResolved) {
1162
- isResolved = true;
1163
- clearTimeout(timeout);
1164
- client.destroy();
1165
- reject(new Error("No DML found in arguments"));
1166
- }
1167
- return;
1168
- }
1169
- const command = {
1170
- action: "execute",
1171
- dml: dmlJson
1172
- };
1173
- client.write(JSON.stringify(command));
1174
- });
1175
- client.on("data", (data) => {
1176
- responseBuffer += data.toString();
1177
- const match = responseBuffer.match(/PROCESS_RESPONSE:(\{.*\})/);
1178
- if (match && !isResolved) {
1179
- isResolved = true;
1180
- clearTimeout(timeout);
1181
- client.destroy();
1182
- try {
1183
- const response = JSON.parse(match[1]);
1184
- resolve5({
1185
- status: response.status,
1186
- message: response.message,
1187
- data: response.data
1188
- });
1189
- } catch (error) {
1190
- reject(new Error("Failed to parse TCP response"));
1191
- }
1192
- }
1193
- });
1194
- client.on("error", (error) => {
1195
- if (!isResolved) {
1196
- isResolved = true;
1197
- clearTimeout(timeout);
1198
- reject(error);
1199
- }
1200
- });
1201
- client.on("close", () => {
1202
- if (!isResolved) {
1203
- clearTimeout(timeout);
1204
- if (!responseBuffer.includes("PROCESS_RESPONSE:")) {
1205
- reject(new Error("Connection closed without valid response"));
1206
- }
1207
- }
1208
- });
1209
- });
1210
- }
1211
- async sendTcpRequestFast(args, serverConnectionId) {
1212
- return new Promise((resolve5, reject) => {
1213
- let queue = connectionQueues.get(serverConnectionId);
1143
+ let queue = connectionQueues.get(this.connectionId);
1214
1144
  if (!queue) {
1215
1145
  queue = [];
1216
- connectionQueues.set(serverConnectionId, queue);
1146
+ connectionQueues.set(this.connectionId, queue);
1217
1147
  }
1218
1148
  queue.push({ args, resolve: resolve5, reject });
1219
- this.processQueue(serverConnectionId);
1149
+ this.processQueue();
1220
1150
  });
1221
1151
  }
1222
- async processQueue(serverConnectionId) {
1223
- if (connectionProcessing.get(serverConnectionId)) {
1152
+ async processQueue() {
1153
+ if (connectionProcessing.get(this.connectionId)) {
1224
1154
  return;
1225
1155
  }
1226
- const queue = connectionQueues.get(serverConnectionId);
1156
+ const queue = connectionQueues.get(this.connectionId);
1227
1157
  if (!queue || queue.length === 0) {
1228
1158
  return;
1229
1159
  }
1230
- connectionProcessing.set(serverConnectionId, true);
1160
+ connectionProcessing.set(this.connectionId, true);
1231
1161
  try {
1232
- const serverInfo = globalTcpServers.get(serverConnectionId);
1162
+ const serverInfo = globalTcpServers.get(this.connectionId);
1233
1163
  if (!serverInfo) {
1234
1164
  throw new Error("Server not initialized");
1235
1165
  }
@@ -1237,20 +1167,20 @@ var QueryEngine = class {
1237
1167
  const request = queue.shift();
1238
1168
  if (!request) break;
1239
1169
  try {
1240
- let connection = globalTcpConnections.get(serverConnectionId);
1170
+ let connection = globalTcpConnections.get(this.connectionId);
1241
1171
  if (!connection || connection.destroyed || connection.readyState !== "open") {
1242
1172
  connection = await this.createNewConnection(serverInfo.port);
1243
- globalTcpConnections.set(serverConnectionId, connection);
1173
+ globalTcpConnections.set(this.connectionId, connection);
1244
1174
  }
1245
1175
  const result = await this.executeOnConnection(connection, request.args);
1246
1176
  request.resolve(result);
1247
1177
  } catch (error) {
1248
- globalTcpConnections.delete(serverConnectionId);
1178
+ globalTcpConnections.delete(this.connectionId);
1249
1179
  request.reject(error);
1250
1180
  }
1251
1181
  }
1252
1182
  } finally {
1253
- connectionProcessing.set(serverConnectionId, false);
1183
+ connectionProcessing.set(this.connectionId, false);
1254
1184
  }
1255
1185
  }
1256
1186
  async createNewConnection(port) {
@@ -1326,161 +1256,6 @@ var QueryEngine = class {
1326
1256
  connection.write(JSON.stringify(command));
1327
1257
  });
1328
1258
  }
1329
- async sendOnExistingConnection(connection, args) {
1330
- return new Promise((resolve5, reject) => {
1331
- const dmlIndex = args.findIndex((arg) => arg === "--dml");
1332
- const dmlJson = dmlIndex !== -1 ? args[dmlIndex + 1] : null;
1333
- if (!dmlJson) {
1334
- reject(new Error("No DML found in arguments"));
1335
- return;
1336
- }
1337
- let responseBuffer = "";
1338
- let isResolved = false;
1339
- const timeout = setTimeout(() => {
1340
- if (!isResolved) {
1341
- isResolved = true;
1342
- connection.off("data", onData);
1343
- connection.off("error", onError);
1344
- globalTcpConnections.delete(this.connectionId);
1345
- reject(new Error("TCP request timeout"));
1346
- }
1347
- }, this.timeout);
1348
- const onData = (data) => {
1349
- responseBuffer += data.toString();
1350
- const match = responseBuffer.match(/PROCESS_RESPONSE:(\{.*\})/);
1351
- if (match && !isResolved) {
1352
- isResolved = true;
1353
- clearTimeout(timeout);
1354
- connection.off("data", onData);
1355
- connection.off("error", onError);
1356
- try {
1357
- const response = JSON.parse(match[1]);
1358
- resolve5({
1359
- status: response.status,
1360
- message: response.message,
1361
- data: response.data
1362
- });
1363
- } catch (error) {
1364
- reject(new Error("Failed to parse TCP response"));
1365
- }
1366
- }
1367
- };
1368
- const onError = (error) => {
1369
- if (!isResolved) {
1370
- isResolved = true;
1371
- clearTimeout(timeout);
1372
- connection.off("data", onData);
1373
- connection.off("error", onError);
1374
- reject(error);
1375
- }
1376
- };
1377
- connection.on("data", onData);
1378
- connection.on("error", onError);
1379
- const command = {
1380
- action: "execute",
1381
- dml: dmlJson
1382
- };
1383
- connection.write(JSON.stringify(command));
1384
- });
1385
- }
1386
- async createPersistentConnection(args) {
1387
- return new Promise((resolve5, reject) => {
1388
- let client = globalTcpConnections.get(this.connectionId);
1389
- let isNewConnection = false;
1390
- if (!client || client.destroyed) {
1391
- client = new net.Socket();
1392
- isNewConnection = true;
1393
- client.setNoDelay(true);
1394
- client.setKeepAlive(true, 6e4);
1395
- client.setMaxListeners(20);
1396
- }
1397
- let responseBuffer = "";
1398
- let isResolved = false;
1399
- const timeout = setTimeout(() => {
1400
- if (!isResolved) {
1401
- isResolved = true;
1402
- client.removeListener("data", dataHandler);
1403
- globalTcpConnections.delete(this.connectionId);
1404
- reject(new Error("TCP connection timeout"));
1405
- }
1406
- }, this.timeout);
1407
- const executeQuery = () => {
1408
- const dmlIndex = args.findIndex((arg) => arg === "--dml");
1409
- const dmlJson = dmlIndex !== -1 ? args[dmlIndex + 1] : null;
1410
- if (!dmlJson) {
1411
- clearTimeout(timeout);
1412
- if (!isResolved) {
1413
- isResolved = true;
1414
- reject(new Error("No DML found in arguments"));
1415
- }
1416
- return;
1417
- }
1418
- const command = {
1419
- action: "execute",
1420
- dml: dmlJson
1421
- };
1422
- const commandStr = JSON.stringify(command);
1423
- const canWrite = client.write(commandStr);
1424
- if (!canWrite) {
1425
- client.once("drain", () => {
1426
- });
1427
- }
1428
- };
1429
- const dataHandler = (data) => {
1430
- responseBuffer += data.toString();
1431
- const match = responseBuffer.match(/PROCESS_RESPONSE:(\{.*\})/);
1432
- if (match && !isResolved) {
1433
- isResolved = true;
1434
- clearTimeout(timeout);
1435
- try {
1436
- const response = JSON.parse(match[1]);
1437
- responseBuffer = "";
1438
- client.removeListener("data", dataHandler);
1439
- resolve5({
1440
- status: response.status,
1441
- message: response.message,
1442
- data: response.data
1443
- });
1444
- } catch (error) {
1445
- client.removeListener("data", dataHandler);
1446
- reject(new Error("Failed to parse TCP response"));
1447
- }
1448
- }
1449
- };
1450
- const errorHandler = (error) => {
1451
- if (!isResolved) {
1452
- isResolved = true;
1453
- clearTimeout(timeout);
1454
- globalTcpConnections.delete(this.connectionId);
1455
- client.removeListener("data", dataHandler);
1456
- reject(error);
1457
- }
1458
- };
1459
- const closeHandler = () => {
1460
- globalTcpConnections.delete(this.connectionId);
1461
- if (!isResolved) {
1462
- isResolved = true;
1463
- clearTimeout(timeout);
1464
- client.removeListener("data", dataHandler);
1465
- reject(new Error("Connection closed unexpectedly"));
1466
- }
1467
- };
1468
- if (isNewConnection) {
1469
- client.connect(this.tcpPort, "127.0.0.1", () => {
1470
- clearTimeout(timeout);
1471
- globalTcpConnections.set(this.connectionId, client);
1472
- client.on("error", errorHandler);
1473
- client.on("close", closeHandler);
1474
- client.on("data", dataHandler);
1475
- executeQuery();
1476
- });
1477
- } else {
1478
- clearTimeout(timeout);
1479
- client.on("data", dataHandler);
1480
- executeQuery();
1481
- }
1482
- });
1483
- }
1484
1259
  async createProcess(binary, args) {
1485
1260
  await this.initializeBinary();
1486
1261
  if (!this.binary) {