-
Notifications
You must be signed in to change notification settings - Fork 199
/
vmContainer.go
151 lines (121 loc) · 3.84 KB
/
vmContainer.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package containers
import (
"fmt"
"github.com/ElrondNetwork/elrond-go-core/core/check"
"github.com/ElrondNetwork/elrond-go-core/core/container"
logger "github.com/ElrondNetwork/elrond-go-logger"
"github.com/ElrondNetwork/elrond-go/process"
vmcommon "github.com/ElrondNetwork/elrond-vm-common"
)
var _ process.VirtualMachinesContainer = (*virtualMachinesContainer)(nil)
var logVMContainer = logger.GetOrCreate("factory/containers/vmContainer")
type closer interface {
Close() error
}
// virtualMachinesContainer is an VM holder organized by type
type virtualMachinesContainer struct {
objects *container.MutexMap
}
// NewVirtualMachinesContainer will create a new instance of a container
func NewVirtualMachinesContainer() *virtualMachinesContainer {
return &virtualMachinesContainer{
objects: container.NewMutexMap(),
}
}
// Get returns the object stored at a certain key.
// Returns an error if the element does not exist
func (vmc *virtualMachinesContainer) Get(key []byte) (vmcommon.VMExecutionHandler, error) {
value, ok := vmc.objects.Get(string(key))
if !ok {
return nil, fmt.Errorf("%w in vm container for key %v", process.ErrInvalidContainerKey, key)
}
vm, ok := value.(vmcommon.VMExecutionHandler)
if !ok {
return nil, process.ErrWrongTypeInContainer
}
return vm, nil
}
// Add will add an object at a given key. Returns
// an error if the element already exists
func (vmc *virtualMachinesContainer) Add(key []byte, vm vmcommon.VMExecutionHandler) error {
if check.IfNilReflect(vm) {
return process.ErrNilContainerElement
}
ok := vmc.objects.Insert(string(key), vm)
if !ok {
return process.ErrContainerKeyAlreadyExists
}
return nil
}
// AddMultiple will add objects with given keys. Returns
// an error if one element already exists, lengths mismatch or an interceptor is nil
func (vmc *virtualMachinesContainer) AddMultiple(keys [][]byte, vms []vmcommon.VMExecutionHandler) error {
if len(keys) != len(vms) {
return process.ErrLenMismatch
}
for idx, key := range keys {
err := vmc.Add(key, vms[idx])
if err != nil {
return err
}
}
return nil
}
// Replace will add (or replace if it already exists) an object at a given key
func (vmc *virtualMachinesContainer) Replace(key []byte, vm vmcommon.VMExecutionHandler) error {
if check.IfNilReflect(vm) {
return process.ErrNilContainerElement
}
vmc.objects.Set(string(key), vm)
return nil
}
// Remove will remove an object at a given key
func (vmc *virtualMachinesContainer) Remove(key []byte) {
vmc.objects.Remove(string(key))
}
// Len returns the length of the added objects
func (vmc *virtualMachinesContainer) Len() int {
return vmc.objects.Len()
}
// Keys returns all the keys from the container
func (vmc *virtualMachinesContainer) Keys() [][]byte {
keys := vmc.objects.Keys()
keysBytes := make([][]byte, 0, len(keys))
for _, k := range keys {
key, ok := k.(string)
if !ok {
continue
}
keysBytes = append(keysBytes, []byte(key))
}
return keysBytes
}
// Close closes the items in the container (meaningful for Arwen out-of-process)
func (vmc *virtualMachinesContainer) Close() error {
var err error
for _, item := range vmc.objects.Values() {
logVMContainer.Debug("closing vm container item", "item", fmt.Sprintf("%T", item))
closingErr := vmc.tryClose(item)
if closingErr != nil {
err = closingErr
}
}
return err
}
func (vmc *virtualMachinesContainer) tryClose(item interface{}) error {
asCloser, ok := item.(closer)
if !ok {
return nil
}
err := asCloser.Close()
if err != nil {
logVMContainer.Warn("cannot close vm container item", "item", fmt.Sprintf("%T", item), "err", err)
} else {
logVMContainer.Debug("vm container item closed", "item", fmt.Sprintf("%T", item))
}
return err
}
// IsInterfaceNil returns true if there is no value under the interface
func (vmc *virtualMachinesContainer) IsInterfaceNil() bool {
return vmc == nil
}