Skip to content

Commit

Permalink
Get rid of toNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
domoritz committed Dec 2, 2023
1 parent ad9cfd5 commit b3cd5b2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
4 changes: 2 additions & 2 deletions js/src/builder/binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ export class BinaryBuilder<TNull = any> extends VariableWidthBuilder<Binary, TNu
let offset = 0;
for (const [index, value] of pending) {
if (value === undefined) {
offsets.set(index, offsets.toNumber(0));
offsets.set(index, 0);
} else {
const length = value.length;
data.set(value, offset);
offsets.set(index, offsets.toNumber(length));
offsets.set(index, length);
offset += length;
}
}
Expand Down
9 changes: 1 addition & 8 deletions js/src/builder/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import { memcpy } from '../util/buffer.js';
import { TypedArray, BigIntArray, ArrayCtor } from '../interfaces.js';
import { DataType } from '../type.js';
import { bigIntToNumber } from '../util/bigint.js';

/** @ignore */
const roundLengthUpToNearest64Bytes = (len: number, BPE: number) => ((((Math.ceil(len) * BPE) + 63) & ~63) || 64) / BPE;
Expand Down Expand Up @@ -129,14 +128,8 @@ export class BitmapBufferBuilder extends DataBufferBuilder<Uint8Array> {
export class OffsetsBufferBuilder<T extends DataType> extends DataBufferBuilder<T['TOffsetArray']> {
constructor(type: T) {
super(new type.OffsetArrayType(1), 1);
this.toNumber = type.OffsetArrayType === BigInt64Array ? BigInt : bigIntToNumber as any;
}

/**
* The correct number constructor for the buffer type.
*/
public toNumber: ((number: number | bigint) => T['TOffsetArray'] extends BigInt64Array ? bigint : number);

public append(value: T['TOffsetArray'][0]) {
return this.set(this.length - 1, value);
}
Expand All @@ -151,7 +144,7 @@ export class OffsetsBufferBuilder<T extends DataType> extends DataBufferBuilder<
}
public flush(length = this.length - 1) {
if (length > this.length) {
this.set(length - 1, this.toNumber(0));
this.set(length - 1, this.BYTES_PER_ELEMENT > 4 ? 0n : 0);
}
return super.flush(length + 1);
}
Expand Down
20 changes: 18 additions & 2 deletions js/src/builder/largeutf8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,23 @@ export class LargeUtf8Builder<TNull = any> extends VariableWidthBuilder<LargeUtf
return super.setValue(index, encodeUtf8(value) as any);
}
// @ts-ignore
protected _flushPending(pending: Map<number, Uint8Array | undefined>, pendingLength: number): void { }
// TODO: move to largeBinaryBuilder when implemented
// protected _flushPending(pending: Map<number, Uint8Array | undefined>, pendingLength: number): void { }
protected _flushPending(pending: Map<number, Uint8Array | undefined>, pendingLength: number) {
const offsets = this._offsets;
const data = this._values.reserve(pendingLength).buffer;
let offset = 0;
for (const [index, value] of pending) {
if (value === undefined) {
offsets.set(index, 0n);
} else {
const length = value.length;
data.set(value, offset);
offsets.set(index, BigInt(length));
offset += length;
}
}
}
}

(LargeUtf8Builder.prototype as any)._flushPending = (BinaryBuilder.prototype as any)._flushPending;
// (LargeUtf8Builder.prototype as any)._flushPending = (LargeBinaryBuilder.prototype as any)._flushPending;
10 changes: 5 additions & 5 deletions js/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ Object.defineProperty(Float32.prototype, 'ArrayType', { value: Float32Array });
Object.defineProperty(Float64.prototype, 'ArrayType', { value: Float64Array });

/** @ignore */
export interface Binary extends DataType<Type.Binary> { TArray: Uint8Array; TValue: Uint8Array; ArrayType: TypedArrayConstructor<Uint8Array> }
export interface Binary extends DataType<Type.Binary> { TArray: Uint8Array; TOffsetArray: Int32Array; TValue: Uint8Array; ArrayType: TypedArrayConstructor<Uint8Array>; OffsetArrayType: TypedArrayConstructor<Int32Array> }
/** @ignore */
export class Binary extends DataType<Type.Binary> {
constructor() {
Expand Down Expand Up @@ -478,13 +478,13 @@ export class Duration<T extends Durations = Durations> extends DataType<T> {
}

/** @ignore */
export class DurationSecond extends Duration<Type.DurationSecond> { constructor() { super(TimeUnit.SECOND); }}
export class DurationSecond extends Duration<Type.DurationSecond> { constructor() { super(TimeUnit.SECOND); } }
/** @ignore */
export class DurationMillisecond extends Duration<Type.DurationMillisecond> { constructor() { super(TimeUnit.MILLISECOND); }}
export class DurationMillisecond extends Duration<Type.DurationMillisecond> { constructor() { super(TimeUnit.MILLISECOND); } }
/** @ignore */
export class DurationMicrosecond extends Duration<Type.DurationMicrosecond> { constructor() { super(TimeUnit.MICROSECOND); }}
export class DurationMicrosecond extends Duration<Type.DurationMicrosecond> { constructor() { super(TimeUnit.MICROSECOND); } }
/** @ignore */
export class DurationNanosecond extends Duration<Type.DurationNanosecond> { constructor() { super(TimeUnit.NANOSECOND); }}
export class DurationNanosecond extends Duration<Type.DurationNanosecond> { constructor() { super(TimeUnit.NANOSECOND); } }


/** @ignore */
Expand Down

0 comments on commit b3cd5b2

Please sign in to comment.