@dbcube/core 3.0.7 → 3.0.9

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
@@ -45,8 +45,12 @@ declare class QueryEngine {
45
45
  private waitForServerReady;
46
46
  private isServerResponding;
47
47
  private sleep;
48
+ private getCachedDML;
48
49
  private startTcpServer;
49
50
  private sendTcpRequest;
51
+ private sendTcpRequestFast;
52
+ private sendOnExistingConnection;
53
+ private createPersistentConnection;
50
54
  private createProcess;
51
55
  disconnect(): Promise<ResponseEngine>;
52
56
  }
package/dist/index.d.ts CHANGED
@@ -45,8 +45,12 @@ declare class QueryEngine {
45
45
  private waitForServerReady;
46
46
  private isServerResponding;
47
47
  private sleep;
48
+ private getCachedDML;
48
49
  private startTcpServer;
49
50
  private sendTcpRequest;
51
+ private sendTcpRequestFast;
52
+ private sendOnExistingConnection;
53
+ private createPersistentConnection;
50
54
  private createProcess;
51
55
  disconnect(): Promise<ResponseEngine>;
52
56
  }
package/dist/index.js CHANGED
@@ -729,6 +729,10 @@ import { createRequire as createRequire2 } from "module";
729
729
  import * as net from "net";
730
730
  import { spawn as spawn2 } from "child_process";
731
731
  var globalTcpServers = /* @__PURE__ */ new Map();
732
+ var globalTcpConnections = /* @__PURE__ */ new Map();
733
+ var queryCache = /* @__PURE__ */ new Map();
734
+ var cacheSize = 0;
735
+ var MAX_CACHE_SIZE = 500;
732
736
  var QueryEngine = class {
733
737
  name;
734
738
  config;
@@ -847,10 +851,10 @@ var QueryEngine = class {
847
851
  if (!serverInfo || !serverInfo.process || serverInfo.process.killed) {
848
852
  console.log("\u{1F680} Starting TCP server for ultra-fast queries...");
849
853
  await this.startTcpServer();
854
+ await this.waitForServerReady();
850
855
  }
851
- await this.waitForServerReady();
852
856
  console.log("\u26A1 Using TCP server (ultra-fast)");
853
- return this.sendTcpRequest(args);
857
+ return this.sendTcpRequestFast(args);
854
858
  }
855
859
  async waitForServerReady() {
856
860
  const maxRetries = 10;
@@ -885,6 +889,17 @@ var QueryEngine = class {
885
889
  sleep(ms) {
886
890
  return new Promise((resolve5) => setTimeout(resolve5, ms));
887
891
  }
892
+ getCachedDML(dmlJson) {
893
+ if (queryCache.has(dmlJson)) {
894
+ return queryCache.get(dmlJson);
895
+ }
896
+ const parsed = JSON.parse(dmlJson);
897
+ if (cacheSize < MAX_CACHE_SIZE) {
898
+ queryCache.set(dmlJson, parsed);
899
+ cacheSize++;
900
+ }
901
+ return parsed;
902
+ }
888
903
  async startTcpServer() {
889
904
  await this.initializeBinary();
890
905
  if (!this.binary) {
@@ -998,6 +1013,137 @@ var QueryEngine = class {
998
1013
  });
999
1014
  });
1000
1015
  }
1016
+ async sendTcpRequestFast(args) {
1017
+ const existingConnection = globalTcpConnections.get(this.connectionId);
1018
+ if (existingConnection && existingConnection.readyState === "open") {
1019
+ try {
1020
+ return await this.sendOnExistingConnection(existingConnection, args);
1021
+ } catch (error) {
1022
+ globalTcpConnections.delete(this.connectionId);
1023
+ existingConnection.destroy();
1024
+ }
1025
+ }
1026
+ return await this.createPersistentConnection(args);
1027
+ }
1028
+ async sendOnExistingConnection(connection, args) {
1029
+ return new Promise((resolve5, reject) => {
1030
+ const dmlIndex = args.findIndex((arg) => arg === "--dml");
1031
+ const dmlJson = dmlIndex !== -1 ? args[dmlIndex + 1] : null;
1032
+ if (!dmlJson) {
1033
+ reject(new Error("No DML found in arguments"));
1034
+ return;
1035
+ }
1036
+ let responseBuffer = "";
1037
+ let isResolved = false;
1038
+ const timeout = setTimeout(() => {
1039
+ if (!isResolved) {
1040
+ isResolved = true;
1041
+ reject(new Error("TCP request timeout"));
1042
+ }
1043
+ }, 100);
1044
+ const onData = (data) => {
1045
+ responseBuffer += data.toString();
1046
+ const match = responseBuffer.match(/PROCESS_RESPONSE:(\{.*\})/);
1047
+ if (match && !isResolved) {
1048
+ isResolved = true;
1049
+ clearTimeout(timeout);
1050
+ connection.off("data", onData);
1051
+ connection.off("error", onError);
1052
+ try {
1053
+ const response = JSON.parse(match[1]);
1054
+ resolve5({
1055
+ status: response.status,
1056
+ message: response.message,
1057
+ data: response.data
1058
+ });
1059
+ } catch (error) {
1060
+ reject(new Error("Failed to parse TCP response"));
1061
+ }
1062
+ }
1063
+ };
1064
+ const onError = (error) => {
1065
+ if (!isResolved) {
1066
+ isResolved = true;
1067
+ clearTimeout(timeout);
1068
+ connection.off("data", onData);
1069
+ connection.off("error", onError);
1070
+ reject(error);
1071
+ }
1072
+ };
1073
+ connection.on("data", onData);
1074
+ connection.on("error", onError);
1075
+ const command = {
1076
+ action: "execute",
1077
+ dml: dmlJson
1078
+ };
1079
+ connection.write(JSON.stringify(command));
1080
+ });
1081
+ }
1082
+ async createPersistentConnection(args) {
1083
+ return new Promise((resolve5, reject) => {
1084
+ const client = new net.Socket();
1085
+ let responseBuffer = "";
1086
+ let isResolved = false;
1087
+ client.setNoDelay(true);
1088
+ client.setKeepAlive(true, 6e4);
1089
+ const timeout = setTimeout(() => {
1090
+ if (!isResolved) {
1091
+ isResolved = true;
1092
+ client.destroy();
1093
+ reject(new Error("TCP connection timeout"));
1094
+ }
1095
+ }, 5e3);
1096
+ client.connect(this.tcpPort, "127.0.0.1", () => {
1097
+ clearTimeout(timeout);
1098
+ const dmlIndex = args.findIndex((arg) => arg === "--dml");
1099
+ const dmlJson = dmlIndex !== -1 ? args[dmlIndex + 1] : null;
1100
+ if (!dmlJson) {
1101
+ if (!isResolved) {
1102
+ isResolved = true;
1103
+ client.destroy();
1104
+ reject(new Error("No DML found in arguments"));
1105
+ }
1106
+ return;
1107
+ }
1108
+ globalTcpConnections.set(this.connectionId, client);
1109
+ const command = {
1110
+ action: "execute",
1111
+ dml: dmlJson
1112
+ };
1113
+ client.write(JSON.stringify(command));
1114
+ });
1115
+ client.on("data", (data) => {
1116
+ responseBuffer += data.toString();
1117
+ const match = responseBuffer.match(/PROCESS_RESPONSE:(\{.*\})/);
1118
+ if (match && !isResolved) {
1119
+ isResolved = true;
1120
+ try {
1121
+ const response = JSON.parse(match[1]);
1122
+ resolve5({
1123
+ status: response.status,
1124
+ message: response.message,
1125
+ data: response.data
1126
+ });
1127
+ } catch (error) {
1128
+ reject(new Error("Failed to parse TCP response"));
1129
+ }
1130
+ }
1131
+ });
1132
+ client.on("error", (error) => {
1133
+ if (!isResolved) {
1134
+ isResolved = true;
1135
+ globalTcpConnections.delete(this.connectionId);
1136
+ reject(error);
1137
+ }
1138
+ });
1139
+ client.on("close", () => {
1140
+ globalTcpConnections.delete(this.connectionId);
1141
+ if (!isResolved && !responseBuffer.includes("PROCESS_RESPONSE:")) {
1142
+ reject(new Error("Connection closed without valid response"));
1143
+ }
1144
+ });
1145
+ });
1146
+ }
1001
1147
  async createProcess(binary, args) {
1002
1148
  await this.initializeBinary();
1003
1149
  if (!this.binary) {
@@ -1086,6 +1232,12 @@ var QueryEngine = class {
1086
1232
  });
1087
1233
  }
1088
1234
  async disconnect() {
1235
+ const connection = globalTcpConnections.get(this.connectionId);
1236
+ if (connection && connection.readyState === "open") {
1237
+ connection.write(JSON.stringify({ action: "disconnect" }));
1238
+ connection.destroy();
1239
+ globalTcpConnections.delete(this.connectionId);
1240
+ }
1089
1241
  const serverInfo = globalTcpServers.get(this.connectionId);
1090
1242
  if (serverInfo && serverInfo.process && !serverInfo.process.killed) {
1091
1243
  console.log("\u{1F50C} Stopping TCP server...");
@@ -1093,7 +1245,7 @@ var QueryEngine = class {
1093
1245
  globalTcpServers.delete(this.connectionId);
1094
1246
  return {
1095
1247
  status: 200,
1096
- message: "TCP server stopped",
1248
+ message: "TCP server and connections stopped",
1097
1249
  data: null
1098
1250
  };
1099
1251
  }