]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/js/src/builder/dictionary.ts
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / js / src / builder / dictionary.ts
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17
18 import { Vector } from '../vector';
19 import { IntBuilder } from './int';
20 import { Dictionary, DataType } from '../type';
21 import { Builder, BuilderOptions } from '../builder';
22
23 type DictionaryHashFunction = (x: any) => string | number;
24
25 export interface DictionaryBuilderOptions<T extends DataType = any, TNull = any> extends BuilderOptions<T, TNull> {
26 dictionaryHashFunction?: DictionaryHashFunction;
27 }
28
29 /** @ignore */
30 export class DictionaryBuilder<T extends Dictionary, TNull = any> extends Builder<T, TNull> {
31
32 protected _dictionaryOffset: number;
33 protected _dictionary?: Vector<T['dictionary']>;
34 protected _keysToIndices: { [key: string]: number };
35 public readonly indices: IntBuilder<T['indices']>;
36 public readonly dictionary: Builder<T['dictionary']>;
37
38 constructor({ 'type': type, 'nullValues': nulls, 'dictionaryHashFunction': hashFn }: DictionaryBuilderOptions<T, TNull>) {
39 super({ type: new Dictionary(type.dictionary, type.indices, type.id, type.isOrdered) as T });
40 this._nulls = <any> null;
41 this._dictionaryOffset = 0;
42 this._keysToIndices = Object.create(null);
43 this.indices = Builder.new({ 'type': this.type.indices, 'nullValues': nulls }) as IntBuilder<T['indices']>;
44 this.dictionary = Builder.new({ 'type': this.type.dictionary, 'nullValues': null }) as Builder<T['dictionary']>;
45 if (typeof hashFn === 'function') {
46 this.valueToKey = hashFn;
47 }
48 }
49
50 public get values() { return this.indices.values; }
51 public get nullCount() { return this.indices.nullCount; }
52 public get nullBitmap() { return this.indices.nullBitmap; }
53 public get byteLength() { return this.indices.byteLength + this.dictionary.byteLength; }
54 public get reservedLength() { return this.indices.reservedLength + this.dictionary.reservedLength; }
55 public get reservedByteLength() { return this.indices.reservedByteLength + this.dictionary.reservedByteLength; }
56 public isValid(value: T['TValue'] | TNull) { return this.indices.isValid(value); }
57 public setValid(index: number, valid: boolean) {
58 const indices = this.indices;
59 valid = indices.setValid(index, valid);
60 this.length = indices.length;
61 return valid;
62 }
63 public setValue(index: number, value: T['TValue']) {
64 const keysToIndices = this._keysToIndices;
65 const key = this.valueToKey(value);
66 let idx = keysToIndices[key];
67 if (idx === undefined) {
68 keysToIndices[key] = idx = this._dictionaryOffset + this.dictionary.append(value).length - 1;
69 }
70 return this.indices.setValue(index, idx);
71 }
72 public flush() {
73 const type = this.type;
74 const prev = this._dictionary;
75 const curr = this.dictionary.toVector();
76 const data = this.indices.flush().clone(type);
77 data.dictionary = prev ? prev.concat(curr) : curr;
78 this.finished || (this._dictionaryOffset += curr.length);
79 this._dictionary = data.dictionary as Vector<T['dictionary']>;
80 this.clear();
81 return data;
82 }
83 public finish() {
84 this.indices.finish();
85 this.dictionary.finish();
86 this._dictionaryOffset = 0;
87 this._keysToIndices = Object.create(null);
88 return super.finish();
89 }
90 public clear() {
91 this.indices.clear();
92 this.dictionary.clear();
93 return super.clear();
94 }
95 public valueToKey(val: any): string | number {
96 return typeof val === 'string' ? val : `${val}`;
97 }
98 }