@thi.ng/column-store 0.2.0 → 0.3.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.
package/README.md CHANGED
@@ -63,7 +63,7 @@ delegates them to the columns.
63
63
  An example table definition looks like this (explanation of column types in next
64
64
  section below):
65
65
 
66
- ```ts
66
+ ```ts tangle:export/readme-types.ts
67
67
  import { Table, FLAG_DICT, FLAG_UNIQUE } from "@thi.ng/column-store";
68
68
 
69
69
  // define a table with the given columns
@@ -81,7 +81,7 @@ const table = new Table({
81
81
  aliases: { type: "str", cardinality: [0, 3] },
82
82
 
83
83
  // required fixed size tuples (aka vectors) of numbers
84
- latlon: { type: "num", cardinality: [2, 2] },
84
+ latlon: { type: "f32vec", cardinality: [2, 2] },
85
85
 
86
86
  // optional tuples of max. 10 strings, with default
87
87
  // the given flags (explained further below) are triggering:
@@ -105,6 +105,11 @@ table.addRow({
105
105
  });
106
106
  ```
107
107
 
108
+ > [!IMPORTANT]
109
+ > Columns can be named freely, with the exception that the `__` name prefix is
110
+ > reserved for internal use. For example, `foo` is allowed, but `__foo` is a
111
+ > reserved name.
112
+
108
113
  ### Column types
109
114
 
110
115
  The current built-in column types only support numeric or string values, though
@@ -149,12 +154,13 @@ configs are: `[0,3]` (with default given) or `[3,3]`.
149
154
  When [querying](#query-engine) vector columns using the standard
150
155
  `(n)or`/`(n)and` operators, always the entire vector is matched (by value).
151
156
 
152
- > [!IMPORTANT] For performance reasons, rows retrieved from vector columns
153
- > contain mutable data views of the underlying column storage. That means when
154
- > manipulating data in these views, the underlying data in the column would be
155
- > changed too. To avoid index corruption, always edit only copies of this vector
156
- > data and then use `table.updateRow()` to properly update the column storage
157
- > (incl. any internal indexes).
157
+ > [!IMPORTANT]
158
+ > For performance reasons, rows retrieved from vector columns contain mutable
159
+ > data views of the underlying column storage. That means when manipulating data
160
+ > in these views, the underlying data in the column would be changed too. To
161
+ > avoid index corruption, always edit only copies of this vector data and then
162
+ > use `table.updateRow()` to properly update the column storage (incl. any
163
+ > internal indexes).
158
164
 
159
165
  ### Serialization options
160
166
 
@@ -310,8 +316,8 @@ TODO see code examples below
310
316
 
311
317
  The query engine works by applying a number of [query
312
318
  terms](https://docs.thi.ng/umbrella/column-store/interfaces/QueryTerm.html) in
313
- series, with each step intersecting its results with the results of the previous
314
- step(s), thereby narrowing down the result set.
319
+ series, with each step intersecting (aka logical AND) its results with the
320
+ results of the previous step(s), thereby narrowing down the result set.
315
321
 
316
322
  By default, individual query terms operate on a single column, but can also can
317
323
  also apply to multiple. Terms are supplied either as array given to the
@@ -406,7 +412,7 @@ For Node.js REPL:
406
412
  const cs = await import("@thi.ng/column-store");
407
413
  ```
408
414
 
409
- Package sizes (brotli'd, pre-treeshake): ESM: 4.65 KB
415
+ Package sizes (brotli'd, pre-treeshake): ESM: 4.67 KB
410
416
 
411
417
  ## Dependencies
412
418
 
@@ -465,19 +471,20 @@ table.addRows([
465
471
  const unsortedImages = table.query().where("type", "img").and("tags", "unsorted");
466
472
 
467
473
  // queries are iterables and only execute when the iterator is consumed
474
+ // each query result includes a `__row` ID
468
475
  console.log([...unsortedImages]);
469
- // [ { id: 102, type: "img", tags: [ "unsorted" ] } ]
476
+ // [ { id: 102, type: "img", tags: [ "unsorted" ], __row: 2 } ]
470
477
 
471
478
  // select items with `a` OR `b` tags, intersect with those which have `c` AND `d` tags
472
479
  const complexTagQuery = table.query().or("tags", ["a", "b"]).and("tags", ["c", "d"]);
473
480
  console.log([...complexTagQuery]);
474
- // [ { id: 104, type: "img", tags: [ "b", "c", "d" ] } ]
481
+ // [ { id: 104, type: "img", tags: [ "b", "c", "d" ], __row: 4 } ]
475
482
 
476
483
  // query using custom predicates
477
484
  console.log([...table.query().matchColumn("id", (id) => id > 102)]);
478
485
  // [
479
- // { id: 103, type: "img", tags: [ "unsorted" ] },
480
- // { id: 104, type: "img", tags: [ "b", "c", "d" ] }
486
+ // { id: 103, type: "img", tags: [ "unsorted" ], __row: 3 },
487
+ // { id: 104, type: "img", tags: [ "b", "c", "d" ], __row: 4 }
481
488
  // ]
482
489
 
483
490
  // serialize table to JSON
@@ -9,7 +9,7 @@ export declare class TypedArrayColumn extends AColumn implements IColumn {
9
9
  protected tmp: TypedArray;
10
10
  readonly isArray = false;
11
11
  constructor(id: string, table: Table);
12
- load(spec: SerializedColumn): void;
12
+ load({ values }: SerializedColumn): void;
13
13
  validate(value: any): boolean;
14
14
  setRow(i: number, value: any): void;
15
15
  getRow(i: number): number;
@@ -18,7 +18,7 @@ export declare class TypedArrayColumn extends AColumn implements IColumn {
18
18
  removeRow(i: number): void;
19
19
  replaceValue(currValue: any, newValue: any): boolean;
20
20
  toJSON(): {
21
- values: number[];
21
+ values: any[];
22
22
  };
23
23
  }
24
24
  //# sourceMappingURL=typedarray.d.ts.map
@@ -1,13 +1,12 @@
1
- import { SIZEOF, typedArray } from "@thi.ng/api/typedarray";
1
+ import { typedArray } from "@thi.ng/api/typedarray";
2
+ import { isArray } from "@thi.ng/checks/is-array";
2
3
  import { isNumber } from "@thi.ng/checks/is-number";
3
- import { decodeBinary, encodeBinary } from "@thi.ng/rle-pack/binary";
4
4
  import {
5
- FLAG_RLE,
6
5
  LIMITS
7
6
  } from "../api.js";
8
7
  import { __replaceValue } from "../internal/replace.js";
8
+ import { __deserializeTyped, __serializeTyped } from "../internal/serialize.js";
9
9
  import { AColumn } from "./acolumn.js";
10
- import { isArray } from "@thi.ng/checks/is-array";
11
10
  class TypedArrayColumn extends AColumn {
12
11
  values;
13
12
  type;
@@ -21,13 +20,8 @@ class TypedArrayColumn extends AColumn {
21
20
  this.values = typedArray(this.type, 8);
22
21
  this.tmp = typedArray(this.type, 1);
23
22
  }
24
- load(spec) {
25
- if (this.spec.flags & FLAG_RLE) {
26
- const values = decodeBinary(spec.values);
27
- this.values = typedArray(this.type, values.buffer);
28
- } else {
29
- this.values = typedArray(this.type, spec.values);
30
- }
23
+ load({ values }) {
24
+ this.values = __deserializeTyped(this.type, this.spec.flags, values);
31
25
  this.reindex();
32
26
  }
33
27
  validate(value) {
@@ -77,11 +71,11 @@ class TypedArrayColumn extends AColumn {
77
71
  return __replaceValue(this.bitmap, this.values, currValue, newValue);
78
72
  }
79
73
  toJSON() {
80
- let values = this.values.subarray(0, this.table.length);
81
- if (this.spec.flags & FLAG_RLE) {
82
- values = encodeBinary(values, values.length, SIZEOF[this.type] * 8);
83
- }
84
- return { values: Array.from(values) };
74
+ return __serializeTyped(
75
+ this.values.subarray(0, this.table.length),
76
+ this.spec,
77
+ this.type
78
+ );
85
79
  }
86
80
  }
87
81
  export {
@@ -10,10 +10,10 @@ export declare class VectorColumn extends AColumn implements IColumn {
10
10
  protected tmp: TypedArray;
11
11
  readonly isArray = false;
12
12
  constructor(id: string, table: Table);
13
- load(spec: SerializedColumn): void;
13
+ load({ values }: SerializedColumn): void;
14
14
  validate(value: any): boolean;
15
15
  setRow(i: number, value: any): void;
16
- getRow(i: number): Uint8Array<ArrayBufferLike> | Float32Array<ArrayBufferLike> | Float64Array<ArrayBufferLike> | Int8Array<ArrayBufferLike> | Int16Array<ArrayBufferLike> | Int32Array<ArrayBufferLike> | Uint8ClampedArray<ArrayBufferLike> | Uint16Array<ArrayBufferLike> | Uint32Array<ArrayBufferLike>;
16
+ getRow(i: number): Float32Array<ArrayBufferLike> | Float64Array<ArrayBufferLike> | Int8Array<ArrayBufferLike> | Int16Array<ArrayBufferLike> | Int32Array<ArrayBufferLike> | Uint8Array<ArrayBufferLike> | Uint8ClampedArray<ArrayBufferLike> | Uint16Array<ArrayBufferLike> | Uint32Array<ArrayBufferLike>;
17
17
  getRowKey(i: number): string;
18
18
  valueKey(value: any): string | string[];
19
19
  removeRow(i: number): void;
package/columns/vector.js CHANGED
@@ -1,14 +1,13 @@
1
- import { SIZEOF, typedArray } from "@thi.ng/api/typedarray";
1
+ import { typedArray } from "@thi.ng/api/typedarray";
2
+ import { isArray } from "@thi.ng/checks/is-array";
2
3
  import { isArrayLike } from "@thi.ng/checks/is-arraylike";
3
4
  import { isNumber } from "@thi.ng/checks/is-number";
4
5
  import { unsupportedOp } from "@thi.ng/errors/unsupported";
5
- import { decodeBinary, encodeBinary } from "@thi.ng/rle-pack/binary";
6
6
  import {
7
- FLAG_RLE,
8
7
  LIMITS
9
8
  } from "../api.js";
9
+ import { __deserializeTyped, __serializeTyped } from "../internal/serialize.js";
10
10
  import { AColumn } from "./acolumn.js";
11
- import { isArray } from "@thi.ng/checks/is-array";
12
11
  class VectorColumn extends AColumn {
13
12
  values;
14
13
  type;
@@ -24,13 +23,8 @@ class VectorColumn extends AColumn {
24
23
  this.values = typedArray(this.type, 8 * this.size);
25
24
  this.tmp = typedArray(this.type, this.size);
26
25
  }
27
- load(spec) {
28
- if (this.spec.flags & FLAG_RLE) {
29
- const values = decodeBinary(spec.values);
30
- this.values = typedArray(this.type, values.buffer);
31
- } else {
32
- this.values = typedArray(this.type, spec.values);
33
- }
26
+ load({ values }) {
27
+ this.values = __deserializeTyped(this.type, this.spec.flags, values);
34
28
  this.reindex();
35
29
  }
36
30
  validate(value) {
@@ -86,18 +80,11 @@ class VectorColumn extends AColumn {
86
80
  unsupportedOp("TODO");
87
81
  }
88
82
  toJSON() {
89
- let $values = this.values.subarray(0, this.table.length * this.size);
90
- if (this.spec.flags & FLAG_RLE) {
91
- $values = encodeBinary(
92
- $values,
93
- $values.length,
94
- SIZEOF[this.type] * 8
95
- );
96
- }
97
- let values = Array.from($values);
98
- const prec = this.spec.opts?.prec;
99
- if (prec != null) values = values.map((x) => +x.toFixed(prec));
100
- return { values };
83
+ return __serializeTyped(
84
+ this.values.subarray(0, this.table.length * this.size),
85
+ this.spec,
86
+ this.type
87
+ );
101
88
  }
102
89
  }
103
90
  export {
@@ -1,7 +1,15 @@
1
+ import { type NumericArray, type Type } from "@thi.ng/api/typedarray";
1
2
  import type { BidirIndex } from "@thi.ng/bidir-index";
3
+ import { type ColumnSpec } from "../api.js";
2
4
  /** @internal */
3
5
  export declare const __serializeDict: (dict: BidirIndex<any>) => {
4
6
  index: any;
5
7
  next: number;
6
8
  };
9
+ /** @internal */
10
+ export declare const __serializeTyped: ($values: NumericArray, spec: ColumnSpec, type: Type) => {
11
+ values: any[];
12
+ };
13
+ /** @internal */
14
+ export declare const __deserializeTyped: (type: Type, flags: number, values: number[]) => Float32Array<ArrayBufferLike> | Float64Array<ArrayBufferLike> | Int8Array<ArrayBufferLike> | Int16Array<ArrayBufferLike> | Int32Array<ArrayBufferLike> | Uint8Array<ArrayBufferLike> | Uint8ClampedArray<ArrayBufferLike> | Uint16Array<ArrayBufferLike> | Uint32Array<ArrayBufferLike>;
7
15
  //# sourceMappingURL=serialize.d.ts.map
@@ -1,8 +1,27 @@
1
+ import {
2
+ SIZEOF,
3
+ typedArray
4
+ } from "@thi.ng/api/typedarray";
5
+ import { decodeBinary, encodeBinary } from "@thi.ng/rle-pack/binary";
6
+ import { decodeSimple, encodeSimple } from "@thi.ng/rle-pack/simple";
7
+ import { FLAG_RLE } from "../api.js";
1
8
  const __serializeDict = (dict) => {
2
9
  const res = [];
3
10
  for (let [val, id] of dict.entries()) res[id] = val;
4
11
  return { index: res, next: dict.nextID };
5
12
  };
13
+ const __serializeTyped = ($values, spec, type) => {
14
+ if (spec.flags & FLAG_RLE) {
15
+ $values = type[0] === "f" ? encodeSimple($values) : encodeBinary($values, $values.length, SIZEOF[type] * 8);
16
+ }
17
+ let values = Array.from($values);
18
+ const prec = spec.opts?.prec;
19
+ if (prec != null) values = values.map((x) => +x.toFixed(prec));
20
+ return { values };
21
+ };
22
+ const __deserializeTyped = (type, flags, values) => flags & FLAG_RLE ? type[0] === "f" ? typedArray(type, decodeSimple(values)) : typedArray(type, decodeBinary(values).buffer) : typedArray(type, values);
6
23
  export {
7
- __serializeDict
24
+ __deserializeTyped,
25
+ __serializeDict,
26
+ __serializeTyped
8
27
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/column-store",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "In-memory column store database with customizable column types, extensible query engine, bitfield indexing for query acceleration, JSON serialization with optional RLE compression",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -126,5 +126,5 @@
126
126
  "status": "alpha",
127
127
  "year": 2025
128
128
  },
129
- "gitHead": "b90a2f41eb0b3c89391bbb7cfff940192f23a83c\n"
129
+ "gitHead": "e3f0a8a9868bb51ae0c0c4882763f68ec4083bc3\n"
130
130
  }
package/query.js CHANGED
@@ -67,7 +67,7 @@ class Query {
67
67
  }
68
68
  if (ctx.bitmap) {
69
69
  for (let i of new Bitfield(ctx.bitmap).ones(table.length))
70
- yield table.getRow(i);
70
+ yield table.getRow(i, false, true);
71
71
  }
72
72
  }
73
73
  }
package/table.d.ts CHANGED
@@ -25,8 +25,8 @@ export declare class Table {
25
25
  addRows(rows: Iterable<Row>): void;
26
26
  updateRow(i: number, row: Row): void;
27
27
  removeRow(i: number): void;
28
- getRow(i: number, safe?: boolean): Row | undefined;
29
- getPartialRow(i: number, columns: string[], safe?: boolean): Row | undefined;
28
+ getRow(i: number, safe?: boolean, includeID?: boolean): Row | undefined;
29
+ getPartialRow(i: number, columns: string[], safe?: boolean, includeID?: boolean): Row | undefined;
30
30
  validateRow(row: Row): void;
31
31
  validateColumnSpec(id: string, spec: ColumnSpec): void;
32
32
  toJSON(): {
package/table.js CHANGED
@@ -82,17 +82,17 @@ class Table {
82
82
  }
83
83
  this.length--;
84
84
  }
85
- getRow(i, safe = true) {
85
+ getRow(i, safe = true, includeID = false) {
86
86
  if (safe && (i < 0 || i >= this.length)) return;
87
- const row = {};
87
+ const row = includeID ? { __row: i } : {};
88
88
  for (let id in this.columns) {
89
89
  row[id] = this.columns[id].getRow(i);
90
90
  }
91
91
  return row;
92
92
  }
93
- getPartialRow(i, columns, safe = true) {
93
+ getPartialRow(i, columns, safe = true, includeID = false) {
94
94
  if (safe && (i < 0 || i >= this.length)) return;
95
- const row = {};
95
+ const row = includeID ? { __row: i } : {};
96
96
  for (let id of columns) {
97
97
  row[id] = this.columns[id]?.getRow(i);
98
98
  }
@@ -139,7 +139,6 @@ const $typed = {
139
139
  cardinality: [0, 1],
140
140
  required: true
141
141
  };
142
- const $float = { ...$typed, flags: FLAG_BITMAP };
143
142
  const $untyped = {
144
143
  impl: (table, id, { flags, cardinality: [_, max] }) => {
145
144
  const isDict = flags & FLAG_DICT;
@@ -159,7 +158,6 @@ const $vec = {
159
158
  cardinality: [0, -1 >>> 0],
160
159
  required: true
161
160
  };
162
- const $fvec = { ...$vec, flags: FLAG_BITMAP };
163
161
  const COLUMN_TYPES = {
164
162
  u8: $typed,
165
163
  i8: $typed,
@@ -167,14 +165,18 @@ const COLUMN_TYPES = {
167
165
  i16: $typed,
168
166
  u32: $typed,
169
167
  i32: $typed,
170
- f32: $float,
171
- f64: $float,
168
+ f32: $typed,
169
+ f64: $typed,
172
170
  num: $untyped,
173
171
  str: $untyped,
174
172
  u8vec: $vec,
175
173
  u16vec: $vec,
176
174
  u32vec: $vec,
177
- f32vec: $fvec
175
+ i8vec: $vec,
176
+ i16vec: $vec,
177
+ i32vec: $vec,
178
+ f32vec: $vec,
179
+ f64vec: $vec
178
180
  };
179
181
  const registerColumnType = (type, spec) => {
180
182
  if (COLUMN_TYPES[type])