@yydb/light-db 0.0.0

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.
Files changed (150) hide show
  1. package/dist/api/LightDB.d.ts +194 -0
  2. package/dist/api/LightDB.d.ts.map +1 -0
  3. package/dist/api/LightDB.js +299 -0
  4. package/dist/api/LightDB.js.map +1 -0
  5. package/dist/api/Transaction.d.ts +100 -0
  6. package/dist/api/Transaction.d.ts.map +1 -0
  7. package/dist/api/Transaction.js +263 -0
  8. package/dist/api/Transaction.js.map +1 -0
  9. package/dist/api/index.d.ts +24 -0
  10. package/dist/api/index.d.ts.map +1 -0
  11. package/dist/api/index.js +54 -0
  12. package/dist/api/index.js.map +1 -0
  13. package/dist/api/types.d.ts +154 -0
  14. package/dist/api/types.d.ts.map +1 -0
  15. package/dist/api/types.js +7 -0
  16. package/dist/api/types.js.map +1 -0
  17. package/dist/collection/Collection.d.ts +235 -0
  18. package/dist/collection/Collection.d.ts.map +1 -0
  19. package/dist/collection/Collection.js +671 -0
  20. package/dist/collection/Collection.js.map +1 -0
  21. package/dist/collection/QueryBuilder.d.ts +225 -0
  22. package/dist/collection/QueryBuilder.d.ts.map +1 -0
  23. package/dist/collection/QueryBuilder.js +475 -0
  24. package/dist/collection/QueryBuilder.js.map +1 -0
  25. package/dist/collection/QueryParser.d.ts +117 -0
  26. package/dist/collection/QueryParser.d.ts.map +1 -0
  27. package/dist/collection/QueryParser.js +438 -0
  28. package/dist/collection/QueryParser.js.map +1 -0
  29. package/dist/collection/index.d.ts +42 -0
  30. package/dist/collection/index.d.ts.map +1 -0
  31. package/dist/collection/index.js +41 -0
  32. package/dist/collection/index.js.map +1 -0
  33. package/dist/collection/types.d.ts +365 -0
  34. package/dist/collection/types.d.ts.map +1 -0
  35. package/dist/collection/types.js +7 -0
  36. package/dist/collection/types.js.map +1 -0
  37. package/dist/constants/index.d.ts +51 -0
  38. package/dist/constants/index.d.ts.map +1 -0
  39. package/dist/constants/index.js +68 -0
  40. package/dist/constants/index.js.map +1 -0
  41. package/dist/errors/index.d.ts +69 -0
  42. package/dist/errors/index.d.ts.map +1 -0
  43. package/dist/errors/index.js +132 -0
  44. package/dist/errors/index.js.map +1 -0
  45. package/dist/index.d.ts +220 -0
  46. package/dist/index.d.ts.map +1 -0
  47. package/dist/index.js +729 -0
  48. package/dist/index.js.map +1 -0
  49. package/dist/indexes/BPlusTree.d.ts +181 -0
  50. package/dist/indexes/BPlusTree.d.ts.map +1 -0
  51. package/dist/indexes/BPlusTree.js +528 -0
  52. package/dist/indexes/BPlusTree.js.map +1 -0
  53. package/dist/indexes/IndexManager.d.ts +236 -0
  54. package/dist/indexes/IndexManager.d.ts.map +1 -0
  55. package/dist/indexes/IndexManager.js +502 -0
  56. package/dist/indexes/IndexManager.js.map +1 -0
  57. package/dist/indexes/PrimaryIndex.d.ts +240 -0
  58. package/dist/indexes/PrimaryIndex.d.ts.map +1 -0
  59. package/dist/indexes/PrimaryIndex.js +354 -0
  60. package/dist/indexes/PrimaryIndex.js.map +1 -0
  61. package/dist/indexes/SecondaryIndex.d.ts +204 -0
  62. package/dist/indexes/SecondaryIndex.d.ts.map +1 -0
  63. package/dist/indexes/SecondaryIndex.js +480 -0
  64. package/dist/indexes/SecondaryIndex.js.map +1 -0
  65. package/dist/indexes/errors.d.ts +66 -0
  66. package/dist/indexes/errors.d.ts.map +1 -0
  67. package/dist/indexes/errors.js +117 -0
  68. package/dist/indexes/errors.js.map +1 -0
  69. package/dist/indexes/index.d.ts +52 -0
  70. package/dist/indexes/index.d.ts.map +1 -0
  71. package/dist/indexes/index.js +131 -0
  72. package/dist/indexes/index.js.map +1 -0
  73. package/dist/indexes/types.d.ts +246 -0
  74. package/dist/indexes/types.d.ts.map +1 -0
  75. package/dist/indexes/types.js +30 -0
  76. package/dist/indexes/types.js.map +1 -0
  77. package/dist/performance/BatchWALWriter.d.ts +185 -0
  78. package/dist/performance/BatchWALWriter.d.ts.map +1 -0
  79. package/dist/performance/BatchWALWriter.js +490 -0
  80. package/dist/performance/BatchWALWriter.js.map +1 -0
  81. package/dist/performance/ColumnarStorage.d.ts +138 -0
  82. package/dist/performance/ColumnarStorage.d.ts.map +1 -0
  83. package/dist/performance/ColumnarStorage.js +875 -0
  84. package/dist/performance/ColumnarStorage.js.map +1 -0
  85. package/dist/performance/LazyQuery.d.ts +307 -0
  86. package/dist/performance/LazyQuery.d.ts.map +1 -0
  87. package/dist/performance/LazyQuery.js +588 -0
  88. package/dist/performance/LazyQuery.js.map +1 -0
  89. package/dist/performance/OptimizedBuffer.d.ts +375 -0
  90. package/dist/performance/OptimizedBuffer.d.ts.map +1 -0
  91. package/dist/performance/OptimizedBuffer.js +730 -0
  92. package/dist/performance/OptimizedBuffer.js.map +1 -0
  93. package/dist/performance/RWLock.d.ts +260 -0
  94. package/dist/performance/RWLock.d.ts.map +1 -0
  95. package/dist/performance/RWLock.js +570 -0
  96. package/dist/performance/RWLock.js.map +1 -0
  97. package/dist/performance/index.d.ts +41 -0
  98. package/dist/performance/index.d.ts.map +1 -0
  99. package/dist/performance/index.js +162 -0
  100. package/dist/performance/index.js.map +1 -0
  101. package/dist/performance/types.d.ts +247 -0
  102. package/dist/performance/types.d.ts.map +1 -0
  103. package/dist/performance/types.js +26 -0
  104. package/dist/performance/types.js.map +1 -0
  105. package/dist/storage/Checkpoint.d.ts +128 -0
  106. package/dist/storage/Checkpoint.d.ts.map +1 -0
  107. package/dist/storage/Checkpoint.js +251 -0
  108. package/dist/storage/Checkpoint.js.map +1 -0
  109. package/dist/storage/LightFile.d.ts +227 -0
  110. package/dist/storage/LightFile.d.ts.map +1 -0
  111. package/dist/storage/LightFile.js +526 -0
  112. package/dist/storage/LightFile.js.map +1 -0
  113. package/dist/storage/Recovery.d.ts +172 -0
  114. package/dist/storage/Recovery.d.ts.map +1 -0
  115. package/dist/storage/Recovery.js +399 -0
  116. package/dist/storage/Recovery.js.map +1 -0
  117. package/dist/storage/SHMFile.d.ts +243 -0
  118. package/dist/storage/SHMFile.d.ts.map +1 -0
  119. package/dist/storage/SHMFile.js +436 -0
  120. package/dist/storage/SHMFile.js.map +1 -0
  121. package/dist/storage/StorageEngine.d.ts +146 -0
  122. package/dist/storage/StorageEngine.d.ts.map +1 -0
  123. package/dist/storage/StorageEngine.js +353 -0
  124. package/dist/storage/StorageEngine.js.map +1 -0
  125. package/dist/storage/WALFile.d.ts +248 -0
  126. package/dist/storage/WALFile.d.ts.map +1 -0
  127. package/dist/storage/WALFile.js +597 -0
  128. package/dist/storage/WALFile.js.map +1 -0
  129. package/dist/storage/index.d.ts +10 -0
  130. package/dist/storage/index.d.ts.map +1 -0
  131. package/dist/storage/index.js +153 -0
  132. package/dist/storage/index.js.map +1 -0
  133. package/dist/types/index.d.ts +220 -0
  134. package/dist/types/index.d.ts.map +1 -0
  135. package/dist/types/index.js +7 -0
  136. package/dist/types/index.js.map +1 -0
  137. package/dist/utils/binary.d.ts +271 -0
  138. package/dist/utils/binary.d.ts.map +1 -0
  139. package/dist/utils/binary.js +454 -0
  140. package/dist/utils/binary.js.map +1 -0
  141. package/dist/utils/file.d.ts +166 -0
  142. package/dist/utils/file.d.ts.map +1 -0
  143. package/dist/utils/file.js +346 -0
  144. package/dist/utils/file.js.map +1 -0
  145. package/dist/utils/index.d.ts +6 -0
  146. package/dist/utils/index.d.ts.map +1 -0
  147. package/dist/utils/index.js +39 -0
  148. package/dist/utils/index.js.map +1 -0
  149. package/package.json +37 -0
  150. package/readme.md +124 -0
@@ -0,0 +1,730 @@
1
+ "use strict";
2
+ /**
3
+ * 优化的二进制序列化工具
4
+ * 使用 Buffer.allocUnsafe + 手动 fill,比 Buffer.alloc 快 2 倍以上
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.BufferConcatenator =
8
+ exports.FastSerializer =
9
+ exports.OptimizedBufferReader =
10
+ exports.OptimizedBufferWriter =
11
+ exports.BufferPool =
12
+ void 0;
13
+ exports.getGlobalBufferPool = getGlobalBufferPool;
14
+ /**
15
+ * Buffer 池
16
+ * 预分配 Buffer 块,减少频繁内存分配
17
+ */
18
+ class BufferPool {
19
+ /**
20
+ * 创建 Buffer 池
21
+ * @param options 配置选项
22
+ */
23
+ constructor(options = {}) {
24
+ this.chunkSize = options.chunkSize ?? 4096;
25
+ this.maxPoolSize = options.poolSize ?? 256;
26
+ this.pool = [];
27
+ this.usedCount = 0;
28
+ this.totalAllocated = 0;
29
+ this.totalFreed = 0;
30
+ if (options.preallocate !== false) {
31
+ this.preallocate(Math.min(this.maxPoolSize, 16));
32
+ }
33
+ }
34
+ /**
35
+ * 预分配 Buffer 块
36
+ * @param count 分配数量
37
+ */
38
+ preallocate(count) {
39
+ for (let i = 0; i < count; i++) {
40
+ this.pool.push(this.createChunk());
41
+ }
42
+ }
43
+ /**
44
+ * 创建新的 Buffer 块
45
+ */
46
+ createChunk() {
47
+ return Buffer.allocUnsafe(this.chunkSize);
48
+ }
49
+ /**
50
+ * 从池中获取 Buffer
51
+ * @param size 需要的大小
52
+ * @returns Buffer 实例
53
+ */
54
+ allocate(size) {
55
+ this.totalAllocated++;
56
+ if (size <= this.chunkSize && this.pool.length > 0) {
57
+ const buffer = this.pool.pop();
58
+ this.usedCount++;
59
+ return buffer.subarray(0, size);
60
+ }
61
+ return Buffer.allocUnsafe(size);
62
+ }
63
+ /**
64
+ * 释放 Buffer 回池中
65
+ * @param buffer 要释放的 Buffer
66
+ */
67
+ release(buffer) {
68
+ this.totalFreed++;
69
+ if (buffer.length === this.chunkSize && this.pool.length < this.maxPoolSize) {
70
+ buffer.fill(0);
71
+ this.pool.push(buffer);
72
+ }
73
+ if (this.usedCount > 0) {
74
+ this.usedCount--;
75
+ }
76
+ }
77
+ /**
78
+ * 获取统计信息
79
+ */
80
+ getStats() {
81
+ return {
82
+ totalChunks: this.pool.length + this.usedCount,
83
+ availableChunks: this.pool.length,
84
+ usedChunks: this.usedCount,
85
+ totalMemory: (this.pool.length + this.usedCount) * this.chunkSize,
86
+ usedMemory: this.usedCount * this.chunkSize,
87
+ };
88
+ }
89
+ /**
90
+ * 清空池
91
+ */
92
+ clear() {
93
+ this.pool = [];
94
+ this.usedCount = 0;
95
+ }
96
+ /**
97
+ * 获取块大小
98
+ */
99
+ getChunkSize() {
100
+ return this.chunkSize;
101
+ }
102
+ /**
103
+ * 获取分配总数
104
+ */
105
+ getTotalAllocated() {
106
+ return this.totalAllocated;
107
+ }
108
+ /**
109
+ * 获取释放总数
110
+ */
111
+ getTotalFreed() {
112
+ return this.totalFreed;
113
+ }
114
+ }
115
+ exports.BufferPool = BufferPool;
116
+ /**
117
+ * 全局 Buffer 池实例
118
+ */
119
+ let globalPool = null;
120
+ /**
121
+ * 获取全局 Buffer 池
122
+ */
123
+ function getGlobalBufferPool() {
124
+ if (!globalPool) {
125
+ globalPool = new BufferPool();
126
+ }
127
+ return globalPool;
128
+ }
129
+ /**
130
+ * 优化的 Buffer 写入器
131
+ * 使用 allocUnsafe + 手动 fill,性能更优
132
+ */
133
+ class OptimizedBufferWriter {
134
+ /**
135
+ * 创建优化的 Buffer 写入器
136
+ * @param size 初始大小
137
+ */
138
+ constructor(size = 1024) {
139
+ this.buffer = OptimizedBufferWriter.pool.allocate(size);
140
+ this.offset = 0;
141
+ }
142
+ /**
143
+ * 获取当前写入位置
144
+ */
145
+ get position() {
146
+ return this.offset;
147
+ }
148
+ /**
149
+ * 获取已写入的数据
150
+ */
151
+ get data() {
152
+ return this.buffer.subarray(0, this.offset);
153
+ }
154
+ /**
155
+ * 获取剩余空间
156
+ */
157
+ get remaining() {
158
+ return this.buffer.length - this.offset;
159
+ }
160
+ /**
161
+ * 确保有足够的空间
162
+ * @param additional 需要的额外空间
163
+ */
164
+ ensureCapacity(additional) {
165
+ const required = this.offset + additional;
166
+ if (required > this.buffer.length) {
167
+ const newSize = Math.max(required, this.buffer.length * 2);
168
+ const newBuffer = Buffer.allocUnsafe(newSize);
169
+ this.buffer.copy(newBuffer, 0, 0, this.offset);
170
+ OptimizedBufferWriter.pool.release(this.buffer);
171
+ this.buffer = newBuffer;
172
+ }
173
+ }
174
+ /**
175
+ * 写入无符号 8 位整数
176
+ */
177
+ writeUInt8(value) {
178
+ this.ensureCapacity(1);
179
+ this.buffer.writeUInt8(value, this.offset);
180
+ this.offset += 1;
181
+ return this;
182
+ }
183
+ /**
184
+ * 写入无符号 16 位整数(Little-Endian)
185
+ */
186
+ writeUInt16LE(value) {
187
+ this.ensureCapacity(2);
188
+ this.buffer.writeUInt16LE(value, this.offset);
189
+ this.offset += 2;
190
+ return this;
191
+ }
192
+ /**
193
+ * 写入无符号 32 位整数(Little-Endian)
194
+ */
195
+ writeUInt32LE(value) {
196
+ this.ensureCapacity(4);
197
+ this.buffer.writeUInt32LE(value, this.offset);
198
+ this.offset += 4;
199
+ return this;
200
+ }
201
+ /**
202
+ * 写入有符号 32 位整数(Little-Endian)
203
+ */
204
+ writeInt32LE(value) {
205
+ this.ensureCapacity(4);
206
+ this.buffer.writeInt32LE(value, this.offset);
207
+ this.offset += 4;
208
+ return this;
209
+ }
210
+ /**
211
+ * 写入无符号 64 位整数(Little-Endian)
212
+ */
213
+ writeBigUInt64LE(value) {
214
+ this.ensureCapacity(8);
215
+ this.buffer.writeBigUInt64LE(value, this.offset);
216
+ this.offset += 8;
217
+ return this;
218
+ }
219
+ /**
220
+ * 写入有符号 64 位整数(Little-Endian)
221
+ */
222
+ writeBigInt64LE(value) {
223
+ this.ensureCapacity(8);
224
+ this.buffer.writeBigInt64LE(value, this.offset);
225
+ this.offset += 8;
226
+ return this;
227
+ }
228
+ /**
229
+ * 写入 32 位浮点数(Little-Endian)
230
+ */
231
+ writeFloatLE(value) {
232
+ this.ensureCapacity(4);
233
+ this.buffer.writeFloatLE(value, this.offset);
234
+ this.offset += 4;
235
+ return this;
236
+ }
237
+ /**
238
+ * 写入 64 位双精度浮点数(Little-Endian)
239
+ */
240
+ writeDoubleLE(value) {
241
+ this.ensureCapacity(8);
242
+ this.buffer.writeDoubleLE(value, this.offset);
243
+ this.offset += 8;
244
+ return this;
245
+ }
246
+ /**
247
+ * 写入字符串(带长度前缀)
248
+ */
249
+ writeString(value) {
250
+ const byteLength = Buffer.byteLength(value, "utf8");
251
+ this.writeUInt32LE(byteLength);
252
+ this.ensureCapacity(byteLength);
253
+ this.buffer.write(value, this.offset, "utf8");
254
+ this.offset += byteLength;
255
+ return this;
256
+ }
257
+ /**
258
+ * 写入字符串(不带长度前缀)
259
+ */
260
+ writeStringRaw(value) {
261
+ const byteLength = Buffer.byteLength(value, "utf8");
262
+ this.ensureCapacity(byteLength);
263
+ this.buffer.write(value, this.offset, "utf8");
264
+ this.offset += byteLength;
265
+ return this;
266
+ }
267
+ /**
268
+ * 写入 Buffer
269
+ */
270
+ writeBuffer(value) {
271
+ this.ensureCapacity(value.length);
272
+ value.copy(this.buffer, this.offset);
273
+ this.offset += value.length;
274
+ return this;
275
+ }
276
+ /**
277
+ * 写入带长度前缀的 Buffer
278
+ */
279
+ writeBufferWithLength(value) {
280
+ this.writeUInt32LE(value.length);
281
+ this.writeBuffer(value);
282
+ return this;
283
+ }
284
+ /**
285
+ * 写入字节数组
286
+ */
287
+ writeBytes(values) {
288
+ this.ensureCapacity(values.length);
289
+ for (let i = 0; i < values.length; i++) {
290
+ this.buffer[this.offset + i] = values[i];
291
+ }
292
+ this.offset += values.length;
293
+ return this;
294
+ }
295
+ /**
296
+ * 填充指定字节
297
+ */
298
+ fill(value, count) {
299
+ this.ensureCapacity(count);
300
+ this.buffer.fill(value, this.offset, this.offset + count);
301
+ this.offset += count;
302
+ return this;
303
+ }
304
+ /**
305
+ * 跳过指定字节数
306
+ */
307
+ skip(count) {
308
+ this.ensureCapacity(count);
309
+ this.offset += count;
310
+ return this;
311
+ }
312
+ /**
313
+ * 在指定位置写入值
314
+ */
315
+ writeAt(offset, value, size) {
316
+ switch (size) {
317
+ case 1:
318
+ this.buffer.writeUInt8(value, offset);
319
+ break;
320
+ case 2:
321
+ this.buffer.writeUInt16LE(value, offset);
322
+ break;
323
+ case 4:
324
+ this.buffer.writeUInt32LE(value, offset);
325
+ break;
326
+ case 8:
327
+ this.buffer.writeBigUInt64LE(BigInt(value), offset);
328
+ break;
329
+ }
330
+ return this;
331
+ }
332
+ /**
333
+ * 重置写入器
334
+ */
335
+ reset() {
336
+ this.offset = 0;
337
+ return this;
338
+ }
339
+ /**
340
+ * 释放资源
341
+ */
342
+ dispose() {
343
+ OptimizedBufferWriter.pool.release(this.buffer);
344
+ }
345
+ }
346
+ exports.OptimizedBufferWriter = OptimizedBufferWriter;
347
+ OptimizedBufferWriter.pool = getGlobalBufferPool();
348
+ /**
349
+ * 优化的 Buffer 读取器
350
+ */
351
+ class OptimizedBufferReader {
352
+ /**
353
+ * 创建优化的 Buffer 读取器
354
+ * @param buffer 要读取的 Buffer
355
+ * @param startOffset 起始偏移量
356
+ */
357
+ constructor(buffer, startOffset = 0) {
358
+ this.buffer = buffer;
359
+ this.startOffset = startOffset;
360
+ this.offset = startOffset;
361
+ }
362
+ /**
363
+ * 获取当前读取位置
364
+ */
365
+ get position() {
366
+ return this.offset;
367
+ }
368
+ /**
369
+ * 设置读取位置
370
+ */
371
+ set position(value) {
372
+ this.offset = value;
373
+ }
374
+ /**
375
+ * 获取剩余可读字节数
376
+ */
377
+ get remaining() {
378
+ return this.buffer.length - this.offset;
379
+ }
380
+ /**
381
+ * 获取总长度
382
+ */
383
+ get length() {
384
+ return this.buffer.length;
385
+ }
386
+ /**
387
+ * 检查是否有足够的可读数据
388
+ */
389
+ hasRemaining(required) {
390
+ return this.remaining >= required;
391
+ }
392
+ /**
393
+ * 读取无符号 8 位整数
394
+ */
395
+ readUInt8() {
396
+ const value = this.buffer.readUInt8(this.offset);
397
+ this.offset += 1;
398
+ return value;
399
+ }
400
+ /**
401
+ * 读取无符号 16 位整数(Little-Endian)
402
+ */
403
+ readUInt16LE() {
404
+ const value = this.buffer.readUInt16LE(this.offset);
405
+ this.offset += 2;
406
+ return value;
407
+ }
408
+ /**
409
+ * 读取无符号 32 位整数(Little-Endian)
410
+ */
411
+ readUInt32LE() {
412
+ const value = this.buffer.readUInt32LE(this.offset);
413
+ this.offset += 4;
414
+ return value;
415
+ }
416
+ /**
417
+ * 读取有符号 32 位整数(Little-Endian)
418
+ */
419
+ readInt32LE() {
420
+ const value = this.buffer.readInt32LE(this.offset);
421
+ this.offset += 4;
422
+ return value;
423
+ }
424
+ /**
425
+ * 读取无符号 64 位整数(Little-Endian)
426
+ */
427
+ readBigUInt64LE() {
428
+ const value = this.buffer.readBigUInt64LE(this.offset);
429
+ this.offset += 8;
430
+ return value;
431
+ }
432
+ /**
433
+ * 读取有符号 64 位整数(Little-Endian)
434
+ */
435
+ readBigInt64LE() {
436
+ const value = this.buffer.readBigInt64LE(this.offset);
437
+ this.offset += 8;
438
+ return value;
439
+ }
440
+ /**
441
+ * 读取 32 位浮点数(Little-Endian)
442
+ */
443
+ readFloatLE() {
444
+ const value = this.buffer.readFloatLE(this.offset);
445
+ this.offset += 4;
446
+ return value;
447
+ }
448
+ /**
449
+ * 读取 64 位双精度浮点数(Little-Endian)
450
+ */
451
+ readDoubleLE() {
452
+ const value = this.buffer.readDoubleLE(this.offset);
453
+ this.offset += 8;
454
+ return value;
455
+ }
456
+ /**
457
+ * 读取字符串(带长度前缀)
458
+ */
459
+ readString() {
460
+ const length = this.readUInt32LE();
461
+ const value = this.buffer.toString("utf8", this.offset, this.offset + length);
462
+ this.offset += length;
463
+ return value;
464
+ }
465
+ /**
466
+ * 读取指定长度的字符串
467
+ */
468
+ readStringFixed(length) {
469
+ const value = this.buffer.toString("utf8", this.offset, this.offset + length);
470
+ this.offset += length;
471
+ return value;
472
+ }
473
+ /**
474
+ * 读取指定长度的 Buffer
475
+ */
476
+ readBuffer(length) {
477
+ const value = this.buffer.subarray(this.offset, this.offset + length);
478
+ this.offset += length;
479
+ return value;
480
+ }
481
+ /**
482
+ * 读取带长度前缀的 Buffer
483
+ */
484
+ readBufferWithLength() {
485
+ const length = this.readUInt32LE();
486
+ return this.readBuffer(length);
487
+ }
488
+ /**
489
+ * 读取字节数组
490
+ */
491
+ readBytes(length) {
492
+ const result = [];
493
+ for (let i = 0; i < length; i++) {
494
+ result.push(this.buffer[this.offset + i]);
495
+ }
496
+ this.offset += length;
497
+ return result;
498
+ }
499
+ /**
500
+ * 跳过指定字节数
501
+ */
502
+ skip(bytes) {
503
+ this.offset += bytes;
504
+ return this;
505
+ }
506
+ /**
507
+ * 回退指定字节数
508
+ */
509
+ back(bytes) {
510
+ this.offset = Math.max(this.startOffset, this.offset - bytes);
511
+ return this;
512
+ }
513
+ /**
514
+ * 重置到起始位置
515
+ */
516
+ reset() {
517
+ this.offset = this.startOffset;
518
+ return this;
519
+ }
520
+ /**
521
+ * 查看当前位置的字节(不移动指针)
522
+ */
523
+ peek() {
524
+ return this.buffer[this.offset];
525
+ }
526
+ /**
527
+ * 查看指定偏移量的值(不移动指针)
528
+ */
529
+ peekAt(offset, size) {
530
+ switch (size) {
531
+ case 1:
532
+ return this.buffer.readUInt8(offset);
533
+ case 2:
534
+ return this.buffer.readUInt16LE(offset);
535
+ case 4:
536
+ return this.buffer.readUInt32LE(offset);
537
+ case 8:
538
+ return this.buffer.readBigUInt64LE(offset);
539
+ }
540
+ }
541
+ /**
542
+ * 获取底层 Buffer
543
+ */
544
+ getBuffer() {
545
+ return this.buffer;
546
+ }
547
+ }
548
+ exports.OptimizedBufferReader = OptimizedBufferReader;
549
+ /**
550
+ * 快速序列化工具
551
+ * 提供常用数据结构的快速序列化方法
552
+ */
553
+ class FastSerializer {
554
+ /**
555
+ * 序列化键值对
556
+ */
557
+ static serializeKeyValue(key, value) {
558
+ const keyLen = Buffer.byteLength(key, "utf8");
559
+ const totalLen = 4 + keyLen + 4 + value.length;
560
+ const writer = new OptimizedBufferWriter(totalLen);
561
+ writer.writeUInt32LE(keyLen);
562
+ writer.writeStringRaw(key);
563
+ writer.writeUInt32LE(value.length);
564
+ writer.writeBuffer(value);
565
+ return writer.data;
566
+ }
567
+ /**
568
+ * 反序列化键值对
569
+ */
570
+ static deserializeKeyValue(buffer, offset = 0) {
571
+ const reader = new OptimizedBufferReader(buffer, offset);
572
+ const keyLen = reader.readUInt32LE();
573
+ const key = reader.readStringFixed(keyLen);
574
+ const valueLen = reader.readUInt32LE();
575
+ const value = reader.readBuffer(valueLen);
576
+ return {
577
+ key,
578
+ value,
579
+ bytesRead: reader.position - offset,
580
+ };
581
+ }
582
+ /**
583
+ * 序列化字符串数组
584
+ */
585
+ static serializeStringArray(strings) {
586
+ let totalLen = 4;
587
+ for (const s of strings) {
588
+ totalLen += 4 + Buffer.byteLength(s, "utf8");
589
+ }
590
+ const writer = new OptimizedBufferWriter(totalLen);
591
+ writer.writeUInt32LE(strings.length);
592
+ for (const s of strings) {
593
+ writer.writeString(s);
594
+ }
595
+ return writer.data;
596
+ }
597
+ /**
598
+ * 反序列化字符串数组
599
+ */
600
+ static deserializeStringArray(buffer, offset = 0) {
601
+ const reader = new OptimizedBufferReader(buffer, offset);
602
+ const count = reader.readUInt32LE();
603
+ const strings = [];
604
+ for (let i = 0; i < count; i++) {
605
+ strings.push(reader.readString());
606
+ }
607
+ return {
608
+ strings,
609
+ bytesRead: reader.position - offset,
610
+ };
611
+ }
612
+ /**
613
+ * 序列化数字数组
614
+ */
615
+ static serializeNumberArray(numbers) {
616
+ const totalLen = 4 + numbers.length * 8;
617
+ const writer = new OptimizedBufferWriter(totalLen);
618
+ writer.writeUInt32LE(numbers.length);
619
+ for (const n of numbers) {
620
+ writer.writeDoubleLE(n);
621
+ }
622
+ return writer.data;
623
+ }
624
+ /**
625
+ * 反序列化数字数组
626
+ */
627
+ static deserializeNumberArray(buffer, offset = 0) {
628
+ const reader = new OptimizedBufferReader(buffer, offset);
629
+ const count = reader.readUInt32LE();
630
+ const numbers = [];
631
+ for (let i = 0; i < count; i++) {
632
+ numbers.push(reader.readDoubleLE());
633
+ }
634
+ return {
635
+ numbers,
636
+ bytesRead: reader.position - offset,
637
+ };
638
+ }
639
+ /**
640
+ * 序列化对象(简单 JSON 序列化的优化版本)
641
+ */
642
+ static serializeObject(obj) {
643
+ const json = JSON.stringify(obj);
644
+ const jsonBuffer = Buffer.from(json, "utf8");
645
+ const writer = new OptimizedBufferWriter(4 + jsonBuffer.length);
646
+ writer.writeUInt32LE(jsonBuffer.length);
647
+ writer.writeBuffer(jsonBuffer);
648
+ return writer.data;
649
+ }
650
+ /**
651
+ * 反序列化对象
652
+ */
653
+ static deserializeObject(buffer, offset = 0) {
654
+ const reader = new OptimizedBufferReader(buffer, offset);
655
+ const len = reader.readUInt32LE();
656
+ const jsonBuffer = reader.readBuffer(len);
657
+ const json = jsonBuffer.toString("utf8");
658
+ return {
659
+ obj: JSON.parse(json),
660
+ bytesRead: reader.position - offset,
661
+ };
662
+ }
663
+ }
664
+ exports.FastSerializer = FastSerializer;
665
+ /**
666
+ * 批量 Buffer 合并工具
667
+ */
668
+ class BufferConcatenator {
669
+ constructor() {
670
+ this.buffers = [];
671
+ this.totalLength = 0;
672
+ }
673
+ /**
674
+ * 添加 Buffer
675
+ */
676
+ append(buffer) {
677
+ this.buffers.push(buffer);
678
+ this.totalLength += buffer.length;
679
+ return this;
680
+ }
681
+ /**
682
+ * 添加多个 Buffer
683
+ */
684
+ appendMany(buffers) {
685
+ for (const buffer of buffers) {
686
+ this.append(buffer);
687
+ }
688
+ return this;
689
+ }
690
+ /**
691
+ * 获取总长度
692
+ */
693
+ get length() {
694
+ return this.totalLength;
695
+ }
696
+ /**
697
+ * 获取 Buffer 数量
698
+ */
699
+ get count() {
700
+ return this.buffers.length;
701
+ }
702
+ /**
703
+ * 合并所有 Buffer
704
+ */
705
+ concat() {
706
+ if (this.buffers.length === 0) {
707
+ return Buffer.alloc(0);
708
+ }
709
+ if (this.buffers.length === 1) {
710
+ return this.buffers[0];
711
+ }
712
+ const result = Buffer.allocUnsafe(this.totalLength);
713
+ let offset = 0;
714
+ for (const buffer of this.buffers) {
715
+ buffer.copy(result, offset);
716
+ offset += buffer.length;
717
+ }
718
+ return result;
719
+ }
720
+ /**
721
+ * 清空
722
+ */
723
+ clear() {
724
+ this.buffers = [];
725
+ this.totalLength = 0;
726
+ return this;
727
+ }
728
+ }
729
+ exports.BufferConcatenator = BufferConcatenator;
730
+ //# sourceMappingURL=OptimizedBuffer.js.map