@dbcube/core 4.1.11 → 4.1.13

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.d.mts CHANGED
@@ -49,6 +49,9 @@ declare class QueryEngine {
49
49
  private startTcpServer;
50
50
  private sendTcpRequest;
51
51
  private sendTcpRequestFast;
52
+ private processQueue;
53
+ private createNewConnection;
54
+ private executeOnConnection;
52
55
  private sendOnExistingConnection;
53
56
  private createPersistentConnection;
54
57
  private createProcess;
package/dist/index.d.ts CHANGED
@@ -49,6 +49,9 @@ declare class QueryEngine {
49
49
  private startTcpServer;
50
50
  private sendTcpRequest;
51
51
  private sendTcpRequestFast;
52
+ private processQueue;
53
+ private createNewConnection;
54
+ private executeOnConnection;
52
55
  private sendOnExistingConnection;
53
56
  private createPersistentConnection;
54
57
  private createProcess;
package/dist/index.js CHANGED
@@ -869,6 +869,8 @@ import * as net from "net";
869
869
  import { spawn as spawn2 } from "child_process";
870
870
  var globalTcpServers = /* @__PURE__ */ new Map();
871
871
  var globalTcpConnections = /* @__PURE__ */ new Map();
872
+ var connectionQueues = /* @__PURE__ */ new Map();
873
+ var connectionProcessing = /* @__PURE__ */ new Map();
872
874
  var queryCache = /* @__PURE__ */ new Map();
873
875
  var cacheSize = 0;
874
876
  var MAX_CACHE_SIZE = 500;
@@ -981,41 +983,49 @@ var QueryEngine = class {
981
983
  return this.config;
982
984
  }
983
985
  async run(binary, args) {
986
+ const isQueryEngine = binary === "query_engine";
987
+ const isSchemaEngine = binary === "schema_engine";
984
988
  const actionIndex = args.findIndex((arg) => arg === "--action");
985
- const isExecuteAction = actionIndex !== -1 && args[actionIndex + 1] === "execute";
986
- if (isExecuteAction) {
987
- return this.executeWithTcpServer(args);
989
+ const action = actionIndex !== -1 ? args[actionIndex + 1] : "";
990
+ const isExecuteAction = action === "execute";
991
+ if (isQueryEngine && isExecuteAction || isSchemaEngine) {
992
+ return this.executeWithTcpServer(args, binary);
988
993
  } else {
989
994
  return this.createProcess(binary, args);
990
995
  }
991
996
  }
992
- async executeWithTcpServer(args) {
993
- const serverInfo = globalTcpServers.get(this.connectionId);
997
+ async executeWithTcpServer(args, binary = "query_engine") {
998
+ const serverConnectionId = `${this.connectionId}_${binary}`;
999
+ const serverInfo = globalTcpServers.get(serverConnectionId);
994
1000
  if (!serverInfo || !serverInfo.process || serverInfo.process.killed) {
995
- await this.startTcpServer();
996
- await this.waitForServerReady();
1001
+ await this.startTcpServer(binary, serverConnectionId);
1002
+ await this.waitForServerReady(serverConnectionId);
997
1003
  }
998
- return this.sendTcpRequestFast(args);
1004
+ return this.sendTcpRequestFast(args, serverConnectionId);
999
1005
  }
1000
- async waitForServerReady() {
1006
+ async waitForServerReady(serverConnectionId) {
1001
1007
  const maxRetries = 10;
1002
1008
  const retryDelay = 500;
1009
+ const serverInfo = globalTcpServers.get(serverConnectionId);
1010
+ if (!serverInfo) {
1011
+ throw new Error("Server not found");
1012
+ }
1003
1013
  for (let i = 0; i < maxRetries; i++) {
1004
- if (await this.isServerResponding()) {
1014
+ if (await this.isServerResponding(serverInfo.port)) {
1005
1015
  return;
1006
1016
  }
1007
1017
  await this.sleep(retryDelay);
1008
1018
  }
1009
1019
  throw new Error("TCP server failed to become ready within timeout");
1010
1020
  }
1011
- async isServerResponding() {
1021
+ async isServerResponding(port) {
1012
1022
  return new Promise((resolve5) => {
1013
1023
  const client = new net.Socket();
1014
1024
  const timeout = setTimeout(() => {
1015
1025
  client.destroy();
1016
1026
  resolve5(false);
1017
1027
  }, 1e3);
1018
- client.connect(this.tcpPort, "127.0.0.1", () => {
1028
+ client.connect(port, "127.0.0.1", () => {
1019
1029
  clearTimeout(timeout);
1020
1030
  client.destroy();
1021
1031
  resolve5(true);
@@ -1040,15 +1050,16 @@ var QueryEngine = class {
1040
1050
  }
1041
1051
  return parsed;
1042
1052
  }
1043
- async startTcpServer() {
1053
+ async startTcpServer(binary, serverConnectionId) {
1044
1054
  await this.initializeBinary();
1045
1055
  if (!this.binary) {
1046
1056
  throw new Error("Binary not initialized");
1047
1057
  }
1048
- this.tcpPort = await this.findAvailablePort(this.tcpPort);
1058
+ const basePort = binary === "schema_engine" ? 9900 : 9944;
1059
+ const tcpPort = await this.findAvailablePort(basePort);
1049
1060
  return new Promise((resolve5, reject) => {
1050
- const serverArgs = [...this.arguments, "--action", "server", "--tcp-port", this.tcpPort.toString()];
1051
- const serverProcess = spawn2(this.binary["query_engine"], serverArgs);
1061
+ const serverArgs = [...this.arguments, "--action", "server", "--tcp-port", tcpPort.toString()];
1062
+ const serverProcess = spawn2(this.binary[binary], serverArgs);
1052
1063
  let started = false;
1053
1064
  const timeout = setTimeout(() => {
1054
1065
  if (!started) {
@@ -1062,8 +1073,8 @@ var QueryEngine = class {
1062
1073
  if (!started) {
1063
1074
  started = true;
1064
1075
  clearTimeout(timeout);
1065
- globalTcpServers.set(this.connectionId, {
1066
- port: this.tcpPort,
1076
+ globalTcpServers.set(serverConnectionId, {
1077
+ port: tcpPort,
1067
1078
  process: serverProcess
1068
1079
  });
1069
1080
  resolve5();
@@ -1071,10 +1082,10 @@ var QueryEngine = class {
1071
1082
  }
1072
1083
  });
1073
1084
  serverProcess.stderr.on("data", (data) => {
1074
- console.error(`[TCP Server Error] ${data.toString().trim()}`);
1085
+ console.error(`[${binary} TCP Server Error] ${data.toString().trim()}`);
1075
1086
  });
1076
1087
  serverProcess.on("close", (code) => {
1077
- globalTcpServers.delete(this.connectionId);
1088
+ globalTcpServers.delete(serverConnectionId);
1078
1089
  });
1079
1090
  serverProcess.on("error", (error) => {
1080
1091
  if (!started) {
@@ -1150,17 +1161,123 @@ var QueryEngine = class {
1150
1161
  });
1151
1162
  });
1152
1163
  }
1153
- async sendTcpRequestFast(args) {
1154
- const existingConnection = globalTcpConnections.get(this.connectionId);
1155
- if (existingConnection && existingConnection.readyState === "open") {
1156
- try {
1157
- return await this.sendOnExistingConnection(existingConnection, args);
1158
- } catch (error) {
1159
- globalTcpConnections.delete(this.connectionId);
1160
- existingConnection.destroy();
1164
+ async sendTcpRequestFast(args, serverConnectionId) {
1165
+ return new Promise((resolve5, reject) => {
1166
+ let queue = connectionQueues.get(serverConnectionId);
1167
+ if (!queue) {
1168
+ queue = [];
1169
+ connectionQueues.set(serverConnectionId, queue);
1170
+ }
1171
+ queue.push({ args, resolve: resolve5, reject });
1172
+ this.processQueue(serverConnectionId);
1173
+ });
1174
+ }
1175
+ async processQueue(serverConnectionId) {
1176
+ if (connectionProcessing.get(serverConnectionId)) {
1177
+ return;
1178
+ }
1179
+ const queue = connectionQueues.get(serverConnectionId);
1180
+ if (!queue || queue.length === 0) {
1181
+ return;
1182
+ }
1183
+ connectionProcessing.set(serverConnectionId, true);
1184
+ try {
1185
+ const serverInfo = globalTcpServers.get(serverConnectionId);
1186
+ if (!serverInfo) {
1187
+ throw new Error("Server not initialized");
1188
+ }
1189
+ while (queue.length > 0) {
1190
+ const request = queue.shift();
1191
+ if (!request) break;
1192
+ try {
1193
+ let connection = globalTcpConnections.get(serverConnectionId);
1194
+ if (!connection || connection.destroyed || connection.readyState !== "open") {
1195
+ connection = await this.createNewConnection(serverInfo.port);
1196
+ globalTcpConnections.set(serverConnectionId, connection);
1197
+ }
1198
+ const result = await this.executeOnConnection(connection, request.args);
1199
+ request.resolve(result);
1200
+ } catch (error) {
1201
+ globalTcpConnections.delete(serverConnectionId);
1202
+ request.reject(error);
1203
+ }
1161
1204
  }
1205
+ } finally {
1206
+ connectionProcessing.set(serverConnectionId, false);
1162
1207
  }
1163
- return await this.createPersistentConnection(args);
1208
+ }
1209
+ async createNewConnection(port) {
1210
+ return new Promise((resolve5, reject) => {
1211
+ const client = new net.Socket();
1212
+ client.setNoDelay(true);
1213
+ client.setKeepAlive(true, 6e4);
1214
+ const timeout = setTimeout(() => {
1215
+ client.destroy();
1216
+ reject(new Error("Connection timeout"));
1217
+ }, 5e3);
1218
+ client.connect(port, "127.0.0.1", () => {
1219
+ clearTimeout(timeout);
1220
+ resolve5(client);
1221
+ });
1222
+ client.on("error", (error) => {
1223
+ clearTimeout(timeout);
1224
+ reject(error);
1225
+ });
1226
+ });
1227
+ }
1228
+ async executeOnConnection(connection, args) {
1229
+ return new Promise((resolve5, reject) => {
1230
+ const command = {};
1231
+ for (let i = 0; i < args.length; i += 2) {
1232
+ if (args[i].startsWith("--")) {
1233
+ const key = args[i].substring(2);
1234
+ const value = args[i + 1];
1235
+ command[key] = value;
1236
+ }
1237
+ }
1238
+ let responseBuffer = "";
1239
+ let isResolved = false;
1240
+ const timeout = setTimeout(() => {
1241
+ if (!isResolved) {
1242
+ isResolved = true;
1243
+ connection.removeListener("data", onData);
1244
+ connection.removeListener("error", onError);
1245
+ reject(new Error("Request timeout"));
1246
+ }
1247
+ }, this.timeout);
1248
+ const onData = (data) => {
1249
+ responseBuffer += data.toString();
1250
+ const match = responseBuffer.match(/PROCESS_RESPONSE:(\{.*\})/);
1251
+ if (match && !isResolved) {
1252
+ isResolved = true;
1253
+ clearTimeout(timeout);
1254
+ connection.removeListener("data", onData);
1255
+ connection.removeListener("error", onError);
1256
+ try {
1257
+ const response = JSON.parse(match[1]);
1258
+ resolve5({
1259
+ status: response.status,
1260
+ message: response.message,
1261
+ data: response.data
1262
+ });
1263
+ } catch (error) {
1264
+ reject(new Error("Failed to parse response"));
1265
+ }
1266
+ }
1267
+ };
1268
+ const onError = (error) => {
1269
+ if (!isResolved) {
1270
+ isResolved = true;
1271
+ clearTimeout(timeout);
1272
+ connection.removeListener("data", onData);
1273
+ connection.removeListener("error", onError);
1274
+ reject(error);
1275
+ }
1276
+ };
1277
+ connection.on("data", onData);
1278
+ connection.on("error", onError);
1279
+ connection.write(JSON.stringify(command));
1280
+ });
1164
1281
  }
1165
1282
  async sendOnExistingConnection(connection, args) {
1166
1283
  return new Promise((resolve5, reject) => {