forked from hazelcast/hazelcast-nodejs-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchema.ts
More file actions
137 lines (120 loc) · 4.47 KB
/
Schema.ts
File metadata and controls
137 lines (120 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* Copyright (c) 2008-2022, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/** @ignore *//** */
import {FieldDescriptor} from '../generic_record/FieldDescriptor';
import * as Long from 'long';
import {FieldOperations} from '../generic_record/FieldOperations';
import {FieldKind} from '../generic_record/FieldKind';
import {BitsUtil} from '../../util/BitsUtil';
import {RabinFingerprint64} from './RabinFingerprint';
/**
* @internal
*/
export class Schema {
typeName: string;
fieldDefinitionMap: Map<string, FieldDescriptor>;
fields: FieldDescriptor[];
numberVarSizeFields: number;
fixedSizeFieldsLength: number;
schemaId: Long;
constructor(typeName: string, fields: FieldDescriptor[]) {
this.typeName = typeName;
this.fields = fields;
this.fields.sort((field1, field2) => {
return field1.fieldName > field2.fieldName ? 1 : -1;
})
this.fieldDefinitionMap = new Map<string, FieldDescriptor>();
for (const field of this.fields) {
// map entries are sorted by insertion order
this.fieldDefinitionMap.set(field.fieldName, field);
}
this.init();
}
init(): void {
const fixedSizeFields = [];
const booleanFields = [];
const variableSizeFields = [];
for (const descriptor of this.fieldDefinitionMap.values()) {
const fieldKind = descriptor.kind;
if (FieldOperations.fieldOperations(fieldKind).kindSizeInBytes() === FieldOperations.VARIABLE_SIZE) {
variableSizeFields.push(descriptor);
} else {
if (fieldKind === FieldKind.BOOLEAN) {
booleanFields.push(descriptor);
} else {
fixedSizeFields.push(descriptor);
}
}
}
fixedSizeFields.sort(
(a, b) => FieldOperations.fieldOperations(b.kind).kindSizeInBytes()
- FieldOperations.fieldOperations(a.kind).kindSizeInBytes()
);
let offset = 0;
for (const descriptor of fixedSizeFields) {
descriptor.offset = offset;
offset += FieldOperations.fieldOperations(descriptor.kind).kindSizeInBytes();
}
let bitOffset = 0;
for (const descriptor of booleanFields) {
descriptor.offset = offset;
descriptor.bitOffset = bitOffset % BitsUtil.BITS_IN_A_BYTE;
bitOffset++;
if (bitOffset % BitsUtil.BITS_IN_A_BYTE === 0) {
offset += 1;
}
}
if (bitOffset % BitsUtil.BITS_IN_A_BYTE !== 0) {
offset += 1;
}
this.fixedSizeFieldsLength = offset;
let index = 0;
for (const descriptor of variableSizeFields) {
descriptor.index = index;
index++;
}
this.numberVarSizeFields = index;
this.schemaId = RabinFingerprint64.ofSchema(this);
}
getFields() : IterableIterator<FieldDescriptor> {
return this.fieldDefinitionMap.values();
}
private hasSameFields(other: Schema): boolean {
if (other.fieldDefinitionMap.size !== this.fieldDefinitionMap.size) {
return false;
}
for (const [fieldName, field] of this.fieldDefinitionMap) {
if (!other.fieldDefinitionMap.has(fieldName)) {
return false;
}
const otherField = other.fieldDefinitionMap.get(fieldName);
if (!otherField.equals(field)) {
return false;
}
}
return true;
}
equals(other: Schema): boolean {
if (this === other) {
return true;
}
return this.numberVarSizeFields === other.numberVarSizeFields &&
this.fixedSizeFieldsLength === other.fixedSizeFieldsLength &&
this.typeName === other.typeName &&
this.schemaId.equals(other.schemaId) &&
this.hasSameFields(other);
}
}