/
schema.go
135 lines (123 loc) · 3.65 KB
/
schema.go
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
/*
* Copyright (c) 2008-2023, 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.
*/
package serialization
import (
"fmt"
"sort"
"github.com/hazelcast/hazelcast-go-client/serialization"
)
type fieldDescriptorIndex struct {
index int
fd FieldDescriptor
}
type Schema struct {
Fields map[string]FieldDescriptor
TypeName string
fieldDefinitions []FieldDescriptor
id int64
numberOfVarSizeFields int32
fixedSizeFieldsLength int32
}
func NewSchema(typeName string, fieldDefinitionMap map[string]FieldDescriptor) *Schema {
fds := make([]FieldDescriptor, 0, len(fieldDefinitionMap))
for _, fd := range fieldDefinitionMap {
fds = append(fds, fd)
}
// Sort according to field name
sort.SliceStable(fds, func(i, j int) bool {
return fds[i].Name < fds[j].Name
})
schema := &Schema{
TypeName: typeName,
Fields: make(map[string]FieldDescriptor, len(fieldDefinitionMap)),
fieldDefinitions: fds,
}
schema.init()
return schema
}
func (s *Schema) GetField(fieldName string) (FieldDescriptor, bool) {
fd, ok := s.Fields[fieldName]
return fd, ok
}
func (s *Schema) ID() int64 {
return s.id
}
func (s *Schema) FieldCount() int {
return len(s.fieldDefinitions)
}
func (s *Schema) FieldDefinitions() []FieldDescriptor {
return s.fieldDefinitions
}
func (s Schema) String() string {
return fmt.Sprintf("Schema{TypeName=%s, numberOfComplexFields=%d, primitivesLength=%d, Fields=%v}",
s.TypeName, s.numberOfVarSizeFields, s.fixedSizeFieldsLength, s.Fields)
}
func (s *Schema) init() {
var fixedSizeFields, varSizeFields, booleanFields []fieldDescriptorIndex
// since FieldDescriptors are copied, it is mandatory to put them back to s.FieldDefinitions and s.Fields
fdis := make([]fieldDescriptorIndex, len(s.fieldDefinitions))
for i, fd := range s.fieldDefinitions {
fdi := fieldDescriptorIndex{
index: i,
fd: fd,
}
k := fd.Kind
if FieldKindSize[k] == variableKindSize {
varSizeFields = append(varSizeFields, fdi)
} else {
if k == serialization.FieldKindBoolean {
booleanFields = append(booleanFields, fdi)
} else {
fixedSizeFields = append(fixedSizeFields, fdi)
}
}
fdis[i] = fdi
}
sort.SliceStable(fixedSizeFields, func(i, j int) bool {
s1 := FieldKindSize[fixedSizeFields[j].fd.Kind]
s2 := FieldKindSize[fixedSizeFields[i].fd.Kind]
return s1 < s2
})
var offset int32
for _, fdi := range fixedSizeFields {
fdi.fd.offset = offset
offset += FieldKindSize[fdi.fd.Kind]
s.fieldDefinitions[fdi.index] = fdi.fd
s.Fields[fdi.fd.Name] = fdi.fd
}
bitOffset := 0
for _, fdi := range booleanFields {
fdi.fd.offset = offset
fdi.fd.bitOffset = int8(bitOffset % BitsInAByte)
bitOffset += 1
if bitOffset%BitsInAByte == 0 {
offset += 1
}
s.fieldDefinitions[fdi.index] = fdi.fd
s.Fields[fdi.fd.Name] = fdi.fd
}
if bitOffset%BitsInAByte != 0 {
offset += 1
}
s.fixedSizeFieldsLength = offset
for i, fdi := range varSizeFields {
fdi.fd.index = int32(i)
s.fieldDefinitions[fdi.index] = fdi.fd
s.Fields[fdi.fd.Name] = fdi.fd
}
s.numberOfVarSizeFields = int32(len(varSizeFields))
s.id = RabinFingerPrint.OfSchema(s)
}