-
Notifications
You must be signed in to change notification settings - Fork 199
/
vmContainer.go
110 lines (90 loc) · 2.62 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
package containers
import (
"github.com/ElrondNetwork/elrond-go/process"
"github.com/ElrondNetwork/elrond-vm-common"
"github.com/cornelk/hashmap"
)
// virtualMachinesContainer is an VM holder organized by type
type virtualMachinesContainer struct {
objects *hashmap.HashMap
}
// NewVirtualMachinesContainer will create a new instance of a container
func NewVirtualMachinesContainer() *virtualMachinesContainer {
return &virtualMachinesContainer{
objects: &hashmap.HashMap{},
}
}
// 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(key)
if !ok {
return nil, process.ErrInvalidContainerKey
}
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 vm == nil {
return process.ErrNilContainerElement
}
ok := vmc.objects.Insert(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 vm == nil {
return process.ErrNilContainerElement
}
vmc.objects.Set(key, vm)
return nil
}
// Remove will remove an object at a given key
func (vmc *virtualMachinesContainer) Remove(key []byte) {
vmc.objects.Del(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 := make([][]byte, 0)
for obj := range vmc.objects.Iter() {
byteKey, ok := obj.Key.([]byte)
if !ok {
continue
}
keys = append(keys, byteKey)
}
return keys
}
// IsInterfaceNil returns true if there is no value under the interface
func (vmc *virtualMachinesContainer) IsInterfaceNil() bool {
if vmc == nil {
return true
}
return false
}