dataply 0.0.21-alpha.0 → 0.0.21

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/cjs/index.js CHANGED
@@ -5695,31 +5695,31 @@ function numberToBytes(value, buffer, offset = 0, length = buffer.length) {
5695
5695
  }
5696
5696
 
5697
5697
  // src/utils/bytesToNumber.ts
5698
+ var tempBuffer = new ArrayBuffer(8);
5699
+ var tempView = new DataView(tempBuffer);
5700
+ var tempArray = new Uint8Array(tempBuffer);
5698
5701
  function bytesToNumber(bytes, offset = 0, length = bytes.length) {
5699
- if (length === 4) {
5700
- return (bytes[offset] | bytes[offset + 1] << 8 | bytes[offset + 2] << 16 | bytes[offset + 3] << 24) >>> 0;
5701
- }
5702
- if (length === 8) {
5703
- const low2 = (bytes[offset] | bytes[offset + 1] << 8 | bytes[offset + 2] << 16 | bytes[offset + 3] << 24) >>> 0;
5704
- const high = (bytes[offset + 4] | bytes[offset + 5] << 8 | bytes[offset + 6] << 16 | bytes[offset + 7] << 24) >>> 0;
5705
- return low2 + high * 4294967296;
5702
+ tempArray.set(bytes.subarray(offset, offset + length));
5703
+ switch (length) {
5704
+ case 1:
5705
+ return tempView.getUint8(0);
5706
+ case 2:
5707
+ return tempView.getUint16(0, true);
5708
+ case 3:
5709
+ return tempView.getUint16(0, true) + (tempView.getUint8(2) << 16);
5710
+ case 4:
5711
+ return tempView.getUint32(0, true);
5712
+ case 5:
5713
+ return tempView.getUint32(0, true) + tempView.getUint8(4) * 4294967296;
5714
+ case 6:
5715
+ return tempView.getUint32(0, true) + tempView.getUint16(4, true) * 4294967296;
5716
+ case 7:
5717
+ return tempView.getUint32(0, true) + (tempView.getUint16(4, true) + (tempView.getUint8(6) << 16)) * 4294967296;
5718
+ case 8:
5719
+ return tempView.getUint32(0, true) + tempView.getUint32(4, true) * 4294967296;
5720
+ default:
5721
+ return 0;
5706
5722
  }
5707
- let low = 0;
5708
- const lenLow = length < 4 ? length : 4;
5709
- for (let i = 0; i < lenLow; i++) {
5710
- low |= bytes[offset + i] << i * 8;
5711
- }
5712
- low >>>= 0;
5713
- if (length > 4) {
5714
- let high = 0;
5715
- const lenHigh = length < 8 ? length : 8;
5716
- for (let i = 4; i < lenHigh; i++) {
5717
- high |= bytes[offset + i] << (i - 4) * 8;
5718
- }
5719
- high >>>= 0;
5720
- return low + high * 4294967296;
5721
- }
5722
- return low;
5723
5723
  }
5724
5724
 
5725
5725
  // src/utils/bitwise.ts
@@ -8301,7 +8301,7 @@ var RowTableEngine = class {
8301
8301
  this.maxBodySize = this.pfs.pageSize - DataPageManager.CONSTANT.SIZE_PAGE_HEADER;
8302
8302
  this.order = this.getOptimalOrder(pfs.pageSize, IndexPageManager.CONSTANT.SIZE_KEY, IndexPageManager.CONSTANT.SIZE_VALUE);
8303
8303
  this.strategy = new RowIdentifierStrategy(this.order, pfs, txContext);
8304
- const budget = import_node_os.default.freemem() * 0.1;
8304
+ const budget = import_node_os.default.totalmem() * 0.1;
8305
8305
  const nodeMemory = this.order * 24 + 256;
8306
8306
  const capacity = Math.max(1e3, Math.min(1e6, Math.floor(budget / nodeMemory)));
8307
8307
  this.bptree = new BPTreeAsync(
@@ -8686,41 +8686,44 @@ var RowTableEngine = class {
8686
8686
  if (pks.length === 0) {
8687
8687
  return [];
8688
8688
  }
8689
+ const pkIndexMap = /* @__PURE__ */ new Map();
8690
+ for (let i = 0, len = pks.length; i < len; i++) {
8691
+ pkIndexMap.set(pks[i], i);
8692
+ }
8689
8693
  const minPk = Math.min(...pks);
8690
8694
  const maxPk = Math.max(...pks);
8691
- const pkSet = new Set(pks);
8692
- const pkRidPairs = [];
8695
+ const pkRidPairs = new Array(pks.length).fill(null);
8693
8696
  const btx = await this.getBPTreeTransaction(tx);
8694
8697
  const stream = btx.whereStream({ gte: minPk, lte: maxPk });
8695
8698
  for await (const [rid, pk] of stream) {
8696
- if (pkSet.has(pk)) {
8697
- pkRidPairs.push({ pk, rid });
8699
+ const index = pkIndexMap.get(pk);
8700
+ if (index !== void 0) {
8701
+ pkRidPairs[index] = { pk, rid, index };
8698
8702
  }
8699
8703
  }
8700
- const resultMap = await this.fetchRowsByRids(pkRidPairs, tx);
8701
- return pks.map((pk) => resultMap.get(pk) ?? null);
8704
+ return this.fetchRowsByRids(pkRidPairs, tx);
8702
8705
  }
8703
8706
  /**
8704
8707
  * Fetches multiple rows by their RID and PK combinations, grouping by page ID to minimize I/O.
8705
8708
  * @param pkRidPairs Array of {pk, rid} pairs
8706
8709
  * @param tx Transaction
8707
- * @returns Map of PK to row data
8710
+ * @returns Array of row data in the same order as input PKs
8708
8711
  */
8709
8712
  async fetchRowsByRids(pkRidPairs, tx) {
8710
- const resultMap = /* @__PURE__ */ new Map();
8711
- if (pkRidPairs.length === 0) return resultMap;
8713
+ const result = new Array(pkRidPairs.length).fill(null);
8714
+ if (pkRidPairs.length === 0) return result;
8712
8715
  const pageGroupMap = /* @__PURE__ */ new Map();
8713
8716
  for (const pair of pkRidPairs) {
8717
+ if (pair === null) continue;
8714
8718
  const rid = pair.rid;
8715
8719
  const slotIndex = rid % 65536;
8716
8720
  const pageId = Math.floor(rid / 65536);
8717
8721
  if (!pageGroupMap.has(pageId)) {
8718
8722
  pageGroupMap.set(pageId, []);
8719
8723
  }
8720
- pageGroupMap.get(pageId).push({ pk: pair.pk, slotIndex });
8724
+ pageGroupMap.get(pageId).push({ pk: pair.pk, slotIndex, index: pair.index });
8721
8725
  }
8722
- const sortedPageEntries = Array.from(pageGroupMap.entries()).sort((a, b) => a[0] - b[0]);
8723
- await Promise.all(sortedPageEntries.map(async ([pageId, items]) => {
8726
+ await Promise.all(Array.from(pageGroupMap).map(async ([pageId, items]) => {
8724
8727
  const page = await this.pfs.get(pageId, tx);
8725
8728
  if (!this.factory.isDataPage(page)) {
8726
8729
  throw new Error(`Page ${pageId} is not a data page`);
@@ -8729,17 +8732,17 @@ var RowTableEngine = class {
8729
8732
  for (const item of items) {
8730
8733
  const row = manager.getRow(page, item.slotIndex);
8731
8734
  if (this.rowManager.getDeletedFlag(row)) {
8732
- resultMap.set(item.pk, null);
8735
+ result[item.index] = null;
8733
8736
  } else if (this.rowManager.getOverflowFlag(row)) {
8734
8737
  const overflowPageId = bytesToNumber(this.rowManager.getBody(row));
8735
8738
  const body = await this.pfs.getBody(overflowPageId, true, tx);
8736
- resultMap.set(item.pk, body);
8739
+ result[item.index] = body;
8737
8740
  } else {
8738
- resultMap.set(item.pk, this.rowManager.getBody(row));
8741
+ result[item.index] = this.rowManager.getBody(row);
8739
8742
  }
8740
8743
  }
8741
8744
  }));
8742
- return resultMap;
8745
+ return result;
8743
8746
  }
8744
8747
  async fetchRowByRid(pk, rid, tx) {
8745
8748
  this.keyManager.setBufferFromKey(rid, this.ridBuffer);
@@ -75,9 +75,9 @@ export declare class Dataply {
75
75
  * @param tx Transaction
76
76
  * @returns Array of selected data in the same order as input PKs
77
77
  */
78
- selectMany(pks: number[], asRaw: true, tx?: Transaction): Promise<(Uint8Array | null)[]>;
79
- selectMany(pks: number[], asRaw: false, tx?: Transaction): Promise<(string | null)[]>;
80
- selectMany(pks: number[], asRaw?: boolean, tx?: Transaction): Promise<(string | null)[]>;
78
+ selectMany(pks: number[] | Float64Array, asRaw: true, tx?: Transaction): Promise<(Uint8Array | null)[]>;
79
+ selectMany(pks: number[] | Float64Array, asRaw: false, tx?: Transaction): Promise<(string | null)[]>;
80
+ selectMany(pks: number[] | Float64Array, asRaw?: boolean, tx?: Transaction): Promise<(string | null)[]>;
81
81
  /**
82
82
  * Closes the dataply file.
83
83
  */
@@ -164,9 +164,9 @@ export declare class DataplyAPI {
164
164
  * @param tx Transaction
165
165
  * @returns Array of selected data in the same order as input PKs
166
166
  */
167
- selectMany(pks: number[], asRaw: true, tx?: Transaction): Promise<(Uint8Array | null)[]>;
168
- selectMany(pks: number[], asRaw: false, tx?: Transaction): Promise<(string | null)[]>;
169
- selectMany(pks: number[], asRaw?: boolean, tx?: Transaction): Promise<(string | null)[]>;
167
+ selectMany(pks: number[] | Float64Array, asRaw: true, tx?: Transaction): Promise<(Uint8Array | null)[]>;
168
+ selectMany(pks: number[] | Float64Array, asRaw: false, tx?: Transaction): Promise<(string | null)[]>;
169
+ selectMany(pks: number[] | Float64Array, asRaw?: boolean, tx?: Transaction): Promise<(string | null)[]>;
170
170
  /**
171
171
  * Closes the dataply file.
172
172
  */
@@ -130,12 +130,12 @@ export declare class RowTableEngine {
130
130
  * @param tx Transaction
131
131
  * @returns Array of raw data of the rows in the same order as input PKs
132
132
  */
133
- selectMany(pks: number[], tx: Transaction): Promise<(Uint8Array | null)[]>;
133
+ selectMany(pks: number[] | Float64Array, tx: Transaction): Promise<(Uint8Array | null)[]>;
134
134
  /**
135
135
  * Fetches multiple rows by their RID and PK combinations, grouping by page ID to minimize I/O.
136
136
  * @param pkRidPairs Array of {pk, rid} pairs
137
137
  * @param tx Transaction
138
- * @returns Map of PK to row data
138
+ * @returns Array of row data in the same order as input PKs
139
139
  */
140
140
  private fetchRowsByRids;
141
141
  private fetchRowByRid;
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Performs a binary search on a sorted array.
3
+ * @param array Sorted array to search in
4
+ * @param comparator Function that returns 0 if the element matches, < 0 if the target is before, > 0 if the target is after
5
+ * @returns Index of the element if found, -1 otherwise
6
+ */
7
+ export declare function binarySearch<T>(array: ArrayLike<T>, comparator: (element: T) => number): number;
8
+ /**
9
+ * Performs a binary search on a sorted numeric array.
10
+ * @param array Sorted numeric array to search in
11
+ * @param target Numeric value to search for
12
+ * @returns Index of the element if found, -1 otherwise
13
+ */
14
+ export declare function binarySearchNumeric(array: ArrayLike<number>, target: number): number;
@@ -2,3 +2,4 @@ export * from './numberToBytes';
2
2
  export * from './bytesToNumber';
3
3
  export * from './bitwise';
4
4
  export * from './crc32';
5
+ export * from './binarySearch';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dataply",
3
- "version": "0.0.21-alpha.0",
3
+ "version": "0.0.21",
4
4
  "description": "A lightweight storage engine for Node.js with support for MVCC, WAL.",
5
5
  "license": "MIT",
6
6
  "author": "izure <admin@izure.org>",
@@ -51,4 +51,4 @@
51
51
  "ryoiki": "^1.2.0",
52
52
  "serializable-bptree": "^8.1.2"
53
53
  }
54
- }
54
+ }