forked from hazelcast/hazelcast-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
portable_serializer.go
129 lines (113 loc) · 4.02 KB
/
portable_serializer.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
// Copyright (c) 2008-2018, 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 internal
import (
"fmt"
"reflect"
"github.com/ahmetmircik/hazelcast-go-client/core"
"github.com/ahmetmircik/hazelcast-go-client/serialization"
)
type PortableSerializer struct {
service *Service
portableContext *PortableContext
factories map[int32]serialization.PortableFactory
}
func NewPortableSerializer(service *Service, portableFactories map[int32]serialization.PortableFactory,
portableVersion int32) *PortableSerializer {
return &PortableSerializer{service, NewPortableContext(service, portableVersion), portableFactories}
}
func (ps *PortableSerializer) ID() int32 {
return ConstantTypePortable
}
func (ps *PortableSerializer) Read(input serialization.DataInput) (interface{}, error) {
factoryID := input.ReadInt32()
classID := input.ReadInt32()
if input.Error() != nil {
return nil, input.Error()
}
return ps.ReadObject(input, factoryID, classID)
}
func (ps *PortableSerializer) ReadObject(input serialization.DataInput, factoryID int32, classID int32) (
serialization.Portable, error) {
version := input.ReadInt32()
if input.Error() != nil {
return nil, input.Error()
}
portable, err := ps.createNewPortableInstance(factoryID, classID)
if err != nil {
return nil, err
}
classDefinition := ps.portableContext.LookUpClassDefinition(factoryID, classID, version)
if classDefinition == nil {
var backupPos = input.Position()
classDefinition, err = ps.portableContext.ReadClassDefinitionFromInput(input, factoryID, classID, version)
if err != nil {
input.SetPosition(backupPos)
return nil, err
}
input.SetPosition(backupPos)
}
var reader serialization.PortableReader
var isMorphing bool
if classDefinition.Version() == ps.portableContext.ClassVersion(portable) {
reader = NewDefaultPortableReader(ps, input, classDefinition)
isMorphing = false
} else {
reader = NewMorphingPortableReader(ps, input, classDefinition)
isMorphing = true
}
err = portable.ReadPortable(reader)
if err != nil {
return nil, err
}
if isMorphing {
reader.(*MorphingPortableReader).End()
} else {
reader.(*DefaultPortableReader).End()
}
return portable, nil
}
func (ps *PortableSerializer) createNewPortableInstance(factoryID int32, classID int32) (serialization.Portable, error) {
factory := ps.factories[factoryID]
if factory == nil {
return nil, core.NewHazelcastSerializationError(fmt.Sprintf("there is no suitable portable factory for factory id: %d",
factoryID), nil)
}
portable := factory.Create(classID)
if portable == nil {
return nil, core.NewHazelcastSerializationError(fmt.Sprintf("%v is not able to create an instance for id: %d on factory id: %d",
reflect.TypeOf(factory), classID, factoryID), nil)
}
return portable, nil
}
func (ps *PortableSerializer) Write(output serialization.DataOutput, i interface{}) error {
output.WriteInt32(i.(serialization.Portable).FactoryID())
output.WriteInt32(i.(serialization.Portable).ClassID())
err := ps.WriteObject(output, i)
return err
}
func (ps *PortableSerializer) WriteObject(output serialization.DataOutput, i interface{}) error {
classDefinition, err := ps.portableContext.LookUpOrRegisterClassDefiniton(i.(serialization.Portable))
if err != nil {
return err
}
output.WriteInt32(classDefinition.Version())
writer := NewDefaultPortableWriter(ps, output.(serialization.PositionalDataOutput), classDefinition)
err = i.(serialization.Portable).WritePortable(writer)
if err != nil {
return err
}
writer.End()
return nil
}