dataply 0.0.3 → 0.0.5

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
@@ -46,6 +46,7 @@ __export(src_exports, {
46
46
  SerializeStrategyAsync: () => SerializeStrategyAsync,
47
47
  SerializeStrategySync: () => SerializeStrategySync,
48
48
  StringComparator: () => StringComparator,
49
+ Transaction: () => Transaction,
49
50
  ValueComparator: () => ValueComparator
50
51
  });
51
52
  module.exports = __toCommonJS(src_exports);
@@ -781,6 +782,9 @@ var BPTreeSync = class extends BPTree {
781
782
  guess = prevValue;
782
783
  }
783
784
  }
785
+ if (!pointer) {
786
+ return;
787
+ }
784
788
  if (node.values.length + pointer.values.length < this.order) {
785
789
  if (!isPredecessor) {
786
790
  const pTemp = pointer;
@@ -920,45 +924,66 @@ var BPTreeSync = class extends BPTree {
920
924
  this.strategy.head.root = root.id;
921
925
  node.parent = root.id;
922
926
  pointer.parent = root.id;
927
+ if (pointer.leaf) {
928
+ node.next = pointer.id;
929
+ pointer.prev = node.id;
930
+ }
923
931
  this.bufferForNodeUpdate(node);
924
932
  this.bufferForNodeUpdate(pointer);
925
933
  return;
926
934
  }
927
935
  const parentNode = this.getNode(node.parent);
928
- for (let i = 0, len = parentNode.keys.length; i < len; i++) {
929
- const nKeys = parentNode.keys[i];
930
- if (nKeys === node.id) {
931
- parentNode.values.splice(i, 0, value);
932
- parentNode.keys.splice(i + 1, 0, pointer.id);
933
- this.bufferForNodeUpdate(parentNode);
934
- if (parentNode.keys.length > this.order) {
935
- const parentPointer = this._createNode(false, [], []);
936
- parentPointer.parent = parentNode.parent;
937
- const mid = Math.ceil(this.order / 2) - 1;
938
- parentPointer.values = parentNode.values.slice(mid + 1);
939
- parentPointer.keys = parentNode.keys.slice(mid + 1);
940
- const midValue = parentNode.values[mid];
941
- if (mid === 0) {
942
- parentNode.values = parentNode.values.slice(0, mid + 1);
943
- } else {
944
- parentNode.values = parentNode.values.slice(0, mid);
945
- }
946
- parentNode.keys = parentNode.keys.slice(0, mid + 1);
947
- for (const k of parentNode.keys) {
948
- const node2 = this.getNode(k);
949
- node2.parent = parentNode.id;
950
- this.bufferForNodeUpdate(node2);
951
- }
952
- for (const k of parentPointer.keys) {
953
- const node2 = this.getNode(k);
954
- node2.parent = parentPointer.id;
955
- this.bufferForNodeUpdate(node2);
956
- }
957
- this._insertInParent(parentNode, midValue, parentPointer);
958
- this.bufferForNodeUpdate(parentNode);
959
- }
936
+ let insertIndex = 0;
937
+ for (let i = 0; i < parentNode.values.length; i++) {
938
+ if (this.comparator.asc(value, parentNode.values[i]) > 0) {
939
+ insertIndex = i + 1;
940
+ } else {
941
+ break;
942
+ }
943
+ }
944
+ parentNode.values.splice(insertIndex, 0, value);
945
+ parentNode.keys.splice(insertIndex + 1, 0, pointer.id);
946
+ pointer.parent = parentNode.id;
947
+ if (pointer.leaf) {
948
+ const leftSiblingId = parentNode.keys[insertIndex];
949
+ const rightSiblingId = parentNode.keys[insertIndex + 2];
950
+ if (leftSiblingId) {
951
+ const leftSibling = this.getNode(leftSiblingId);
952
+ pointer.prev = leftSibling.id;
953
+ pointer.next = leftSibling.next;
954
+ leftSibling.next = pointer.id;
955
+ this.bufferForNodeUpdate(leftSibling);
956
+ }
957
+ if (rightSiblingId) {
958
+ const rightSibling = this.getNode(rightSiblingId);
959
+ rightSibling.prev = pointer.id;
960
+ this.bufferForNodeUpdate(rightSibling);
960
961
  }
961
962
  }
963
+ this.bufferForNodeUpdate(parentNode);
964
+ this.bufferForNodeUpdate(pointer);
965
+ if (parentNode.keys.length > this.order) {
966
+ const parentPointer = this._createNode(false, [], []);
967
+ parentPointer.parent = parentNode.parent;
968
+ const mid = Math.ceil(this.order / 2) - 1;
969
+ parentPointer.values = parentNode.values.slice(mid + 1);
970
+ parentPointer.keys = parentNode.keys.slice(mid + 1);
971
+ const midValue = parentNode.values[mid];
972
+ parentNode.values = parentNode.values.slice(0, mid);
973
+ parentNode.keys = parentNode.keys.slice(0, mid + 1);
974
+ for (const k of parentNode.keys) {
975
+ const node2 = this.getNode(k);
976
+ node2.parent = parentNode.id;
977
+ this.bufferForNodeUpdate(node2);
978
+ }
979
+ for (const k of parentPointer.keys) {
980
+ const node2 = this.getNode(k);
981
+ node2.parent = parentPointer.id;
982
+ this.bufferForNodeUpdate(node2);
983
+ }
984
+ this._insertInParent(parentNode, midValue, parentPointer);
985
+ this.bufferForNodeUpdate(parentNode);
986
+ }
962
987
  }
963
988
  init() {
964
989
  const head = this.strategy.readHead();
@@ -979,6 +1004,9 @@ var BPTreeSync = class extends BPTree {
979
1004
  }
980
1005
  }
981
1006
  getNode(id) {
1007
+ if (this._nodeUpdateBuffer.has(id)) {
1008
+ return this._nodeUpdateBuffer.get(id);
1009
+ }
982
1010
  if (this._nodeCreateBuffer.has(id)) {
983
1011
  return this._nodeCreateBuffer.get(id);
984
1012
  }
@@ -986,7 +1014,7 @@ var BPTreeSync = class extends BPTree {
986
1014
  return cache.raw;
987
1015
  }
988
1016
  insertableNode(value) {
989
- let node = this.root;
1017
+ let node = this.getNode(this.root.id);
990
1018
  while (!node.leaf) {
991
1019
  for (let i = 0, len = node.values.length; i < len; i++) {
992
1020
  const nValue = node.values[i];
@@ -1123,21 +1151,14 @@ var BPTreeSync = class extends BPTree {
1123
1151
  [],
1124
1152
  true,
1125
1153
  before.parent,
1126
- before.next,
1127
- before.id
1154
+ null,
1155
+ null
1128
1156
  );
1129
1157
  const mid = Math.ceil(this.order / 2) - 1;
1130
- const beforeNext = before.next;
1131
1158
  after.values = before.values.slice(mid + 1);
1132
1159
  after.keys = before.keys.slice(mid + 1);
1133
1160
  before.values = before.values.slice(0, mid + 1);
1134
1161
  before.keys = before.keys.slice(0, mid + 1);
1135
- before.next = after.id;
1136
- if (beforeNext) {
1137
- const node = this.getNode(beforeNext);
1138
- node.prev = after.id;
1139
- this.bufferForNodeUpdate(node);
1140
- }
1141
1162
  this._insertInParent(before, after.values[0], after);
1142
1163
  this.bufferForNodeUpdate(before);
1143
1164
  }
@@ -1647,6 +1668,9 @@ var BPTreeAsync = class extends BPTree {
1647
1668
  guess = prevValue;
1648
1669
  }
1649
1670
  }
1671
+ if (!pointer) {
1672
+ return;
1673
+ }
1650
1674
  if (node.values.length + pointer.values.length < this.order) {
1651
1675
  if (!isPredecessor) {
1652
1676
  const pTemp = pointer;
@@ -1786,45 +1810,66 @@ var BPTreeAsync = class extends BPTree {
1786
1810
  this.strategy.head.root = root.id;
1787
1811
  node.parent = root.id;
1788
1812
  pointer.parent = root.id;
1813
+ if (pointer.leaf) {
1814
+ node.next = pointer.id;
1815
+ pointer.prev = node.id;
1816
+ }
1789
1817
  this.bufferForNodeUpdate(node);
1790
1818
  this.bufferForNodeUpdate(pointer);
1791
1819
  return;
1792
1820
  }
1793
1821
  const parentNode = await this.getNode(node.parent);
1794
- for (let i = 0, len = parentNode.keys.length; i < len; i++) {
1795
- const nKeys = parentNode.keys[i];
1796
- if (nKeys === node.id) {
1797
- parentNode.values.splice(i, 0, value);
1798
- parentNode.keys.splice(i + 1, 0, pointer.id);
1799
- this.bufferForNodeUpdate(parentNode);
1800
- if (parentNode.keys.length > this.order) {
1801
- const parentPointer = await this._createNode(false, [], []);
1802
- parentPointer.parent = parentNode.parent;
1803
- const mid = Math.ceil(this.order / 2) - 1;
1804
- parentPointer.values = parentNode.values.slice(mid + 1);
1805
- parentPointer.keys = parentNode.keys.slice(mid + 1);
1806
- const midValue = parentNode.values[mid];
1807
- if (mid === 0) {
1808
- parentNode.values = parentNode.values.slice(0, mid + 1);
1809
- } else {
1810
- parentNode.values = parentNode.values.slice(0, mid);
1811
- }
1812
- parentNode.keys = parentNode.keys.slice(0, mid + 1);
1813
- for (const k of parentNode.keys) {
1814
- const node2 = await this.getNode(k);
1815
- node2.parent = parentNode.id;
1816
- this.bufferForNodeUpdate(node2);
1817
- }
1818
- for (const k of parentPointer.keys) {
1819
- const node2 = await this.getNode(k);
1820
- node2.parent = parentPointer.id;
1821
- this.bufferForNodeUpdate(node2);
1822
- }
1823
- await this._insertInParent(parentNode, midValue, parentPointer);
1824
- this.bufferForNodeUpdate(parentNode);
1825
- }
1822
+ let insertIndex = 0;
1823
+ for (let i = 0; i < parentNode.values.length; i++) {
1824
+ if (this.comparator.asc(value, parentNode.values[i]) > 0) {
1825
+ insertIndex = i + 1;
1826
+ } else {
1827
+ break;
1828
+ }
1829
+ }
1830
+ parentNode.values.splice(insertIndex, 0, value);
1831
+ parentNode.keys.splice(insertIndex + 1, 0, pointer.id);
1832
+ pointer.parent = parentNode.id;
1833
+ if (pointer.leaf) {
1834
+ const leftSiblingId = parentNode.keys[insertIndex];
1835
+ const rightSiblingId = parentNode.keys[insertIndex + 2];
1836
+ if (leftSiblingId) {
1837
+ const leftSibling = await this.getNode(leftSiblingId);
1838
+ pointer.prev = leftSibling.id;
1839
+ pointer.next = leftSibling.next;
1840
+ leftSibling.next = pointer.id;
1841
+ this.bufferForNodeUpdate(leftSibling);
1842
+ }
1843
+ if (rightSiblingId) {
1844
+ const rightSibling = await this.getNode(rightSiblingId);
1845
+ rightSibling.prev = pointer.id;
1846
+ this.bufferForNodeUpdate(rightSibling);
1826
1847
  }
1827
1848
  }
1849
+ this.bufferForNodeUpdate(parentNode);
1850
+ this.bufferForNodeUpdate(pointer);
1851
+ if (parentNode.keys.length > this.order) {
1852
+ const parentPointer = await this._createNode(false, [], []);
1853
+ parentPointer.parent = parentNode.parent;
1854
+ const mid = Math.ceil(this.order / 2) - 1;
1855
+ parentPointer.values = parentNode.values.slice(mid + 1);
1856
+ parentPointer.keys = parentNode.keys.slice(mid + 1);
1857
+ const midValue = parentNode.values[mid];
1858
+ parentNode.values = parentNode.values.slice(0, mid);
1859
+ parentNode.keys = parentNode.keys.slice(0, mid + 1);
1860
+ for (const k of parentNode.keys) {
1861
+ const node2 = await this.getNode(k);
1862
+ node2.parent = parentNode.id;
1863
+ this.bufferForNodeUpdate(node2);
1864
+ }
1865
+ for (const k of parentPointer.keys) {
1866
+ const node2 = await this.getNode(k);
1867
+ node2.parent = parentPointer.id;
1868
+ this.bufferForNodeUpdate(node2);
1869
+ }
1870
+ await this._insertInParent(parentNode, midValue, parentPointer);
1871
+ this.bufferForNodeUpdate(parentNode);
1872
+ }
1828
1873
  }
1829
1874
  async init() {
1830
1875
  const head = await this.strategy.readHead();
@@ -1845,6 +1890,9 @@ var BPTreeAsync = class extends BPTree {
1845
1890
  }
1846
1891
  }
1847
1892
  async getNode(id) {
1893
+ if (this._nodeUpdateBuffer.has(id)) {
1894
+ return this._nodeUpdateBuffer.get(id);
1895
+ }
1848
1896
  if (this._nodeCreateBuffer.has(id)) {
1849
1897
  return this._nodeCreateBuffer.get(id);
1850
1898
  }
@@ -1852,7 +1900,7 @@ var BPTreeAsync = class extends BPTree {
1852
1900
  return cache.raw;
1853
1901
  }
1854
1902
  async insertableNode(value) {
1855
- let node = this.root;
1903
+ let node = await this.getNode(this.root.id);
1856
1904
  while (!node.leaf) {
1857
1905
  for (let i = 0, len = node.values.length; i < len; i++) {
1858
1906
  const nValue = node.values[i];
@@ -1994,21 +2042,14 @@ var BPTreeAsync = class extends BPTree {
1994
2042
  [],
1995
2043
  true,
1996
2044
  before.parent,
1997
- before.next,
1998
- before.id
2045
+ null,
2046
+ null
1999
2047
  );
2000
2048
  const mid = Math.ceil(this.order / 2) - 1;
2001
- const beforeNext = before.next;
2002
2049
  after.values = before.values.slice(mid + 1);
2003
2050
  after.keys = before.keys.slice(mid + 1);
2004
2051
  before.values = before.values.slice(0, mid + 1);
2005
2052
  before.keys = before.keys.slice(0, mid + 1);
2006
- before.next = after.id;
2007
- if (beforeNext) {
2008
- const node = await this.getNode(beforeNext);
2009
- node.prev = after.id;
2010
- this.bufferForNodeUpdate(node);
2011
- }
2012
2053
  await this._insertInParent(before, after.values[0], after);
2013
2054
  this.bufferForNodeUpdate(before);
2014
2055
  }
@@ -5289,11 +5330,8 @@ var PageFileSystem = class {
5289
5330
  const chunk = data.subarray(dataOffset, dataOffset + writeSize);
5290
5331
  page.set(chunk, bodyStart + currentOffset);
5291
5332
  const currentUsedSize = currentOffset + writeSize;
5292
- const currentRemaining = manager.getRemainingCapacity(page);
5293
5333
  const newRemaining = bodySize - currentUsedSize;
5294
- if (newRemaining < currentRemaining) {
5295
- manager.setRemainingCapacity(page, newRemaining);
5296
- }
5334
+ manager.setRemainingCapacity(page, newRemaining);
5297
5335
  await this.setPage(currentPageId, page, tx);
5298
5336
  dataOffset += writeSize;
5299
5337
  currentOffset = 0;
@@ -5307,6 +5345,20 @@ var PageFileSystem = class {
5307
5345
  } else {
5308
5346
  currentPageId = nextPageId;
5309
5347
  }
5348
+ } else {
5349
+ let nextPageId = manager.getNextPageId(page);
5350
+ if (nextPageId !== -1) {
5351
+ let pendingFreePageId = nextPageId;
5352
+ while (pendingFreePageId !== -1) {
5353
+ const pendingPage = await this.get(pendingFreePageId, tx);
5354
+ const pendingManager = this.pageFactory.getManager(pendingPage);
5355
+ const next = pendingManager.getNextPageId(pendingPage);
5356
+ await this.setFreePage(pendingFreePageId, tx);
5357
+ pendingFreePageId = next;
5358
+ }
5359
+ manager.setNextPageId(page, -1);
5360
+ await this.setPage(currentPageId, page, tx);
5361
+ }
5310
5362
  }
5311
5363
  }
5312
5364
  }
@@ -5408,7 +5460,7 @@ var RowIdentifierStrategy = class extends SerializeStrategyAsync {
5408
5460
  const values = this.indexPageManger.getValues(page);
5409
5461
  let res;
5410
5462
  if (isLeaf) res = {
5411
- leaf: isLeaf,
5463
+ leaf: true,
5412
5464
  id: indexId + "",
5413
5465
  parent: parentIndexId ? parentIndexId + "" : null,
5414
5466
  next: nextIndexId ? nextIndexId + "" : null,
@@ -5417,7 +5469,7 @@ var RowIdentifierStrategy = class extends SerializeStrategyAsync {
5417
5469
  values
5418
5470
  };
5419
5471
  else res = {
5420
- leaf: isLeaf,
5472
+ leaf: false,
5421
5473
  id: indexId + "",
5422
5474
  parent: parentIndexId ? parentIndexId + "" : null,
5423
5475
  next: nextIndexId ? nextIndexId + "" : null,
@@ -5464,7 +5516,7 @@ var RowIdentifierStrategy = class extends SerializeStrategyAsync {
5464
5516
  }
5465
5517
  async delete(id) {
5466
5518
  const tx = TxContext.get();
5467
- const manager = this.factory.getManagerFromType(PageManager.CONSTANT.PAGE_TYPE_EMPTY);
5519
+ const manager = this.factory.getManagerFromType(PageManager.CONSTANT.PAGE_TYPE_INDEX);
5468
5520
  let pageId = +id;
5469
5521
  while (true) {
5470
5522
  const page = await this.pfs.get(pageId, tx);
@@ -6606,5 +6658,6 @@ var GlobalTransaction = class {
6606
6658
  SerializeStrategyAsync,
6607
6659
  SerializeStrategySync,
6608
6660
  StringComparator,
6661
+ Transaction,
6609
6662
  ValueComparator
6610
6663
  });
@@ -0,0 +1,6 @@
1
+ import { BPTreeAsync } from 'serializable-bptree';
2
+ export declare class RowIndexBPTree<K, V, C> extends BPTreeAsync<K, V> {
3
+ private _bindContext?;
4
+ bindContext(context?: C): void;
5
+ getContext(): C | undefined;
6
+ }
@@ -4,4 +4,5 @@ export * from 'cache-entanglement';
4
4
  export type { DataplyOptions } from './types';
5
5
  export { Dataply } from './core/Dataply';
6
6
  export { DataplyAPI } from './core/DataplyAPI';
7
+ export { Transaction } from './core/transaction/Transaction';
7
8
  export { GlobalTransaction } from './core/transaction/GlobalTransaction';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dataply",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
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>",
@@ -47,6 +47,6 @@
47
47
  "cache-entanglement": "^1.7.1",
48
48
  "hookall": "^2.2.0",
49
49
  "ryoiki": "^1.2.0",
50
- "serializable-bptree": "^5.2.1"
50
+ "serializable-bptree": "^6.0.2"
51
51
  }
52
52
  }