dataply 0.0.21-alpha.0 → 0.0.21-alpha.1

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
@@ -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,45 @@ 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
+ const pk = pks[i];
8692
+ pkIndexMap.set(pk, i);
8693
+ }
8689
8694
  const minPk = Math.min(...pks);
8690
8695
  const maxPk = Math.max(...pks);
8691
- const pkSet = new Set(pks);
8692
- const pkRidPairs = [];
8696
+ const pkRidPairs = new Array(pks.length).fill(null);
8693
8697
  const btx = await this.getBPTreeTransaction(tx);
8694
8698
  const stream = btx.whereStream({ gte: minPk, lte: maxPk });
8695
8699
  for await (const [rid, pk] of stream) {
8696
- if (pkSet.has(pk)) {
8697
- pkRidPairs.push({ pk, rid });
8700
+ const index = pkIndexMap.get(pk);
8701
+ if (index !== void 0) {
8702
+ pkRidPairs[index] = { pk, rid, index };
8698
8703
  }
8699
8704
  }
8700
- const resultMap = await this.fetchRowsByRids(pkRidPairs, tx);
8701
- return pks.map((pk) => resultMap.get(pk) ?? null);
8705
+ return this.fetchRowsByRids(pkRidPairs, tx);
8702
8706
  }
8703
8707
  /**
8704
8708
  * Fetches multiple rows by their RID and PK combinations, grouping by page ID to minimize I/O.
8705
8709
  * @param pkRidPairs Array of {pk, rid} pairs
8706
8710
  * @param tx Transaction
8707
- * @returns Map of PK to row data
8711
+ * @returns Array of row data in the same order as input PKs
8708
8712
  */
8709
8713
  async fetchRowsByRids(pkRidPairs, tx) {
8710
- const resultMap = /* @__PURE__ */ new Map();
8711
- if (pkRidPairs.length === 0) return resultMap;
8714
+ const result = new Array(pkRidPairs.length).fill(null);
8715
+ if (pkRidPairs.length === 0) return result;
8712
8716
  const pageGroupMap = /* @__PURE__ */ new Map();
8713
8717
  for (const pair of pkRidPairs) {
8718
+ if (pair === null) continue;
8714
8719
  const rid = pair.rid;
8715
8720
  const slotIndex = rid % 65536;
8716
8721
  const pageId = Math.floor(rid / 65536);
8717
8722
  if (!pageGroupMap.has(pageId)) {
8718
8723
  pageGroupMap.set(pageId, []);
8719
8724
  }
8720
- pageGroupMap.get(pageId).push({ pk: pair.pk, slotIndex });
8725
+ pageGroupMap.get(pageId).push({ pk: pair.pk, slotIndex, index: pair.index });
8721
8726
  }
8722
- const sortedPageEntries = Array.from(pageGroupMap.entries()).sort((a, b) => a[0] - b[0]);
8723
- await Promise.all(sortedPageEntries.map(async ([pageId, items]) => {
8727
+ await Promise.all(Array.from(pageGroupMap).map(async ([pageId, items]) => {
8724
8728
  const page = await this.pfs.get(pageId, tx);
8725
8729
  if (!this.factory.isDataPage(page)) {
8726
8730
  throw new Error(`Page ${pageId} is not a data page`);
@@ -8729,17 +8733,17 @@ var RowTableEngine = class {
8729
8733
  for (const item of items) {
8730
8734
  const row = manager.getRow(page, item.slotIndex);
8731
8735
  if (this.rowManager.getDeletedFlag(row)) {
8732
- resultMap.set(item.pk, null);
8736
+ result[item.index] = null;
8733
8737
  } else if (this.rowManager.getOverflowFlag(row)) {
8734
8738
  const overflowPageId = bytesToNumber(this.rowManager.getBody(row));
8735
8739
  const body = await this.pfs.getBody(overflowPageId, true, tx);
8736
- resultMap.set(item.pk, body);
8740
+ result[item.index] = body;
8737
8741
  } else {
8738
- resultMap.set(item.pk, this.rowManager.getBody(row));
8742
+ result[item.index] = this.rowManager.getBody(row);
8739
8743
  }
8740
8744
  }
8741
8745
  }));
8742
- return resultMap;
8746
+ return result;
8743
8747
  }
8744
8748
  async fetchRowByRid(pk, rid, tx) {
8745
8749
  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-alpha.1",
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
+ }